diff --git a/CHANGELOG b/CHANGELOG index 4d8f2e37..13c87999 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,7 +1,8 @@ jc changelog 20200730 v1.13.2 -- Add key/value parser (wrapper for ini parser) +- Add key/value file parser (wrapper for ini parser) +- Add date command parser - Update traceroute parser to more gracefully handle missing header row - Update traceroute parser to handle annotations - Update traceroute parser to only return successful probes diff --git a/docgen.sh b/docgen.sh index 6a9bbdc1..86986db5 100755 --- a/docgen.sh +++ b/docgen.sh @@ -11,6 +11,7 @@ pydocmd simple jc.parsers.blkid+ > ../docs/parsers/blkid.md pydocmd simple jc.parsers.crontab+ > ../docs/parsers/crontab.md pydocmd simple jc.parsers.crontab_u+ > ../docs/parsers/crontab_u.md pydocmd simple jc.parsers.csv+ > ../docs/parsers/csv.md +pydocmd simple jc.parsers.date+ > ../docs/parsers/date.md pydocmd simple jc.parsers.df+ > ../docs/parsers/df.md pydocmd simple jc.parsers.dig+ > ../docs/parsers/dig.md pydocmd simple jc.parsers.dmidecode+ > ../docs/parsers/dmidecode.md diff --git a/docs/parsers/date.md b/docs/parsers/date.md new file mode 100644 index 00000000..2c2e8867 --- /dev/null +++ b/docs/parsers/date.md @@ -0,0 +1,89 @@ + +# jc.parsers.date +jc - JSON CLI output utility date Parser + +Usage: + + specify --date as the first argument if the piped input is coming from date + +Compatibility: + + 'linux', 'darwin', 'freebsd' + +Examples: + + $ date | jc --date -p + { + "year": 2020, + "month": "Jul", + "day": 31, + "weekday": "Fri", + "hour": 14, + "minute": 35, + "second": 55, + "timezone": "PDT" + } + + $ date | jc --date -p -r + { + "year": "2020", + "month": "Jul", + "day": "31", + "weekday": "Fri", + "hour": "14", + "minute": "36", + "second": "14", + "timezone": "PDT" + } + + +## info +```python +info() +``` + + +## 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: + + { + "year": integer, + "month": string, + "day": integer, + "weekday": string, + "hour": integer, + "minute": integer, + "second": integer, + "timezone": 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/cli.py b/jc/cli.py index d3f8bff3..5ae412cb 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -37,6 +37,7 @@ parsers = [ 'crontab', 'crontab-u', 'csv', + 'date', 'df', 'dig', 'dmidecode', diff --git a/jc/parsers/date.py b/jc/parsers/date.py new file mode 100644 index 00000000..c3f0bda5 --- /dev/null +++ b/jc/parsers/date.py @@ -0,0 +1,127 @@ +"""jc - JSON CLI output utility date Parser + +Usage: + + specify --date as the first argument if the piped input is coming from date + +Compatibility: + + 'linux', 'darwin', 'freebsd' + +Examples: + + $ date | jc --date -p + { + "year": 2020, + "month": "Jul", + "day": 31, + "weekday": "Fri", + "hour": 14, + "minute": 35, + "second": 55, + "timezone": "PDT" + } + + $ date | jc --date -p -r + { + "year": "2020", + "month": "Jul", + "day": "31", + "weekday": "Fri", + "hour": "14", + "minute": "36", + "second": "14", + "timezone": "PDT" + } +""" +import jc.utils + + +class info(): + version = '1.0' + description = 'date 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', 'darwin', 'freebsd'] + magic_commands = ['date'] + + +__version__ = info.version + + +def 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: + + { + "year": integer, + "month": string, + "day": integer, + "weekday": string, + "hour": integer, + "minute": integer, + "second": integer, + "timezone": string + } + """ + return { + "year": int(proc_data['year']), + "month": proc_data['month'], + "day": int(proc_data['day']), + "weekday": proc_data['weekday'], + "hour": int(proc_data['hour']), + "minute": int(proc_data['minute']), + "second": int(proc_data['second']), + "timezone": proc_data['timezone'] + } + + +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: + + List of dictionaries. Raw or processed structured data. + """ + if not quiet: + jc.utils.compatibility(__name__, info.compatible) + + raw_output = {} + + if jc.utils.has_data(data): + data = data.replace(':', ' ') + split_data = data.split() + + raw_output = { + "year": split_data[7], + "month": split_data[1], + "day": split_data[2], + "weekday": split_data[0], + "hour": split_data[3], + "minute": split_data[4], + "second": split_data[5], + "timezone": split_data[6] + } + + if raw: + return raw_output + else: + return process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index c1672cf3..5091605c 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -44,6 +44,10 @@ crontab file parser with user support CSV file parser .TP .B +\fB--date\fP +date command parser +.TP +.B \fB--df\fP df command parser .TP