mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-15 01:24:29 +02:00
add yaml file parser
This commit is contained in:
@ -39,6 +39,7 @@ import jc.parsers.systemctl_luf
|
|||||||
import jc.parsers.uname
|
import jc.parsers.uname
|
||||||
import jc.parsers.uptime
|
import jc.parsers.uptime
|
||||||
import jc.parsers.w
|
import jc.parsers.w
|
||||||
|
import jc.parsers.yaml
|
||||||
|
|
||||||
parser_map = {
|
parser_map = {
|
||||||
'--arp': jc.parsers.arp,
|
'--arp': jc.parsers.arp,
|
||||||
@ -72,12 +73,13 @@ parser_map = {
|
|||||||
'--systemctl-luf': jc.parsers.systemctl_luf,
|
'--systemctl-luf': jc.parsers.systemctl_luf,
|
||||||
'--uname': jc.parsers.uname,
|
'--uname': jc.parsers.uname,
|
||||||
'--uptime': jc.parsers.uptime,
|
'--uptime': jc.parsers.uptime,
|
||||||
'--w': jc.parsers.w
|
'--w': jc.parsers.w,
|
||||||
|
'--yaml': jc.parsers.yaml
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class info():
|
class info():
|
||||||
version = '1.6.1'
|
version = '1.7.1'
|
||||||
description = 'jc cli output JSON conversion tool'
|
description = 'jc cli output JSON conversion tool'
|
||||||
author = 'Kelly Brazil'
|
author = 'Kelly Brazil'
|
||||||
author_email = 'kellyjonbrazil@gmail.com'
|
author_email = 'kellyjonbrazil@gmail.com'
|
||||||
|
90
jc/parsers/yaml.py
Normal file
90
jc/parsers/yaml.py
Normal file
@ -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)
|
Reference in New Issue
Block a user