Skip to content

Commit 4ce3d01

Browse files
committed
Merge pull request #64 from Hwesta/code-cleanup
Code cleanup
2 parents 9e1bb9a + d51d858 commit 4ce3d01

File tree

2 files changed

+52
-49
lines changed

2 files changed

+52
-49
lines changed

bagit.py

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
% bagit.py --help
3232
"""
3333

34+
import argparse
3435
import codecs
3536
import hashlib
3637
import logging
3738
import multiprocessing
38-
import optparse
3939
import os
4040
import re
4141
import signal
@@ -247,7 +247,7 @@ def compare_fetch_with_fs(self):
247247
def payload_files(self):
248248
payload_dir = os.path.join(self.path, "data")
249249

250-
for dirpath, dirnames, filenames in os.walk(payload_dir):
250+
for dirpath, _, filenames in os.walk(payload_dir):
251251
for f in filenames:
252252
# Jump through some hoops here to make the payload files come out
253253
# looking like data/dir/file, rather than having the entire path.
@@ -344,7 +344,7 @@ def fetch_entries(self):
344344
yield (parts[0], parts[1], parts[2])
345345

346346
def files_to_be_fetched(self):
347-
for f, size, path in self.fetch_entries():
347+
for f, _, _ in self.fetch_entries():
348348
yield f
349349

350350
def has_oxum(self):
@@ -521,7 +521,7 @@ def _init_worker():
521521
finally:
522522
try:
523523
pool.terminate()
524-
except:
524+
except Exception:
525525
# we really don't care about any exception in terminate()
526526
pass
527527
# Any unhandled exceptions are probably fatal
@@ -877,42 +877,45 @@ def _decode_filename(s):
877877

878878
# following code is used for command line program
879879

880-
class BagOptionParser(optparse.OptionParser):
881-
def __init__(self, *args, **opts):
880+
class BagArgumentParser(argparse.ArgumentParser):
881+
def __init__(self, *args, **kwargs):
882882
self.bag_info = {}
883-
optparse.OptionParser.__init__(self, *args, **opts)
883+
argparse.ArgumentParser.__init__(self, *args, **kwargs)
884884

885885

886-
def _bag_info_store(option, opt, value, parser):
887-
opt = opt.lstrip('--')
888-
opt_caps = '-'.join([o.capitalize() for o in opt.split('-')])
889-
parser.bag_info[opt_caps] = value
886+
class BagHeaderAction(argparse.Action):
887+
def __call__(self, parser, _, values, option_string=None):
888+
opt = option_string.lstrip('--')
889+
opt_caps = '-'.join([o.capitalize() for o in opt.split('-')])
890+
parser.bag_info[opt_caps] = values
890891

891892

892-
def _make_opt_parser():
893-
parser = BagOptionParser(usage='usage: %prog [options] dir1 dir2 ...')
894-
parser.add_option('--processes', action='store', type="int",
895-
dest='processes', default=1,
893+
def _make_parser():
894+
parser = BagArgumentParser()
895+
parser.add_argument('--processes', type=int, dest='processes', default=1,
896896
help='parallelize checksums generation and verification')
897-
parser.add_option('--log', action='store', dest='log', help='The name of the log file')
898-
parser.add_option('--quiet', action='store_true', dest='quiet')
899-
parser.add_option('--validate', action='store_true', dest='validate')
900-
parser.add_option('--fast', action='store_true', dest='fast')
897+
parser.add_argument('--log', help='The name of the log file')
898+
parser.add_argument('--quiet', action='store_true')
899+
parser.add_argument('--validate', action='store_true')
900+
parser.add_argument('--fast', action='store_true')
901901

902902
# optionally specify which checksum algorithm(s) to use when creating a bag
903903
# NOTE: could generate from checksum_algos ?
904-
parser.add_option('--md5', action='append_const', dest='checksum', const='md5',
904+
parser.add_argument('--md5', action='append_const', dest='checksum', const='md5',
905905
help='Generate MD5 manifest when creating a bag (default)')
906-
parser.add_option('--sha1', action='append_const', dest='checksum', const='sha1',
906+
parser.add_argument('--sha1', action='append_const', dest='checksum', const='sha1',
907907
help='Generate SHA1 manifest when creating a bag')
908-
parser.add_option('--sha256', action='append_const', dest='checksum', const='sha256',
908+
parser.add_argument('--sha256', action='append_const', dest='checksum', const='sha256',
909909
help='Generate SHA-256 manifest when creating a bag')
910-
parser.add_option('--sha512', action='append_const', dest='checksum', const='sha512',
910+
parser.add_argument('--sha512', action='append_const', dest='checksum', const='sha512',
911911
help='Generate SHA-512 manifest when creating a bag')
912912

913913
for header in STANDARD_BAG_INFO_HEADERS:
914-
parser.add_option('--%s' % header.lower(), type="string",
915-
action='callback', callback=_bag_info_store)
914+
parser.add_argument('--%s' % header.lower(), type=str,
915+
action=BagHeaderAction)
916+
917+
parser.add_argument('directory', nargs='+', help='directory to make a bag from')
918+
916919
return parser
917920

918921

@@ -929,24 +932,24 @@ def _configure_logging(opts):
929932

930933

931934
def main():
932-
opt_parser = _make_opt_parser()
933-
opts, args = opt_parser.parse_args()
935+
parser = _make_parser()
936+
args = parser.parse_args()
934937

935-
if opts.processes < 0:
936-
opt_parser.error("number of processes needs to be 0 or more")
938+
if args.processes < 0:
939+
parser.error("number of processes needs to be 0 or more")
937940

938-
_configure_logging(opts)
941+
_configure_logging(args)
939942

940943
rc = 0
941-
for bag_dir in args:
944+
for bag_dir in args.directory:
942945

943946
# validate the bag
944-
if opts.validate:
947+
if args.validate:
945948
try:
946949
bag = Bag(bag_dir)
947950
# validate throws a BagError or BagValidationError
948-
bag.validate(processes=opts.processes, fast=opts.fast)
949-
if opts.fast:
951+
bag.validate(processes=args.processes, fast=args.fast)
952+
if args.fast:
950953
LOGGER.info("%s valid according to Payload-Oxum", bag_dir)
951954
else:
952955
LOGGER.info("%s is valid", bag_dir)
@@ -957,9 +960,9 @@ def main():
957960
# make the bag
958961
else:
959962
try:
960-
make_bag(bag_dir, bag_info=opt_parser.bag_info,
961-
processes=opts.processes,
962-
checksum=opts.checksum)
963+
make_bag(bag_dir, bag_info=parser.bag_info,
964+
processes=args.processes,
965+
checksum=args.checksum)
963966
except Exception as exc:
964967
LOGGER.error("Failed to create bag in %s: %s", bag_dir, exc, exc_info=True)
965968
rc = 1

test.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def test_allow_extraneous_files_in_base(self):
181181
bag = bagit.make_bag(self.tmpdir)
182182
self.assertTrue(self.validate(bag))
183183
f = j(self.tmpdir, "IGNOREFILE")
184-
with open(f, 'w') as whatever:
184+
with open(f, 'w'):
185185
self.assertTrue(self.validate(bag))
186186

187187
def test_allow_extraneous_dirs_in_base(self):
@@ -206,7 +206,7 @@ def test_missing_manifest_raises_error(self):
206206
def test_mixed_case_checksums(self):
207207
bag = bagit.make_bag(self.tmpdir)
208208
hashstr = {}
209-
#Extract entries only for the payload and ignore
209+
# Extract entries only for the payload and ignore
210210
# entries from the tagmanifest file
211211
for key in bag.entries.keys():
212212
if key.startswith('data' + os.sep):
@@ -218,7 +218,7 @@ def test_mixed_case_checksums(self):
218218
with open(j(self.tmpdir, "manifest-md5.txt"), "w") as m:
219219
m.write(manifest)
220220

221-
#Since manifest-md5.txt file is updated, re-calculate its
221+
# Since manifest-md5.txt file is updated, re-calculate its
222222
# md5 checksum and update it in the tagmanifest-md5.txt file
223223
hasher = hashlib.new('md5')
224224
with open(j(self.tmpdir, "manifest-md5.txt"), "r") as manifest:
@@ -300,7 +300,7 @@ def tearDown(self):
300300

301301
def test_make_bag(self):
302302
info = {'Bagging-Date': '1970-01-01', 'Contact-Email': '[email protected]'}
303-
bag = bagit.make_bag(self.tmpdir, bag_info=info)
303+
bagit.make_bag(self.tmpdir, bag_info=info)
304304

305305
# data dir should've been created
306306
self.assertTrue(os.path.isdir(j(self.tmpdir, 'data')))
@@ -340,7 +340,7 @@ def test_make_bag(self):
340340
self.assertTrue('6a5090e27cb29d5dda8a0142fbbdf37e bag-info.txt' in tagmanifest_txt)
341341

342342
def test_make_bag_sha1_manifest(self):
343-
bag = bagit.make_bag(self.tmpdir, checksum=['sha1'])
343+
bagit.make_bag(self.tmpdir, checksum=['sha1'])
344344
# check manifest
345345
self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-sha1.txt')))
346346
with open(j(self.tmpdir, 'manifest-sha1.txt')) as m:
@@ -352,7 +352,7 @@ def test_make_bag_sha1_manifest(self):
352352
self.assertTrue('db49ef009f85a5d0701829f38d29f8cf9c5df2ea data/si/4011399822_65987a4806_b_d.jpg' in manifest_txt)
353353

354354
def test_make_bag_sha256_manifest(self):
355-
bag = bagit.make_bag(self.tmpdir, checksum=['sha256'])
355+
bagit.make_bag(self.tmpdir, checksum=['sha256'])
356356
# check manifest
357357
self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-sha256.txt')))
358358
with open(j(self.tmpdir, 'manifest-sha256.txt')) as m:
@@ -363,7 +363,7 @@ def test_make_bag_sha256_manifest(self):
363363
self.assertTrue('45d257c93e59ec35187c6a34c8e62e72c3e9cfbb548984d6f6e8deb84bac41f4 data/si/4011399822_65987a4806_b_d.jpg' in manifest_txt)
364364

365365
def test_make_bag_sha512_manifest(self):
366-
bag = bagit.make_bag(self.tmpdir, checksum=['sha512'])
366+
bagit.make_bag(self.tmpdir, checksum=['sha512'])
367367
# check manifest
368368
self.assertTrue(os.path.isfile(j(self.tmpdir, 'manifest-sha512.txt')))
369369
with open(j(self.tmpdir, 'manifest-sha512.txt')) as m:
@@ -378,7 +378,7 @@ def test_make_bag_unknown_algorithm(self):
378378

379379
def test_make_bag_with_data_dir_present(self):
380380
os.mkdir(j(self.tmpdir, 'data'))
381-
bag = bagit.make_bag(self.tmpdir)
381+
bagit.make_bag(self.tmpdir)
382382

383383
# data dir should now contain another data dir
384384
self.assertTrue(os.path.isdir(j(self.tmpdir, 'data', 'data')))
@@ -425,7 +425,7 @@ def test_garbage_in_bagit_txt(self):
425425
self.assertRaises(bagit.BagValidationError, bagit.Bag, self.tmpdir)
426426

427427
def test_make_bag_multiprocessing(self):
428-
bag = bagit.make_bag(self.tmpdir, processes=2)
428+
bagit.make_bag(self.tmpdir, processes=2)
429429
self.assertTrue(os.path.isdir(j(self.tmpdir, 'data')))
430430

431431
def test_multiple_meta_values(self):
@@ -437,7 +437,7 @@ def test_multiple_meta_values(self):
437437

438438
def test_default_bagging_date(self):
439439
info = {'Contact-Email': '[email protected]'}
440-
bag = bagit.make_bag(self.tmpdir, bag_info=info)
440+
bagit.make_bag(self.tmpdir, bag_info=info)
441441
with open(j(self.tmpdir, 'bag-info.txt')) as bi:
442442
bag_info_txt = bi.read()
443443
self.assertTrue('Contact-Email: [email protected]' in bag_info_txt)
@@ -468,7 +468,7 @@ def test_payload_permissions(self):
468468
new_perms = perms | stat.S_IWOTH
469469
self.assertTrue(perms != new_perms)
470470
os.chmod(self.tmpdir, new_perms)
471-
bag = bagit.make_bag(self.tmpdir)
471+
bagit.make_bag(self.tmpdir)
472472
payload_dir = j(self.tmpdir, 'data')
473473
self.assertEqual(os.stat(payload_dir).st_mode, new_perms)
474474

@@ -525,7 +525,7 @@ def test_save_only_baginfo(self):
525525
nf.write('newfile')
526526
bag.info["foo"] = "bar"
527527
bag.save()
528-
528+
529529
bag = bagit.Bag(self.tmpdir)
530530
self.assertEqual(bag.info["foo"], "bar")
531531
self.assertFalse(bag.is_valid())

0 commit comments

Comments
 (0)