Skip to content

Commit dc66eef

Browse files
authored
Merge pull request #9 from zmx27/fix-flake8-tools
style: fix flake8 error for tools.py
2 parents 7904691 + b752126 commit dc66eef

File tree

4 files changed

+59
-32
lines changed

4 files changed

+59
-32
lines changed

src/diffpy/srxconfutils/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
#
1313
##############################################################################
1414

15-
# some convenience imports
16-
from diffpy.srxconfutils.config import ConfigBase
17-
1815
# package version
1916
from diffpy.srxconfutils.version import __version__
2017

@@ -30,4 +27,7 @@ def test():
3027
return test()
3128

3229

30+
# silence the pyflakes syntax checker
31+
assert __version__ or True
32+
3333
# End of file

src/diffpy/srxconfutils/tools.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def temp_file(tmp_path):
6+
"""Create a temporary file with known content."""
7+
file_path = tmp_path / "testfile.txt"
8+
content = b"Hello world!\nThis is a test.\n"
9+
file_path.write_bytes(content)
10+
return file_path, content

tests/test_tools.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import hashlib
2+
import zlib
3+
4+
from diffpy.srxconfutils import tools
5+
6+
7+
def test_get_md5(temp_file):
8+
file_path, content = temp_file
9+
expected_md5 = hashlib.md5(content).hexdigest()
10+
result = tools.get_md5(file_path)
11+
assert result == expected_md5
12+
13+
14+
def test_get_crc32(temp_file):
15+
"""Test the get_crc32 function."""
16+
file_path, content = temp_file
17+
val = tools.get_crc32(file_path)
18+
expected = zlib.crc32(content)
19+
assert val == expected

0 commit comments

Comments
 (0)