1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-13 01:20:24 +02:00

doc update

This commit is contained in:
Kelly Brazil
2022-09-15 16:57:45 -07:00
parent fd28fea3b6
commit e4e07b76ec
16 changed files with 198 additions and 14 deletions

View File

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

View File

@ -189,7 +189,7 @@ Examples:
...
]
$ proc_cpuinfo | jc --proc_cpuinfo -p -r
$ cat /proc/cpuinfo | jc --proc_cpuinfo -p -r
[
{
"processor": "0",

View File

@ -78,7 +78,7 @@ Examples:
...
]
$ proc_crypto | jc --proc_crypto -p -r
$ cat /proc/crypto | jc --proc_crypto -p -r
[
{
"name": "ecdh",

View File

@ -105,7 +105,7 @@ Examples:
...
]
$ proc_diskstats | jc --proc_diskstats -p -r
$ cat /proc/diskstats | jc --proc_diskstats -p -r
[
{
"maj": "7",

View File

@ -76,7 +76,7 @@ Examples:
...
]
$ proc_interrupts | jc --proc_interrupts -p -r
$ cat /proc/interrupts | jc --proc_interrupts -p -r
[
{
"irq": "0",

View File

@ -75,7 +75,7 @@ Examples:
...
]
$ proc_locks | jc --proc_locks -p -r
$ cat /proc/locks | jc --proc_locks -p -r
[
{
"id": "1",

View File

@ -75,7 +75,7 @@ Examples:
...
]
$ proc_modules | jc --proc_modules -p -r
$ cat /proc/modules | jc --proc_modules -p -r
[
{
"module": "binfmt_misc",

View File

@ -100,6 +100,7 @@ parsers = [
'proc-locks',
'proc-meminfo',
'proc-modules',
'proc-mtrr',
'ps',
'route',
'rpm-qi',

View File

@ -184,7 +184,7 @@ Examples:
...
]
$ proc_cpuinfo | jc --proc_cpuinfo -p -r
$ cat /proc/cpuinfo | jc --proc_cpuinfo -p -r
[
{
"processor": "0",

View File

@ -73,7 +73,7 @@ Examples:
...
]
$ proc_crypto | jc --proc_crypto -p -r
$ cat /proc/crypto | jc --proc_crypto -p -r
[
{
"name": "ecdh",

View File

@ -100,7 +100,7 @@ Examples:
...
]
$ proc_diskstats | jc --proc_diskstats -p -r
$ cat /proc/diskstats | jc --proc_diskstats -p -r
[
{
"maj": "7",

View File

@ -71,7 +71,7 @@ Examples:
...
]
$ proc_interrupts | jc --proc_interrupts -p -r
$ cat /proc/interrupts | jc --proc_interrupts -p -r
[
{
"irq": "0",

View File

@ -70,7 +70,7 @@ Examples:
...
]
$ proc_locks | jc --proc_locks -p -r
$ cat /proc/locks | jc --proc_locks -p -r
[
{
"id": "1",

View File

@ -70,7 +70,7 @@ Examples:
...
]
$ proc_modules | jc --proc_modules -p -r
$ cat /proc/modules | jc --proc_modules -p -r
[
{
"module": "binfmt_misc",

183
jc/parsers/proc_mtrr.py Normal file
View File

@ -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,
"<key>": 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)

View File

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