66import odml
77from .rdf_converter import RDFWriter
88from .version_converter import VersionConverter
9+ from .utils import ConversionFormats
910
1011try :
1112 unicode = unicode
1516
1617class 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 ):
0 commit comments