Skip to content

Commit

Permalink
general enhancements to the Statement, in reading from files and for …
Browse files Browse the repository at this point in the history
…outputting elements only when necessary

git-svn-id: http://sword-app.svn.sourceforge.net/svnroot/sword-app/sss/branches/sss-2@457 2bf6ea0f-123d-0410-b71a-f1a21eb24612
  • Loading branch information
richard-jones committed Jan 20, 2012
1 parent 888788d commit c7c7d0d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
41 changes: 29 additions & 12 deletions sss/core.py
Expand Up @@ -601,7 +601,7 @@ class Statement(object):
"""
Class representing the Statement; a description of the object as it appears on the server
"""
def __init__(self, aggregation_uri=None, rem_uri=None, original_deposits=[], aggregates=[], states=[]):
def __init__(self, rdf_file=None, aggregation_uri=None, rem_uri=None, original_deposits=[], aggregates=[], states=[]):
"""
The statement has 4 important properties:
- aggregation_uri - The URI of the aggregation in ORE terms
Expand All @@ -621,6 +621,10 @@ def __init__(self, aggregation_uri=None, rem_uri=None, original_deposits=[], agg
self.smap = {"rdf" : self.ns.RDF_NS, "ore" : self.ns.ORE_NS, "sword" : self.ns.SWORD_NS}
self.asmap = {"oreatom" : self.ns.ORE_ATOM_NS, "atom" : self.ns.ATOM_NS, "rdf" : self.ns.RDF_NS, "ore" : self.ns.ORE_NS, "sword" : self.ns.SWORD_NS}
self.fmap = {"atom" : self.ns.ATOM_NS, "sword" : self.ns.SWORD_NS}

self.rdf = None
if rdf_file is not None:
self.load_from_rdf(rdf_file)

def __str__(self):
return str(self.aggregation_uri) + ", " + str(self.rem_uri) + ", " + str(self.original_deposits)
Expand All @@ -646,11 +650,16 @@ def add_normalised_aggregations(self, aggs):
if agg not in self.aggregates:
self.aggregates.append(agg)

def load(self, filepath):
def load_from_rdf(self, filepath_or_filehandle):
"""
Populate this statement object from the XML serialised statement to be found at the specified filepath
"""
f = open(filepath, "r")
f = None
if hasattr(filepath_or_filehandle, "read"):
f = filepath_or_filehandle
else:
f = open(filepath_or_filehandle, "r")

rdf = etree.fromstring(f.read())

aggs = []
Expand Down Expand Up @@ -700,6 +709,8 @@ def load(self, filepath):
for agg in aggs:
if agg not in ods:
self.aggregates.append(agg)

self.rdf = rdf

def serialise_rdf(self, existing_rdf_as_string=None):
"""
Expand Down Expand Up @@ -823,7 +834,7 @@ def get_rdf_xml(self, existing_rdf_as_string=None):
rdf = etree.fromstring(existing_rdf_as_string)
is_rem = self._is_rem(rdf)
if is_rem:
aggregation = self._get_aggregation_element()
aggregation = self._get_aggregation_element(rdf)
else:
aggregation = self._get_description_element(rdf, self.aggregation_uri)
else:
Expand Down Expand Up @@ -897,19 +908,25 @@ def get_rdf_xml(self, existing_rdf_as_string=None):
# Build the Description elements for the original deposits, with their sword:depositedOn and sword:packaging
# relations
for (uri, datestamp, format_uri, by, obo) in self.original_deposits:
if uri is None:
continue

desc = etree.SubElement(rdf, self.ns.RDF + "Description", nsmap=self.smap)
desc.set(self.ns.RDF + "about", uri)

format = etree.SubElement(desc, self.ns.SWORD + "packaging", nsmap=self.smap)
format.set(self.ns.RDF + "resource", format_uri)
if format_uri is not None:
format = etree.SubElement(desc, self.ns.SWORD + "packaging", nsmap=self.smap)
format.set(self.ns.RDF + "resource", format_uri)

deposited = etree.SubElement(desc, self.ns.SWORD + "depositedOn", nsmap=self.smap)
deposited.set(self.ns.RDF + "datatype", "http://www.w3.org/2001/XMLSchema#dateTime")
deposited.text = datestamp.strftime("%Y-%m-%dT%H:%M:%SZ")
if datestamp is not None:
deposited = etree.SubElement(desc, self.ns.SWORD + "depositedOn", nsmap=self.smap)
deposited.set(self.ns.RDF + "datatype", "http://www.w3.org/2001/XMLSchema#dateTime")
deposited.text = datestamp.strftime("%Y-%m-%dT%H:%M:%SZ")

deposit_by = etree.SubElement(desc, self.ns.SWORD + "depositedBy", nsmap=self.smap)
deposit_by.set(self.ns.RDF + "datatype", "http://www.w3.org/2001/XMLSchema#string")
deposit_by.text = by
if by is not None:
deposit_by = etree.SubElement(desc, self.ns.SWORD + "depositedBy", nsmap=self.smap)
deposit_by.set(self.ns.RDF + "datatype", "http://www.w3.org/2001/XMLSchema#string")
deposit_by.text = by

if obo is not None:
deposit_obo = etree.SubElement(desc, self.ns.SWORD + "depositedOnBehalfOf", nsmap=self.smap)
Expand Down
3 changes: 1 addition & 2 deletions sss/repository.py
Expand Up @@ -1139,8 +1139,7 @@ def load_statement(self, collection, id):
Returns a Statement object fully populated to represent this object
"""
sfile = os.path.join(self.configuration.store_dir, collection, id, "sss_statement.xml")
s = Statement()
s.load(sfile)
s = Statement(rdf_file=sfile)
return s

def list_content(self, collection, id, exclude=[]):
Expand Down

0 comments on commit c7c7d0d

Please sign in to comment.