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
|
|
|
|
|
|
|
|
2019-12-13 20:01:51 -08:00
|
|
|
class info():
|
|
|
|
version = '1.0'
|
|
|
|
description = 'free parser'
|
|
|
|
author = 'Kelly Brazil'
|
|
|
|
author_email = 'kellyjonbrazil@gmail.com'
|
|
|
|
|
|
|
|
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
|
|
|
compatible = ['linux']
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
2019-12-17 09:56:09 -08:00
|
|
|
List of dictionaries. 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:
|
|
|
|
|
2019-12-17 10:09:19 -08:00
|
|
|
List of dictionaries. Raw or processed structured data.
|
2019-11-11 18:30:46 -08:00
|
|
|
"""
|
2019-11-07 08:07:43 -08:00
|
|
|
if not quiet:
|
2019-12-13 20:01:51 -08:00
|
|
|
jc.utils.compatibility(__name__, info.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)
|