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

128 lines
2.7 KiB
Python
Raw Normal View History

2019-10-22 11:10:11 -07:00
"""jc - JSON CLI output utility free Parser
Usage:
2019-12-12 09:47:14 -08:00
2019-10-22 11:10:11 -07:00
specify --free as the first argument if the piped input is coming from free
2019-12-12 09:21:20 -08:00
Compatibility:
2019-12-12 09:47:14 -08:00
2019-12-12 09:21:20 -08:00
'linux'
2019-11-04 15:11:18 -08:00
Examples:
2019-11-04 15:19:50 -08:00
2019-11-11 18:30:46 -08:00
$ free | jc --free -p
[
{
"type": "Mem",
"total": 3861340,
"used": 220508,
"free": 3381972,
"shared": 11800,
"buff_cache": 258860,
"available": 3397784
},
{
"type": "Swap",
"total": 2097148,
"used": 0,
"free": 2097148
}
]
2019-10-22 11:10:11 -07:00
2019-11-11 18:30:46 -08:00
$ free | jc --free -p -r
2019-11-04 15:11:18 -08:00
[
{
2019-11-11 18:30:46 -08:00
"type": "Mem",
"total": "2017300",
"used": "213104",
"free": "1148452",
"shared": "1176",
"buff_cache": "655744",
"available": "1622204"
},
{
"type": "Swap",
"total": "2097148",
"used": "0",
"free": "2097148"
2019-11-04 15:11:18 -08:00
}
]
2019-11-11 18:30:46 -08:00
"""
import jc.utils
2019-12-12 14:11:59 -08:00
import jc.parsers.universal
2019-11-11 18:30:46 -08:00
def process(proc_data):
"""
2019-11-12 11:28:10 -08:00
Final processing to conform to the schema.
Parameters:
2019-11-14 16:32:11 -08:00
2019-11-13 08:04:40 -08:00
proc_data: (dictionary) raw structured data to process
2019-11-12 11:28:10 -08:00
Returns:
dictionary structured data with the following schema:
2019-11-11 18:30:46 -08:00
[
{
"type": string,
"total": integer,
"used": integer,
"free": integer,
"shared": integer,
"buff_cache": integer,
"available": integer
}
]
"""
2019-11-04 15:11:18 -08:00
for entry in proc_data:
int_list = ['total', 'used', 'free', 'shared', 'buff_cache', 'available']
for key in int_list:
if key in entry:
try:
key_int = int(entry[key])
entry[key] = key_int
2019-11-06 08:47:54 -06:00
except (ValueError):
2019-11-04 15:11:18 -08:00
entry[key] = None
return proc_data
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
raw: (boolean) output preprocessed JSON if True
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 raw or processed structured data
2019-11-11 18:30:46 -08:00
"""
2019-11-14 16:32:11 -08:00
2019-11-05 22:42:48 -06:00
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
2019-11-07 08:07:43 -08:00
compatible = ['linux']
if not quiet:
jc.utils.compatibility(__name__, compatible)
2019-10-22 11:10:11 -07:00
cleandata = data.splitlines()
2019-12-12 14:11:59 -08:00
cleandata[0] = cleandata[0].lower()
cleandata[0] = cleandata[0].replace('buff/cache', 'buff_cache')
cleandata[0] = 'type ' + cleandata[0]
2019-10-25 09:46:03 -07:00
2019-12-12 14:11:59 -08:00
raw_output = jc.parsers.universal.simple_table_parse(cleandata)
2019-10-22 11:26:58 -07:00
2019-11-04 15:11:18 -08:00
for entry in raw_output:
2019-10-22 11:26:58 -07:00
entry['type'] = entry['type'].rstrip(':')
2019-11-04 15:11:18 -08:00
if raw:
return raw_output
else:
return process(raw_output)