From 0431798178740b03cdefececc6df958e5adf62fc Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Wed, 30 Dec 2020 11:02:02 -0800 Subject: [PATCH] add cksum parser --- CHANGELOG | 1 + docgen.sh | 1 + docs/parsers/cksum.md | 95 +++++++++++++++++++++++++++++ docs/parsers/hashsum.md | 2 +- jc/cli.py | 3 +- jc/parsers/cksum.py | 128 ++++++++++++++++++++++++++++++++++++++++ jc/parsers/hashsum.py | 2 +- 7 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 docs/parsers/cksum.md create mode 100644 jc/parsers/cksum.py diff --git a/CHANGELOG b/CHANGELOG index f15326ba..0a7f3ad0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ jc changelog 20201229 v1.14.0 - Add hashsum parser tested on linux, macos +- Add cksum parser tested on linux, macos - Add python 3.9 to github automation tests 20200805 v1.13.4 diff --git a/docgen.sh b/docgen.sh index 4dc8a624..99f27e18 100755 --- a/docgen.sh +++ b/docgen.sh @@ -9,6 +9,7 @@ pydocmd simple jc.parsers.airport+ > ../docs/parsers/airport.md pydocmd simple jc.parsers.airport_s+ > ../docs/parsers/airport_s.md pydocmd simple jc.parsers.arp+ > ../docs/parsers/arp.md pydocmd simple jc.parsers.blkid+ > ../docs/parsers/blkid.md +pydocmd simple jc.parsers.cksum+ > ../docs/parsers/cksum.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 diff --git a/docs/parsers/cksum.md b/docs/parsers/cksum.md new file mode 100644 index 00000000..42c525ef --- /dev/null +++ b/docs/parsers/cksum.md @@ -0,0 +1,95 @@ + +# jc.parsers.cksum +jc - JSON CLI output utility `cksum` command output parser + +This parser works with the following checksum calculation utilities: +- `sum` +- `cksum` + +Usage (cli): + + $ cksum file.txt | jc --cksum + + or + + $ jc cksum file.txt + +Usage (module): + + import jc.parsers.cksum + result = jc.parsers.cksum.parse(cksum_command_output) + +Compatibility: + + 'linux', 'darwin', 'cygwin', 'aix', 'freebsd' + +Examples: + + $ cksum * | jc --cksum -p + [ + { + "filename": "__init__.py", + "checksum": 4294967295, + "blocks": 0 + }, + { + "filename": "airport.py", + "checksum": 2208551092, + "blocks": 3745 + }, + { + "filename": "airport_s.py", + "checksum": 1113817598, + "blocks": 4572 + }, + ... + ] + + +## 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: + + [ + { + "filename": string, + "checksum": integer, + "blocks": integer + } + ] + + +## 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/docs/parsers/hashsum.md b/docs/parsers/hashsum.md index 5d0d66e7..4c8c2494 100644 --- a/docs/parsers/hashsum.md +++ b/docs/parsers/hashsum.md @@ -2,7 +2,7 @@ # jc.parsers.hashsum jc - JSON CLI output utility `hash sum` command output parser -This parser works with the following hash calculation programs: +This parser works with the following hash calculation utilities: - `md5` - `md5sum` - `shasum` diff --git a/jc/cli.py b/jc/cli.py index c396226d..8b09fb73 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -21,7 +21,7 @@ import jc.appdirs as appdirs class info(): - version = '1.13.4' + version = '1.14.0' description = 'JSON CLI output utility' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' @@ -34,6 +34,7 @@ parsers = [ 'airport-s', 'arp', 'blkid', + 'cksum', 'crontab', 'crontab-u', 'csv', diff --git a/jc/parsers/cksum.py b/jc/parsers/cksum.py new file mode 100644 index 00000000..1b312362 --- /dev/null +++ b/jc/parsers/cksum.py @@ -0,0 +1,128 @@ +"""jc - JSON CLI output utility `cksum` command output parser + +This parser works with the following checksum calculation utilities: +- `sum` +- `cksum` + +Usage (cli): + + $ cksum file.txt | jc --cksum + + or + + $ jc cksum file.txt + +Usage (module): + + import jc.parsers.cksum + result = jc.parsers.cksum.parse(cksum_command_output) + +Compatibility: + + 'linux', 'darwin', 'cygwin', 'aix', 'freebsd' + +Examples: + + $ cksum * | jc --cksum -p + [ + { + "filename": "__init__.py", + "checksum": 4294967295, + "blocks": 0 + }, + { + "filename": "airport.py", + "checksum": 2208551092, + "blocks": 3745 + }, + { + "filename": "airport_s.py", + "checksum": 1113817598, + "blocks": 4572 + }, + ... + ] +""" +import jc.utils + + +class info(): + version = '1.0' + description = 'cksum command and file parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + details = 'Parses cksum and sum program output' + + # compatible options: linux, darwin, cygwin, win32, aix, freebsd + compatible = ['linux', 'darwin', 'cygwin', 'aix', 'freebsd'] + magic_commands = ['cksum', 'sum'] + + +__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: + + [ + { + "filename": string, + "checksum": integer, + "blocks": integer + } + ] + """ + + for entry in proc_data: + int_list = ['checksum', 'blocks'] + for key in int_list: + if key in entry: + try: + entry[key] = int(entry[key]) + except (ValueError): + entry[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: + + 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): + + for line in filter(None, data.splitlines()): + item = { + 'filename': line.split(maxsplit=2)[2], + 'checksum': line.split(maxsplit=2)[0], + 'blocks': line.split(maxsplit=2)[1] + } + raw_output.append(item) + + if raw: + return raw_output + else: + return process(raw_output) diff --git a/jc/parsers/hashsum.py b/jc/parsers/hashsum.py index f0f3f8d7..f3e5722c 100644 --- a/jc/parsers/hashsum.py +++ b/jc/parsers/hashsum.py @@ -1,6 +1,6 @@ """jc - JSON CLI output utility `hash sum` command output parser -This parser works with the following hash calculation programs: +This parser works with the following hash calculation utilities: - `md5` - `md5sum` - `shasum`