Skip to content

Commit

Permalink
Added a new script so you can do xml filename required... from the cl…
Browse files Browse the repository at this point in the history
…i instead of xml filename required...
  • Loading branch information
shanahanjrs committed Feb 5, 2018
1 parent c0baaa7 commit 002e82b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,5 @@ venv.bak/
# mypy
.mypy_cache/

# IDEs
.idea
44 changes: 44 additions & 0 deletions scripts/xmltojson
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
include_package_data=True,
packages=find_packages('.'),
py_modules=['xmltojson'],
scripts=['xmltojson.py']
scripts=['xmltojson.py', 'scripts/xmltojson']
)

0 comments on commit 002e82b

Please sign in to comment.