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

add proc-crypto parser and doc update

This commit is contained in:
Kelly Brazil
2022-09-06 17:29:56 -07:00
parent b83cd24d57
commit 1f2fe65185
5 changed files with 336 additions and 4 deletions

139
docs/parsers/proc_crypto.md Normal file
View File

@ -0,0 +1,139 @@
[Home](https://kellyjonbrazil.github.io/jc/)
<a id="jc.parsers.proc_crypto"></a>
# jc.parsers.proc\_crypto
jc - JSON Convert `/proc/crypto` file parser
Usage (cli):
$ cat /proc/crypto | jc --proc
or
$ cat /proc/crypto | jc --proc-crypto
Usage (module):
import jc
result = jc.parse('proc', proc_crypto_file)
or
import jc
result = jc.parse('proc_crypto', proc_crypto_file)
Schema:
"Well-known" keys like `priority` and `refcnt` are converted to integers.
Also, keynames ending in "size" are converted to integers.
If this is not desired, then use the `--raw` (CLI) or `raw=True` (Module)
option.
[
{
"name": string,
"driver": string,
"module": string,
"priority": integer,
"refcnt": integer,
"selftest": string,
"internal": string,
"type": string,
"*size": integer
}
]
Examples:
$ cat /proc/crypto | jc --proc -p
[
{
"name": "ecdh",
"driver": "ecdh-generic",
"module": "ecdh_generic",
"priority": 100,
"refcnt": 1,
"selftest": "passed",
"internal": "no",
"type": "kpp"
},
{
"name": "blake2b-512",
"driver": "blake2b-512-generic",
"module": "blake2b_generic",
"priority": 100,
"refcnt": 1,
"selftest": "passed",
"internal": "no",
"type": "shash",
"blocksize": 128,
"digestsize": 64
},
...
]
$ proc_crypto | jc --proc_crypto -p -r
[
{
"name": "ecdh",
"driver": "ecdh-generic",
"module": "ecdh_generic",
"priority": "100",
"refcnt": "1",
"selftest": "passed",
"internal": "no",
"type": "kpp"
},
{
"name": "blake2b-512",
"driver": "blake2b-512-generic",
"module": "blake2b_generic",
"priority": "100",
"refcnt": "1",
"selftest": "passed",
"internal": "no",
"type": "shash",
"blocksize": "128",
"digestsize": "64"
},
{
"name": "blake2b-384",
"driver": "blake2b-384-generic",
"module": "blake2b_generic",
"priority": "100",
"refcnt": "1",
"selftest": "passed",
"internal": "no",
"type": "shash",
"blocksize": "128",
"digestsize": "48"
},
...
]
<a id="jc.parsers.proc_crypto.parse"></a>
### parse
```python
def parse(data: str, raw: bool = False, quiet: bool = False) -> List[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:
List of Dictionaries. Raw or processed structured data.
### Parser Information
Compatibility: linux
Version 1.0 by Kelly Brazil (kellyjonbrazil@gmail.com)

View File

@ -88,6 +88,7 @@ parsers = [
'proc-buddyinfo',
'proc-consoles',
'proc-cpuinfo',
'proc-crypto',
'proc-meminfo',
'proc-modules',
'ps',

View File

@ -245,8 +245,6 @@ def _process(proc_data: List[Dict]) -> List[Dict]:
List of Dictionaries. Structured to conform to the schema.
"""
int_list = {'size', 'used'}
for entry in proc_data:
for key in entry:
if entry[key] == '':
@ -327,7 +325,7 @@ def parse(
key, val = line.split(':', maxsplit=1)
output_line[key.strip()] = val.strip()
if output_line:
raw_output.append(output_line)
if output_line:
raw_output.append(output_line)
return raw_output if raw else _process(raw_output)

189
jc/parsers/proc_crypto.py Normal file
View File

@ -0,0 +1,189 @@
"""jc - JSON Convert `/proc/crypto` file parser
Usage (cli):
$ cat /proc/crypto | jc --proc
or
$ cat /proc/crypto | jc --proc-crypto
Usage (module):
import jc
result = jc.parse('proc', proc_crypto_file)
or
import jc
result = jc.parse('proc_crypto', proc_crypto_file)
Schema:
"Well-known" keys like `priority` and `refcnt` are converted to integers.
Also, keynames ending in "size" are converted to integers.
If this is not desired, then use the `--raw` (CLI) or `raw=True` (Module)
option.
[
{
"name": string,
"driver": string,
"module": string,
"priority": integer,
"refcnt": integer,
"selftest": string,
"internal": string,
"type": string,
"*size": integer
}
]
Examples:
$ cat /proc/crypto | jc --proc -p
[
{
"name": "ecdh",
"driver": "ecdh-generic",
"module": "ecdh_generic",
"priority": 100,
"refcnt": 1,
"selftest": "passed",
"internal": "no",
"type": "kpp"
},
{
"name": "blake2b-512",
"driver": "blake2b-512-generic",
"module": "blake2b_generic",
"priority": 100,
"refcnt": 1,
"selftest": "passed",
"internal": "no",
"type": "shash",
"blocksize": 128,
"digestsize": 64
},
...
]
$ proc_crypto | jc --proc_crypto -p -r
[
{
"name": "ecdh",
"driver": "ecdh-generic",
"module": "ecdh_generic",
"priority": "100",
"refcnt": "1",
"selftest": "passed",
"internal": "no",
"type": "kpp"
},
{
"name": "blake2b-512",
"driver": "blake2b-512-generic",
"module": "blake2b_generic",
"priority": "100",
"refcnt": "1",
"selftest": "passed",
"internal": "no",
"type": "shash",
"blocksize": "128",
"digestsize": "64"
},
{
"name": "blake2b-384",
"driver": "blake2b-384-generic",
"module": "blake2b_generic",
"priority": "100",
"refcnt": "1",
"selftest": "passed",
"internal": "no",
"type": "shash",
"blocksize": "128",
"digestsize": "48"
},
...
]
"""
from typing import List, Dict
import jc.utils
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.0'
description = '`/proc/crypto` file parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
compatible = ['linux']
hidden = True
__version__ = info.version
def _process(proc_data: List[Dict]) -> List[Dict]:
"""
Final processing to conform to the schema.
Parameters:
proc_data: (List of Dictionaries) raw structured data to process
Returns:
List of Dictionaries. Structured to conform to the schema.
"""
int_list = {'priority', 'refcnt'}
for entry in proc_data:
for key in entry:
if key in int_list or key.endswith('size'):
entry[key] = jc.utils.convert_to_int(entry[key])
return proc_data
def parse(
data: str,
raw: bool = False,
quiet: bool = False
) -> List[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:
List of Dictionaries. Raw or processed structured data.
"""
jc.utils.compatibility(__name__, info.compatible, quiet)
jc.utils.input_type_check(data)
raw_output: List = []
output_line: Dict = {}
if jc.utils.has_data(data):
for line in filter(None, data.splitlines()):
if line.startswith('name'):
if output_line:
raw_output.append(output_line)
output_line = {}
key, val = line.split(':', maxsplit=1)
output_line[key.strip()] = val.strip()
if output_line:
raw_output.append(output_line)
return raw_output if raw else _process(raw_output)

View File

@ -412,6 +412,11 @@ PLIST file parser
\fB--proc-cpuinfo\fP
`/proc/cpuinfo` file parser
.TP
.B
\fB--proc-crypto\fP
`/proc/crypto` file parser
.TP
.B
\fB--proc-meminfo\fP