Skip to content

Commit

Permalink
Merge pull request #409 from SynBioDex/407-408-document-improvements
Browse files Browse the repository at this point in the history
Document interface improvements
  • Loading branch information
jakebeal authored Jul 13, 2022
2 parents 908d214 + 9fdb957 commit 02e3421
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
21 changes: 15 additions & 6 deletions sbol3/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import posixpath
import warnings
from pathlib import Path
from typing import Dict, Callable, List, Optional, Any, Union, Iterable

# import typing for typing.Sequence, which we don't want to confuse
Expand Down Expand Up @@ -51,6 +52,12 @@ def register_builder(type_uri: str,
# RDF triples.
_uri_type_map: Dict[str, Callable[[str, str], Identified]] = BUILDER_REGISTER

@staticmethod
def open(location: Union[Path, str], file_format: str = None) -> Document:
doc = Document()
doc.read(location, file_format=file_format)
return doc

def __init__(self):
self.logger = logging.getLogger(SBOL_LOGGER_NAME)
self.objects: List[TopLevel] = []
Expand Down Expand Up @@ -313,15 +320,16 @@ def file_extension(file_format: str) -> str:
raise ValueError('Provided file format is not a valid one.')

# Formats: 'n3', 'nt', 'turtle', 'xml'
def read(self, location: str, file_format: str = None) -> None:
def read(self, location: Union[Path, str], file_format: str = None) -> None:
_location = str(location) # normalize location to a string
if file_format is None:
file_format = self._guess_format(location)
file_format = self._guess_format(_location)
if file_format is None:
raise ValueError('Unable to determine file format')
if file_format == SORTED_NTRIPLES:
file_format = NTRIPLES
graph = rdflib.Graph()
graph.parse(location, format=file_format)
graph.parse(_location, format=file_format)
return self._parse_graph(graph)

# Formats: 'n3', 'nt', 'turtle', 'xml'
Expand Down Expand Up @@ -453,18 +461,19 @@ def write_string(self, file_format: str) -> str:
result = result.decode()
return result

def write(self, fpath: str, file_format: str = None) -> None:
def write(self, fpath: Union[Path, 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.
"""
_fpath = str(fpath) # normalize fpath to a string
if file_format is None:
file_format = self._guess_format(fpath)
file_format = self._guess_format(_fpath)
if file_format is None:
raise ValueError('Unable to determine file format')
with open(fpath, 'w') as outfile:
with open(_fpath, 'w') as outfile:
outfile.write(self.write_string(file_format))

def graph(self) -> rdflib.Graph:
Expand Down
9 changes: 4 additions & 5 deletions test/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import tempfile
import unittest
from pathlib import Path
from typing import Optional

import rdflib
Expand Down Expand Up @@ -56,12 +57,10 @@ def test_file_extension(self):
def test_read_ntriples(self):
# Initial test of Document.read
filename = 'model.nt'
test_path = os.path.join(SBOL3_LOCATION, 'entity', 'model',
filename)
doc = sbol3.Document()
doc.read(test_path)
test_path = Path(SBOL3_LOCATION) / 'entity' / 'model' / filename
doc = sbol3.Document.open(test_path)
with tempfile.TemporaryDirectory() as tmpdirname:
test_file = os.path.join(tmpdirname, filename)
test_file = Path(tmpdirname) / filename
doc.write(test_file, sbol3.NTRIPLES)

def test_read_turtle(self):
Expand Down

0 comments on commit 02e3421

Please sign in to comment.