Skip to content

Commit

Permalink
Profile version (#17)
Browse files Browse the repository at this point in the history
* Set defaults for Serialization and Allow-Fetch.txt if not explicitly provided
* Support BagIt-Profile-Version, bagit-profiles/bagit-profiles-specification#16
* re-enable tests blocked by bagit-profiles/bagit-profiles-specification#17
  • Loading branch information
kba authored and ruebot committed Dec 10, 2018
1 parent 036d7da commit 386156e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
24 changes: 16 additions & 8 deletions bagit_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,19 @@ def get_profile(self):
# are validated in validate_serialization().
def validate(self, bag):
self.report = ProfileValidationReport()
for (fn, msg) in [
(self.validate_bag_info, 'Error in bag-info.txt'),
(self.validate_manifests_required, 'Required manifests not found'),
(self.validate_tag_manifests_required, 'Required tag manifests not found'),
(self.validate_tag_files_required, 'Required tag files not found'),
(self.validate_tag_files_allowed, 'Tag files not allowed'),
(self.validate_allow_fetch, 'fetch.txt is present but is not allowed'),
(self.validate_accept_bagit_version, 'Required BagIt version not found'),
for (fn, msg, min_version) in [
(self.validate_bag_info, 'Error in bag-info.txt', None),
(self.validate_manifests_required, 'Required manifests not found', None),
(self.validate_tag_manifests_required, 'Required tag manifests not found', None),
(self.validate_tag_files_required, 'Required tag files not found', None),
(self.validate_allow_fetch, 'fetch.txt is present but is not allowed', None),
(self.validate_accept_bagit_version, 'Required BagIt version not found', None),
(self.validate_tag_files_allowed, 'Tag files not allowed', (1, 2, 0)),
]:
try:
if min_version and self.profile_version_info < min_version:
logging.info("Skipping %s introduced in version %s (version validated: %s)", fn, min_version, self.profile_version_info)
continue
fn(bag)
except ProfileValidationError as e:
# self._warn("%s: %s" % (msg, e))
Expand All @@ -136,6 +139,11 @@ def validate_bagit_profile(self, profile):
profile['Serialization'] = 'optional'
if 'Allow-Fetch.txt' not in profile:
profile['Allow-Fetch.txt'] = True
if 'BagIt-Profile-Info' in profile and 'BagIt-Profile-Version' in profile['BagIt-Profile-Info']:
profile_version = profile['BagIt-Profile-Info']['BagIt-Profile-Version']
else:
profile_version = '1.1.0'
self.profile_version_info = tuple(int(i) for i in profile_version.split("."))
self.validate_bagit_profile_info(profile)
self.validate_bagit_profile_accept_bagit_versions(profile)

Expand Down
1 change: 1 addition & 0 deletions fixtures/bagProfileBar.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"BagIt-Profile-Info":{
"BagIt-Profile-Identifier":"http://canadiana.org/standards/bagit/tdr_ingest.json",
"BagIt-Profile-Version": "1.2.0",
"Source-Organization":"Candiana.org",
"Contact-Name":"William Wueppelmann",
"Contact-Email":"[email protected]",
Expand Down
1 change: 1 addition & 0 deletions fixtures/test-tag-files-allowed/profile.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"BagIt-Profile-Info": {
"BagIt-Profile-Identifier": "TEST",
"BagIt-Profile-Version": "1.2.0",
"Source-Organization": "bagit-profiles.py",
"Contact-Name": "John Doe",
"Contact-Email": "[email protected]",
Expand Down
18 changes: 12 additions & 6 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from unittest import TestCase, main
from shutil import rmtree, copytree

import bagit
from bagit import Bag
from bagit_profile import Profile, find_tag_files

PROFILE_URL = 'https://raw.github.com/bagit-profiles/bagit-profiles/master/bagProfileBar.json'
Expand All @@ -28,7 +28,7 @@ def setUp(self):

def test_not_given(self):
profile = Profile('TEST', self.profile_dict)
bag = bagit.Bag(self.bagdir)
bag = Bag(self.bagdir)
result = profile.validate(bag)
self.assertTrue(result)

Expand All @@ -37,7 +37,7 @@ def test_required_not_allowed(self):
self.profile_dict["Tag-Files-Required"] = ['tag-foo']
with open(join(self.bagdir, 'tag-foo'), 'w'): pass
profile = Profile('TEST', self.profile_dict)
result = profile.validate(bagit.Bag(self.bagdir))
result = profile.validate(Bag(self.bagdir))
self.assertFalse(result)
self.assertEqual(len(profile.report.errors), 1)
self.assertTrue('Required tag files' in profile.report.errors[0].value)
Expand All @@ -46,7 +46,7 @@ def test_existing_not_allowed(self):
self.profile_dict["Tag-Files-Allowed"] = []
with open(join(self.bagdir, 'tag-foo'), 'w'): pass
profile = Profile('TEST', self.profile_dict)
result = profile.validate(bagit.Bag(self.bagdir))
result = profile.validate(Bag(self.bagdir))
self.assertFalse(result)
self.assertEqual(len(profile.report.errors), 1)
self.assertTrue("Existing tag file" in profile.report.errors[0].value)
Expand All @@ -63,14 +63,20 @@ def test_profile_kwarg(self):
profile_url = Profile(PROFILE_URL)
profile_dict = Profile(PROFILE_URL, profile=self.profile_dict)
profile_str = Profile(PROFILE_URL, profile=self.profile_str)
self.maxDiff = None
self.assertEqual(json.dumps(profile_str.profile), json.dumps(profile_dict.profile), 'Loaded from string')
self.assertEqual(json.dumps(profile_url.profile), json.dumps(profile_dict.profile), 'Loaded from URL')

def testVersionInfo(self):
profile = Profile(PROFILE_URL, profile=self.profile_dict)
self.assertEqual(profile.profile_version_info, (1, 2, 0), 'Bundled: 1.2.0')
del(self.profile_dict['BagIt-Profile-Info']['BagIt-Profile-Version'])
profile = Profile(PROFILE_URL, profile=self.profile_dict)
self.assertEqual(profile.profile_version_info, (1, 1, 0), 'Default profile version 1.1.0')

class Test_bag_profile(TestCase):

def setUp(self):
self.bag = bagit.Bag('fixtures/test-bar')
self.bag = Bag('fixtures/test-bar')
self.profile = Profile(PROFILE_URL)
self.retrieved_profile = self.profile.get_profile()

Expand Down

0 comments on commit 386156e

Please sign in to comment.