Skip to content

Commit

Permalink
Merge pull request #117 from tcmitchell/115-write-string
Browse files Browse the repository at this point in the history
Add Document.write_string()
  • Loading branch information
tcmitchell authored Oct 21, 2020
2 parents 9962005 + 111d154 commit 20e4838
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
28 changes: 19 additions & 9 deletions sbol3/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,7 @@ def find(self, search_string: str) -> Optional[Identified]:
return obj
return self._find_in_objects(search_string)

def write(self, fpath: str, file_format: str = None) -> None:
if file_format is None:
file_format = self._guess_format(fpath)
if file_format is None:
raise ValueError('Unable to determine file format')
def write_string(self, file_format: str) -> bytes:
graph = self.graph()
if file_format == SORTED_NTRIPLES:
# have RDFlib give us the ntriples as a string
Expand All @@ -240,14 +236,28 @@ def write(self, fpath: str, file_format: str = None) -> None:
lines.sort()
# write out the lines
# RDFlib gives us bytes, so open file in binary mode
with open(fpath, 'wb') as outfile:
outfile.writelines(lines)
result = b''.join(lines)
elif file_format == JSONLD:
context = {f'@{prefix}': uri for prefix, uri in self._namespaces.items()}
context['@vocab'] = 'https://sbolstandard.org/examples/'
graph.serialize(fpath, format=file_format, context=context)
result = graph.serialize(format=file_format, context=context)
else:
graph.serialize(fpath, format=file_format)
result = graph.serialize(format=file_format)
return result

def write(self, fpath: str, file_format: str = None) -> None:
"""Write the document to file.
If file_format is None the desired format is guessed from the
extension of fpath. If file_format cannot be guessed a ValueError
is raised.
"""
if file_format is None:
file_format = self._guess_format(fpath)
if file_format is None:
raise ValueError('Unable to determine file format')
with open(fpath, 'wb') as outfile:
outfile.write(self.write_string(file_format))

def graph(self) -> rdflib.Graph:
"""Convert document to an RDF Graph.
Expand Down
22 changes: 20 additions & 2 deletions test/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ def setUpClass(cls) -> None:

def test_read_ntriples(self):
# Initial test of Document.read
filename = 'model.nt'
test_path = os.path.join(SBOL3_LOCATION, 'entity', 'model',
'model.nt')
filename)
doc = sbol3.Document()
doc.read(test_path)
with tempfile.TemporaryDirectory() as tmpdirname:
test_file = os.path.join(tmpdirname)
test_file = os.path.join(tmpdirname, filename)
doc.write(test_file, sbol3.NTRIPLES)

def test_read_turtle(self):
Expand Down Expand Up @@ -147,6 +148,23 @@ def test_graph(self):
self.assertIn(sbol3.SBOL_DISPLAY_ID, predicates)
self.assertIn(sbol3.SBOL_TYPE, predicates)

def test_write_string(self):
# Make sure Document.write_string produces the same output
# as Document.write. Must use sorted NTriples for this test
# because other formats do not have a guaranteed order.
filename = 'model.nt'
test_path = os.path.join(SBOL3_LOCATION, 'entity', 'model',
filename)
doc = sbol3.Document()
doc.read(test_path)
with tempfile.TemporaryDirectory() as tmpdirname:
test_file = os.path.join(tmpdirname, filename)
doc.write(test_file, sbol3.SORTED_NTRIPLES)
with open(test_file, 'rb') as infile:
expected = infile.read()
actual = doc.write_string(sbol3.SORTED_NTRIPLES)
self.assertEqual(expected, actual)


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

0 comments on commit 20e4838

Please sign in to comment.