diff --git a/docgen.sh b/docgen.sh index 149a671a..14bbdc17 100755 --- a/docgen.sh +++ b/docgen.sh @@ -20,6 +20,7 @@ pydocmd simple jc.parsers.ifconfig+ > ../docs/parsers/ifconfig.md pydocmd simple jc.parsers.ini+ > ../docs/parsers/ini.md pydocmd simple jc.parsers.iptables+ > ../docs/parsers/iptables.md pydocmd simple jc.parsers.jobs+ > ../docs/parsers/jobs.md +pydocmd simple jc.parsers.last+ > ../docs/parsers/last.md pydocmd simple jc.parsers.ls+ > ../docs/parsers/ls.md pydocmd simple jc.parsers.lsblk+ > ../docs/parsers/lsblk.md pydocmd simple jc.parsers.lsmod+ > ../docs/parsers/lsmod.md diff --git a/docs/parsers/last.md b/docs/parsers/last.md new file mode 100644 index 00000000..3b4fffe9 --- /dev/null +++ b/docs/parsers/last.md @@ -0,0 +1,118 @@ +# jc.parsers.last +jc - JSON CLI output utility last Parser + +Usage: + + specify --last as the first argument if the piped input is coming from last or lastb + +Compatibility: + + 'linux', 'darwin', 'aix', 'freebsd' + +Examples: + + $ last | jc --last -p + [ + { + "user": "kbrazil", + "tty": "ttys002", + "hostname": null, + "login": "Thu Feb 27 14:31", + "logout": "still logged in" + }, + { + "user": "kbrazil", + "tty": "ttys003", + "hostname": null, + "login": "Thu Feb 27 10:38", + "logout": "10:38", + "duration": "00:00" + }, + { + "user": "kbrazil", + "tty": "ttys003", + "hostname": null, + "login": "Thu Feb 27 10:18", + "logout": "10:18", + "duration": "00:00" + }, + ... + ] + + $ last | jc --last -p -r + [ + { + "user": "kbrazil", + "tty": "ttys002", + "hostname": "-", + "login": "Thu Feb 27 14:31", + "logout": "still_logged_in" + }, + { + "user": "kbrazil", + "tty": "ttys003", + "hostname": "-", + "login": "Thu Feb 27 10:38", + "logout": "10:38", + "duration": "00:00" + }, + { + "user": "kbrazil", + "tty": "ttys003", + "hostname": "-", + "login": "Thu Feb 27 10:18", + "logout": "10:18", + "duration": "00:00" + }, + ... + ] + + +## 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: + + List of dictionaries. Structured data with the following schema: + + [ + { + "user": string, + "tty": string, + "hostname": string, + "login": string, + "logout": string, + "duration": string + } + ] + +## 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: + + List of dictionaries. Raw or processed structured data. + diff --git a/jc/parsers/last.py b/jc/parsers/last.py index 1b4d398c..6b2c2504 100644 --- a/jc/parsers/last.py +++ b/jc/parsers/last.py @@ -11,10 +11,61 @@ Compatibility: Examples: $ last | jc --last -p - [] + [ + { + "user": "kbrazil", + "tty": "ttys002", + "hostname": null, + "login": "Thu Feb 27 14:31", + "logout": "still logged in" + }, + { + "user": "kbrazil", + "tty": "ttys003", + "hostname": null, + "login": "Thu Feb 27 10:38", + "logout": "10:38", + "duration": "00:00" + }, + { + "user": "kbrazil", + "tty": "ttys003", + "hostname": null, + "login": "Thu Feb 27 10:18", + "logout": "10:18", + "duration": "00:00" + }, + ... + ] $ last | jc --last -p -r - [] + [ + { + "user": "kbrazil", + "tty": "ttys002", + "hostname": "-", + "login": "Thu Feb 27 14:31", + "logout": "still_logged_in" + }, + { + "user": "kbrazil", + "tty": "ttys003", + "hostname": "-", + "login": "Thu Feb 27 10:38", + "logout": "10:38", + "duration": "00:00" + }, + { + "user": "kbrazil", + "tty": "ttys003", + "hostname": "-", + "login": "Thu Feb 27 10:18", + "logout": "10:18", + "duration": "00:00" + }, + ... + ] + """ import re import jc.utils @@ -49,14 +100,28 @@ def process(proc_data): [ { - "last": string, - "bar": boolean, - "baz": integer + "user": string, + "tty": string, + "hostname": string, + "login": string, + "logout": string, + "duration": string } ] """ + for entry in proc_data: + if 'user' in entry and entry['user'] == 'system_boot': + entry['user'] = 'system boot' + + if 'tty' in entry and entry['tty'] == '~': + entry['tty'] = None + + if 'hostname' in entry and entry['hostname'] == '-': + entry['hostname'] = None + + if 'logout' in entry and entry['logout'] == 'still_logged_in': + entry['logout'] = 'still logged in' - # rebuild output for added semantic information return proc_data