diff --git a/docs/parsers/proc_devices.md b/docs/parsers/proc_devices.md new file mode 100644 index 00000000..b269b321 --- /dev/null +++ b/docs/parsers/proc_devices.md @@ -0,0 +1,99 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_devices + +jc - JSON Convert `/proc/devices` file parser + +Usage (cli): + + $ cat /proc/devices | jc --proc + +or + + $ cat /proc/devices | jc --proc-devices + +Usage (module): + + import jc + result = jc.parse('proc', proc_devices_file) + +or + + import jc + result = jc.parse('proc_devices', proc_devices_file) + +Schema: + +Since devices can be members of multiple groups, the value for each device +is a list. + + { + "character": { + "": [ + string + ] + }, + "block": { + "": [ + string + ] + } + } + +Examples: + + $ cat /proc/devices | jc --proc -p + { + "character": { + "1": [ + "mem" + ], + "4": [ + "/dev/vc/0", + "tty", + "ttyS" + ], + "5": [ + "/dev/tty", + "/dev/console", + "/dev/ptmx", + "ttyprintk" + ], + "block": { + "7": [ + "loop" + ], + "8": [ + "sd" + ], + "9": [ + "md" + ] + } + } + + + +### parse + +```python +def parse(data: str, raw: bool = False, quiet: bool = False) -> 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: + + Dictionary. 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 d5624c06..625bdcea 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -89,6 +89,7 @@ parsers = [ 'proc-consoles', 'proc-cpuinfo', 'proc-crypto', + 'proc-devices', 'proc-meminfo', 'proc-modules', 'ps', diff --git a/jc/parsers/proc_devices.py b/jc/parsers/proc_devices.py new file mode 100644 index 00000000..deec4866 --- /dev/null +++ b/jc/parsers/proc_devices.py @@ -0,0 +1,162 @@ +"""jc - JSON Convert `/proc/devices` file parser + +Usage (cli): + + $ cat /proc/devices | jc --proc + +or + + $ cat /proc/devices | jc --proc-devices + +Usage (module): + + import jc + result = jc.parse('proc', proc_devices_file) + +or + + import jc + result = jc.parse('proc_devices', proc_devices_file) + +Schema: + +Since devices can be members of multiple groups, the value for each device +is a list. + + { + "character": { + "": [ + string + ] + }, + "block": { + "": [ + string + ] + } + } + +Examples: + + $ cat /proc/devices | jc --proc -p + { + "character": { + "1": [ + "mem" + ], + "4": [ + "/dev/vc/0", + "tty", + "ttyS" + ], + "5": [ + "/dev/tty", + "/dev/console", + "/dev/ptmx", + "ttyprintk" + ], + "block": { + "7": [ + "loop" + ], + "8": [ + "sd" + ], + "9": [ + "md" + ] + } + } +""" +from typing import Dict +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`/proc/devices` file parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + compatible = ['linux'] + hidden = True + + +__version__ = info.version + + +def _process(proc_data: Dict) -> Dict: + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (Dictionary) raw structured data to process + + Returns: + + Dictionary. Structured to conform to the schema. + """ + return proc_data + + +def parse( + data: str, + raw: bool = False, + quiet: bool = False +) -> 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: + + Dictionary. Raw or processed structured data. + """ + jc.utils.compatibility(__name__, info.compatible, quiet) + jc.utils.input_type_check(data) + + raw_output: Dict = {} + character: Dict = {} + block: Dict = {} + section = '' + + if jc.utils.has_data(data): + + for line in filter(None, data.splitlines()): + if 'Character devices:' in line: + section = 'character' + continue + + if 'Block devices:' in line: + section = 'block' + continue + + devnum, group = line.split() + + if section == 'character': + if not devnum in character: + character[devnum] = [] + + character[devnum].append(group) + continue + + if section == 'block': + if not devnum in block: + block[devnum] = [] + + block[devnum].append(group) + continue + + if character or block: + raw_output = { + 'character': character, + 'block': block + } + + return raw_output if raw else _process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index 3ba16bea..33be6f91 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -417,6 +417,11 @@ PLIST file parser \fB--proc-crypto\fP `/proc/crypto` file parser +.TP +.B +\fB--proc-devices\fP +`/proc/devices` file parser + .TP .B \fB--proc-meminfo\fP