1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-19 00:17:51 +02:00
Files
jc/jc/parsers/env.py

149 lines
3.1 KiB
Python
Raw Normal View History

2024-03-15 12:17:39 -07:00
r"""jc - JSON Convert `env` and `printenv` command output parser
2019-10-22 11:10:11 -07:00
2022-01-19 17:29:22 -08:00
This parser will output a list of dictionaries each containing `name` and
`value` keys. If you would like a simple dictionary output, then use the
`-r` command-line option or the `raw=True` argument in the `parse()`
function.
2020-08-06 07:48:08 -07:00
2020-08-05 13:32:59 -07:00
Usage (cli):
2019-12-12 09:47:14 -08:00
2020-08-05 15:34:17 -07:00
$ env | jc --env
2022-08-15 13:51:48 -07:00
or
2020-08-05 15:34:17 -07:00
$ jc env
2019-10-22 11:10:11 -07:00
2020-08-05 13:32:59 -07:00
Usage (module):
2022-01-18 15:38:03 -08:00
import jc
result = jc.parse('env', env_command_output)
2021-04-08 12:42:01 -07:00
Schema:
[
{
"name": string,
"value": string
}
]
2019-11-04 15:03:16 -08:00
Examples:
2019-10-24 17:11:17 -07:00
2019-11-11 18:30:46 -08:00
$ env | jc --env -p
[
{
"name": "XDG_SESSION_ID",
"value": "1"
},
{
"name": "HOSTNAME",
"value": "localhost.localdomain"
},
{
"name": "TERM",
"value": "vt220"
},
{
"name": "SHELL",
"value": "/bin/bash"
},
{
"name": "HISTSIZE",
"value": "1000"
},
...
]
$ env | jc --env -p -r
{
"TERM": "xterm-256color",
"SHELL": "/bin/bash",
"USER": "root",
"PATH": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
"PWD": "/root",
"LANG": "en_US.UTF-8",
"HOME": "/root",
"LOGNAME": "root",
"_": "/usr/bin/env"
}
2019-10-22 11:10:11 -07:00
"""
import re
2019-11-07 08:07:43 -08:00
import jc.utils
2019-10-22 11:10:11 -07:00
2019-12-13 20:01:51 -08:00
class info():
2021-04-08 12:42:01 -07:00
"""Provides parser metadata (version, author, etc.)"""
version = '1.5'
description = '`env` command parser'
2019-12-13 20:01:51 -08:00
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd']
2020-12-31 14:11:25 -08:00
magic_commands = ['env', 'printenv']
tags = ['command']
2019-12-13 20:01:51 -08:00
2020-02-03 16:11:58 -08:00
__version__ = info.version
VAR_DEF_PATTERN = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*=\S*.*$')
2020-02-03 16:11:58 -08:00
2021-04-08 12:42:01 -07:00
def _process(proc_data):
2019-11-11 18:30:46 -08:00
"""
2019-11-12 11:28:10 -08:00
Final processing to conform to the schema.
Parameters:
2019-11-14 16:32:11 -08:00
2021-01-04 18:01:16 -08:00
proc_data: (Dictionary) raw structured data to process
2019-11-12 11:28:10 -08:00
Returns:
2021-04-08 12:42:01 -07:00
List of Dictionaries. Structured data to conform to the schema.
2019-11-11 18:30:46 -08:00
"""
2019-11-04 15:03:16 -08:00
processed = []
for k, v in proc_data.items():
proc_line = {}
proc_line['name'] = k
proc_line['value'] = v
processed.append(proc_line)
return processed
2019-11-07 08:07:43 -08:00
def parse(data, raw=False, quiet=False):
2019-11-11 18:30:46 -08:00
"""
2019-11-12 11:17:33 -08:00
Main text parsing function
2019-11-11 18:30:46 -08:00
2019-11-12 11:17:33 -08:00
Parameters:
2019-11-14 16:32:11 -08:00
2019-11-12 11:17:33 -08:00
data: (string) text data to parse
2022-01-21 07:42:03 -08:00
raw: (boolean) unprocessed output if True
2019-11-12 11:17:33 -08:00
quiet: (boolean) suppress warning messages if True
2019-11-11 18:30:46 -08:00
2019-11-12 11:17:33 -08:00
Returns:
Dictionary of raw structured data or (default)
List of Dictionaries of processed structured data (raw)
2019-11-11 18:30:46 -08:00
"""
jc.utils.compatibility(__name__, info.compatible, quiet)
jc.utils.input_type_check(data)
2019-11-05 22:42:48 -06:00
2019-11-04 15:03:16 -08:00
raw_output = {}
key = ''
value = None
2019-10-22 11:10:11 -07:00
if jc.utils.has_data(data):
for line in data.splitlines():
if VAR_DEF_PATTERN.match(line):
if not value is None:
raw_output[key] = value
key, value = line.split('=', maxsplit=1)
continue
if not value is None:
value = value + '\n' + line
if not value is None:
raw_output[key] = value
2019-10-22 11:10:11 -07:00
return raw_output if raw else _process(raw_output)
2019-10-22 11:10:11 -07:00