Skip to content

Commit ae2474d

Browse files
authored
Merge pull request #322 from fschrader1992/master
LGTM
2 parents 6884a7f + 5460606 commit ae2474d

File tree

4 files changed

+57
-35
lines changed

4 files changed

+57
-35
lines changed

odml/tools/format_converter.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import odml
77
from .rdf_converter import RDFWriter
88
from .version_converter import VersionConverter
9+
from .utils import ConversionFormats
910

1011
try:
1112
unicode = unicode
@@ -15,23 +16,6 @@
1516

1617
class FormatConverter(object):
1718

18-
_conversion_formats = {
19-
'v1_1': '.xml',
20-
'odml': '.odml',
21-
# rdflib version "4.2.2" serialization formats
22-
'xml': '.rdf',
23-
'pretty-xml': '.rdf',
24-
'trix': '.rdf',
25-
'n3': '.n3',
26-
'turtle': '.ttl',
27-
'ttl': '.ttl',
28-
'ntriples': '.nt',
29-
'nt': '.nt',
30-
'nt11': '.nt',
31-
'trig': '.trig',
32-
'json-ld': '.jsonld'
33-
}
34-
3519
@classmethod
3620
def convert(cls, args=None):
3721
"""
@@ -50,7 +34,7 @@ def convert(cls, args=None):
5034
"""
5135
parser = argparse.ArgumentParser(description="Convert directory with odml files to another format")
5236
parser.add_argument("input_dir", help="Path to input directory")
53-
parser.add_argument("result_format", choices=list(cls._conversion_formats),
37+
parser.add_argument("result_format", choices=list(ConversionFormats),
5438
help="Format of output files")
5539
parser.add_argument("-out", "--output_dir", help="Path for output directory")
5640
parser.add_argument("-r", "--recursive", action="store_true",
@@ -70,11 +54,11 @@ def convert_dir(cls, input_dir, output_dir, parse_subdirs, res_format):
7054
Possible choices: "v1_1" (converts to version 1.1 from version 1 xml)
7155
"odml" (converts to .odml from version 1.1 .xml files)
7256
"turtle", "nt" etc. (converts to rdf formats from version 1.1 .odml files)
73-
(see full list of rdf serializers in FormatConverter._conversion_formats)
57+
(see full list of rdf serializers in utils.ConversionFormats)
7458
"""
75-
if res_format not in cls._conversion_formats:
59+
if res_format not in ConversionFormats:
7660
raise ValueError("Format for output files is incorrect. "
77-
"Please choose from the list: {}".format(cls._conversion_formats.keys()))
61+
"Please choose from the list: {}".format(list(ConversionFormats)))
7862

7963
cls._check_input_output_directory(input_dir, output_dir)
8064
input_dir = os.path.join(input_dir, '')
@@ -114,11 +98,14 @@ def _convert_file(cls, input_path, output_path, res_format):
11498
p, _ = os.path.splitext(output_path)
11599
output_path = p + ".odml"
116100
odml.save(odml.load(input_path), output_path)
117-
elif res_format in cls._conversion_formats:
118-
if not output_path.endswith(cls._conversion_formats[res_format]):
101+
elif res_format in ConversionFormats:
102+
if not output_path.endswith(ConversionFormats[res_format]):
119103
p, _ = os.path.splitext(output_path)
120-
output_path = p + cls._conversion_formats[res_format]
104+
output_path = p + ConversionFormats[res_format]
121105
RDFWriter(odml.load(input_path)).write_file(output_path, res_format)
106+
else:
107+
raise ValueError("Format for output files is incorrect. "
108+
"Please choose from the list: {}".format(list(ConversionFormats)))
122109

123110
@staticmethod
124111
def _create_sub_directory(dir_path):

odml/tools/rdf_converter.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .dict_parser import DictReader
1414
from .parser_utils import ParserException
1515
from ..info import FORMAT_VERSION
16+
from .utils import RDFConversionFormats
1617

1718
try:
1819
unicode = unicode
@@ -33,7 +34,7 @@ class RDFWriter(object):
3334

3435
def __init__(self, odml_documents):
3536
"""
36-
:param odml_documents: list of odml documents
37+
:param odml_documents: list of odml documents
3738
"""
3839
self.docs = odml_documents if not isinstance(odml_documents, odml.doc.BaseDocument) else [odml_documents]
3940
self.hub_root = None
@@ -66,9 +67,9 @@ def convert_to_rdf(self):
6667
def save_element(self, e, node=None):
6768
"""
6869
Save the current element to the RDF graph
69-
:param e: current element
70+
:param e: current element
7071
:param node: A node to pass the earlier created node to inner elements
71-
:return: the RDF graph
72+
:return: the RDF graph
7273
"""
7374
fmt = e.format()
7475

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

183-
def get_rdf_str(self, rdf_format):
184+
def get_rdf_str(self, rdf_format="turtle"):
184185
"""
185-
Get converted into one of the supported formats data
186-
:param rdf_format: possible formats: 'xml', 'n3', 'turtle',
187-
'nt', 'pretty-xml', 'trix',
186+
Get converted into one of the supported formats data
187+
:param rdf_format: possible formats: 'xml', 'n3', 'turtle',
188+
'nt', 'pretty-xml', 'trix',
188189
'trig', 'nquads', 'json-ld'.
189-
Full lists see in odml.tools.format_converter.FormatConverter._conversion_formats
190+
Full lists see in utils.RDFConversionFormats
190191
:return: string object
191192
"""
193+
if rdf_format not in RDFConversionFormats:
194+
raise ValueError("odml.RDFWriter.get_rdf_str: Format for output files is incorrect. "
195+
"Please choose from the list: {}".format(list(RDFConversionFormats)))
192196
return self.convert_to_rdf().serialize(format=rdf_format).decode("utf-8")
193197

194-
def write_file(self, filename, rdf_format):
198+
def write_file(self, filename, rdf_format="turtle"):
195199
data = self.get_rdf_str(rdf_format)
196-
with open(filename, "w") as file:
197-
file.write(data)
200+
filename_ext = filename
201+
if filename.find(RDFConversionFormats.get(rdf_format)) < 0:
202+
filename_ext += RDFConversionFormats.get(rdf_format)
203+
with open(filename_ext, "w") as wFile:
204+
wFile.write(data)
198205

199206

200207
class RDFReader(object):

odml/tools/utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import copy
2+
3+
RDFConversionFormats = {
4+
# rdflib version "4.2.2" serialization formats
5+
'xml': '.rdf',
6+
'pretty-xml': '.rdf',
7+
'trix': '.rdf',
8+
'n3': '.n3',
9+
'turtle': '.ttl',
10+
'ttl': '.ttl',
11+
'ntriples': '.nt',
12+
'nt': '.nt',
13+
'nt11': '.nt',
14+
'trig': '.trig',
15+
'json-ld': '.jsonld'
16+
}
17+
18+
ConversionFormats = copy.deepcopy(RDFConversionFormats)
19+
ConversionFormats.update({
20+
'v1_1': '.xml',
21+
'odml': '.odml'
22+
})

test/test_rdf_writer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,9 @@ def test_adding_other_entities_properties(self):
195195
self.assertEqual(len(list(w.g.subjects(predicate=odmlns.hasDtype, object=Literal(p_dtype)))), 1)
196196
self.assertEqual(len(list(w.g.subjects(predicate=odmlns.hasValueOrigin, object=Literal(p_value_origin)))), 1)
197197
self.assertEqual(len(list(w.g.subjects(predicate=odmlns.hasReference, object=Literal(p_ref)))), 1)
198+
199+
def test_get_rdf_string(self):
200+
w = RDFWriter([self.doc1])
201+
w.get_rdf_str()
202+
with self.assertRaises(ValueError):
203+
w.get_rdf_str("abc")

0 commit comments

Comments
 (0)