6
6
import odml
7
7
from .rdf_converter import RDFWriter
8
8
from .version_converter import VersionConverter
9
+ from .utils import ConversionFormats
9
10
10
11
try :
11
12
unicode = unicode
15
16
16
17
class FormatConverter (object ):
17
18
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
-
35
19
@classmethod
36
20
def convert (cls , args = None ):
37
21
"""
@@ -50,7 +34,7 @@ def convert(cls, args=None):
50
34
"""
51
35
parser = argparse .ArgumentParser (description = "Convert directory with odml files to another format" )
52
36
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 ),
54
38
help = "Format of output files" )
55
39
parser .add_argument ("-out" , "--output_dir" , help = "Path for output directory" )
56
40
parser .add_argument ("-r" , "--recursive" , action = "store_true" ,
@@ -70,11 +54,11 @@ def convert_dir(cls, input_dir, output_dir, parse_subdirs, res_format):
70
54
Possible choices: "v1_1" (converts to version 1.1 from version 1 xml)
71
55
"odml" (converts to .odml from version 1.1 .xml files)
72
56
"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 )
74
58
"""
75
- if res_format not in cls . _conversion_formats :
59
+ if res_format not in ConversionFormats :
76
60
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 )))
78
62
79
63
cls ._check_input_output_directory (input_dir , output_dir )
80
64
input_dir = os .path .join (input_dir , '' )
@@ -114,11 +98,14 @@ def _convert_file(cls, input_path, output_path, res_format):
114
98
p , _ = os .path .splitext (output_path )
115
99
output_path = p + ".odml"
116
100
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 ]):
119
103
p , _ = os .path .splitext (output_path )
120
- output_path = p + cls . _conversion_formats [res_format ]
104
+ output_path = p + ConversionFormats [res_format ]
121
105
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 )))
122
109
123
110
@staticmethod
124
111
def _create_sub_directory (dir_path ):
0 commit comments