mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-17 00:07:37 +02:00
use jc.utils for conversions
This commit is contained in:
@ -100,4 +100,4 @@ Returns:
|
||||
## Parser Information
|
||||
Compatibility: linux
|
||||
|
||||
Version 1.4 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
Version 1.5 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
|
@ -148,4 +148,4 @@ Returns:
|
||||
## Parser Information
|
||||
Compatibility: linux, darwin, cygwin, aix, freebsd
|
||||
|
||||
Version 1.1 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
Version 1.2 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
|
@ -88,4 +88,4 @@ Returns:
|
||||
## Parser Information
|
||||
Compatibility: linux
|
||||
|
||||
Version 1.3 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
Version 1.4 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
|
@ -156,4 +156,4 @@ Returns:
|
||||
## Parser Information
|
||||
Compatibility: linux
|
||||
|
||||
Version 1.1 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
Version 1.2 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
|
@ -142,4 +142,4 @@ Returns:
|
||||
## Parser Information
|
||||
Compatibility: linux, darwin, freebsd
|
||||
|
||||
Version 1.2 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
Version 1.3 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
|
@ -219,4 +219,4 @@ Returns:
|
||||
## Parser Information
|
||||
Compatibility: linux
|
||||
|
||||
Version 1.1 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
Version 1.2 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
|
@ -90,4 +90,4 @@ Returns:
|
||||
## Parser Information
|
||||
Compatibility: linux, darwin, cygwin, aix, freebsd
|
||||
|
||||
Version 1.4 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
Version 1.5 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
|
@ -79,4 +79,4 @@ Returns:
|
||||
## Parser Information
|
||||
Compatibility: linux, darwin, cygwin, aix, freebsd
|
||||
|
||||
Version 1.1 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
Version 1.2 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
|
@ -157,4 +157,4 @@ Returns:
|
||||
## Parser Information
|
||||
Compatibility: linux, darwin, cygwin, aix, freebsd
|
||||
|
||||
Version 1.3 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
Version 1.4 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
||||
|
@ -78,7 +78,7 @@ Converts string input to integer by stripping all non-numeric characters
|
||||
|
||||
Parameters:
|
||||
|
||||
value: (string) Input value
|
||||
value: (string/integer/float) Input value
|
||||
|
||||
Returns:
|
||||
|
||||
|
@ -256,25 +256,18 @@ def _process(proc_data):
|
||||
float_list = ['temperature']
|
||||
|
||||
for entry in proc_data:
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
if 'trip_points' in entry:
|
||||
for tp in entry['trip_points']:
|
||||
for key in int_list:
|
||||
if key in tp:
|
||||
tp[key] = jc.utils.convert_to_int(tp[key])
|
||||
|
||||
for entry in proc_data:
|
||||
for key in float_list:
|
||||
if key in entry:
|
||||
if key in float_list:
|
||||
entry[key] = jc.utils.convert_to_float(entry[key])
|
||||
|
||||
if 'trip_points' in entry:
|
||||
for tp in entry['trip_points']:
|
||||
for key in float_list:
|
||||
if key in tp:
|
||||
for key in tp:
|
||||
if key in int_list:
|
||||
tp[key] = jc.utils.convert_to_int(tp[key])
|
||||
if key in float_list:
|
||||
tp[key] = jc.utils.convert_to_float(tp[key])
|
||||
|
||||
for entry in proc_data:
|
||||
|
@ -137,15 +137,12 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
|
||||
# integers
|
||||
# convert integers and booleans
|
||||
int_list = ['rssi']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
# booleans
|
||||
bool_list = ['ht']
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
if key in bool_list:
|
||||
entry[key] = jc.utils.convert_to_bool(entry[key])
|
||||
|
||||
|
@ -150,8 +150,8 @@ def _process(proc_data):
|
||||
entry['name'] = None
|
||||
|
||||
int_list = ['expires']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -155,8 +155,8 @@ def _process(proc_data):
|
||||
'id_part_entry_offset', 'id_part_entry_size', 'minimum_io_size', 'physical_sector_size',
|
||||
'logical_sector_size', 'id_iolimit_minimum_io_size', 'id_iolimit_physical_sector_size',
|
||||
'id_iolimit_logical_sector_size']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -82,8 +82,8 @@ def _process(proc_data):
|
||||
|
||||
for entry in proc_data:
|
||||
int_list = ['checksum', 'blocks']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -158,8 +158,8 @@ def _process(proc_data):
|
||||
|
||||
# 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:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -304,8 +304,8 @@ def _process(proc_data):
|
||||
for entry in proc_data:
|
||||
int_list = ['id', 'query_num', 'answer_num', 'authority_num', 'additional_num', 'rcvd',
|
||||
'query_size', 'query_time']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
if 'axfr' in entry:
|
||||
|
@ -155,8 +155,8 @@ def _process(proc_data):
|
||||
|
||||
# add ints
|
||||
int_list = ["size"]
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -153,8 +153,8 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['type', 'bytes']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
if not entry['values']:
|
||||
|
@ -116,8 +116,8 @@ def _process(proc_data):
|
||||
"""
|
||||
int_list = ['size']
|
||||
for entry in proc_data:
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -101,8 +101,8 @@ def _process(proc_data):
|
||||
|
||||
for entry in proc_data:
|
||||
int_list = ['total', 'used', 'free', 'shared', 'buff_cache', 'available']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -111,8 +111,8 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['fs_freq', 'fs_passno']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -136,8 +136,8 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['gid']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
if entry['members'] == ['']:
|
||||
|
@ -64,8 +64,8 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['hits']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -348,8 +348,8 @@ def _process(proc_data):
|
||||
|
||||
int_list = ['acl_mtu', 'acl_mtu_packets', 'sco_mtu', 'sco_mtu_packets', 'rx_bytes', 'rx_acl', 'rx_sco',
|
||||
'rx_events', 'rx_errors', 'tx_bytes', 'tx_acl', 'tx_sco', 'tx_commands', 'tx_errors']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
if 'service_classes' in entry and len(entry['service_classes']) == 1 and 'Unspecified' in entry['service_classes']:
|
||||
|
@ -434,8 +434,8 @@ def _process(proc_data):
|
||||
int_list = ['flags', 'mtu', 'ipv6_mask', 'rx_packets', 'rx_bytes', 'rx_errors', 'rx_dropped', 'rx_overruns',
|
||||
'rx_frame', 'tx_packets', 'tx_bytes', 'tx_errors', 'tx_dropped', 'tx_overruns', 'tx_carrier',
|
||||
'tx_collisions', 'metric']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
# convert OSX-style subnet mask to dotted quad
|
||||
|
@ -191,8 +191,8 @@ def _process(proc_data):
|
||||
for entry in proc_data:
|
||||
for rule in entry['rules']:
|
||||
int_list = ['num', 'pkts']
|
||||
for key in int_list:
|
||||
if key in rule:
|
||||
for key in rule:
|
||||
if key in int_list:
|
||||
rule[key] = jc.utils.convert_to_int(rule[key])
|
||||
|
||||
if 'bytes' in rule:
|
||||
|
@ -122,8 +122,8 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['job_number', 'pid']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -134,8 +134,8 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['links', 'size']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
if 'date' in entry:
|
||||
|
@ -295,16 +295,13 @@ def _process(proc_data):
|
||||
List of Dictionaries. Structured data to conform to the schema.
|
||||
"""
|
||||
for entry in proc_data:
|
||||
# boolean changes
|
||||
# boolean and integer changes
|
||||
bool_list = ['rm', 'ro', 'rota', 'disc_zero', 'rand']
|
||||
for key in bool_list:
|
||||
if key in entry:
|
||||
entry[key] = jc.utils.convert_to_bool(entry[key])
|
||||
|
||||
# integer changes
|
||||
int_list = ['ra', 'alignment', 'min_io', 'opt_io', 'phy_sec', 'log_sec', 'rq_size', 'disc_aln']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in bool_list:
|
||||
entry[key] = jc.utils.convert_to_bool(entry[key])
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -153,8 +153,8 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['size', 'used']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -147,8 +147,8 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['pid', 'tid', 'size_off', 'node']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -380,7 +380,7 @@ def _process(proc_data):
|
||||
List of Dictionaries. Structured data to conform to the schema.
|
||||
"""
|
||||
for entry in proc_data:
|
||||
# integer changes
|
||||
# integer and float conversions
|
||||
int_list = ['recv_q', 'send_q', 'pid', 'refcnt', 'inode', 'unit', 'vendor', 'class',
|
||||
'osx_flags', 'subcla', 'pcbcount', 'rcvbuf', 'sndbuf', 'rxbytes', 'txbytes',
|
||||
'route_refs', 'use', 'mtu', 'mss', 'window', 'irtt', 'metric', 'ipkts',
|
||||
@ -388,10 +388,14 @@ def _process(proc_data):
|
||||
'tx_ok', 'tx_err', 'tx_drp', 'tx_ovr', 'idrop', 'ibytes', 'obytes', 'r_mbuf',
|
||||
's_mbuf', 'r_clus', 's_clus', 'r_hiwa', 's_hiwa', 'r_lowa', 's_lowa', 'r_bcnt',
|
||||
's_bcnt', 'r_bmax', 's_bmax', 'rexmit', 'ooorcv', '0_win']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
float_list = ['rexmt', 'persist', 'keep', '2msl', 'delack', 'rcvtime']
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
if key in float_list:
|
||||
entry[key] = jc.utils.convert_to_float(entry[key])
|
||||
|
||||
# add number keys
|
||||
if 'local_port' in entry:
|
||||
local_num = jc.utils.convert_to_int(entry['local_port'])
|
||||
if local_num:
|
||||
@ -402,12 +406,6 @@ def _process(proc_data):
|
||||
if foreign_num:
|
||||
entry['foreign_port_num'] = foreign_num
|
||||
|
||||
# float changes
|
||||
float_list = ['rexmt', 'persist', 'keep', '2msl', 'delack', 'rcvtime']
|
||||
for key in float_list:
|
||||
if key in entry:
|
||||
entry[key] = jc.utils.convert_to_float(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
||||
|
||||
|
@ -240,13 +240,11 @@ def _process(proc_data):
|
||||
entry['state'] = entry.pop('s')
|
||||
|
||||
int_list = ['st', 'when', 'poll', 'reach']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
float_list = ['delay', 'offset', 'jitter']
|
||||
for key in float_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
if key in float_list:
|
||||
entry[key] = jc.utils.convert_to_float(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -121,8 +121,8 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['uid', 'gid']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -241,16 +241,13 @@ def _process(proc_data):
|
||||
if '%mem' in entry:
|
||||
entry['mem_percent'] = entry.pop('%mem')
|
||||
|
||||
# change to int
|
||||
# convert ints and floats
|
||||
int_list = ['pid', 'ppid', 'c', 'vsz', 'rss']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
# change to float
|
||||
float_list = ['cpu_percent', 'mem_percent']
|
||||
for key in float_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
if key in float_list:
|
||||
entry[key] = jc.utils.convert_to_float(entry[key])
|
||||
|
||||
# clean up other fields
|
||||
|
@ -138,8 +138,8 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['metric', 'ref', 'use', 'mss', 'window', 'irtt']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
# add flags_pretty
|
||||
|
@ -185,8 +185,8 @@ def _process(proc_data):
|
||||
for entry in proc_data:
|
||||
|
||||
int_list = ['epoch', 'size']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
if 'build_date' in entry:
|
||||
|
@ -128,8 +128,8 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['last_changed', 'minimum', 'maximum', 'warn', 'inactive', 'expire']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
@ -306,8 +306,8 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['recv_q', 'send_q', 'pid']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
if 'local_port' in entry:
|
||||
|
@ -197,8 +197,8 @@ def _process(proc_data):
|
||||
for entry in proc_data:
|
||||
int_list = ['size', 'blocks', 'io_blocks', 'inode', 'links', 'uid', 'gid', 'unix_device',
|
||||
'rdev', 'block_size']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
# turn - into null for time fields and add calculated timestamp fields
|
||||
|
@ -75,7 +75,7 @@ import jc.utils
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.4'
|
||||
version = '1.5'
|
||||
description = '`systemctl list-jobs` command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -102,13 +102,10 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['job']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
try:
|
||||
key_int = int(entry[key])
|
||||
entry[key] = key_int
|
||||
except (ValueError):
|
||||
entry[key] = None
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
||||
|
||||
|
@ -241,7 +241,7 @@ def _process(proc_data):
|
||||
proc_data[item] = None
|
||||
|
||||
for i, nic in enumerate(proc_data["network_cards"]):
|
||||
proc_data["network_cards"][i]["dhcp_enabled"] = _convert_to_boolean(
|
||||
proc_data["network_cards"][i]["dhcp_enabled"] = jc.utils.convert_to_bool(
|
||||
nic["dhcp_enabled"]
|
||||
)
|
||||
|
||||
@ -258,7 +258,7 @@ def _process(proc_data):
|
||||
"virtual_memory_in_use_mb",
|
||||
]
|
||||
for key in int_list:
|
||||
proc_data[key] = _convert_to_int(proc_data.get(key))
|
||||
proc_data[key] = jc.utils.convert_to_int(proc_data[key])
|
||||
|
||||
dt_list = ["original_install_date", "system_boot_time"]
|
||||
for key in dt_list:
|
||||
@ -282,7 +282,7 @@ def _process(proc_data):
|
||||
if hyperv_key in proc_data:
|
||||
for key in hyperv_subkey_list:
|
||||
if key in proc_data[hyperv_key]:
|
||||
proc_data[hyperv_key][key] = _convert_to_boolean(
|
||||
proc_data[hyperv_key][key] = jc.utils.convert_to_bool(
|
||||
proc_data[hyperv_key][key]
|
||||
)
|
||||
|
||||
|
@ -123,7 +123,7 @@ import jc.utils
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.1'
|
||||
version = '1.2'
|
||||
description = '`/usr/bin/time` command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -156,38 +156,30 @@ def _process(proc_data):
|
||||
*hours, minutes, seconds, centiseconds = proc_data['elapsed_time'].split(':')
|
||||
proc_data['elapsed_time'] = proc_data['elapsed_time'][::-1].replace(':', '.', 1)[::-1]
|
||||
if hours:
|
||||
proc_data['elapsed_time_hours'] = int(hours[0])
|
||||
proc_data['elapsed_time_hours'] = jc.utils.convert_to_int(hours[0])
|
||||
else:
|
||||
proc_data['elapsed_time_hours'] = 0
|
||||
proc_data['elapsed_time_minutes'] = int(minutes)
|
||||
proc_data['elapsed_time_seconds'] = int(seconds)
|
||||
proc_data['elapsed_time_centiseconds'] = int(centiseconds)
|
||||
proc_data['elapsed_time_minutes'] = jc.utils.convert_to_int(minutes)
|
||||
proc_data['elapsed_time_seconds'] = jc.utils.convert_to_int(seconds)
|
||||
proc_data['elapsed_time_centiseconds'] = jc.utils.convert_to_int(centiseconds)
|
||||
proc_data['elapsed_time_total_seconds'] = (proc_data['elapsed_time_hours'] * 3600) + \
|
||||
(proc_data['elapsed_time_minutes'] * 60) + \
|
||||
(proc_data['elapsed_time_seconds']) + \
|
||||
(proc_data['elapsed_time_centiseconds'] / 100)
|
||||
|
||||
int_list = ['elapsed_time_hours', 'elapsed_time_minutes', 'elapsed_time_seconds', 'elapsed_time_microseconds',
|
||||
'cpu_percent', 'average_shared_text_size', 'average_unshared_data_size', 'average_unshared_stack_size',
|
||||
# convert ints and floats
|
||||
int_list = ['cpu_percent', 'average_shared_text_size', 'average_unshared_data_size', 'average_unshared_stack_size',
|
||||
'average_shared_memory_size', 'maximum_resident_set_size', 'block_input_operations',
|
||||
'block_output_operations', 'major_pagefaults', 'minor_pagefaults', 'swaps', 'page_reclaims',
|
||||
'page_faults', 'messages_sent', 'messages_received', 'signals_received', 'voluntary_context_switches',
|
||||
'involuntary_context_switches', 'average_stack_size', 'average_total_size', 'average_resident_set_size',
|
||||
'signals_delivered', 'page_size', 'exit_status']
|
||||
for key in int_list:
|
||||
if key in proc_data:
|
||||
try:
|
||||
proc_data[key] = int(proc_data[key])
|
||||
except (ValueError, TypeError):
|
||||
proc_data[key] = None
|
||||
|
||||
float_list = ['real_time', 'user_time', 'system_time']
|
||||
for key in float_list:
|
||||
if key in proc_data:
|
||||
try:
|
||||
proc_data[key] = float(proc_data[key])
|
||||
except (ValueError):
|
||||
proc_data[key] = None
|
||||
for key in proc_data:
|
||||
if key in int_list:
|
||||
proc_data[key] = jc.utils.convert_to_int(proc_data[key])
|
||||
if key in float_list:
|
||||
proc_data[key] = jc.utils.convert_to_float(proc_data[key])
|
||||
|
||||
return proc_data
|
||||
|
||||
|
@ -63,7 +63,7 @@ import jc.utils
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.3'
|
||||
version = '1.4'
|
||||
description = '`timedatectl status` command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -89,15 +89,11 @@ def _process(proc_data):
|
||||
|
||||
Dictionary. Structured data to conform to the schema.
|
||||
"""
|
||||
# boolean changes
|
||||
bool_list = ['ntp_enabled', 'ntp_synchronized', 'rtc_in_local_tz', 'dst_active',
|
||||
'system_clock_synchronized', 'systemd-timesyncd.service_active']
|
||||
for key in proc_data:
|
||||
if key in bool_list:
|
||||
try:
|
||||
proc_data[key] = True if proc_data[key] == 'yes' else False
|
||||
except (ValueError):
|
||||
proc_data[key] = None
|
||||
proc_data[key] = jc.utils.convert_to_bool(proc_data[key])
|
||||
|
||||
if 'universal_time' in proc_data:
|
||||
ts = jc.utils.timestamp(proc_data['universal_time'])
|
||||
|
@ -132,7 +132,7 @@ import jc.utils
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.1'
|
||||
version = '1.2'
|
||||
description = '`tracepath` and `tracepath6` command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -157,39 +157,23 @@ def _process(proc_data):
|
||||
|
||||
Dictionary. Structured data to conform to the schema.
|
||||
"""
|
||||
# convert ints and floats
|
||||
int_list = ['pmtu', 'forward_hops', 'return_hops', 'ttl', 'asymmetric_difference']
|
||||
float_list = ['reply_ms']
|
||||
|
||||
for key, value in proc_data.items():
|
||||
for item in int_list:
|
||||
if key in int_list:
|
||||
try:
|
||||
proc_data[key] = int(proc_data[key])
|
||||
except (ValueError, TypeError):
|
||||
proc_data[key] = None
|
||||
|
||||
for item in int_list:
|
||||
if key in float_list:
|
||||
try:
|
||||
proc_data[key] = float(proc_data[key])
|
||||
except (ValueError, TypeError):
|
||||
proc_data[key] = None
|
||||
for key in proc_data:
|
||||
if key in int_list:
|
||||
proc_data[key] = jc.utils.convert_to_int(proc_data[key])
|
||||
if key in float_list:
|
||||
proc_data[key] = jc.utils.convert_to_float(proc_data[key])
|
||||
|
||||
if 'hops' in proc_data:
|
||||
for entry in proc_data['hops']:
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
try:
|
||||
entry[key] = int(entry[key])
|
||||
except (ValueError, TypeError):
|
||||
entry[key] = None
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
for key in float_list:
|
||||
if key in entry:
|
||||
try:
|
||||
entry[key] = float(entry[key])
|
||||
except (ValueError, TypeError):
|
||||
entry[key] = None
|
||||
if key in float_list:
|
||||
entry[key] = jc.utils.convert_to_float(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
||||
|
@ -119,7 +119,7 @@ import jc.utils
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.2'
|
||||
version = '1.3'
|
||||
description = '`traceroute` and `traceroute6` command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -335,35 +335,21 @@ def _process(proc_data):
|
||||
|
||||
if 'hops' in proc_data:
|
||||
for entry in proc_data['hops']:
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
try:
|
||||
entry[key] = int(entry[key])
|
||||
except (ValueError, TypeError):
|
||||
entry[key] = None
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
for key in float_list:
|
||||
if key in entry:
|
||||
try:
|
||||
entry[key] = float(entry[key])
|
||||
except (ValueError, TypeError):
|
||||
entry[key] = None
|
||||
if key in float_list:
|
||||
entry[key] = jc.utils.convert_to_float(entry[key])
|
||||
|
||||
if 'probes' in entry:
|
||||
for item in entry['probes']:
|
||||
for key in int_list:
|
||||
if key in item:
|
||||
try:
|
||||
item[key] = int(item[key])
|
||||
except (ValueError, TypeError):
|
||||
item[key] = None
|
||||
for key in item:
|
||||
if key in int_list:
|
||||
item[key] = jc.utils.convert_to_int(item[key])
|
||||
|
||||
for key in float_list:
|
||||
if key in item:
|
||||
try:
|
||||
item[key] = float(item[key])
|
||||
except (ValueError, TypeError):
|
||||
item[key] = None
|
||||
if key in float_list:
|
||||
item[key] = jc.utils.convert_to_float(item[key])
|
||||
|
||||
return proc_data
|
||||
|
||||
|
@ -194,7 +194,7 @@ import jc.utils
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.1'
|
||||
version = '1.2'
|
||||
description = '`upower` command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -225,7 +225,7 @@ def _process(proc_data):
|
||||
if 'updated' in entry:
|
||||
updated_list = entry['updated'].replace('(', '').replace(')', '').split()
|
||||
entry['updated'] = ' '.join(updated_list[:-3])
|
||||
entry['updated_seconds_ago'] = int(updated_list[-3])
|
||||
entry['updated_seconds_ago'] = jc.utils.convert_to_int(updated_list[-3])
|
||||
|
||||
if entry['updated']:
|
||||
ts = jc.utils.timestamp(entry['updated'])
|
||||
@ -236,20 +236,14 @@ def _process(proc_data):
|
||||
bool_list = ['power_supply', 'has_history', 'has_statistics', 'on_battery', 'lid_is_closed', 'lid_is_present']
|
||||
for key in entry:
|
||||
if key in bool_list:
|
||||
if entry[key].lower() == 'yes':
|
||||
entry[key] = True
|
||||
else:
|
||||
entry[key] = False
|
||||
entry[key] = jc.utils.convert_to_bool(entry[key])
|
||||
|
||||
# detail level boolean conversions
|
||||
bool_list = ['online', 'present', 'rechargeable']
|
||||
if 'detail' in entry:
|
||||
for key in entry['detail']:
|
||||
if key in bool_list:
|
||||
if entry['detail'][key].lower() == 'yes':
|
||||
entry['detail'][key] = True
|
||||
else:
|
||||
entry['detail'][key] = False
|
||||
entry['detail'][key] = jc.utils.convert_to_bool(entry['detail'][key])
|
||||
|
||||
# detail level convert warning to null if value is none
|
||||
if 'detail' in entry:
|
||||
@ -262,8 +256,8 @@ def _process(proc_data):
|
||||
add_items = []
|
||||
for key, value in entry['detail'].items():
|
||||
if value and isinstance(value, str):
|
||||
if len(value.split()) == 2 and value.split()[0].replace('.', '').isnumeric():
|
||||
entry['detail'][key] = float(value.split()[0])
|
||||
if len(value.split()) == 2:
|
||||
entry['detail'][key] = jc.utils.convert_to_float(value.split()[0])
|
||||
add_items.append({
|
||||
key + '_unit': value.split()[1]
|
||||
})
|
||||
@ -278,7 +272,7 @@ def _process(proc_data):
|
||||
for key, value in entry['detail'].items():
|
||||
if value and isinstance(value, str):
|
||||
if value[-1] == '%':
|
||||
entry['detail'][key] = float(value[:-1])
|
||||
entry['detail'][key] = jc.utils.convert_to_float(value)
|
||||
|
||||
# detail level fix quoted values
|
||||
if 'detail' in entry:
|
||||
@ -302,9 +296,9 @@ def _process(proc_data):
|
||||
new_history_obj = {}
|
||||
for key, value in history_obj.items():
|
||||
if key == 'time':
|
||||
new_history_obj[key] = int(value)
|
||||
new_history_obj[key] = jc.utils.convert_to_int(value)
|
||||
elif key == 'percent_charged':
|
||||
new_history_obj[key] = float(value)
|
||||
new_history_obj[key] = jc.utils.convert_to_float(value)
|
||||
else:
|
||||
new_history_obj[key] = value
|
||||
|
||||
|
@ -65,7 +65,7 @@ import jc.utils
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.4'
|
||||
version = '1.5'
|
||||
description = '`uptime` command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -92,10 +92,10 @@ def _process(proc_data):
|
||||
"""
|
||||
if 'time' in proc_data:
|
||||
time_list = proc_data['time'].split(':')
|
||||
proc_data['time_hour'] = int(time_list[0])
|
||||
proc_data['time_minute'] = int(time_list[1])
|
||||
proc_data['time_hour'] = jc.utils.convert_to_int(time_list[0])
|
||||
proc_data['time_minute'] = jc.utils.convert_to_int(time_list[1])
|
||||
if len(time_list) == 3:
|
||||
proc_data['time_second'] = int(time_list[2])
|
||||
proc_data['time_second'] = jc.utils.convert_to_int(time_list[2])
|
||||
else:
|
||||
proc_data['time_second'] = None
|
||||
|
||||
@ -113,14 +113,14 @@ def _process(proc_data):
|
||||
uptime_total_seconds = 0
|
||||
|
||||
if 'min' in proc_data['uptime']:
|
||||
uptime_minutes = int(proc_data['uptime'].split()[-2])
|
||||
uptime_minutes = jc.utils.convert_to_int(proc_data['uptime'].split()[-2])
|
||||
|
||||
if ':' in proc_data['uptime']:
|
||||
uptime_hours = int(proc_data['uptime'].split()[-1].split(':')[-2])
|
||||
uptime_minutes = int(proc_data['uptime'].split(':')[-1])
|
||||
uptime_hours = jc.utils.convert_to_int(proc_data['uptime'].split()[-1].split(':')[-2])
|
||||
uptime_minutes = jc.utils.convert_to_int(proc_data['uptime'].split(':')[-1])
|
||||
|
||||
if 'day' in proc_data['uptime']:
|
||||
uptime_days = int(proc_data['uptime'].split()[0])
|
||||
uptime_days = jc.utils.convert_to_int(proc_data['uptime'].split()[0])
|
||||
|
||||
proc_data['uptime_days'] = uptime_days
|
||||
proc_data['uptime_hours'] = uptime_hours
|
||||
@ -129,25 +129,14 @@ def _process(proc_data):
|
||||
uptime_total_seconds = (uptime_days * 86400) + (uptime_hours * 3600) + (uptime_minutes * 60)
|
||||
proc_data['uptime_total_seconds'] = uptime_total_seconds
|
||||
|
||||
# integer conversions
|
||||
# integer and float conversions
|
||||
int_list = ['users']
|
||||
for key in int_list:
|
||||
if key in proc_data:
|
||||
try:
|
||||
key_int = int(proc_data[key])
|
||||
proc_data[key] = key_int
|
||||
except (ValueError):
|
||||
proc_data[key] = None
|
||||
|
||||
# float conversions
|
||||
float_list = ['load_1m', 'load_5m', 'load_15m']
|
||||
for key in float_list:
|
||||
if key in proc_data:
|
||||
try:
|
||||
key_float = float(proc_data[key])
|
||||
proc_data[key] = key_float
|
||||
except (ValueError):
|
||||
proc_data[key] = None
|
||||
for key in proc_data:
|
||||
if key in int_list:
|
||||
proc_data[key] = jc.utils.convert_to_int(proc_data[key])
|
||||
if key in float_list:
|
||||
proc_data[key] = jc.utils.convert_to_float(proc_data[key])
|
||||
|
||||
return proc_data
|
||||
|
||||
|
@ -131,8 +131,8 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
null_list = ['user', 'tty', 'from', 'login_at', 'idle', 'what']
|
||||
for key in null_list:
|
||||
if key in entry:
|
||||
for key in entry:
|
||||
if key in null_list:
|
||||
if entry[key] == '-':
|
||||
entry[key] = None
|
||||
|
||||
|
@ -54,7 +54,7 @@ import jc.utils
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.1'
|
||||
version = '1.2'
|
||||
description = '`wc` command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -82,12 +82,10 @@ def _process(proc_data):
|
||||
|
||||
for entry in proc_data:
|
||||
int_list = ['lines', 'words', 'characters']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
try:
|
||||
entry[key] = int(entry[key])
|
||||
except (ValueError):
|
||||
entry[key] = None
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
return proc_data
|
||||
|
||||
|
||||
|
@ -133,7 +133,7 @@ import jc.utils
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.3'
|
||||
version = '1.4'
|
||||
description = '`who` command parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
@ -161,13 +161,9 @@ def _process(proc_data):
|
||||
"""
|
||||
for entry in proc_data:
|
||||
int_list = ['pid']
|
||||
for key in int_list:
|
||||
if key in entry:
|
||||
try:
|
||||
key_int = int(entry[key])
|
||||
entry[key] = key_int
|
||||
except (ValueError):
|
||||
entry[key] = None
|
||||
for key in entry:
|
||||
if key in int_list:
|
||||
entry[key] = jc.utils.convert_to_int(entry[key])
|
||||
|
||||
if 'time' in entry:
|
||||
ts = jc.utils.timestamp(entry['time'])
|
||||
|
36
jc/utils.py
36
jc/utils.py
@ -89,21 +89,26 @@ def convert_to_int(value):
|
||||
|
||||
Parameters:
|
||||
|
||||
value: (string) Input value
|
||||
value: (string/integer/float) Input value
|
||||
|
||||
Returns:
|
||||
|
||||
integer/None Integer if successful conversion, otherwise None
|
||||
"""
|
||||
try:
|
||||
value = int(re.sub(r'[^0-9\-\.]', '', value))
|
||||
except (ValueError, TypeError):
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
value = round(convert_to_float(value))
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
return int(re.sub(r'[^0-9\-\.]', '', value))
|
||||
except ValueError:
|
||||
try:
|
||||
return round(convert_to_float(value))
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
|
||||
return value
|
||||
elif isinstance(value, (int, float)):
|
||||
return int(value)
|
||||
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def convert_to_float(value):
|
||||
@ -118,12 +123,17 @@ def convert_to_float(value):
|
||||
|
||||
float/None Float if successful conversion, otherwise None
|
||||
"""
|
||||
try:
|
||||
value = float(re.sub(r'[^0-9\-\.]', '', value))
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
return float(re.sub(r'[^0-9\-\.]', '', value))
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
|
||||
return value
|
||||
elif isinstance(value, (int, float)):
|
||||
return float(value)
|
||||
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def convert_to_bool(value):
|
||||
|
Reference in New Issue
Block a user