@@ -59,9 +59,9 @@ def _configPropertyRW(name):
5959 read and write
6060 """
6161 rv = property (
62- fget = lambda self : getattr (self .config , nm ),
63- fset = lambda self , value : setattr (self .config , nm , value ),
64- fdel = lambda self : delattr (self , nm ),
62+ fget = lambda self : getattr (self .config , name ),
63+ fset = lambda self , value : setattr (self .config , name , value ),
64+ fdel = lambda self : delattr (self , name ),
6565 doc = "attribute forwarded to self.config, read/write" ,
6666 )
6767 return rv
@@ -119,7 +119,7 @@ def str2Opt(opttype, optvalue):
119119 # base converter
120120 conv = StrConv (opttype )
121121 if opttype .endswith ("list" ):
122- temp = re .split ("\s*,\s*" , optvalue )
122+ temp = re .split (r "\s*,\s*" , optvalue )
123123 rv = list (map (conv , temp )) if len (temp ) > 0 else []
124124 else :
125125 rv = conv (optvalue )
@@ -163,41 +163,39 @@ def __next__(self):
163163 return line
164164
165165
166- def checkCRC32 (filename ):
166+ def get_crc32 (filename ):
167167 """Calculate the crc32 value of file.
168168
169169 :param filename: path to the file
170170 :return: crc32 value of file
171171 """
172172 try :
173- fd = open (filename , "rb" )
174- except :
175- return "Read error"
176- eachLine = fd .readline ()
177- prev = 0
178- while eachLine :
179- prev = zlib .crc32 (eachLine , prev )
180- eachLine = fd .readline ()
181- fd .close ()
173+ with open (filename , "rb" ) as fd :
174+ eachLine = fd .readline ()
175+ prev = 0
176+ while eachLine :
177+ prev = zlib .crc32 (eachLine , prev )
178+ eachLine = fd .readline ()
179+ except OSError as e :
180+ raise RuntimeError (f"Failed to read file { filename } " ) from e
182181 return prev
183182
184183
185- def checkMD5 (filename , blocksize = 65536 ):
184+ def get_md5 (filename , blocksize = 65536 ):
186185 """Calculate the MD5 value of file.
187186
188187 :param filename: path to the file
189188 :return: md5 value of file
190189 """
191190 try :
192- fd = open (filename , "rb" )
193- except :
194- return "Read error"
195- buf = fd .read (blocksize )
196- md5 = hashlib .md5 ()
197- while len (buf ) > 0 :
198- md5 .update (buf )
199- buf = fd .read (blocksize )
200- fd .close ()
191+ with open (filename , "rb" ) as fd :
192+ buf = fd .read (blocksize )
193+ md5 = hashlib .md5 ()
194+ while len (buf ) > 0 :
195+ md5 .update (buf )
196+ buf = fd .read (blocksize )
197+ except OSError as e :
198+ raise RuntimeError (f"Failed to read file { filename } " ) from e
201199 return md5 .hexdigest ()
202200
203201
@@ -209,16 +207,16 @@ def checkFileVal(filename):
209207 :param filename: path to the file
210208 """
211209 valflag = False
212- lastcrc = checkCRC32 (filename )
210+ lastcrc = get_crc32 (filename )
213211 while not valflag :
214- currcrc = checkCRC32 (filename )
212+ currcrc = get_crc32 (filename )
215213 if currcrc == lastcrc :
216- lastmd5 = checkMD5 (filename )
214+ lastmd5 = get_md5 (filename )
217215 time .sleep (0.01 )
218- currmd5 = checkMD5 (filename )
216+ currmd5 = get_md5 (filename )
219217 if lastmd5 == currmd5 :
220218 valflag = True
221219 else :
222220 time .sleep (0.5 )
223- lastcrc = checkCRC32 (filename )
221+ lastcrc = get_crc32 (filename )
224222 return
0 commit comments