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