diff --git a/docs/parsers/proc_crypto.md b/docs/parsers/proc_crypto.md new file mode 100644 index 00000000..119a3378 --- /dev/null +++ b/docs/parsers/proc_crypto.md @@ -0,0 +1,139 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# 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" + }, + ... + ] + + + +### 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) diff --git a/jc/lib.py b/jc/lib.py index 9cecbe5d..d5624c06 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -88,6 +88,7 @@ parsers = [ 'proc-buddyinfo', 'proc-consoles', 'proc-cpuinfo', + 'proc-crypto', 'proc-meminfo', 'proc-modules', 'ps', diff --git a/jc/parsers/proc_cpuinfo.py b/jc/parsers/proc_cpuinfo.py index a059eea5..0627783f 100644 --- a/jc/parsers/proc_cpuinfo.py +++ b/jc/parsers/proc_cpuinfo.py @@ -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) diff --git a/jc/parsers/proc_crypto.py b/jc/parsers/proc_crypto.py new file mode 100644 index 00000000..8e75c21f --- /dev/null +++ b/jc/parsers/proc_crypto.py @@ -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) diff --git a/man/jc.1 b/man/jc.1 index f21b2ea9..3ba16bea 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -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