Skip to content

Commit

Permalink
Use Inventory more
Browse files Browse the repository at this point in the history
  • Loading branch information
zimeon committed Nov 1, 2024
1 parent 369b0ee commit 6f88b11
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 40 deletions.
48 changes: 43 additions & 5 deletions ocfl/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ def digest_algorithm(self, value):
self.data["digestAlgorithm"] = value

@property
def identifier(self):
"""Get object identifier."""
def id(self):
"""Get object id."""
return self.data.get("id")

@identifier.setter
def identifier(self, value):
"""Set object identifier."""
@id.setter
def id(self, value):
"""Set object id."""
self.data["id"] = value

@property
Expand Down Expand Up @@ -126,6 +126,11 @@ def manifest(self):
"""
return self.data.get("manifest", {})

@manifest.setter
def manifest(self, value):
"""Set the manifest to the supplied dict()."""
self.data["manifest"] = value

@property
def manifest_add_if_not_present(self):
"""Get the manifest of digests and corresponding content paths.
Expand All @@ -138,6 +143,16 @@ def manifest_add_if_not_present(self):
self.data["manifest"] = {}
return self.data["manifest"]

@property
def fixity(self):
"""Get fixity block as dict()."""
return self.data["fixity"]

@fixity.setter
def fixity(self, value):
"""Set the fixity to the supplied dict()."""
self.data["fixity"] = value

@property
def content(self):
"""Get all the content paths and their digests stored within the object.
Expand Down Expand Up @@ -172,6 +187,11 @@ def versions_block(self):
"""
return self.data.get("versions", {})

@versions_block.setter
def versions_block(self, value):
"""Set dict of the versions block."""
self.data["versions"] = value

@property
def version_directories(self):
"""List of all version directories.
Expand Down Expand Up @@ -291,6 +311,24 @@ def as_json(self):
"""Serlialize JSON representation."""
return json.dumps(self.data, sort_keys=True, indent=2)

def init_manifest_and_versions(self):
"""Initialize manifest and versions blocks for building new inventory."""
self.manifest = {}
self.versions_block = {}

def add_fixity_type(self, digest_algorithm):
"""Add fixity type with no file data.
Arguments:
digest_algorithm: string of the digest algorithm specifying this
fixity type
If there is no fixity data then will start a fixity block.
"""
if "fixity" not in self.data:
self.data["fixity"] = {}
self.data["fixity"][digest_algorithm] = {}


class Version():
"""Version class to represent version information in an Inventory.
Expand Down
44 changes: 21 additions & 23 deletions ocfl/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import fs.copy

from .digest import file_digest, normalized_digest
from .inventory import Inventory
from .inventory_validator import InventoryValidator
from .object_utils import make_unused_filepath, next_version, \
parse_version_directory, ObjectException
Expand Down Expand Up @@ -170,25 +171,23 @@ def map_filepath(self, filepath, vdir, used):
def start_inventory(self):
"""Create inventory start with metadata from self.
Returns the start of an inventory dict based on the instance data
Returns the start of an Inventory object based on the instance data
in this object.
"""
inventory = dict({
'id': self.id,
'type': 'https://ocfl.io/' + self.spec_version + '/spec/#inventory',
'digestAlgorithm': self.digest_algorithm,
'versions': {},
'manifest': {}
})
inventory = Inventory()
inventory.id = self.id
inventory.spec_version = self.spec_version
inventory.digest_algorithm = self.digest_algorithm
inventory.init_manifest_and_versions()
# Add contentDirectory if not 'content'
if self.content_directory != 'content':
inventory['contentDirectory'] = self.content_directory
inventory.content_directory = self.content_directory
# Add fixity section if requested
if self.fixity is not None and len(self.fixity) > 0:
inventory['fixity'] = {}
for fixity_type in self.fixity:
inventory['fixity'][fixity_type] = {}
inventory.add_fixity_type(fixity_type)
else:
# Make sure None rather than just zero length
self.fixity = None
return inventory

Expand Down Expand Up @@ -275,7 +274,7 @@ def build_inventory(self, src_fs, versions_metadata=None):
"""
if versions_metadata is None:
versions_metadata = {}
inventory = self.start_inventory()
inventory = self.start_inventory().data
# Find the versions
versions = {}
for vdir in src_fs.listdir('/'):
Expand Down Expand Up @@ -398,23 +397,22 @@ def create(self, srcdir, metadata=None, objdir=None):
self.open_fs(objdir, create=True)
inventory = self.start_inventory()
vdir = 'v1'
manifest_to_srcfile = self.add_version(inventory=inventory, src_fs=src_fs,
manifest_to_srcfile = self.add_version(inventory=inventory.data, src_fs=src_fs,
src_dir='', vdir=vdir,
metadata=metadata)
if objdir is None:
return inventory
# Else write out object
self.write_inventory_and_sidecar(inventory, vdir)
# Write object declaration, inventory and sidecar
return inventory.data
# Write out v1 object
self.write_inventory_and_sidecar(inventory.data, vdir)
# Write object root with object declaration, inventory and sidecar
self.write_object_declaration()
self.write_inventory_and_sidecar(inventory)
self.write_inventory_and_sidecar(inventory.data)
# Write version files
for paths in inventory['manifest'].values():
for path in paths:
srcfile = manifest_to_srcfile[path]
self.copy_into_object(src_fs, srcfile, path, create_dirs=True)
for path in inventory.content_paths:
srcfile = manifest_to_srcfile[path]
self.copy_into_object(src_fs, srcfile, path, create_dirs=True)
logging.info("Created OCFL object %s in %s", self.id, objdir)
return inventory
return inventory.data

def update(self, objdir, srcdir=None, metadata=None):
"""Update object creating a new version with content matching srcdir.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_getter_properties_minimal_example(self):
inv = Inventory(filepath="fixtures/1.1/good-objects/minimal_one_version_one_file/inventory.json")
self.assertEqual(inv.spec_version, "1.1")
self.assertEqual(inv.digest_algorithm, "sha512")
self.assertEqual(inv.identifier, "ark:123/abc")
self.assertEqual(inv.id, "ark:123/abc")
self.assertEqual(inv.head, "v1")
self.assertEqual(inv.content_paths, ["v1/content/a_file.txt"])
self.assertEqual(inv.digest_for_content_path("v1/content/a_file.txt"),
Expand All @@ -42,7 +42,7 @@ def test_creation_of_minimal_example(self):
inv = Inventory()
inv.spec_version = "1.1"
inv.digest_algorithm = "sha512"
inv.identifier = "ark:123/abc"
inv.id = "ark:123/abc"
v = inv.add_version()
v.created = "2019-01-01T02:03:04Z"
v.message = "An version with one file"
Expand Down
21 changes: 11 additions & 10 deletions tests/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,23 @@ def test04_start_inventory(self):
"""Test start_inventory mehthod stub."""
oo = Object(identifier="info:a", digest_algorithm="sha256")
inventory = oo.start_inventory()
self.assertEqual(inventory['id'], "info:a")
self.assertEqual(inventory['digestAlgorithm'], "sha256")
self.assertEqual(inventory['versions'], {})
self.assertEqual(inventory['manifest'], {})
self.assertNotIn('contentDirectory', inventory)
self.assertNotIn('fixity', inventory)
self.assertEqual(inventory.id, "info:a")
self.assertEqual(inventory.digest_algorithm, "sha256")
self.assertEqual(inventory.versions_block, {})
self.assertEqual(inventory.manifest, {})
self.assertNotIn('contentDirectory', inventory.data)
self.assertNotIn('fixity', inventory.data)
#
oo = Object(identifier="info:b", digest_algorithm="sha256",
fixity=['md5', 'sha1'])
inventory = oo.start_inventory()
self.assertEqual(inventory['fixity'], {'md5': {}, 'sha1': {}})
self.assertEqual(inventory.fixity, {'md5': {}, 'sha1': {}})
#
oo = Object(identifier="info:b", content_directory="stuff")
inventory = oo.start_inventory()
self.assertEqual(inventory['id'], "info:b")
self.assertEqual(inventory['contentDirectory'], "stuff")
self.assertEqual(inventory['digestAlgorithm'], "sha512")
self.assertEqual(inventory.id, "info:b")
self.assertEqual(inventory.content_directory, "stuff")
self.assertEqual(inventory.digest_algorithm, "sha512")

def test05_add_version(self):
"""Test add_version method."""
Expand Down

0 comments on commit 6f88b11

Please sign in to comment.