Skip to content

Commit 5531241

Browse files
committed
[scripts] Add plain version conv script
1 parent b3188df commit 5531241

1 file changed

Lines changed: 123 additions & 0 deletions

File tree

odml/scripts/odml_conversion.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"""odmlConversion
2+
3+
odmlConversion searches for odML files within a provided SEARCHDIR
4+
and converts them to the newest odML format version.
5+
Original files will never be overwritten.
6+
7+
Usage: odmlconversion [-r] [-o OUT] SEARCHDIR
8+
9+
Arguments:
10+
SEARCHDIR Directory to search for odML files.
11+
12+
Options:
13+
-o OUT Output directory. Must exist if specified.
14+
If not specified, output files will be
15+
written to the current directory.
16+
-r Search recursively.
17+
-h --help Show this screen.
18+
--version Show version.
19+
"""
20+
21+
import os
22+
import pathlib
23+
import sys
24+
import tempfile
25+
26+
try:
27+
from StringIO import StringIO
28+
except ImportError:
29+
from io import StringIO
30+
31+
import odml
32+
33+
from docopt import docopt
34+
from odml.tools.version_converter import VersionConverter as VerConf
35+
36+
try:
37+
unicode = unicode
38+
except NameError:
39+
unicode = str
40+
41+
42+
def run_conversion(file_list, output_dir, report, source_format="XML"):
43+
"""
44+
Convert a list of odML files to the latest odML version.
45+
:param file_list: list of files to be converted.
46+
:param output_dir: Directory where odML files converted to
47+
the latest odML version will be saved.
48+
:param report: Reporting StringIO.
49+
:param source_format: Original file format of the odML source files.
50+
XML, JSON and YAML are supported, default is XML.
51+
"""
52+
# Exceptions are kept as broad as possible to ignore any non-odML or
53+
# invalid odML files and ensuring everything that can be will be converted.
54+
for curr_file in file_list:
55+
file_path = unicode(curr_file.absolute())
56+
report.write("[Info] Handling file '%s'\n" % file_path)
57+
# When loading the current file succeeds, it is
58+
# a recent odML format file and can be ignored.
59+
try:
60+
odml.load(file_path, source_format)
61+
report.write("[Info] Skip recent version file '%s'" % file_path)
62+
except Exception as exc:
63+
out_name = os.path.splitext(os.path.basename(file_path))[0]
64+
outfile = os.path.join(output_dir, "%s_conv.xml" % out_name)
65+
try:
66+
VerConf(file_path).write_to_file(outfile, source_format)
67+
except Exception as exc:
68+
# Ignore files we cannot parse or convert
69+
report.write("[Error] version converting file '%s': '%s'\n" %
70+
(file_path, exc))
71+
72+
73+
def main(args=None):
74+
"""
75+
Convenience script to automatically convert odML files
76+
within a directory (tree) to the newest file version.
77+
Check the cli help for details.
78+
:param args: Command line arguments
79+
"""
80+
parser = docopt(__doc__, argv=args, version="0.1.0")
81+
82+
root = parser['SEARCHDIR']
83+
if not os.path.isdir(root):
84+
print(docopt(__doc__, "-h"))
85+
exit(1)
86+
87+
# Handle all supported odML file formats.
88+
if parser['-r']:
89+
xfiles = list(pathlib.Path(root).rglob('*.odml'))
90+
xfiles.extend(list(pathlib.Path(root).rglob('*.xml')))
91+
jfiles = list(pathlib.Path(root).rglob('*.json'))
92+
yfiles = list(pathlib.Path(root).rglob('*.yaml'))
93+
else:
94+
xfiles = list(pathlib.Path(root).glob('*.odml'))
95+
xfiles.extend(list(pathlib.Path(root).glob('*.xml')))
96+
jfiles = list(pathlib.Path(root).glob('*.json'))
97+
yfiles = list(pathlib.Path(root).glob('*.yaml'))
98+
99+
out_root = os.getcwd()
100+
if parser["-o"]:
101+
if not os.path.isdir(parser["-o"]):
102+
print("[Error] Could not find output directory '%s'" % parser["-o"])
103+
exit(1)
104+
105+
out_root = parser["-o"]
106+
107+
out_dir = tempfile.mkdtemp(prefix="odmlconv_", dir=out_root)
108+
109+
# Use this monkeypatch reporter until there is a way
110+
# to run the converters silently.
111+
report = StringIO()
112+
report.write("[Info] Files will be saved to '%s'\n" % out_dir)
113+
114+
run_conversion(xfiles, out_dir, report)
115+
run_conversion(jfiles, out_dir, report, "JSON")
116+
run_conversion(yfiles, out_dir, report, "YAML")
117+
118+
print(report.getvalue())
119+
report.close()
120+
121+
122+
if __name__ == "__main__":
123+
main(sys.argv[1:])

0 commit comments

Comments
 (0)