diff --git a/README.md b/README.md index 339deb06..3d86869c 100755 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ jc PARSER [OPTIONS] - `--uname` enables the `uname -a` parser - `--uptime` enables the `uptime` parser - `--w` enables the `w` parser +- `--xml` enables the `XML` file parser - `--yaml` enables the `YAML` file parser ### Options @@ -1635,6 +1636,51 @@ $ w | jc --w -p } ] ``` +### XML +``` +$ cat cd_catalog.xml + + + + Empire Burlesque + Bob Dylan + USA + Columbia + 10.90 + 1985 + + + Hide your heart + Bonnie Tyler + UK + CBS Records + 9.90 + 1988 + + ... + +$ cat cd_catalog.xml | jc --xml -p +{ + "CATALOG": { + "CD": [ + { + "TITLE": "Empire Burlesque", + "ARTIST": "Bob Dylan", + "COUNTRY": "USA", + "COMPANY": "Columbia", + "PRICE": "10.90", + "YEAR": "1985" + }, + { + "TITLE": "Hide your heart", + "ARTIST": "Bonnie Tyler", + "COUNTRY": "UK", + "COMPANY": "CBS Records", + "PRICE": "9.90", + "YEAR": "1988" + }, +... +``` ### YAML ``` $ cat istio-mtls-permissive.yaml diff --git a/changelog.txt b/changelog.txt index b1cc6ef9..2f34da24 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,8 +1,9 @@ jc changelog 202001XX v1.7.1 -- Add YAML file parser (tested on linux and OSX) +- Add YAML file parser - Add ini file parser +- Add XML file parser - Add __version__ variable to parser modules 20191217 v1.6.1 diff --git a/jc/cli.py b/jc/cli.py index f0604f7c..0bb3ea0a 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -40,8 +40,10 @@ import jc.parsers.systemctl_luf import jc.parsers.uname import jc.parsers.uptime import jc.parsers.w +import jc.parsers.xml import jc.parsers.yaml + parser_map = { '--arp': jc.parsers.arp, '--crontab': jc.parsers.crontab, @@ -76,6 +78,7 @@ parser_map = { '--uname': jc.parsers.uname, '--uptime': jc.parsers.uptime, '--w': jc.parsers.w, + '--xml': jc.parsers.xml, '--yaml': jc.parsers.yaml } diff --git a/jc/parsers/xml.py b/jc/parsers/xml.py new file mode 100644 index 00000000..5206fbc8 --- /dev/null +++ b/jc/parsers/xml.py @@ -0,0 +1,119 @@ +"""jc - JSON CLI output utility XML Parser + +Usage: + + specify --xml as the first argument if the piped input is coming from a XML file + +Compatibility: + + 'linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd' + +Examples: + + $ cat cd_catalog.xml + + + + Empire Burlesque + Bob Dylan + USA + Columbia + 10.90 + 1985 + + + Hide your heart + Bonnie Tyler + UK + CBS Records + 9.90 + 1988 + + ... + + $ cat cd_catalog.xml | jc --xml -p + { + "CATALOG": { + "CD": [ + { + "TITLE": "Empire Burlesque", + "ARTIST": "Bob Dylan", + "COUNTRY": "USA", + "COMPANY": "Columbia", + "PRICE": "10.90", + "YEAR": "1985" + }, + { + "TITLE": "Hide your heart", + "ARTIST": "Bonnie Tyler", + "COUNTRY": "UK", + "COMPANY": "CBS Records", + "PRICE": "9.90", + "YEAR": "1988" + }, + ... +""" +import jc.utils +import xmltodict + + +class info(): + version = '1.0' + description = 'XML file parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + details = 'Using the xmltodict library at https://github.com/martinblech/xmltodict' + + # compatible options: linux, darwin, cygwin, win32, aix, freebsd + compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] + + +__version__ = info.version + + +def process(proc_data): + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (dictionary) raw structured data to process + + Returns: + + Dictionary representing an XML document: + + { + XML Document converted to a Dictionary + See https://github.com/martinblech/xmltodict for details + } + """ + + # No further processing + return proc_data + + +def parse(data, raw=False, quiet=False): + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) output preprocessed JSON if True + quiet: (boolean) suppress warning messages if True + + Returns: + + Dictionary. Raw or processed structured data. + """ + if not quiet: + jc.utils.compatibility(__name__, info.compatible) + + if data: + raw_output = xmltodict.parse(data) + + if raw: + return raw_output + else: + return process(raw_output) diff --git a/setup.py b/setup.py index d073a1a5..d316a499 100755 --- a/setup.py +++ b/setup.py @@ -11,7 +11,8 @@ setuptools.setup( description='This tool serializes the output of popular command line tools to structured JSON output.', install_requires=[ 'ifconfig-parser>=0.0.5', - 'ruamel.yaml>=0.15.0' + 'ruamel.yaml>=0.15.0', + 'xmltodict>=0.12.0' ], license='MIT', long_description=long_description,