Skip to content
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

File format to extension function #378

Merged
merged 5 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions sbol3/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,27 @@ def _guess_format(self, fpath: str):
rdf_format = 'nt11'
return rdf_format

@staticmethod
def file_extension(file_format: str) -> str:
"""Return standard extensions when provided the document's file format

:param file_format: The format of the file
:return: A file extension, including the leading '.'
"""
# dictionary having keys as valid file formats,
# and their standard extensions as value
types_with_standard_extension = {
SORTED_NTRIPLES: '.nt',
NTRIPLES: '.nt',
JSONLD: '.json',
RDF_XML: '.xml',
TURTLE: '.ttl'
}
if file_format in types_with_standard_extension:
return types_with_standard_extension[file_format]
else:
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:
if file_format is None:
Expand Down
15 changes: 15 additions & 0 deletions test/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ def document_checker(thing: sbol3.Identified):
self.assertEqual(document, thing.document)
return document_checker

def test_file_extension(self):
""" Test obtaining standard extensions from file format"""
file_format_1 = sbol3.SORTED_NTRIPLES
file_format_2 = sbol3.RDF_XML
file_format_3 = 'Test_Format'
# 1. testing for sorted ntriples
extension_1 = sbol3.Document.file_extension(file_format_1)
self.assertEqual(extension_1, '.nt')
# 2. testing for rdf xml
extension_2 = sbol3.Document.file_extension(file_format_2)
self.assertEqual(extension_2, '.xml')
# 3. for invalid file formats, valueError must be raised
with self.assertRaises(ValueError):
extension_3 = sbol3.Document.file_extension(file_format_3)

def test_read_ntriples(self):
# Initial test of Document.read
filename = 'model.nt'
Expand Down