Skip to content

Commit 9294b7e

Browse files
committed
[tools] Merge util files
- dict RDFConversionFormats from file 'tools.utils' has been moved to RDF_CONVERSION_FORMATS in file 'tools.parser_utils' and all usages have been switched to the new dict. - dict ConversionFormats from file 'tools.utils' has been moved to the only file its using it, 'tools.converters.format_converter'. - The file 'tools.utils' has been removed. - Docstrings have been updated.
1 parent e7e0ba0 commit 9294b7e

File tree

4 files changed

+44
-43
lines changed

4 files changed

+44
-43
lines changed

odml/tools/converters/format_converter.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import copy
23
import os
34
import re
45
import sys
@@ -7,14 +8,21 @@
78

89
from .. import RDFWriter
910
from . import VersionConverter
10-
from ..utils import ConversionFormats
11+
from ..parser_utils import RDF_CONVERSION_FORMATS
1112

1213
try:
1314
unicode = unicode
1415
except NameError:
1516
unicode = str
1617

1718

19+
CONVERSION_FORMATS = copy.deepcopy(RDF_CONVERSION_FORMATS)
20+
CONVERSION_FORMATS.update({
21+
'v1_1': '.xml',
22+
'odml': '.odml'
23+
})
24+
25+
1826
class FormatConverter(object):
1927

2028
@classmethod
@@ -35,7 +43,7 @@ def convert(cls, args=None):
3543
"""
3644
parser = argparse.ArgumentParser(description="Convert directory with odml files to another format")
3745
parser.add_argument("input_dir", help="Path to input directory")
38-
parser.add_argument("result_format", choices=list(ConversionFormats),
46+
parser.add_argument("result_format", choices=list(CONVERSION_FORMATS),
3947
help="Format of output files")
4048
parser.add_argument("-out", "--output_dir", help="Path for output directory")
4149
parser.add_argument("-r", "--recursive", action="store_true",
@@ -55,11 +63,11 @@ def convert_dir(cls, input_dir, output_dir, parse_subdirs, res_format):
5563
Possible choices: "v1_1" (converts to version 1.1 from version 1 xml)
5664
"odml" (converts to .odml from version 1.1 .xml files)
5765
"turtle", "nt" etc. (converts to rdf formats from version 1.1 .odml files)
58-
(see full list of rdf serializers in utils.ConversionFormats)
66+
(see full list of rdf serializers in CONVERSION_FORMATS)
5967
"""
60-
if res_format not in ConversionFormats:
68+
if res_format not in CONVERSION_FORMATS:
6169
raise ValueError("Format for output files is incorrect. "
62-
"Please choose from the list: {}".format(list(ConversionFormats)))
70+
"Please choose from the list: {}".format(list(CONVERSION_FORMATS)))
6371

6472
cls._check_input_output_directory(input_dir, output_dir)
6573
input_dir = os.path.join(input_dir, '')
@@ -99,14 +107,14 @@ def _convert_file(cls, input_path, output_path, res_format):
99107
p, _ = os.path.splitext(output_path)
100108
output_path = p + ".odml"
101109
odml.save(odml.load(input_path), output_path)
102-
elif res_format in ConversionFormats:
103-
if not output_path.endswith(ConversionFormats[res_format]):
110+
elif res_format in CONVERSION_FORMATS:
111+
if not output_path.endswith(CONVERSION_FORMATS[res_format]):
104112
p, _ = os.path.splitext(output_path)
105-
output_path = p + ConversionFormats[res_format]
113+
output_path = p + CONVERSION_FORMATS[res_format]
106114
RDFWriter(odml.load(input_path)).write_file(output_path, res_format)
107115
else:
108116
raise ValueError("Format for output files is incorrect. "
109-
"Please choose from the list: {}".format(list(ConversionFormats)))
117+
"Please choose from the list: {}".format(list(CONVERSION_FORMATS)))
110118

111119
@staticmethod
112120
def _create_sub_directory(dir_path):

odml/tools/parser_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66
SUPPORTED_PARSERS = ['XML', 'YAML', 'JSON', 'RDF']
77

88

9+
RDF_CONVERSION_FORMATS = {
10+
# rdflib version "4.2.2" serialization formats
11+
'xml': '.rdf',
12+
'pretty-xml': '.rdf',
13+
'trix': '.rdf',
14+
'n3': '.n3',
15+
'turtle': '.ttl',
16+
'ttl': '.ttl',
17+
'ntriples': '.nt',
18+
'nt': '.nt',
19+
'nt11': '.nt',
20+
'trig': '.trig',
21+
'json-ld': '.jsonld'
22+
}
23+
24+
925
class ParserException(Exception):
1026
"""
1127
Exception wrapper used by various odML parsers.

odml/tools/rdf_converter.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
from ..format import Format, Document, Section, Property
1919
from ..info import FORMAT_VERSION, INSTALL_PATH
2020
from .dict_parser import DictReader
21-
from .parser_utils import ParserException
22-
from .utils import RDFConversionFormats
21+
from .parser_utils import ParserException, RDF_CONVERSION_FORMATS
2322

2423
try:
2524
unicode = unicode
@@ -303,15 +302,15 @@ def get_rdf_str(self, rdf_format="turtle"):
303302
Convert the current odML content of the parser to a common RDF graph
304303
and return the graph as a string object in the specified RDF format.
305304
306-
:param rdf_format: possible formats: 'xml', 'n3', 'turtle', 'nt', 'pretty-xml',
307-
'trix', 'trig', 'nquads', 'json-ld'.
308-
Full lists see in utils.RDFConversionFormats
305+
:param rdf_format: RDF output format. Default format is 'turtle'.
306+
Available formats: 'xml', 'n3', 'turtle', 'nt',
307+
'pretty-xml', 'trix', 'trig', 'nquads', 'json-ld'.
309308
310309
:return: string object
311310
"""
312-
if rdf_format not in RDFConversionFormats:
311+
if rdf_format not in RDF_CONVERSION_FORMATS:
313312
msg = "odml.RDFWriter.get_rdf_str: Format for output files is incorrect."
314-
msg = "%s Please choose from the list: %s" % (msg, list(RDFConversionFormats))
313+
msg = "%s Please choose from the list: %s" % (msg, list(RDF_CONVERSION_FORMATS))
315314
raise ValueError(msg)
316315

317316
return self.convert_to_rdf().serialize(format=rdf_format).decode("utf-8")
@@ -323,14 +322,14 @@ def write_file(self, filename, rdf_format="turtle"):
323322
RDF output format.
324323
325324
:param filename:
326-
:param rdf_format: Possible RDF output format. See utils.RDFConversionFormats
327-
for a full list of supported formats.
328-
Default format is 'turtle'.
325+
:param rdf_format: RDF output format. Default format is 'turtle'.
326+
Available formats: 'xml', 'n3', 'turtle', 'nt',
327+
'pretty-xml', 'trix', 'trig', 'nquads', 'json-ld'.
329328
"""
330329
data = self.get_rdf_str(rdf_format)
331330
filename_ext = filename
332-
if filename.find(RDFConversionFormats.get(rdf_format)) < 0:
333-
filename_ext += RDFConversionFormats.get(rdf_format)
331+
if filename.find(RDF_CONVERSION_FORMATS.get(rdf_format)) < 0:
332+
filename_ext += RDF_CONVERSION_FORMATS.get(rdf_format)
334333

335334
with open(filename_ext, "w") as out_file:
336335
out_file.write(data)

odml/tools/utils.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)