From 61cd9acaa2a618872c4c88dfbc3711fed2813622 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 6 Sep 2022 15:19:59 -0700 Subject: [PATCH] add buddyinfo parser and doc update --- docs/parsers/proc_buddyinfo.md | 124 +++++++++++++++++++++++ jc/lib.py | 1 + jc/parsers/proc_buddyinfo.py | 175 +++++++++++++++++++++++++++++++++ jc/parsers/proc_meminfo.py | 2 +- jc/parsers/proc_modules.py | 2 +- man/jc.1 | 9 +- 6 files changed, 309 insertions(+), 4 deletions(-) create mode 100644 docs/parsers/proc_buddyinfo.md create mode 100644 jc/parsers/proc_buddyinfo.py diff --git a/docs/parsers/proc_buddyinfo.md b/docs/parsers/proc_buddyinfo.md new file mode 100644 index 00000000..df66ef22 --- /dev/null +++ b/docs/parsers/proc_buddyinfo.md @@ -0,0 +1,124 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_buddyinfo + +jc - JSON Convert `/proc/buddyinfo` file parser + +Usage (cli): + + $ cat /proc/buddyinfo | jc --proc + +or + + $ cat /proc/buddyinfo | jc --proc-buddyinfo + +Usage (module): + + import jc + result = jc.parse('proc', proc_buddyinfo_file) + +or + + import jc + result = jc.parse('proc_buddyinfo', proc_buddyinfo_file) + +Schema: + +All values are integers. + + [ + { + "node": integer, + "zone": string, + "free_chunks": [ + integer # [0] + ] + } + ] + + [0] array index correlates to the Order number. + E.g. free_chunks[0] is the value for Order 0 + + +Examples: + + $ cat /proc/buddyinfo | jc --proc -p + [ + { + "node": 0, + "zone": "DMA", + "free_chunks": [ + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 3 + ] + }, + { + "node": 0, + "zone": "DMA32", + "free_chunks": [ + 78, + 114, + 82, + 52, + 38, + 25, + 13, + 9, + 3, + 4, + 629 + ] + }, + { + "node": 0, + "zone": "Normal", + "free_chunks": [ + 0, + 22, + 8, + 10, + 1, + 1, + 2, + 11, + 13, + 0, + 0 + ] + } + ] + + + +### 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 49a343b7..51261484 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -85,6 +85,7 @@ parsers = [ 'plist', 'postconf', 'proc', + 'proc-buddyinfo', 'proc-meminfo', 'proc-modules', 'ps', diff --git a/jc/parsers/proc_buddyinfo.py b/jc/parsers/proc_buddyinfo.py new file mode 100644 index 00000000..3c2bd44e --- /dev/null +++ b/jc/parsers/proc_buddyinfo.py @@ -0,0 +1,175 @@ +"""jc - JSON Convert `/proc/buddyinfo` file parser + +Usage (cli): + + $ cat /proc/buddyinfo | jc --proc + +or + + $ cat /proc/buddyinfo | jc --proc-buddyinfo + +Usage (module): + + import jc + result = jc.parse('proc', proc_buddyinfo_file) + +or + + import jc + result = jc.parse('proc_buddyinfo', proc_buddyinfo_file) + +Schema: + +All values are integers. + + [ + { + "node": integer, + "zone": string, + "free_chunks": [ + integer # [0] + ] + } + ] + + [0] array index correlates to the Order number. + E.g. free_chunks[0] is the value for Order 0 + + +Examples: + + $ cat /proc/buddyinfo | jc --proc -p + [ + { + "node": 0, + "zone": "DMA", + "free_chunks": [ + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 3 + ] + }, + { + "node": 0, + "zone": "DMA32", + "free_chunks": [ + 78, + 114, + 82, + 52, + 38, + 25, + 13, + 9, + 3, + 4, + 629 + ] + }, + { + "node": 0, + "zone": "Normal", + "free_chunks": [ + 0, + 22, + 8, + 10, + 1, + 1, + 2, + 11, + 13, + 0, + 0 + ] + } + ] +""" +from typing import List, Dict +import jc.utils + + +class info(): + """Provides parser metadata (version, author, etc.)""" + version = '1.0' + description = '`/proc/buddyinfo` 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 = {'node'} + + for entry in proc_data: + for key in entry: + if key in int_list: + entry[key] = jc.utils.convert_to_int(entry[key]) + + if 'free_chunks' in entry: + entry['free_chunks'] = [int(x) for x in entry['free_chunks']] + + 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()): + + buddy_list = line.split() + + raw_output.append( + { + 'node': buddy_list[1][:-1], + 'zone': buddy_list[3], + 'free_chunks': buddy_list[4:] + } + ) + + return raw_output if raw else _process(raw_output) diff --git a/jc/parsers/proc_meminfo.py b/jc/parsers/proc_meminfo.py index f1571441..daed6188 100644 --- a/jc/parsers/proc_meminfo.py +++ b/jc/parsers/proc_meminfo.py @@ -90,7 +90,7 @@ import jc.utils class info(): """Provides parser metadata (version, author, etc.)""" version = '1.0' - description = '`/proc/meminfo` command parser' + description = '`/proc/meminfo` file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' compatible = ['linux'] diff --git a/jc/parsers/proc_modules.py b/jc/parsers/proc_modules.py index 92bb82c4..59715dff 100644 --- a/jc/parsers/proc_modules.py +++ b/jc/parsers/proc_modules.py @@ -104,7 +104,7 @@ import jc.utils class info(): """Provides parser metadata (version, author, etc.)""" version = '1.0' - description = '`/proc/modules` command parser' + description = '`/proc/modules` file parser' author = 'Kelly Brazil' author_email = 'kellyjonbrazil@gmail.com' compatible = ['linux'] diff --git a/man/jc.1 b/man/jc.1 index 92497f32..37af3839 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -397,15 +397,20 @@ PLIST file parser \fB--proc\fP `/proc/` file parser +.TP +.B +\fB--proc-buddyinfo\fP +`/proc/buddyinfo` file parser + .TP .B \fB--proc-meminfo\fP -`/proc/meminfo` command parser +`/proc/meminfo` file parser .TP .B \fB--proc-modules\fP -`/proc/modules` command parser +`/proc/modules` file parser .TP .B