Skip to content

Commit 56a7900

Browse files
fkloftFelix Kloftacdha
authored
self.path shouldn't be None (#167)
* remove direct imports for consistency since os is imported anyway and os.path.[...] is used elsewhere * don't overwrite self.path after it was already normalized (closes #166) --------- Co-authored-by: Felix Kloft <[email protected]> Co-authored-by: Chris Adams <[email protected]>
1 parent 70eeb74 commit 56a7900

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

bagit.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from collections import defaultdict
1818
from datetime import date
1919
from functools import partial
20-
from os.path import abspath, isdir, isfile, join
2120

2221
try:
2322
from importlib.metadata import version
@@ -278,7 +277,7 @@ class Bag(object):
278277
valid_files = ["bagit.txt", "fetch.txt"]
279278
valid_directories = ["data"]
280279

281-
def __init__(self, path=None):
280+
def __init__(self, path):
282281
super(Bag, self).__init__()
283282
self.tags = {}
284283
self.info = {}
@@ -300,12 +299,8 @@ def __init__(self, path=None):
300299

301300
self.algorithms = []
302301
self.tag_file_name = None
303-
self.path = abspath(path)
304-
if path:
305-
# if path ends in a path separator, strip it off
306-
if path[-1] == os.sep:
307-
self.path = path[:-1]
308-
self._open()
302+
self.path = os.path.abspath(path)
303+
self._open()
309304

310305
def __str__(self):
311306
# FIXME: develop a more informative string representation for a Bag
@@ -329,7 +324,7 @@ def _open(self):
329324
# the required version and encoding.
330325
bagit_file_path = os.path.join(self.path, "bagit.txt")
331326

332-
if not isfile(bagit_file_path):
327+
if not os.path.isfile(bagit_file_path):
333328
raise BagError(_("Expected bagit.txt does not exist: %s") % bagit_file_path)
334329

335330
self.tags = tags = _load_tag_file(bagit_file_path)
@@ -378,13 +373,13 @@ def _open(self):
378373
def manifest_files(self):
379374
for filename in ["manifest-%s.txt" % a for a in CHECKSUM_ALGOS]:
380375
f = os.path.join(self.path, filename)
381-
if isfile(f):
376+
if os.path.isfile(f):
382377
yield f
383378

384379
def tagmanifest_files(self):
385380
for filename in ["tagmanifest-%s.txt" % a for a in CHECKSUM_ALGOS]:
386381
f = os.path.join(self.path, filename)
387-
if isfile(f):
382+
if os.path.isfile(f):
388383
yield f
389384

390385
def compare_manifests_with_fs(self):
@@ -558,7 +553,7 @@ def fetch_entries(self):
558553

559554
fetch_file_path = os.path.join(self.path, "fetch.txt")
560555

561-
if isfile(fetch_file_path):
556+
if os.path.isfile(fetch_file_path):
562557
with open_text_file(
563558
fetch_file_path, "r", encoding=self.encoding
564559
) as fetch_file:
@@ -744,7 +739,7 @@ def _validate_structure(self):
744739
def _validate_structure_payload_directory(self):
745740
data_dir_path = os.path.join(self.path, "data")
746741

747-
if not isdir(data_dir_path):
742+
if not os.path.isdir(data_dir_path):
748743
raise BagValidationError(
749744
_("Expected data directory %s does not exist") % data_dir_path
750745
)
@@ -1284,14 +1279,14 @@ def make_manifests(data_dir, processes, algorithms=DEFAULT_CHECKSUMS, encoding="
12841279

12851280

12861281
def _make_tagmanifest_file(alg, bag_dir, encoding="utf-8"):
1287-
tagmanifest_file = join(bag_dir, "tagmanifest-%s.txt" % alg)
1282+
tagmanifest_file = os.path.join(bag_dir, "tagmanifest-%s.txt" % alg)
12881283
LOGGER.info(_("Creating %s"), tagmanifest_file)
12891284

12901285
checksums = []
12911286
for f in _find_tag_files(bag_dir):
12921287
if re.match(r"^tagmanifest-.+\.txt$", f):
12931288
continue
1294-
with open(join(bag_dir, f), "rb") as fh:
1289+
with open(os.path.join(bag_dir, f), "rb") as fh:
12951290
m = hashlib.new(alg)
12961291
while True:
12971292
block = fh.read(HASH_BLOCK_SIZE)
@@ -1301,7 +1296,7 @@ def _make_tagmanifest_file(alg, bag_dir, encoding="utf-8"):
13011296
checksums.append((m.hexdigest(), f))
13021297

13031298
with open_text_file(
1304-
join(bag_dir, tagmanifest_file), mode="w", encoding=encoding
1299+
os.path.join(bag_dir, tagmanifest_file), mode="w", encoding=encoding
13051300
) as tagmanifest:
13061301
for digest, filename in checksums:
13071302
tagmanifest.write("%s %s\n" % (digest, filename))
@@ -1317,7 +1312,7 @@ def _find_tag_files(bag_dir):
13171312
if filename.startswith("tagmanifest-"):
13181313
continue
13191314
# remove everything up to the bag_dir directory
1320-
p = join(dir_name, filename)
1315+
p = os.path.join(dir_name, filename)
13211316
yield os.path.relpath(p, bag_dir)
13221317

13231318

0 commit comments

Comments
 (0)