1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

add cksum parser

This commit is contained in:
Kelly Brazil
2020-12-30 11:02:02 -08:00
parent 62432f3c48
commit 0431798178
7 changed files with 229 additions and 3 deletions

View File

@ -2,6 +2,7 @@ jc changelog
20201229 v1.14.0 20201229 v1.14.0
- Add hashsum parser tested on linux, macos - Add hashsum parser tested on linux, macos
- Add cksum parser tested on linux, macos
- Add python 3.9 to github automation tests - Add python 3.9 to github automation tests
20200805 v1.13.4 20200805 v1.13.4

View File

@ -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.airport_s+ > ../docs/parsers/airport_s.md
pydocmd simple jc.parsers.arp+ > ../docs/parsers/arp.md pydocmd simple jc.parsers.arp+ > ../docs/parsers/arp.md
pydocmd simple jc.parsers.blkid+ > ../docs/parsers/blkid.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+ > ../docs/parsers/crontab.md
pydocmd simple jc.parsers.crontab_u+ > ../docs/parsers/crontab_u.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.csv+ > ../docs/parsers/csv.md

95
docs/parsers/cksum.md Normal file
View File

@ -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.

View File

@ -2,7 +2,7 @@
# jc.parsers.hashsum # jc.parsers.hashsum
jc - JSON CLI output utility `hash sum` command output parser 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` - `md5`
- `md5sum` - `md5sum`
- `shasum` - `shasum`

View File

@ -21,7 +21,7 @@ import jc.appdirs as appdirs
class info(): class info():
version = '1.13.4' version = '1.14.0'
description = 'JSON CLI output utility' description = 'JSON CLI output utility'
author = 'Kelly Brazil' author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com' author_email = 'kellyjonbrazil@gmail.com'
@ -34,6 +34,7 @@ parsers = [
'airport-s', 'airport-s',
'arp', 'arp',
'blkid', 'blkid',
'cksum',
'crontab', 'crontab',
'crontab-u', 'crontab-u',
'csv', 'csv',

128
jc/parsers/cksum.py Normal file
View File

@ -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)

View File

@ -1,6 +1,6 @@
"""jc - JSON CLI output utility `hash sum` command output parser """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` - `md5`
- `md5sum` - `md5sum`
- `shasum` - `shasum`