From e1f6007dea6647115e58c6f7965b5d632611182b Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 9 Sep 2022 17:03:51 -0700 Subject: [PATCH] add proc-filesystems parser --- docs/parsers/proc_filesystems.md | 81 ++++++++++++++++++++ jc/lib.py | 1 + jc/parsers/proc_filesystems.py | 122 +++++++++++++++++++++++++++++++ man/jc.1 | 5 ++ 4 files changed, 209 insertions(+) create mode 100644 docs/parsers/proc_filesystems.md create mode 100644 jc/parsers/proc_filesystems.py diff --git a/docs/parsers/proc_filesystems.md b/docs/parsers/proc_filesystems.md new file mode 100644 index 00000000..b4b8b443 --- /dev/null +++ b/docs/parsers/proc_filesystems.md @@ -0,0 +1,81 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_filesystems + +jc - JSON Convert `/proc/filesystems` file parser + +Usage (cli): + + $ cat /proc/filesystems | jc --proc + +or + + $ jc /proc/filesystems + +or + + $ cat /proc/filesystems | jc --proc-filesystems + +Usage (module): + + import jc + result = jc.parse('proc', proc_filesystems_file) + +or + + import jc + result = jc.parse('proc_filesystems', proc_filesystems_file) + +Schema: + + [ + { + "filesystem": string, + "nodev": boolean + } + ] + +Examples: + + $ cat /proc/filesystems | jc --proc -p + [ + { + "filesystem": "sysfs", + "nodev": true + }, + { + "filesystem": "tmpfs", + "nodev": true + }, + { + "filesystem": "bdev", + "nodev": true + }, + ... + ] + + + +### 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 fe5d7d2c..f6bbc733 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -92,6 +92,7 @@ parsers = [ 'proc-devices', 'proc-diskstats', 'proc-driver-rtc', + 'proc-filesystems', 'proc-meminfo', 'proc-modules', 'ps', diff --git a/jc/parsers/proc_filesystems.py b/jc/parsers/proc_filesystems.py new file mode 100644 index 00000000..91170de4 --- /dev/null +++ b/jc/parsers/proc_filesystems.py @@ -0,0 +1,122 @@ +"""jc - JSON Convert `/proc/filesystems` file parser + +Usage (cli): + + $ cat /proc/filesystems | jc --proc + +or + + $ jc /proc/filesystems + +or + + $ cat /proc/filesystems | jc --proc-filesystems + +Usage (module): + + import jc + result = jc.parse('proc', proc_filesystems_file) + +or + + import jc + result = jc.parse('proc_filesystems', proc_filesystems_file) + +Schema: + + [ + { + "filesystem": string, + "nodev": boolean + } + ] + +Examples: + + $ cat /proc/filesystems | jc --proc -p + [ + { + "filesystem": "sysfs", + "nodev": true + }, + { + "filesystem": "tmpfs", + "nodev": true + }, + { + "filesystem": "bdev", + "nodev": true + }, + ... + ] +""" +from typing import List, Dict +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`/proc/filesystems` 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. + """ + 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 = line.split() + output_line = {'filesystem': split_line[-1]} + + if len(split_line) == 2: + output_line['nodev'] = True # type: ignore + else: + output_line['nodev'] = False # type: ignore + + 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 bb13036b..23069a54 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -445,6 +445,11 @@ PLIST file parser \fB--proc-driver-rtc\fP `/proc/driver_rtc` file parser +.TP +.B +\fB--proc-filesystems\fP +`/proc/filesystems` file parser + .TP .B \fB--proc-meminfo\fP