diff --git a/sbol3/document.py b/sbol3/document.py index bbabcd0..55b2caf 100644 --- a/sbol3/document.py +++ b/sbol3/document.py @@ -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: diff --git a/test/test_document.py b/test/test_document.py index b46fcf5..2655233 100644 --- a/test/test_document.py +++ b/test/test_document.py @@ -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'