From bc816bb8584423ec39bd336d699c322e8ac1913e Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Fri, 16 Sep 2022 16:04:25 -0700 Subject: [PATCH] add proc-swaps parser --- docs/parsers/proc_swaps.md | 91 +++++++++++++++++++++++++ jc/lib.py | 1 + jc/parsers/proc_swaps.py | 132 +++++++++++++++++++++++++++++++++++++ man/jc.1 | 5 ++ 4 files changed, 229 insertions(+) create mode 100644 docs/parsers/proc_swaps.md create mode 100644 jc/parsers/proc_swaps.py diff --git a/docs/parsers/proc_swaps.md b/docs/parsers/proc_swaps.md new file mode 100644 index 00000000..47237358 --- /dev/null +++ b/docs/parsers/proc_swaps.md @@ -0,0 +1,91 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_swaps + +jc - JSON Convert `/proc/swaps` file parser + +Usage (cli): + + $ cat /proc/swaps | jc --proc + +or + + $ jc /proc/swaps + +or + + $ cat /proc/swaps | jc --proc-swaps + +Usage (module): + + import jc + result = jc.parse('proc', proc_swaps_file) + +or + + import jc + result = jc.parse('proc_swaps', proc_swaps_file) + +Schema: + + [ + { + "filename": string, + "type": string, + "size": integer, + "used": integer, + "priority": integer + } + ] + +Examples: + + $ cat /proc/swaps | jc --proc -p + [ + { + "filename": "/swap.img", + "type": "file", + "size": 3996668, + "used": 0, + "priority": -2 + }, + ... + ] + + $ cat /proc/swaps | jc --proc_swaps -p -r + [ + { + "filename": "/swap.img", + "type": "file", + "size": "3996668", + "used": "0", + "priority": "-2" + }, + ... + ] + + + +### 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 c43b3e33..36e26946 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -106,6 +106,7 @@ parsers = [ 'proc-slabinfo', 'proc-softirqs', 'proc-stat', + 'proc-swaps', 'proc-pid-numa-maps', 'ps', 'route', diff --git a/jc/parsers/proc_swaps.py b/jc/parsers/proc_swaps.py new file mode 100644 index 00000000..2669159d --- /dev/null +++ b/jc/parsers/proc_swaps.py @@ -0,0 +1,132 @@ +"""jc - JSON Convert `/proc/swaps` file parser + +Usage (cli): + + $ cat /proc/swaps | jc --proc + +or + + $ jc /proc/swaps + +or + + $ cat /proc/swaps | jc --proc-swaps + +Usage (module): + + import jc + result = jc.parse('proc', proc_swaps_file) + +or + + import jc + result = jc.parse('proc_swaps', proc_swaps_file) + +Schema: + + [ + { + "filename": string, + "type": string, + "size": integer, + "used": integer, + "priority": integer + } + ] + +Examples: + + $ cat /proc/swaps | jc --proc -p + [ + { + "filename": "/swap.img", + "type": "file", + "size": 3996668, + "used": 0, + "priority": -2 + }, + ... + ] + + $ cat /proc/swaps | jc --proc_swaps -p -r + [ + { + "filename": "/swap.img", + "type": "file", + "size": "3996668", + "used": "0", + "priority": "-2" + }, + ... + ] +""" +from typing import List, Dict +import jc.utils +from jc.parsers.universal import simple_table_parse + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`/proc/swaps` 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', 'used', 'priority'} + + 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): + + cleandata = list(filter(None, data.splitlines())) + cleandata[0] = cleandata[0].lower() + raw_output = simple_table_parse(cleandata) + + return raw_output if raw else _process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index d6d52b16..c2545fb4 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -515,6 +515,11 @@ PLIST file parser \fB--proc-stat\fP `/proc/stat` file parser +.TP +.B +\fB--proc-swaps\fP +`/proc/swaps` file parser + .TP .B \fB--proc-pid-numa-maps\fP