diff --git a/CHANGELOG b/CHANGELOG index 8fd769e5..f6796594 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ jc changelog 20220510 v1.19.0 (in progress) +- Add chage --list command parser tested on linux - Add git log command streaming parser - Add top -b command parser tested on linux - Add top -b command streaming parser tested on linux diff --git a/README.md b/README.md index 7482a8a3..6cae1790 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,7 @@ option. | `--asciitable` | ASCII and Unicode table parser | [📃](https://kellyjonbrazil.github.io/jc/docs/parsers/asciitable) | | `--asciitable-m` | multi-line ASCII and Unicode table parser | [📃](https://kellyjonbrazil.github.io/jc/docs/parsers/asciitable_m) | | `--blkid` | `blkid` command parser | [📃](https://kellyjonbrazil.github.io/jc/docs/parsers/blkid) | +| `--chage` | `chage --list` command parser | [📃](https://kellyjonbrazil.github.io/jc/docs/parsers/chage) | | `--cksum` | `cksum` and `sum` command parser | [📃](https://kellyjonbrazil.github.io/jc/docs/parsers/cksum) | | `--crontab` | `crontab` command and file parser | [📃](https://kellyjonbrazil.github.io/jc/docs/parsers/crontab) | | `--crontab-u` | `crontab` file parser with user support | [📃](https://kellyjonbrazil.github.io/jc/docs/parsers/crontab_u) | diff --git a/docs/parsers/chage.md b/docs/parsers/chage.md new file mode 100644 index 00000000..6b1c4f73 --- /dev/null +++ b/docs/parsers/chage.md @@ -0,0 +1,82 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.chage + +jc - JSON Convert `chage --list` command output parser + +Supports `chage -l ` or `chage --list ` + +Usage (cli): + + $ chage -l johndoe | jc --chage + + or + + $ jc chage -l johndoe + +Usage (module): + + import jc + result = jc.parse('chage', chage_command_output) + +Schema: + + { + "password_last_changed": string, + "password_expires": string, + "password_inactive": string, + "account_expires": string, + "min_days_between_password_change": integer, + "max_days_between_password_change": integer, + "warning_days_before_password_expires": integer + } + +Examples: + + $ chage | jc --chage -p + { + "password_last_changed": "never", + "password_expires": "never", + "password_inactive": "never", + "account_expires": "never", + "min_days_between_password_change": 0, + "max_days_between_password_change": 99999, + "warning_days_before_password_expires": 7 + } + + $ chage | jc --chage -p -r + { + "password_last_changed": "never", + "password_expires": "never", + "password_inactive": "never", + "account_expires": "never", + "min_days_between_password_change": "0", + "max_days_between_password_change": "99999", + "warning_days_before_password_expires": "7" + } + + + +### parse + +```python +def parse(data: str, raw: bool = False, quiet: bool = False) -> Dict +``` + +Main text parsing function + +Parameters: + + data: (string) text data to parse + raw: (boolean) unprocessed output if True + quiet: (boolean) suppress warning messages if True + +Returns: + + Dictionary. Raw or processed structured data. + +### Parser Information +Compatibility: linux + +Version 1.0 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/jc/lib.py b/jc/lib.py index 2aac2fe1..fcab3b60 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -16,6 +16,7 @@ parsers = [ 'asciitable', 'asciitable-m', 'blkid', + 'chage', 'cksum', 'crontab', 'crontab-u', diff --git a/jc/parsers/chage.py b/jc/parsers/chage.py new file mode 100644 index 00000000..49795f56 --- /dev/null +++ b/jc/parsers/chage.py @@ -0,0 +1,151 @@ +"""jc - JSON Convert `chage --list` command output parser + +Supports `chage -l ` or `chage --list ` + +Usage (cli): + + $ chage -l johndoe | jc --chage + + or + + $ jc chage -l johndoe + +Usage (module): + + import jc + result = jc.parse('chage', chage_command_output) + +Schema: + + { + "password_last_changed": string, + "password_expires": string, + "password_inactive": string, + "account_expires": string, + "min_days_between_password_change": integer, + "max_days_between_password_change": integer, + "warning_days_before_password_expires": integer + } + +Examples: + + $ chage | jc --chage -p + { + "password_last_changed": "never", + "password_expires": "never", + "password_inactive": "never", + "account_expires": "never", + "min_days_between_password_change": 0, + "max_days_between_password_change": 99999, + "warning_days_before_password_expires": 7 + } + + $ chage | jc --chage -p -r + { + "password_last_changed": "never", + "password_expires": "never", + "password_inactive": "never", + "account_expires": "never", + "min_days_between_password_change": "0", + "max_days_between_password_change": "99999", + "warning_days_before_password_expires": "7" + } +""" +from typing import List, Dict +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`chage --list` command parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + compatible = ['linux'] + magic_commands = ['chage --list', 'chage -l'] + + +__version__ = info.version + + +def _process(proc_data: Dict) -> Dict: + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (Dictionary) raw structured data to process + + Returns: + + Dictionary. Structured to conform to the schema. + """ + int_list = ['min_days_between_password_change', 'max_days_between_password_change', + 'warning_days_before_password_expires'] + + for key in proc_data: + if key in int_list: + proc_data[key] = jc.utils.convert_to_int(proc_data[key]) + + return proc_data + + +def parse( + data: str, + raw: bool = False, + quiet: bool = False +) -> Dict: + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) unprocessed output if True + quiet: (boolean) suppress warning messages if True + + Returns: + + Dictionary. Raw or processed structured data. + """ + jc.utils.compatibility(__name__, info.compatible, quiet) + jc.utils.input_type_check(data) + + raw_output: Dict = {} + + if jc.utils.has_data(data): + + for line in filter(None, data.splitlines()): + key, val = line.split(':') + key = key.strip() + val = val.strip() + + if key == 'Last password change': + raw_output['password_last_changed'] = val + continue + + if key == 'Password expires': + raw_output['password_expires'] = val + continue + + if key == 'Password inactive': + raw_output['password_inactive'] = val + continue + + if key == 'Account expires': + raw_output['account_expires'] = val + continue + + if key == 'Minimum number of days between password change': + raw_output['min_days_between_password_change'] = val + continue + + if key == 'Maximum number of days between password change': + raw_output['max_days_between_password_change'] = val + continue + + if key == 'Number of days of warning before password expires': + raw_output['warning_days_before_password_expires'] = val + continue + + return raw_output if raw else _process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index ab8495e5..ec3aa35a 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -1,4 +1,4 @@ -.TH jc 1 2022-05-10 1.19.0 "JSON Convert" +.TH jc 1 2022-05-11 1.19.0 "JSON Convert" .SH NAME jc \- JSONifies the output of many CLI tools and file-types .SH SYNOPSIS @@ -52,6 +52,11 @@ multi-line ASCII and Unicode table parser \fB--blkid\fP `blkid` command parser +.TP +.B +\fB--chage\fP +`chage --list` command parser + .TP .B \fB--cksum\fP diff --git a/tests/fixtures/centos-7.7/chage.out b/tests/fixtures/centos-7.7/chage.out new file mode 100644 index 00000000..6dae862c --- /dev/null +++ b/tests/fixtures/centos-7.7/chage.out @@ -0,0 +1,7 @@ +Last password change : never +Password expires : never +Password inactive : never +Account expires : never +Minimum number of days between password change : 0 +Maximum number of days between password change : 99999 +Number of days of warning before password expires : 7