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

working parser and processor

This commit is contained in:
Kelly Brazil
2021-03-18 16:59:19 -07:00
parent bd68ad4034
commit aafbe576b3

View File

@ -25,6 +25,8 @@ Examples:
$ upower | jc --upower -p -r $ upower | jc --upower -p -r
[] []
""" """
import locale
from datetime import datetime
import jc.utils import jc.utils
@ -57,56 +59,155 @@ def process(proc_data):
[ [
{ {
"type": string, "type": string,
"device_name": string, "device_name": string,
"native_path": string, "native_path": string,
"power_supply": boolean, "power_supply": boolean,
"updated": "Thu 11 Mar 2021 06:28:08 PM UTC (441975 seconds ago)", "updated": string,
"has_history": boolean, "updated_epoch": integer, # works best with C locale. null if conversion fails
"has_statistics": boolean, "updated_seconds_ago": integer,
"has_history": boolean,
"has_statistics": boolean,
"detail": { "detail": {
"type": string, "type": string,
"warning_level": string, # null if none "warning_level": string, # null if none
"online": boolean, "online": boolean,
"icon_name": string "icon_name": string
"present": boolean, "present": boolean,
"rechargeable": boolean, "rechargeable": boolean,
"state": string, "state": string,
"energy": "22.3998 Wh", "energy": float,
"energy_empty": "0 Wh", "energy_unit": string,
"energy_full": "52.6473 Wh", "energy_empty": float,
"energy_full_design": "62.16 Wh", "energy_empty_unit": string,
"energy_rate": "31.6905 W", "energy_full": float,
"voltage": "12.191 V", "energy_full_unit": string,
"time_to_full": "57.3 minutes", "energy_full_design": float,
"percentage": "42.5469%", "energy_full_design_unit": string,
"capacity": "84.6964%", "energy_rate": float,
"technology": string "energy_rate_unit": string,
"voltage": float,
"voltage_unit": string,
"time_to_full": float,
"time_to_full_unit": string,
"percentage": float,
"capacity": float,
"technology": string
}, },
"history_charge": [ "history_charge": [
{ {
"time": integer, "time": integer,
"percent_charged": float, "percent_charged": float,
"status": string "status": string
} }
], ],
"history_rate":[ "history_rate":[
{ {
"time": integer, "time": integer,
"percent_charged": float, "percent_charged": float,
"status": string "status": string
} }
], ],
"daemon_version": string, "daemon_version": string,
"on_battery": boolean, "on_battery": boolean,
"lid_is_closed": boolean, "lid_is_closed": boolean,
"lid_is_present": boolean, "lid_is_present": boolean,
"critical_action": string "critical_action": string
} }
] ]
""" """
for entry in proc_data:
# time conversions
if 'updated' in entry:
updated_list = entry['updated'].replace('(', '').replace(')', '').split()
entry['updated'] = ' '.join(updated_list[:-3])
# try C locale. If that fails, try current locale. If that failes, give up
entry['updated_epoch'] = None
try:
entry['updated_epoch'] = int(datetime.strptime(entry['updated'], '%c').strftime('%s'))
except Exception:
try:
locale.setlocale(locale.LC_TIME, '')
entry['updated_epoch'] = int(datetime.strptime(entry['updated'], '%c').strftime('%s'))
except Exception:
pass
entry['updated_seconds_ago'] = int(updated_list[-3])
# top level boolean conversions
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
# 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
# detail level convert warning to null if value is none
if 'detail' in entry:
if 'warning_level' in entry['detail']:
if entry['detail']['warning_level'] == 'none':
entry['detail']['warning_level'] = None
# detail level convert energy readings to float and add unit keys
if 'detail' in entry:
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])
add_items.append({
key + '_unit': value.split()[1]
})
if add_items:
for item in add_items:
for key, value in item.items():
entry['detail'][key] = value
# detail level fix percentages
if 'detail' in entry:
for key, value in entry['detail'].items():
if value and isinstance(value, str):
if value[-1] == '%':
entry['detail'][key] = float(value[:-1])
# history_charge and history_rate level convert floats and ints
histories = []
if 'history_charge' in entry:
histories.append('history_charge')
if 'history_rate' in entry:
histories.append('history_rate')
if histories:
for history_obj_list in histories:
new_history_list = []
for history_obj in entry[history_obj_list]:
new_history_obj = {}
for key, value in history_obj.items():
if key == 'time':
new_history_obj[key] = int(value)
elif key == 'percent_charged':
new_history_obj[key] = float(value)
else:
new_history_obj[key] = value
new_history_list.append(new_history_obj)
entry[history_obj_list] = new_history_list
# rebuild output for added semantic information
return proc_data return proc_data