mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
update df to use universal sparse table parser for osx compatibility
This commit is contained in:
@ -64,6 +64,7 @@ Examples:
|
|||||||
]
|
]
|
||||||
"""
|
"""
|
||||||
import jc.utils
|
import jc.utils
|
||||||
|
import jc.parsers.universal
|
||||||
|
|
||||||
|
|
||||||
def process(proc_data):
|
def process(proc_data):
|
||||||
@ -80,32 +81,49 @@ def process(proc_data):
|
|||||||
|
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"filesystem": string,
|
"filesystem": string,
|
||||||
"size": string,
|
"size": string,
|
||||||
"1k-blocks": integer,
|
"1k_blocks": integer,
|
||||||
"used": integer,
|
"512_blocks": integer,
|
||||||
"available": integer,
|
"used": integer,
|
||||||
"use_percent": integer,
|
"available": integer,
|
||||||
"mounted_on": string
|
"capacity_percent": integer, #
|
||||||
|
"ifree": integer, #
|
||||||
|
"iused": integer, #
|
||||||
|
"use_percent": integer,
|
||||||
|
"iused_percent": integer, #
|
||||||
|
"mounted_on": string
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# TODO change 'avail' to 'available'
|
||||||
|
# TODO change 'use%' to 'use_percent'
|
||||||
|
# TODO change 'capacity' to 'capacity_percent'
|
||||||
|
# TODO change '%iused' to 'iused_percent'
|
||||||
|
|
||||||
for entry in proc_data:
|
for entry in proc_data:
|
||||||
# change any entry for key with '-blocks' in the name to int
|
# change any entry for key with '_blocks' in the name to int
|
||||||
for k in entry:
|
for k in entry:
|
||||||
if str(k).find('-blocks') != -1:
|
if str(k).find('_blocks') != -1:
|
||||||
try:
|
try:
|
||||||
blocks_int = int(entry[k])
|
blocks_int = int(entry[k])
|
||||||
entry[k] = blocks_int
|
entry[k] = blocks_int
|
||||||
except (ValueError):
|
except (ValueError):
|
||||||
entry[k] = None
|
entry[k] = None
|
||||||
|
|
||||||
# remove percent sign from 'use_percent'
|
# remove percent sign from 'use_percent', 'capacity_percent', and 'iused_percent'
|
||||||
if 'use_percent' in entry:
|
if 'use_percent' in entry:
|
||||||
entry['use_percent'] = entry['use_percent'].rstrip('%')
|
entry['use_percent'] = entry['use_percent'].rstrip('%')
|
||||||
|
|
||||||
# change used, available, and use_percent to int
|
if 'capacity_percent' in entry:
|
||||||
int_list = ['used', 'available', 'use_percent']
|
entry['capacity_percent'] = entry['capacity_percent'].rstrip('%')
|
||||||
|
|
||||||
|
if 'iused_percent' in entry:
|
||||||
|
entry['iused_percent'] = entry['iused_percent'].rstrip('%')
|
||||||
|
|
||||||
|
# change used, available, use_percent, capacity_percent, ifree, iused, iused_percent to int
|
||||||
|
int_list = ['used', 'available', 'use_percent', 'capacity_percent', 'ifree', 'iused', 'iused_percent']
|
||||||
for key in int_list:
|
for key in int_list:
|
||||||
if key in entry:
|
if key in entry:
|
||||||
try:
|
try:
|
||||||
@ -133,19 +151,20 @@ def parse(data, raw=False, quiet=False):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
||||||
compatible = ['linux']
|
compatible = ['linux', 'darwin']
|
||||||
|
|
||||||
if not quiet:
|
if not quiet:
|
||||||
jc.utils.compatibility(__name__, compatible)
|
jc.utils.compatibility(__name__, compatible)
|
||||||
|
|
||||||
cleandata = data.splitlines()
|
cleandata = data.splitlines()
|
||||||
fix_headers = cleandata[0].lower().replace('avail ', 'available ')
|
|
||||||
fix_headers = fix_headers.replace('use%', 'use_percent')
|
|
||||||
fix_headers = fix_headers.replace('mounted on', 'mounted_on')
|
|
||||||
headers = [h for h in ' '.join(fix_headers.strip().split()).split() if h]
|
|
||||||
|
|
||||||
raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), cleandata[1:])
|
# fix headers
|
||||||
raw_output = [dict(zip(headers, r)) for r in raw_data]
|
cleandata[0] = cleandata[0].lower()
|
||||||
|
cleandata[0] = cleandata[0].replace('-', '_')
|
||||||
|
cleandata[0] = cleandata[0].replace('mounted on', 'mounted_on')
|
||||||
|
|
||||||
|
# parse the data
|
||||||
|
raw_output = jc.parsers.universal.sparse_table_parse(cleandata)
|
||||||
|
|
||||||
if raw:
|
if raw:
|
||||||
return raw_output
|
return raw_output
|
||||||
|
Reference in New Issue
Block a user