Skip to content

Commit

Permalink
Testing addNamespace in conjunction with a custom annotation property
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Bartley committed Jan 23, 2020
1 parent e879c78 commit 2fb31df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
10 changes: 7 additions & 3 deletions sbol/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def __init__(self, filename=None):
# Initialized when parsing a graph.
# Updated when writing a graph.
self.graph = rdflib.Graph()
self.graph.namespace_manager = rdflib.namespace.NamespaceManager(self.graph)
# The Document's register of objects
self.objectCache = {} # Needed?
self.SBOLObjects = {} # Needed?
Expand Down Expand Up @@ -218,8 +217,13 @@ def addNamespace(self, namespace, prefix):
:param prefix: The namespace prefix, eg. sbol
:return:
"""
namespace = rdflib.namespace.Namespace(namespace)
self.graph.namespace_manager.bind(prefix, namespace, override=False)
if namespace[-1] != '#' and namespace[-1] != '/':
raise ValueError('Invalid namespace. Namespace must end with # or /')
namespace = rdflib.term.URIRef(namespace)
# Overwrite the prefix, if the namespace already has a conflicting one
inv_namespace_map = {ns: p for p, ns in self._namespaces.items()}
inv_namespace_map[namespace] = prefix
self._namespaces = {p: ns for ns, p in inv_namespace_map.items()}

def addComponentDefinition(self, sbol_obj):
"""
Expand Down
14 changes: 11 additions & 3 deletions sbol/test/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,22 @@ def test_find_property_value(self):
def test_add_namespace(self):
doc = sbol.Document()
cd = doc.componentDefinitions.create('cd')
cd.foo = sbol.property.LiteralProperty(cd, 'http://examples.org#', '0', '1', 'bar')
cd.foo = sbol.property.LiteralProperty(cd, 'http://examples.org#foo', '0', '1', None, 'bar')
doc.readString(doc.writeString())
namespaces = [n[1] for n in doc.graph.namespace_manager.namespaces()]
self.assertFalse('http://examples.org#' in namespaces)
doc.addNamespace('http://examples.org#', 'foo')
doc.addNamespace('http://examples.org#', 'examples')
cd.foo = sbol.property.LiteralProperty(cd, 'http://examples.org#foo', '0', '1', None, 'bar')
namespaces = [n for n in doc.graph.namespace_manager.namespaces()]
doc.readString(doc.writeString())
namespaces = [n for n in doc.graph.namespace_manager.namespaces()]
self.assertTrue(('foo', URIRef('http://examples.org#')) in namespaces)
self.assertTrue(('examples', URIRef('http://examples.org#')) in namespaces)

def test_namespace_fail(self):
doc = sbol.Document()
with self.assertRaises(ValueError):
doc.addNamespace('http://examples.org', 'foo')


if __name__ == '__main__':
unittest.main()

0 comments on commit 2fb31df

Please sign in to comment.