Skip to content

Commit 7ca9a0e

Browse files
committed
Ruff simplifications
1 parent 8624e57 commit 7ca9a0e

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

src/bagit/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,11 @@ def make_bag(
236236
break
237237
except PermissionError as e:
238238
if hasattr(e, "winerror") and e.winerror == 5:
239-
LOGGER.warning(_("PermissionError [WinError 5] when renaming temp folder. Retrying in 10 seconds..."))
239+
LOGGER.warning(
240+
_(
241+
"PermissionError [WinError 5] when renaming temp folder. Retrying in 10 seconds..."
242+
)
243+
)
240244
time.sleep(10)
241245
else:
242246
raise
@@ -1124,12 +1128,12 @@ def _calc_hashes(args):
11241128
full_path = os.path.join(base_path, rel_path)
11251129

11261130
# Create a clone of the default empty hash objects:
1127-
f_hashers = dict((alg, hashlib.new(alg)) for alg in hashes if alg in algorithms)
1131+
f_hashers = {alg: hashlib.new(alg) for alg in hashes if alg in algorithms}
11281132

11291133
try:
11301134
f_hashes = _calculate_file_hashes(full_path, f_hashers)
11311135
except BagValidationError as e:
1132-
f_hashes = dict((alg, str(e)) for alg in f_hashers.keys())
1136+
f_hashes = {alg: str(e) for alg in f_hashers}
11331137

11341138
return rel_path, f_hashes, hashes
11351139

test.py

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,10 +1157,9 @@ def test_invalid_fast_validate(self):
11571157
os.remove(j(self.tmpdir, "data", "loc", "2478433644_2839c5e8b8_o_d.jpg"))
11581158
testargs = ["bagit.py", "--validate", "--completeness-only", self.tmpdir]
11591159

1160-
with self.assertLogs() as captured:
1161-
with self.assertRaises(SystemExit) as cm:
1162-
with mock.patch.object(sys, "argv", testargs):
1163-
bagit.main()
1160+
with self.assertLogs() as captured, self.assertRaises(SystemExit) as cm:
1161+
with mock.patch.object(sys, "argv", testargs):
1162+
bagit.main()
11641163

11651164
self.assertEqual(cm.exception.code, 1)
11661165
self.assertIn(
@@ -1172,10 +1171,9 @@ def test_valid_fast_validate(self):
11721171
bagit.make_bag(self.tmpdir)
11731172
testargs = ["bagit.py", "--validate", "--fast", self.tmpdir]
11741173

1175-
with self.assertLogs() as captured:
1176-
with self.assertRaises(SystemExit) as cm:
1177-
with mock.patch.object(sys, "argv", testargs):
1178-
bagit.main()
1174+
with self.assertLogs() as captured, self.assertRaises(SystemExit) as cm:
1175+
with mock.patch.object(sys, "argv", testargs):
1176+
bagit.main()
11791177

11801178
self.assertEqual(cm.exception.code, 0)
11811179
self.assertEqual(
@@ -1206,10 +1204,9 @@ def test_invalid_completeness_validate(self):
12061204

12071205
testargs = ["bagit.py", "--validate", "--completeness-only", self.tmpdir]
12081206

1209-
with self.assertLogs() as captured:
1210-
with self.assertRaises(SystemExit) as cm:
1211-
with mock.patch.object(sys, "argv", testargs):
1212-
bagit.main()
1207+
with self.assertLogs() as captured, self.assertRaises(SystemExit) as cm:
1208+
with mock.patch.object(sys, "argv", testargs):
1209+
bagit.main()
12131210

12141211
self.assertEqual(cm.exception.code, 1)
12151212
self.assertIn(
@@ -1221,10 +1218,9 @@ def test_valid_completeness_validate(self):
12211218
bagit.make_bag(self.tmpdir)
12221219
testargs = ["bagit.py", "--validate", "--completeness-only", self.tmpdir]
12231220

1224-
with self.assertLogs() as captured:
1225-
with self.assertRaises(SystemExit) as cm:
1226-
with mock.patch.object(sys, "argv", testargs):
1227-
bagit.main()
1221+
with self.assertLogs() as captured, self.assertRaises(SystemExit) as cm:
1222+
with mock.patch.object(sys, "argv", testargs):
1223+
bagit.main()
12281224

12291225
self.assertEqual(cm.exception.code, 0)
12301226
self.assertEqual(
@@ -1242,10 +1238,9 @@ def test_invalid_full_validate(self):
12421238

12431239
testargs = ["bagit.py", "--validate", self.tmpdir]
12441240

1245-
with self.assertLogs() as captured:
1246-
with self.assertRaises(SystemExit) as cm:
1247-
with mock.patch.object(sys, "argv", testargs):
1248-
bagit.main()
1241+
with self.assertLogs() as captured, self.assertRaises(SystemExit) as cm:
1242+
with mock.patch.object(sys, "argv", testargs):
1243+
bagit.main()
12491244

12501245
self.assertEqual(cm.exception.code, 1)
12511246
self.assertIn("Bag validation failed", captured.records[-1].getMessage())
@@ -1254,10 +1249,9 @@ def test_valid_full_validate(self):
12541249
bagit.make_bag(self.tmpdir)
12551250
testargs = ["bagit.py", "--validate", self.tmpdir]
12561251

1257-
with self.assertLogs() as captured:
1258-
with self.assertRaises(SystemExit) as cm:
1259-
with mock.patch.object(sys, "argv", testargs):
1260-
bagit.main()
1252+
with self.assertLogs() as captured, self.assertRaises(SystemExit) as cm:
1253+
with mock.patch.object(sys, "argv", testargs):
1254+
bagit.main()
12611255

12621256
self.assertEqual(cm.exception.code, 0)
12631257
self.assertEqual("%s is valid" % self.tmpdir, captured.records[-1].getMessage())
@@ -1267,10 +1261,9 @@ def test_failed_create_bag(self):
12671261

12681262
testargs = ["bagit.py", self.tmpdir]
12691263

1270-
with self.assertLogs() as captured:
1271-
with self.assertRaises(SystemExit) as cm:
1272-
with mock.patch.object(sys, "argv", testargs):
1273-
bagit.main()
1264+
with self.assertLogs() as captured, self.assertRaises(SystemExit) as cm:
1265+
with mock.patch.object(sys, "argv", testargs):
1266+
bagit.main()
12741267

12751268
self.assertEqual(cm.exception.code, 1)
12761269
self.assertIn(
@@ -1281,10 +1274,9 @@ def test_failed_create_bag(self):
12811274
def test_create_bag(self):
12821275
testargs = ["bagit.py", self.tmpdir]
12831276

1284-
with self.assertLogs() as captured:
1285-
with self.assertRaises(SystemExit) as cm:
1286-
with mock.patch.object(sys, "argv", testargs):
1287-
bagit.main()
1277+
with self.assertLogs() as captured, self.assertRaises(SystemExit) as cm:
1278+
with mock.patch.object(sys, "argv", testargs):
1279+
bagit.main()
12881280

12891281
for rec in captured.records:
12901282
print(rec.getMessage())

0 commit comments

Comments
 (0)