mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-17 01:32:37 +02:00
add integer and float conversions
This commit is contained in:
@ -2,6 +2,8 @@ jc changelog
|
|||||||
|
|
||||||
20230305 v1.23.1
|
20230305 v1.23.1
|
||||||
- Fix `zpool-status` command parser for lines that start with tab
|
- Fix `zpool-status` command parser for lines that start with tab
|
||||||
|
- Fix `timedatectl` command parser when RTC set to local
|
||||||
|
- Add support for the `timesync-status` for the `timedatectl` command parser
|
||||||
|
|
||||||
20230227 v1.23.0
|
20230227 v1.23.0
|
||||||
- Add input slicing as a `jc` command-line option
|
- Add input slicing as a `jc` command-line option
|
||||||
|
@ -35,16 +35,20 @@ Schema:
|
|||||||
"server": string,
|
"server": string,
|
||||||
"poll_interval": string,
|
"poll_interval": string,
|
||||||
"leap": string,
|
"leap": string,
|
||||||
"version": string,
|
"version": integer,
|
||||||
"stratum": string,
|
"stratum": integer,
|
||||||
"reference": string,
|
"reference": string,
|
||||||
"precision": string,
|
"precision": string,
|
||||||
"root_distance": string,
|
"root_distance": string,
|
||||||
"offset": string,
|
"offset": float,
|
||||||
"delay": string,
|
"offset_unit": string,
|
||||||
"jitter": string,
|
"delay": float,
|
||||||
"packet_count": string,
|
"delay_unit": string,
|
||||||
"frequency": string
|
"jitter": float,
|
||||||
|
"jitter_unit": string,
|
||||||
|
"packet_count": integer,
|
||||||
|
"frequency": float,
|
||||||
|
"frequency_unit": string
|
||||||
}
|
}
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
@ -105,11 +109,26 @@ def _process(proc_data):
|
|||||||
"""
|
"""
|
||||||
bool_list = {'ntp_enabled', 'ntp_synchronized', 'rtc_in_local_tz', 'dst_active',
|
bool_list = {'ntp_enabled', 'ntp_synchronized', 'rtc_in_local_tz', 'dst_active',
|
||||||
'system_clock_synchronized', 'systemd-timesyncd.service_active'}
|
'system_clock_synchronized', 'systemd-timesyncd.service_active'}
|
||||||
|
int_list = {'version', 'stratum', 'packet_count'}
|
||||||
|
float_list = {'offset', 'delay', 'jitter', 'frequency'}
|
||||||
|
|
||||||
|
for key in ['offset', 'delay', 'jitter']:
|
||||||
|
if key in proc_data:
|
||||||
|
proc_data[key + '_unit'] = proc_data[key][-2:]
|
||||||
|
|
||||||
|
if 'frequency' in proc_data:
|
||||||
|
proc_data['frequency_unit'] = proc_data['frequency'][-3:]
|
||||||
|
|
||||||
for key in proc_data:
|
for key in proc_data:
|
||||||
if key in bool_list:
|
if key in bool_list:
|
||||||
proc_data[key] = jc.utils.convert_to_bool(proc_data[key])
|
proc_data[key] = jc.utils.convert_to_bool(proc_data[key])
|
||||||
|
|
||||||
|
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 'universal_time' in proc_data:
|
if 'universal_time' in proc_data:
|
||||||
ts = jc.utils.timestamp(proc_data['universal_time'], format_hint=(7300,))
|
ts = jc.utils.timestamp(proc_data['universal_time'], format_hint=(7300,))
|
||||||
proc_data['epoch_utc'] = ts.utc
|
proc_data['epoch_utc'] = ts.utc
|
||||||
|
1
tests/fixtures/generic/timedatectl-rtc-local.json
vendored
Normal file
1
tests/fixtures/generic/timedatectl-rtc-local.json
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"local_time":"Mon 2023-03-13 14:17:38 EET","universal_time":"Mon 2023-03-13 12:17:38 UTC","rtc_time":"Mon 2023-03-13 12:17:38","time_zone":"Europe/Athens (EET, +0200)","system_clock_synchronized":true,"ntp_service":"active","rtc_in_local_tz":true,"epoch_utc":1678709858}
|
14
tests/fixtures/generic/timedatectl-rtc-local.out
vendored
Normal file
14
tests/fixtures/generic/timedatectl-rtc-local.out
vendored
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
Local time: Mon 2023-03-13 14:17:38 EET
|
||||||
|
Universal time: Mon 2023-03-13 12:17:38 UTC
|
||||||
|
RTC time: Mon 2023-03-13 12:17:38
|
||||||
|
Time zone: Europe/Athens (EET, +0200)
|
||||||
|
System clock synchronized: yes
|
||||||
|
NTP service: active
|
||||||
|
RTC in local TZ: yes
|
||||||
|
|
||||||
|
Warning: The system is configured to read the RTC time in the local time zone.
|
||||||
|
This mode cannot be fully supported. It will create various problems
|
||||||
|
with time zone changes and daylight saving time adjustments. The RTC
|
||||||
|
time is never updated, it relies on external facilities to maintain it.
|
||||||
|
If at all possible, use RTC in UTC by calling
|
||||||
|
'timedatectl set-local-rtc 0'.
|
1
tests/fixtures/generic/timedatectl-timesync-status.json
vendored
Normal file
1
tests/fixtures/generic/timedatectl-timesync-status.json
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"server":"216.239.35.8 (time.google.com)","poll_interval":"8min 32s (min: 32s; max 34min 8s)","leap":"normal","version":4,"stratum":1,"reference":"GOOG","precision":"1us (-20)","root_distance":"91us (max: 5s)","offset":-5.224,"delay":65.884,"jitter":5.386,"packet_count":5,"frequency":27.071,"offset_unit":"ms","delay_unit":"ms","jitter_unit":"ms","frequency_unit":"ppm"}
|
13
tests/fixtures/generic/timedatectl-timesync-status.out
vendored
Normal file
13
tests/fixtures/generic/timedatectl-timesync-status.out
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
Server: 216.239.35.8 (time.google.com)
|
||||||
|
Poll interval: 8min 32s (min: 32s; max 34min 8s)
|
||||||
|
Leap: normal
|
||||||
|
Version: 4
|
||||||
|
Stratum: 1
|
||||||
|
Reference: GOOG
|
||||||
|
Precision: 1us (-20)
|
||||||
|
Root distance: 91us (max: 5s)
|
||||||
|
Offset: -5.224ms
|
||||||
|
Delay: 65.884ms
|
||||||
|
Jitter: 5.386ms
|
||||||
|
Packet count: 5
|
||||||
|
Frequency: +27.071ppm
|
@ -15,6 +15,12 @@ class MyTests(unittest.TestCase):
|
|||||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/timedatectl.out'), 'r', encoding='utf-8') as f:
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/timedatectl.out'), 'r', encoding='utf-8') as f:
|
||||||
ubuntu_18_4_timedatectl = f.read()
|
ubuntu_18_4_timedatectl = f.read()
|
||||||
|
|
||||||
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/timedatectl-rtc-local.out'), 'r', encoding='utf-8') as f:
|
||||||
|
timedatectl_rtc_local = f.read()
|
||||||
|
|
||||||
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/timedatectl-timesync-status.out'), 'r', encoding='utf-8') as f:
|
||||||
|
timedatectl_timesync_status = f.read()
|
||||||
|
|
||||||
# output
|
# output
|
||||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/timedatectl.json'), 'r', encoding='utf-8') as f:
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/timedatectl.json'), 'r', encoding='utf-8') as f:
|
||||||
centos_7_7_timedatectl_json = json.loads(f.read())
|
centos_7_7_timedatectl_json = json.loads(f.read())
|
||||||
@ -22,6 +28,12 @@ class MyTests(unittest.TestCase):
|
|||||||
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/timedatectl.json'), 'r', encoding='utf-8') as f:
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/timedatectl.json'), 'r', encoding='utf-8') as f:
|
||||||
ubuntu_18_4_timedatectl_json = json.loads(f.read())
|
ubuntu_18_4_timedatectl_json = json.loads(f.read())
|
||||||
|
|
||||||
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/timedatectl-rtc-local.json'), 'r', encoding='utf-8') as f:
|
||||||
|
timedatectl_rtc_local_json = json.loads(f.read())
|
||||||
|
|
||||||
|
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/timedatectl-timesync-status.json'), 'r', encoding='utf-8') as f:
|
||||||
|
timedatectl_timesync_status_json = json.loads(f.read())
|
||||||
|
|
||||||
|
|
||||||
def test_timedatectl_nodata(self):
|
def test_timedatectl_nodata(self):
|
||||||
"""
|
"""
|
||||||
@ -41,6 +53,18 @@ class MyTests(unittest.TestCase):
|
|||||||
"""
|
"""
|
||||||
self.assertEqual(jc.parsers.timedatectl.parse(self.ubuntu_18_4_timedatectl, quiet=True), self.ubuntu_18_4_timedatectl_json)
|
self.assertEqual(jc.parsers.timedatectl.parse(self.ubuntu_18_4_timedatectl, quiet=True), self.ubuntu_18_4_timedatectl_json)
|
||||||
|
|
||||||
|
def test_timedatectl_rtc_local(self):
|
||||||
|
"""
|
||||||
|
Test 'timedatectl' with RTC set to local
|
||||||
|
"""
|
||||||
|
self.assertEqual(jc.parsers.timedatectl.parse(self.timedatectl_rtc_local, quiet=True), self.timedatectl_rtc_local_json)
|
||||||
|
|
||||||
|
def test_timedatectl_timesync_status(self):
|
||||||
|
"""
|
||||||
|
Test 'timedatectl timesync-status'
|
||||||
|
"""
|
||||||
|
self.assertEqual(jc.parsers.timedatectl.parse(self.timedatectl_timesync_status, quiet=True), self.timedatectl_timesync_status_json)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Reference in New Issue
Block a user