diff --git a/README.md b/README.md index 79c55443..c0d1f9f1 100755 --- a/README.md +++ b/README.md @@ -122,6 +122,7 @@ The JSON output can be compact (default) or pretty formatted with the `-p` optio - `--systemctl-lj` enables the `systemctl list-jobs` command parser - `--systemctl-ls` enables the `systemctl list-sockets` command parser - `--systemctl-luf` enables the `systemctl list-unit-files` command parser +- `--timedatectl` enables the `timedatectl status` command parser - `--uname` enables the `uname -a` command parser - `--uptime` enables the `uptime` command parser - `--w` enables the `w` command parser @@ -2024,6 +2025,20 @@ $ systemctl list-unit-files | jc --systemctl-luf -p # or: jc -p system ... ] ``` +### timedatectl status +``` +$ timedatectl | jc --timedatectl -p +{ + "local_time": "Tue 2020-03-10 17:53:21 PDT", + "universal_time": "Wed 2020-03-11 00:53:21 UTC", + "rtc_time": "Wed 2020-03-11 00:53:21", + "time_zone": "America/Los_Angeles (PDT, -0700)", + "ntp_enabled": true, + "ntp_synchronized": true, + "rtc_in_local_tz": false, + "dst_active": true +} +``` ### uname -a ``` $ uname -a | jc --uname -p # or: jc -p uname -a diff --git a/changelog.txt b/changelog.txt index 744fbef1..68fe5f6e 100644 --- a/changelog.txt +++ b/changelog.txt @@ -2,6 +2,7 @@ jc changelog 20200xxxx vX.X.X - Added ntpq command parser +- Added timedatectl status parser 20200308 v1.8.1 - CLI and history parser optimizations by https://github.com/philippeitis diff --git a/docgen.sh b/docgen.sh index a9746502..244adc54 100755 --- a/docgen.sh +++ b/docgen.sh @@ -44,6 +44,7 @@ pydocmd simple jc.parsers.systemctl+ > ../docs/parsers/systemctl.md pydocmd simple jc.parsers.systemctl_lj+ > ../docs/parsers/systemctl_lj.md pydocmd simple jc.parsers.systemctl_ls+ > ../docs/parsers/systemctl_ls.md pydocmd simple jc.parsers.systemctl_luf+ > ../docs/parsers/systemctl_luf.md +pydocmd simple jc.parsers.timedatectl+ > ../docs/parsers/timedatectl.md pydocmd simple jc.parsers.uname+ > ../docs/parsers/uname.md pydocmd simple jc.parsers.uptime+ > ../docs/parsers/uptime.md pydocmd simple jc.parsers.w+ > ../docs/parsers/w.md diff --git a/docs/parsers/timedatectl.md b/docs/parsers/timedatectl.md new file mode 100644 index 00000000..7f8341bd --- /dev/null +++ b/docs/parsers/timedatectl.md @@ -0,0 +1,85 @@ +# jc.parsers.timedatectl +jc - JSON CLI output utility timedatectl Parser + +Usage: + + specify --timedatectl as the first argument if the piped input is coming from timedatectl or timedatectl status + +Compatibility: + + 'linux' + +Examples: + + $ timedatectl | jc --timedatectl -p + { + "local_time": "Tue 2020-03-10 17:53:21 PDT", + "universal_time": "Wed 2020-03-11 00:53:21 UTC", + "rtc_time": "Wed 2020-03-11 00:53:21", + "time_zone": "America/Los_Angeles (PDT, -0700)", + "ntp_enabled": true, + "ntp_synchronized": true, + "rtc_in_local_tz": false, + "dst_active": true + } + + $ timedatectl | jc --timedatectl -p -r + { + "local_time": "Tue 2020-03-10 17:53:21 PDT", + "universal_time": "Wed 2020-03-11 00:53:21 UTC", + "rtc_time": "Wed 2020-03-11 00:53:21", + "time_zone": "America/Los_Angeles (PDT, -0700)", + "ntp_enabled": "yes", + "ntp_synchronized": "yes", + "rtc_in_local_tz": "no", + "dst_active": "yes" + } + +## info +```python +info(self, /, *args, **kwargs) +``` + +## process +```python +process(proc_data) +``` + +Final processing to conform to the schema. + +Parameters: + + proc_data: (dictionary) raw structured data to process + +Returns: + + Dictionary. Structured data with the following schema: + + { + "local_time": string, + "universal_time": string, + "rtc_time": string, + "time_zone": string, + "ntp_enabled": boolean, + "ntp_synchronized": boolean, + "rtc_in_local_tz": boolean, + "dst_active": boolean + } + +## parse +```python +parse(data, raw=False, quiet=False) +``` + +Main text parsing function + +Parameters: + + data: (string) text data to parse + raw: (boolean) output preprocessed JSON if True + quiet: (boolean) suppress warning messages if True + +Returns: + + Dictionary. Raw or processed structured data. + diff --git a/jc/cli.py b/jc/cli.py index c49f9d04..5e819aab 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -62,6 +62,7 @@ parsers = [ 'systemctl-lj', 'systemctl-ls', 'systemctl-luf', + 'timedatectl', 'uname', 'uptime', 'w', diff --git a/jc/parsers/timedatectl.py b/jc/parsers/timedatectl.py new file mode 100644 index 00000000..5c796d78 --- /dev/null +++ b/jc/parsers/timedatectl.py @@ -0,0 +1,119 @@ +"""jc - JSON CLI output utility timedatectl Parser + +Usage: + + specify --timedatectl as the first argument if the piped input is coming from timedatectl or timedatectl status + +Compatibility: + + 'linux' + +Examples: + + $ timedatectl | jc --timedatectl -p + { + "local_time": "Tue 2020-03-10 17:53:21 PDT", + "universal_time": "Wed 2020-03-11 00:53:21 UTC", + "rtc_time": "Wed 2020-03-11 00:53:21", + "time_zone": "America/Los_Angeles (PDT, -0700)", + "ntp_enabled": true, + "ntp_synchronized": true, + "rtc_in_local_tz": false, + "dst_active": true + } + + $ timedatectl | jc --timedatectl -p -r + { + "local_time": "Tue 2020-03-10 17:53:21 PDT", + "universal_time": "Wed 2020-03-11 00:53:21 UTC", + "rtc_time": "Wed 2020-03-11 00:53:21", + "time_zone": "America/Los_Angeles (PDT, -0700)", + "ntp_enabled": "yes", + "ntp_synchronized": "yes", + "rtc_in_local_tz": "no", + "dst_active": "yes" + } +""" +import jc.utils + + +class info(): + version = '1.0' + description = 'timedatectl status command parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + # details = 'enter any other details here' + + # compatible options: linux, darwin, cygwin, win32, aix, freebsd + compatible = ['linux'] + magic_commands = ['timedatectl', 'timedatectl status'] + + +__version__ = info.version + + +def process(proc_data): + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (dictionary) raw structured data to process + + Returns: + + Dictionary. Structured data with the following schema: + + { + "local_time": string, + "universal_time": string, + "rtc_time": string, + "time_zone": string, + "ntp_enabled": boolean, + "ntp_synchronized": boolean, + "rtc_in_local_tz": boolean, + "dst_active": boolean + } + """ + # boolean changes + bool_list = ['ntp_enabled', 'ntp_synchronized', 'rtc_in_local_tz', 'dst_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 + + return proc_data + + +def parse(data, raw=False, quiet=False): + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) output preprocessed JSON if True + quiet: (boolean) suppress warning messages if True + + Returns: + + Dictionary. Raw or processed structured data. + """ + if not quiet: + jc.utils.compatibility(__name__, info.compatible) + + raw_output = {} + + for line in filter(None, data.splitlines()): + linedata = line.split(':', maxsplit=1) + raw_output[linedata[0].strip().lower().replace(' ', '_')] = linedata[1].strip() + + if linedata[0].strip() == 'DST active': + break + + if raw: + return raw_output + else: + return process(raw_output)