Skip to content

Commit 2484c73

Browse files
committed
Merge pull request #46 from acdha/pylint-warnings
More linter cleanup
2 parents 7142f83 + 4e53b4e commit 2484c73

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

bagit.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ def make_bag(bag_dir, bag_info=None, processes=1, checksum=None):
8080
the bag_info dictionary.
8181
"""
8282
bag_dir = os.path.abspath(bag_dir)
83-
logger.info("creating bag for directory %s" % bag_dir)
83+
logger.info("creating bag for directory %s", bag_dir)
8484
# assume md5 checksum if not specified
8585
if not checksum:
8686
checksum = ['md5']
8787

8888
if not os.path.isdir(bag_dir):
89-
logger.error("no such bag directory %s" % bag_dir)
89+
logger.error("no such bag directory %s", bag_dir)
9090
raise RuntimeError("no such bag directory %s" % bag_dir)
9191

9292
old_dir = os.path.abspath(os.path.curdir)
@@ -114,18 +114,18 @@ def make_bag(bag_dir, bag_info=None, processes=1, checksum=None):
114114
if os.path.abspath(f) == temp_data:
115115
continue
116116
new_f = os.path.join(temp_data, f)
117-
logger.info("moving %s to %s" % (f, new_f))
117+
logger.info("moving %s to %s", f, new_f)
118118
os.rename(f, new_f)
119119

120-
logger.info("moving %s to %s" % (temp_data, 'data'))
120+
logger.info("moving %s to %s", temp_data, 'data')
121121
os.rename(temp_data, 'data')
122122

123123
# permissions for the payload directory should match those of the
124124
# original directory
125125
os.chmod('data', os.stat(cwd).st_mode)
126126

127127
for c in checksum:
128-
logger.info("writing manifest-%s.txt" % c)
128+
logger.info("writing manifest-%s.txt", c)
129129
Oxum = _make_manifest('manifest-%s.txt' % c, 'data', processes, c)
130130

131131
logger.info("writing bagit.txt")
@@ -556,6 +556,8 @@ class BagError(Exception):
556556

557557
class BagValidationError(BagError):
558558
def __init__(self, message, details=None):
559+
super(BagValidationError, self).__init__()
560+
559561
if details is None:
560562
details = []
561563

@@ -571,11 +573,15 @@ def __str__(self):
571573

572574
class ManifestErrorDetail(BagError):
573575
def __init__(self, path):
576+
super(ManifestErrorDetail, self).__init__()
577+
574578
self.path = path
575579

576580

577581
class ChecksumMismatch(ManifestErrorDetail):
578582
def __init__(self, path, algorithm=None, expected=None, found=None):
583+
super(ChecksumMismatch, self).__init__(path)
584+
579585
self.path = path
580586
self.algorithm = algorithm
581587
self.expected = expected
@@ -651,13 +657,14 @@ def _load_tag_file(tag_file_name):
651657
tags[name] = value
652658
continue
653659

654-
if not type(tags[name]) is list:
660+
if not isinstance(tags[name], list):
655661
tags[name] = [tags[name], value]
656662
else:
657663
tags[name].append(value)
658664
return tags
659665

660-
def _parse_tags(file):
666+
667+
def _parse_tags(tag_file):
661668
"""Parses a tag file, according to RFC 2822. This
662669
includes line folding, permitting extra-long
663670
field values.
@@ -671,7 +678,7 @@ def _parse_tags(file):
671678

672679
# Line folding is handled by yielding values only after we encounter
673680
# the start of a new tag, or if we pass the EOF.
674-
for num, line in enumerate(file):
681+
for num, line in enumerate(tag_file):
675682
# If byte-order mark ignore it for now.
676683
if num == 0:
677684
if line.startswith(BOM):
@@ -687,7 +694,8 @@ def _parse_tags(file):
687694
yield (tag_name, tag_value.strip())
688695

689696
if ':' not in line:
690-
raise BagValidationError("invalid line '%s' in %s" % (line.strip(), os.path.basename(file.name)))
697+
raise BagValidationError("invalid line '%s' in %s" % (line.strip(),
698+
os.path.basename(tag_file.name)))
691699

692700
parts = line.strip().split(':', 1)
693701
tag_name = parts[0].strip()
@@ -703,18 +711,18 @@ def _make_tag_file(bag_info_path, bag_info):
703711
headers.sort()
704712
with open(bag_info_path, 'w') as f:
705713
for h in headers:
706-
if type(bag_info[h]) == list:
714+
if isinstance(bag_info[h], list):
707715
for val in bag_info[h]:
708716
f.write("%s: %s\n" % (h, val))
709717
else:
710718
txt = bag_info[h]
711719
# strip CR, LF and CRLF so they don't mess up the tag file
712-
txt = re.sub('\n|\r|(\r\n)', '', txt)
720+
txt = re.sub(r'\n|\r|(\r\n)', '', txt)
713721
f.write("%s: %s\n" % (h, txt))
714722

715723

716724
def _make_manifest(manifest_file, data_dir, processes, algorithm='md5'):
717-
logger.info('writing manifest with %s processes' % processes)
725+
logger.info('writing manifest with %s processes', processes)
718726

719727
if algorithm == 'md5':
720728
manifest_line = _manifest_line_md5
@@ -739,9 +747,9 @@ def _make_manifest(manifest_file, data_dir, processes, algorithm='md5'):
739747
num_files = 0
740748
total_bytes = 0
741749

742-
for digest, filename, bytes in checksums:
750+
for digest, filename, byte_count in checksums:
743751
num_files += 1
744-
total_bytes += bytes
752+
total_bytes += byte_count
745753
manifest.write("%s %s\n" % (digest, _encode_filename(filename)))
746754
manifest.close()
747755
return "%s.%s" % (total_bytes, num_files)
@@ -753,15 +761,15 @@ def _make_tagmanifest_file(alg, bag_dir):
753761
files = [f for f in listdir(bag_dir) if isfile(join(bag_dir, f))]
754762
checksums = []
755763
for f in files:
756-
if re.match('^tagmanifest-.+\.txt$', f):
764+
if re.match(r'^tagmanifest-.+\.txt$', f):
757765
continue
758766
with open(join(bag_dir, f), 'rb') as fh:
759767
m = _hasher(alg)
760768
while True:
761-
bytes = fh.read(16384)
762-
if not bytes:
769+
block = fh.read(16384)
770+
if not block:
763771
break
764-
m.update(bytes)
772+
m.update(block)
765773
checksums.append((m.hexdigest(), f))
766774

767775
with open(join(bag_dir, tagmanifest_file), 'w') as tagmanifest:
@@ -844,11 +852,11 @@ def _manifest_line(filename, algorithm='md5'):
844852

845853
total_bytes = 0
846854
while True:
847-
bytes = fh.read(16384)
848-
total_bytes += len(bytes)
849-
if not bytes:
855+
block = fh.read(16384)
856+
total_bytes += len(block)
857+
if not block:
850858
break
851-
m.update(bytes)
859+
m.update(block)
852860

853861
return (m.hexdigest(), _decode_filename(filename), total_bytes)
854862

@@ -860,8 +868,8 @@ def _encode_filename(s):
860868

861869

862870
def _decode_filename(s):
863-
s = re.sub("%0D", "\r", s, re.IGNORECASE)
864-
s = re.sub("%0A", "\n", s, re.IGNORECASE)
871+
s = re.sub(r"%0D", "\r", s, re.IGNORECASE)
872+
s = re.sub(r"%0A", "\n", s, re.IGNORECASE)
865873
return s
866874

867875

0 commit comments

Comments
 (0)