-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add yapremisrw2, yet another PREMIS reader/writer plugin #34
Open
jrwdunham
wants to merge
6
commits into
master
Choose a base branch
from
dev/issue-11581-premis-parsing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ea868f3
Improved version retrieval in setup.py
jrwdunham ee177cf
Add yapremisrw, Yet Another PREMIS reader/writer
jrwdunham 804a295
Bump version to 0.2.1
jrwdunham afa67e2
Improve version retrieval in setup.py again
jrwdunham 13d78b1
Allow fsentry instances with no amdSecs
jrwdunham 55a394e
WIP: code review fixes
jrwdunham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ pip-delete-this-directory.txt | |
.tox/ | ||
.coverage | ||
.cache | ||
htmlcov | ||
nosetests.xml | ||
coverage.xml | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
Example usage | ||
============= | ||
|
||
Parsing METS documents | ||
---------------------- | ||
|
||
Example of listing the relative file paths of preservation files referenced in | ||
a METS file::: | ||
|
||
import metsrw | ||
|
||
mets = metsrw.METSDocument.fromfile('fixtures/complete_mets_2.xml') | ||
for entry in mets.all_files(): | ||
if entry.use == 'preservation': | ||
print entry.path | ||
|
||
Example of retrieving a file by UUID::: | ||
|
||
import metsrw | ||
|
||
mets = metsrw.METSDocument.fromfile('fixtures/complete_mets_2.xml') | ||
entry = mets.get_file('46b7cb96-792c-4441-a5d6-67c83313501c') | ||
print entry.path | ||
|
||
Creating/modifying METS documents | ||
--------------------------------- | ||
|
||
Example creation of a METS document (without PREMIS or Dublin Core metadata)::: | ||
|
||
import metsrw | ||
import uuid | ||
|
||
mw = metsrw.METSDocument() | ||
|
||
# Create object entries | ||
file1 = metsrw.FSEntry('objects/cat.png', file_uuid=str(uuid.uuid4())) | ||
file2 = metsrw.FSEntry('objects/dog.jpg', file_uuid=str(uuid.uuid4())) | ||
|
||
# Create preservation derivative entries | ||
file1p = metsrw.FSEntry('objects/cat-preservation.tiff', use='preservation', file_uuid=str(uuid.uuid4()), derived_from=file1) | ||
file2p = metsrw.FSEntry('objects/dog-preservation.tiff', use='preservation', file_uuid=str(uuid.uuid4()), derived_from=file2) | ||
|
||
# Create object directory entry | ||
objects = metsrw.FSEntry('objects', type='Directory', children=[file1, file2, file1p, file2p]) | ||
|
||
# Create metadata subdirectories then metadata directory entry | ||
children = [ | ||
metsrw.FSEntry('transfers', type='Directory', children=[]), | ||
metsrw.FSEntry('metadata/metadata.csv', use='metadata', file_uuid=str(uuid.uuid4())), | ||
] | ||
metadata = metsrw.FSEntry('metadata', type='Directory', children=children) | ||
|
||
# Create submission METS entry and submission documentation parent directory entry | ||
children = [ | ||
metsrw.FSEntry('submissionDocumentation/METS.xml', use='submissionDocumentation', file_uuid=str(uuid.uuid4())), | ||
] | ||
sub_doc = metsrw.FSEntry('submissionDocumentation', type='Directory', children=children) | ||
|
||
# Create SIP entry containing objects, metadata, and submission documentaton entries | ||
children = [objects, metadata, sub_doc] | ||
sip = metsrw.FSEntry('sipname-uuid', type='Directory', children=children) | ||
|
||
# Add SIP entry to METS document and write to file | ||
mw.append_file(sip) | ||
mw.write('mets.xml', fully_qualified=True, pretty_print=True) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from __future__ import absolute_import | ||
|
||
import logging | ||
|
||
from .dc import DublinCoreXmlData | ||
from .utils import ( | ||
NAMESPACES, | ||
DUBLINCORE_SCHEMA_LOCATIONS, | ||
lxmlns, | ||
) | ||
from .exceptions import ( | ||
DcError, | ||
ConstructError, | ||
ParseError | ||
) | ||
|
||
|
||
LOGGER = logging.getLogger(__name__) | ||
LOGGER.addHandler(logging.NullHandler()) | ||
|
||
|
||
__all__ = [ | ||
'DublinCoreXmlData', | ||
'NAMESPACES', | ||
'DUBLINCORE_SCHEMA_LOCATIONS', | ||
'lxmlns', | ||
'DcError', | ||
'ConstructError', | ||
'ParseError', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from __future__ import absolute_import | ||
|
||
from collections import OrderedDict | ||
import logging | ||
from lxml import etree | ||
|
||
from .exceptions import ParseError | ||
from .utils import lxmlns, NAMESPACES, DUBLINCORE_SCHEMA_LOCATIONS | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class DublinCoreXmlData(object): | ||
""" | ||
An object representing a METS xmlData element containing a Dublin Core element. | ||
|
||
:raises ParseError: If the root element tag is not xmlData. | ||
""" | ||
DC_ELEMENTS = ['title', 'creator', 'subject', 'description', 'publisher', | ||
'contributor', 'date', 'format', 'identifier', 'source', | ||
'relation', 'language', 'coverage', 'rights'] | ||
|
||
def __init__(self, **kwargs): | ||
for element in self.DC_ELEMENTS: | ||
setattr(self, element, kwargs.get(element)) | ||
|
||
@classmethod | ||
def parse(cls, root): | ||
""" | ||
Parse an xmlData element containing a Dublin Core dublincore element. | ||
|
||
:param root: Element or ElementTree to be parsed into an object. | ||
:raises ParseError: If the root is not xmlData or doesn't contain a dublincore element. | ||
""" | ||
if root.tag != lxmlns('mets') + 'xmlData': | ||
raise ParseError('DublinCoreXmlData can only parse xmlData elements with mets namespace.') | ||
|
||
dc_el = root.find('dcterms:dublincore', namespaces=NAMESPACES) | ||
|
||
if dc_el is None or dc_el.tag != lxmlns('dcterms') + 'dublincore': | ||
raise ParseError('xmlData can only contain a dublincore element with the dcterms namespace.') | ||
|
||
kwargs = {} | ||
|
||
for element in DublinCoreXmlData.DC_ELEMENTS: | ||
kwargs[element] = dc_el.findtext("dc:" + element, namespaces=NAMESPACES) | ||
|
||
return cls(**kwargs) | ||
|
||
fromtree = parse | ||
|
||
def serialize(self): | ||
nsmap = OrderedDict([ | ||
('mets', NAMESPACES['mets']), | ||
('xsi', NAMESPACES['xsi']), | ||
('xlink', NAMESPACES['xlink']) | ||
]) | ||
root = etree.Element(lxmlns('mets') + 'xmlData', nsmap=nsmap) | ||
root.append(self._serialize_dublincore()) | ||
return root | ||
|
||
def _serialize_dublincore(self): | ||
nsmap = OrderedDict([ | ||
('dcterms', NAMESPACES['dcterms']), | ||
('dc', NAMESPACES['dc']) | ||
]) | ||
attrib = {'{}schemaLocation'.format(lxmlns('xsi')): DUBLINCORE_SCHEMA_LOCATIONS} | ||
dc_root = etree.Element(lxmlns('dcterms') + 'dublincore', nsmap=nsmap, attrib=attrib) | ||
|
||
for element in DublinCoreXmlData.DC_ELEMENTS: | ||
dc_el = etree.Element(lxmlns('dc') + element) | ||
dc_el.text = getattr(self, element) | ||
dc_root.append(dc_el) | ||
|
||
return dc_root |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Exceptions for dcrw. | ||
|
||
All exceptions generated by this library will descend from DcError. | ||
""" | ||
|
||
|
||
class DcError(Exception): | ||
""" Base Exception for this module. """ | ||
pass | ||
|
||
|
||
class ConstructError(DcError): | ||
""" Error constructing an object. """ | ||
pass | ||
|
||
|
||
class ParseError(DcError): | ||
""" Error parsing a DC element. """ | ||
pass |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment by @Hwesta: Rather than creating a new attribute, this could use document to store the child document - whether that's a string, ElementTree, or plugin class of the appropriate type.