Skip to content
Snippets Groups Projects
Commit 8eac54af authored by Erwan Rouchet's avatar Erwan Rouchet
Browse files

Add custom volume name option

parent 764dffda
No related branches found
No related tags found
1 merge request!24Import volumes from CSV
......@@ -109,9 +109,10 @@ class ManifestsImporter(ABC):
Parses JSON manifests and annotation data to import them in the database.
"""
def __init__(self, imgserv, offline=False, annotations=True):
def __init__(self, imgserv, offline=False, annotations=True, volume_name=None):
"""Initialize a manifest importer
`imgserv` can be either one ImageServer or a list of ImageServers."""
`imgserv` can be either one ImageServer or a list of ImageServers.
When `volume_name` is set, it overrides the manifest volume name."""
if isinstance(imgserv, ImageServer):
self.imgserv = [imgserv]
else:
......@@ -120,6 +121,7 @@ class ManifestsImporter(ABC):
self.offline = offline
self.annotations = annotations
self.volume_name = volume_name
# This dictionary associates canvas IDs with images and pages
# Filled by parse_manifest ; used by parse_annotation_list
......@@ -163,8 +165,9 @@ class ManifestsImporter(ABC):
self.parse_annotation_list(stream)
break
def parse_manifest(self, stream):
"""Parse a IIIF manifest loaded as a stream."""
def _extract_volume_name(self, stream):
if self.volume_name is not None:
return self.volume_name
# Get this file's volume range ID from the top-most structure
try:
range_id = next(struct['ranges'][0]
......@@ -175,11 +178,15 @@ class ManifestsImporter(ABC):
# Get our volume's structure and label
vol_struct = next(struct for struct in ijson.items(stream, "structures.item")
if struct.get('@id') == range_id)
vol_name = vol_struct['label']
return vol_struct['label']
except StopIteration:
logger.debug("Invalid structures in manifest - using manifest label as volume name")
stream.seek(0)
vol_name = next(ijson.items(stream, 'label'))
return next(ijson.items(stream, 'label'))
def parse_manifest(self, stream):
"""Parse a IIIF manifest loaded as a stream."""
vol_name = self._extract_volume_name(stream)
# Create a volume and a register
logger.debug("Creating volume {}".format(vol_name))
......
......@@ -38,6 +38,10 @@ class Command(BaseCommand):
help='Ignore annotation files',
dest='annotations',
)
parser.add_argument(
'--volume-name',
help='Override the manifest volume name with a custom name.',
)
def handle(self, *args, **options):
# Handle verbosity level
......@@ -67,6 +71,7 @@ class Command(BaseCommand):
options['manifest_folder'],
offline=options['offline'],
annotations=options['annotations'],
volume_name=options['volume_name'],
)
importer.run()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment