Skip to content
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
35 changes: 11 additions & 24 deletions odml/tools/format_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import odml
from .rdf_converter import RDFWriter
from .version_converter import VersionConverter
from .utils import ConversionFormats

try:
unicode = unicode
Expand All @@ -15,23 +16,6 @@

class FormatConverter(object):

_conversion_formats = {
'v1_1': '.xml',
'odml': '.odml',
# rdflib version "4.2.2" serialization formats
'xml': '.rdf',
'pretty-xml': '.rdf',
'trix': '.rdf',
'n3': '.n3',
'turtle': '.ttl',
'ttl': '.ttl',
'ntriples': '.nt',
'nt': '.nt',
'nt11': '.nt',
'trig': '.trig',
'json-ld': '.jsonld'
}

@classmethod
def convert(cls, args=None):
"""
Expand All @@ -50,7 +34,7 @@ def convert(cls, args=None):
"""
parser = argparse.ArgumentParser(description="Convert directory with odml files to another format")
parser.add_argument("input_dir", help="Path to input directory")
parser.add_argument("result_format", choices=list(cls._conversion_formats),
parser.add_argument("result_format", choices=list(ConversionFormats),
help="Format of output files")
parser.add_argument("-out", "--output_dir", help="Path for output directory")
parser.add_argument("-r", "--recursive", action="store_true",
Expand All @@ -70,11 +54,11 @@ def convert_dir(cls, input_dir, output_dir, parse_subdirs, res_format):
Possible choices: "v1_1" (converts to version 1.1 from version 1 xml)
"odml" (converts to .odml from version 1.1 .xml files)
"turtle", "nt" etc. (converts to rdf formats from version 1.1 .odml files)
(see full list of rdf serializers in FormatConverter._conversion_formats)
(see full list of rdf serializers in utils.ConversionFormats)
"""
if res_format not in cls._conversion_formats:
if res_format not in ConversionFormats:
raise ValueError("Format for output files is incorrect. "
"Please choose from the list: {}".format(cls._conversion_formats.keys()))
"Please choose from the list: {}".format(list(ConversionFormats)))

cls._check_input_output_directory(input_dir, output_dir)
input_dir = os.path.join(input_dir, '')
Expand Down Expand Up @@ -114,11 +98,14 @@ def _convert_file(cls, input_path, output_path, res_format):
p, _ = os.path.splitext(output_path)
output_path = p + ".odml"
odml.save(odml.load(input_path), output_path)
elif res_format in cls._conversion_formats:
if not output_path.endswith(cls._conversion_formats[res_format]):
elif res_format in ConversionFormats:
if not output_path.endswith(ConversionFormats[res_format]):
p, _ = os.path.splitext(output_path)
output_path = p + cls._conversion_formats[res_format]
output_path = p + ConversionFormats[res_format]
RDFWriter(odml.load(input_path)).write_file(output_path, res_format)
else:
raise ValueError("Format for output files is incorrect. "
"Please choose from the list: {}".format(list(ConversionFormats)))

@staticmethod
def _create_sub_directory(dir_path):
Expand Down
29 changes: 18 additions & 11 deletions odml/tools/rdf_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .dict_parser import DictReader
from .parser_utils import ParserException
from ..info import FORMAT_VERSION
from .utils import RDFConversionFormats

try:
unicode = unicode
Expand All @@ -33,7 +34,7 @@ class RDFWriter(object):

def __init__(self, odml_documents):
"""
:param odml_documents: list of odml documents
:param odml_documents: list of odml documents
"""
self.docs = odml_documents if not isinstance(odml_documents, odml.doc.BaseDocument) else [odml_documents]
self.hub_root = None
Expand Down Expand Up @@ -66,9 +67,9 @@ def convert_to_rdf(self):
def save_element(self, e, node=None):
"""
Save the current element to the RDF graph
:param e: current element
:param e: current element
:param node: A node to pass the earlier created node to inner elements
:return: the RDF graph
:return: the RDF graph
"""
fmt = e.format()

Expand Down Expand Up @@ -180,21 +181,27 @@ def __str__(self):
def __unicode__(self):
return self.convert_to_rdf().serialize(format='turtle').decode("utf-8")

def get_rdf_str(self, rdf_format):
def get_rdf_str(self, rdf_format="turtle"):
"""
Get converted into one of the supported formats data
:param rdf_format: possible formats: 'xml', 'n3', 'turtle',
'nt', 'pretty-xml', 'trix',
Get converted into one of the supported formats data
:param rdf_format: possible formats: 'xml', 'n3', 'turtle',
'nt', 'pretty-xml', 'trix',
'trig', 'nquads', 'json-ld'.
Full lists see in odml.tools.format_converter.FormatConverter._conversion_formats
Full lists see in utils.RDFConversionFormats
:return: string object
"""
if rdf_format not in RDFConversionFormats:
raise ValueError("odml.RDFWriter.get_rdf_str: Format for output files is incorrect. "
"Please choose from the list: {}".format(list(RDFConversionFormats)))
return self.convert_to_rdf().serialize(format=rdf_format).decode("utf-8")

def write_file(self, filename, rdf_format):
def write_file(self, filename, rdf_format="turtle"):
data = self.get_rdf_str(rdf_format)
with open(filename, "w") as file:
file.write(data)
filename_ext = filename
if filename.find(RDFConversionFormats.get(rdf_format)) < 0:
filename_ext += RDFConversionFormats.get(rdf_format)
with open(filename_ext, "w") as wFile:
wFile.write(data)


class RDFReader(object):
Expand Down
22 changes: 22 additions & 0 deletions odml/tools/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import copy

RDFConversionFormats = {
# rdflib version "4.2.2" serialization formats
'xml': '.rdf',
'pretty-xml': '.rdf',
'trix': '.rdf',
'n3': '.n3',
'turtle': '.ttl',
'ttl': '.ttl',
'ntriples': '.nt',
'nt': '.nt',
'nt11': '.nt',
'trig': '.trig',
'json-ld': '.jsonld'
}

ConversionFormats = copy.deepcopy(RDFConversionFormats)
ConversionFormats.update({
'v1_1': '.xml',
'odml': '.odml'
})
6 changes: 6 additions & 0 deletions test/test_rdf_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,9 @@ def test_adding_other_entities_properties(self):
self.assertEqual(len(list(w.g.subjects(predicate=odmlns.hasDtype, object=Literal(p_dtype)))), 1)
self.assertEqual(len(list(w.g.subjects(predicate=odmlns.hasValueOrigin, object=Literal(p_value_origin)))), 1)
self.assertEqual(len(list(w.g.subjects(predicate=odmlns.hasReference, object=Literal(p_ref)))), 1)

def test_get_rdf_string(self):
w = RDFWriter([self.doc1])
w.get_rdf_str()
with self.assertRaises(ValueError):
w.get_rdf_str("abc")