diff --git a/jc/cli.py b/jc/cli.py index e81c5c5e..7eaa1707 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -39,6 +39,7 @@ import jc.parsers.systemctl_luf import jc.parsers.uname import jc.parsers.uptime import jc.parsers.w +import jc.parsers.yaml parser_map = { '--arp': jc.parsers.arp, @@ -72,12 +73,13 @@ parser_map = { '--systemctl-luf': jc.parsers.systemctl_luf, '--uname': jc.parsers.uname, '--uptime': jc.parsers.uptime, - '--w': jc.parsers.w + '--w': jc.parsers.w, + '--yaml': jc.parsers.yaml } class info(): - version = '1.6.1' + version = '1.7.1' description = 'jc cli output JSON conversion tool' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' diff --git a/jc/parsers/yaml.py b/jc/parsers/yaml.py new file mode 100644 index 00000000..12cd4753 --- /dev/null +++ b/jc/parsers/yaml.py @@ -0,0 +1,90 @@ +"""jc - JSON CLI output utility yaml Parser + +Usage: + + specify --yaml as the first argument if the piped input is coming from a yaml file + +Compatibility: + + 'linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd' + +Examples: + + $ cat example.yaml | jc --yaml -p + [] + + $ cat example.yaml | jc --yaml -p -r + [] +""" +import jc.utils +from ruamel.yaml import YAML + + +class info(): + version = '1.0' + description = 'yaml file parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + details = 'Using the ruamel.yaml library at https://pypi.org/project/ruamel.yaml/' + + # 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: + + List of dictionaries. Structured data with the following schema: + + [ + { + "yaml": string, + "bar": boolean, + "baz": integer + } + ] + """ + + # rebuild output for added semantic information + 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: + + List of dictionaries. Raw or processed structured data. + """ + if not quiet: + jc.utils.compatibility(__name__, info.compatible) + + raw_output = [] + cleandata = data.split('---') + + if cleandata: + for document in cleandata: + yaml = YAML(typ='safe') + raw_output.append(yaml.load(document)) + + if raw: + return raw_output + else: + return process(raw_output)