diff --git a/docs/parsers/id.md b/docs/parsers/id.md index eaf32554..0bac2060 100644 --- a/docs/parsers/id.md +++ b/docs/parsers/id.md @@ -128,4 +128,4 @@ Returns: ### Parser Information Compatibility: linux, darwin, aix, freebsd -Version 1.5 by Kelly Brazil (kellyjonbrazil@gmail.com) +Version 1.6 by Kelly Brazil (kellyjonbrazil@gmail.com) diff --git a/docs/parsers/proc_cpuinfo.md b/docs/parsers/proc_cpuinfo.md index 0c24ddc7..574c919f 100644 --- a/docs/parsers/proc_cpuinfo.md +++ b/docs/parsers/proc_cpuinfo.md @@ -189,7 +189,7 @@ Examples: ... ] - $ proc_cpuinfo | jc --proc_cpuinfo -p -r + $ cat /proc/cpuinfo | jc --proc_cpuinfo -p -r [ { "processor": "0", diff --git a/docs/parsers/proc_crypto.md b/docs/parsers/proc_crypto.md index 835489d7..25e9adc0 100644 --- a/docs/parsers/proc_crypto.md +++ b/docs/parsers/proc_crypto.md @@ -78,7 +78,7 @@ Examples: ... ] - $ proc_crypto | jc --proc_crypto -p -r + $ cat /proc/crypto | jc --proc_crypto -p -r [ { "name": "ecdh", diff --git a/docs/parsers/proc_diskstats.md b/docs/parsers/proc_diskstats.md index 5273814a..43accf9e 100644 --- a/docs/parsers/proc_diskstats.md +++ b/docs/parsers/proc_diskstats.md @@ -105,7 +105,7 @@ Examples: ... ] - $ proc_diskstats | jc --proc_diskstats -p -r + $ cat /proc/diskstats | jc --proc_diskstats -p -r [ { "maj": "7", diff --git a/docs/parsers/proc_interrupts.md b/docs/parsers/proc_interrupts.md index 412449eb..f0f8c123 100644 --- a/docs/parsers/proc_interrupts.md +++ b/docs/parsers/proc_interrupts.md @@ -76,7 +76,7 @@ Examples: ... ] - $ proc_interrupts | jc --proc_interrupts -p -r + $ cat /proc/interrupts | jc --proc_interrupts -p -r [ { "irq": "0", diff --git a/docs/parsers/proc_locks.md b/docs/parsers/proc_locks.md index 38ebb3f9..a611bb62 100644 --- a/docs/parsers/proc_locks.md +++ b/docs/parsers/proc_locks.md @@ -75,7 +75,7 @@ Examples: ... ] - $ proc_locks | jc --proc_locks -p -r + $ cat /proc/locks | jc --proc_locks -p -r [ { "id": "1", diff --git a/docs/parsers/proc_modules.md b/docs/parsers/proc_modules.md index 82833a42..f300ae06 100644 --- a/docs/parsers/proc_modules.md +++ b/docs/parsers/proc_modules.md @@ -75,7 +75,7 @@ Examples: ... ] - $ proc_modules | jc --proc_modules -p -r + $ cat /proc/modules | jc --proc_modules -p -r [ { "module": "binfmt_misc", diff --git a/jc/lib.py b/jc/lib.py index 29e28d78..69065411 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -100,6 +100,7 @@ parsers = [ 'proc-locks', 'proc-meminfo', 'proc-modules', + 'proc-mtrr', 'ps', 'route', 'rpm-qi', diff --git a/jc/parsers/proc_cpuinfo.py b/jc/parsers/proc_cpuinfo.py index 5e826e99..5de13991 100644 --- a/jc/parsers/proc_cpuinfo.py +++ b/jc/parsers/proc_cpuinfo.py @@ -184,7 +184,7 @@ Examples: ... ] - $ proc_cpuinfo | jc --proc_cpuinfo -p -r + $ cat /proc/cpuinfo | jc --proc_cpuinfo -p -r [ { "processor": "0", diff --git a/jc/parsers/proc_crypto.py b/jc/parsers/proc_crypto.py index f2b4fa4a..93cab164 100644 --- a/jc/parsers/proc_crypto.py +++ b/jc/parsers/proc_crypto.py @@ -73,7 +73,7 @@ Examples: ... ] - $ proc_crypto | jc --proc_crypto -p -r + $ cat /proc/crypto | jc --proc_crypto -p -r [ { "name": "ecdh", diff --git a/jc/parsers/proc_diskstats.py b/jc/parsers/proc_diskstats.py index 309bb609..c230b2f8 100644 --- a/jc/parsers/proc_diskstats.py +++ b/jc/parsers/proc_diskstats.py @@ -100,7 +100,7 @@ Examples: ... ] - $ proc_diskstats | jc --proc_diskstats -p -r + $ cat /proc/diskstats | jc --proc_diskstats -p -r [ { "maj": "7", diff --git a/jc/parsers/proc_interrupts.py b/jc/parsers/proc_interrupts.py index 76b76fe3..606cadc1 100644 --- a/jc/parsers/proc_interrupts.py +++ b/jc/parsers/proc_interrupts.py @@ -71,7 +71,7 @@ Examples: ... ] - $ proc_interrupts | jc --proc_interrupts -p -r + $ cat /proc/interrupts | jc --proc_interrupts -p -r [ { "irq": "0", diff --git a/jc/parsers/proc_locks.py b/jc/parsers/proc_locks.py index 9550645a..24d3cd58 100644 --- a/jc/parsers/proc_locks.py +++ b/jc/parsers/proc_locks.py @@ -70,7 +70,7 @@ Examples: ... ] - $ proc_locks | jc --proc_locks -p -r + $ cat /proc/locks | jc --proc_locks -p -r [ { "id": "1", diff --git a/jc/parsers/proc_modules.py b/jc/parsers/proc_modules.py index f74e9539..18e35b15 100644 --- a/jc/parsers/proc_modules.py +++ b/jc/parsers/proc_modules.py @@ -70,7 +70,7 @@ Examples: ... ] - $ proc_modules | jc --proc_modules -p -r + $ cat /proc/modules | jc --proc_modules -p -r [ { "module": "binfmt_misc", diff --git a/jc/parsers/proc_mtrr.py b/jc/parsers/proc_mtrr.py new file mode 100644 index 00000000..bc325f26 --- /dev/null +++ b/jc/parsers/proc_mtrr.py @@ -0,0 +1,183 @@ +"""jc - JSON Convert `/proc/mtrr` file parser + +Usage (cli): + + $ cat /proc/mtrr | jc --proc + +or + + $ jc /proc/mtrr + +or + + $ cat /proc/mtrr | jc --proc-mtrr + +Usage (module): + + import jc + result = jc.parse('proc', proc_mtrr_file) + +or + + import jc + result = jc.parse('proc_mtrr', proc_mtrr_file) + +Schema: + + [ + { + "register": string, + "type": string, + "base": string, + "base_mb": integer, + "size": integer, + "count": integer, + "": string # additional key/values are strings + } + ] + +Examples: + + $ cat /proc/mtrr | jc --proc -p + [ + { + "register": "reg00", + "type": "write-back", + "base": "0x000000000", + "base_mb": 0, + "size": 2048, + "count": 1 + }, + { + "register": "reg01", + "type": "write-back", + "base": "0x080000000", + "base_mb": 2048, + "size": 1024, + "count": 1 + }, + ... + ] + + $ cat /proc/mtrr | jc --proc_mtrr -p -r + [ + { + "register": "reg00", + "type": "write-back", + "base": "0x000000000", + "base_mb": "0", + "size": "2048MB", + "count": "1" + }, + { + "register": "reg01", + "type": "write-back", + "base": "0x080000000", + "base_mb": "2048", + "size": "1024MB", + "count": "1" + }, + ... + ] +""" +import re +from typing import List, Dict +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`/proc/mtrr` 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 = {'size', 'count', 'base_mb'} + + for entry in proc_data: + for key in entry: + if key in int_list: + 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 = [] + + if jc.utils.has_data(data): + + for line in filter(None, data.splitlines()): + + split_line = re.split(r',|:', line) + register = split_line.pop(0) + type_ = None + key_vals: list = [] + + base, base_mb = split_line.pop(0).split(maxsplit=1) + key_vals.append(base) + + base_mb = base_mb.replace('(', '').replace(')', '').replace('MB', '').strip() + key_vals.append(f'base_mb={base_mb}') + + for item in split_line: + if '=' in item: + key_vals.append(item.strip()) + + else: + type_ = item.strip() + + output_line = { + 'register': register, + 'type': type_ + } + + kv_dict = {} + + for item in key_vals: + key, val = item.split('=') + kv_dict[key.strip()] = val.strip() + + output_line.update(kv_dict) + 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 78afe6e0..0a41ffe5 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -1,4 +1,4 @@ -.TH jc 1 2022-09-13 1.21.2 "JSON Convert" +.TH jc 1 2022-09-15 1.21.2 "JSON Convert" .SH NAME \fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings .SH SYNOPSIS