mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-13 01:20:24 +02:00
fix quoted values in detail level. Add examples
This commit is contained in:
@ -19,11 +19,112 @@ Compatibility:
|
||||
|
||||
Examples:
|
||||
|
||||
$ upower | jc --upower -p
|
||||
[]
|
||||
$ upower -i /org/freedesktop/UPower/devices/battery | jc --upower -p
|
||||
[
|
||||
{
|
||||
"native_path": "/sys/devices/LNXSYSTM:00/device:00/PNP0C0A:00/power_supply/BAT0",
|
||||
"vendor": "NOTEBOOK",
|
||||
"model": "BAT",
|
||||
"serial": "0001",
|
||||
"power_supply": true,
|
||||
"updated": "Thu Feb 9 18:42:15 2012",
|
||||
"has_history": true,
|
||||
"has_statistics": true,
|
||||
"detail": {
|
||||
"type": "battery",
|
||||
"present": true,
|
||||
"rechargeable": true,
|
||||
"state": "charging",
|
||||
"energy": 22.3998,
|
||||
"energy_empty": 0.0,
|
||||
"energy_full": 52.6473,
|
||||
"energy_full_design": 62.16,
|
||||
"energy_rate": 31.6905,
|
||||
"voltage": 12.191,
|
||||
"time_to_full": 57.3,
|
||||
"percentage": 42.5469,
|
||||
"capacity": 84.6964,
|
||||
"technology": "lithium-ion",
|
||||
"energy_unit": "Wh",
|
||||
"energy_empty_unit": "Wh",
|
||||
"energy_full_unit": "Wh",
|
||||
"energy_full_design_unit": "Wh",
|
||||
"energy_rate_unit": "W",
|
||||
"voltage_unit": "V",
|
||||
"time_to_full_unit": "minutes"
|
||||
},
|
||||
"history_charge": [
|
||||
{
|
||||
"time": 1328809335,
|
||||
"percent_charged": 42.547,
|
||||
"status": "charging"
|
||||
},
|
||||
{
|
||||
"time": 1328809305,
|
||||
"percent_charged": 42.02,
|
||||
"status": "charging"
|
||||
}
|
||||
],
|
||||
"history_rate": [
|
||||
{
|
||||
"time": 1328809335,
|
||||
"percent_charged": 31.691,
|
||||
"status": "charging"
|
||||
}
|
||||
],
|
||||
"updated_epoch": 1328841735,
|
||||
"updated_seconds_ago": 1
|
||||
}
|
||||
]
|
||||
|
||||
$ upower | jc --upower -p -r
|
||||
[]
|
||||
$ upower -i /org/freedesktop/UPower/devices/battery | jc --upower -p -r
|
||||
[
|
||||
{
|
||||
"native_path": "/sys/devices/LNXSYSTM:00/device:00/PNP0C0A:00/power_supply/BAT0",
|
||||
"vendor": "NOTEBOOK",
|
||||
"model": "BAT",
|
||||
"serial": "0001",
|
||||
"power_supply": "yes",
|
||||
"updated": "Thu Feb 9 18:42:15 2012 (1 seconds ago)",
|
||||
"has_history": "yes",
|
||||
"has_statistics": "yes",
|
||||
"detail": {
|
||||
"type": "battery",
|
||||
"present": "yes",
|
||||
"rechargeable": "yes",
|
||||
"state": "charging",
|
||||
"energy": "22.3998 Wh",
|
||||
"energy_empty": "0 Wh",
|
||||
"energy_full": "52.6473 Wh",
|
||||
"energy_full_design": "62.16 Wh",
|
||||
"energy_rate": "31.6905 W",
|
||||
"voltage": "12.191 V",
|
||||
"time_to_full": "57.3 minutes",
|
||||
"percentage": "42.5469%",
|
||||
"capacity": "84.6964%",
|
||||
"technology": "lithium-ion"
|
||||
},
|
||||
"history_charge": [
|
||||
{
|
||||
"time": "1328809335",
|
||||
"percent_charged": "42.547",
|
||||
"status": "charging"
|
||||
},
|
||||
{
|
||||
"time": "1328809305",
|
||||
"percent_charged": "42.020",
|
||||
"status": "charging"
|
||||
}
|
||||
],
|
||||
"history_rate": [
|
||||
{
|
||||
"time": "1328809335",
|
||||
"percent_charged": "31.691",
|
||||
"status": "charging"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
"""
|
||||
import locale
|
||||
from datetime import datetime
|
||||
@ -122,7 +223,7 @@ def process(proc_data):
|
||||
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
|
||||
# try C locale. If that fails, try current locale. If that fails, give up
|
||||
entry['updated_epoch'] = None
|
||||
try:
|
||||
entry['updated_epoch'] = int(datetime.strptime(entry['updated'], '%c').strftime('%s'))
|
||||
@ -183,6 +284,13 @@ def process(proc_data):
|
||||
if value[-1] == '%':
|
||||
entry['detail'][key] = float(value[:-1])
|
||||
|
||||
# detail level fix quoted values
|
||||
if 'detail' in entry:
|
||||
for key, value in entry['detail'].items():
|
||||
if value and isinstance(value, str):
|
||||
if value.startswith("'") and value.endswith("'"):
|
||||
entry['detail'][key] = value[1:-1]
|
||||
|
||||
# history_charge and history_rate level convert floats and ints
|
||||
histories = []
|
||||
|
||||
|
Reference in New Issue
Block a user