From 002e82bd33c42d2b2408045bf115019be97c1b49 Mon Sep 17 00:00:00 2001 From: shanahanjrs Date: Sun, 4 Feb 2018 20:46:23 -0500 Subject: [PATCH] Added a new script so you can do xml filename required... from the cli instead of xml filename required... --- .gitignore | 2 ++ scripts/xmltojson | 44 ++++++++++++++++++++++++++++++++++++++++++++ setup.py | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 scripts/xmltojson diff --git a/.gitignore b/.gitignore index ff77194..b7c2004 100644 --- a/.gitignore +++ b/.gitignore @@ -103,3 +103,5 @@ venv.bak/ # mypy .mypy_cache/ +# IDEs +.idea diff --git a/scripts/xmltojson b/scripts/xmltojson new file mode 100644 index 0000000..d9e78d5 --- /dev/null +++ b/scripts/xmltojson @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +import os +import sys + +import xmltojson + +def main(): + + if not len(sys.argv) > 1: + print('xml filename required...') + sys.exit(1) + + xml_filename = sys.argv[1] + + option_version = ['v', '-v', '--v', 'version', '-version', '--version'] + option_file = ['f', '-f', '--f', 'file', '-file', '--file'] + + if xmltojson._lists_share_element(sys.argv, option_version): + xmltojson._usage() + sys.exit() + + if not os.path.exists(xml_filename): + print('File not found...') + sys.exit(1) + + # Create filename for json output file + json_filename = xmltojson._tr_xml_to_json_extension(xml_filename) + + # Grab xml from file + xml_string = xmltojson._read_xml_file(xml_filename) + + # Translate xml to json from the string we got + json_obj = xmltojson.parse(xml_string) + + # Create json output file + if xmltojson._lists_share_element(sys.argv, option_file): + xmltojson._write_output_file(json_filename, json_obj) + else: + print(json_obj) + + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py index 4a769a6..6e4d2a7 100644 --- a/setup.py +++ b/setup.py @@ -32,5 +32,5 @@ include_package_data=True, packages=find_packages('.'), py_modules=['xmltojson'], - scripts=['xmltojson.py'] + scripts=['xmltojson.py', 'scripts/xmltojson'] )