From fcfbbc6d84a1494dc98ba87f91d7045070e92c16 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Sun, 25 Sep 2022 14:13:20 -0700 Subject: [PATCH] add proc-net-if-inet6 parser and tests --- docs/parsers/proc_net_if_inet6.md | 88 ++++++++++++++ jc/lib.py | 1 + jc/parsers/proc_net_if_inet6.py | 122 ++++++++++++++++++++ tests/fixtures/linux-proc/net_if_inet6.json | 1 + tests/test_proc_net_if_inet6.py | 44 +++++++ 5 files changed, 256 insertions(+) create mode 100644 docs/parsers/proc_net_if_inet6.md create mode 100644 jc/parsers/proc_net_if_inet6.py create mode 100644 tests/fixtures/linux-proc/net_if_inet6.json create mode 100644 tests/test_proc_net_if_inet6.py diff --git a/docs/parsers/proc_net_if_inet6.md b/docs/parsers/proc_net_if_inet6.md new file mode 100644 index 00000000..893d6b3e --- /dev/null +++ b/docs/parsers/proc_net_if_inet6.md @@ -0,0 +1,88 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_net\_if\_inet6 + +jc - JSON Convert `/proc/net/if_inet6` file parser + +Usage (cli): + + $ cat /proc/net/if_inet6 | jc --proc + +or + + $ jc /proc/net/if_inet6 + +or + + $ cat /proc/net/if_inet6 | jc --proc-net-if-inet6 + +Usage (module): + + import jc + result = jc.parse('proc', proc_net_if_inet6_file) + +or + + import jc + result = jc.parse('proc_net_if_inet6', proc_net_if_inet6_file) + +Schema: + + [ + { + "address": string, + "index": string, + "prefix": string, + "scope": string, + "flags": string, + "name": string + } + ] + +Examples: + + $ cat /proc/net/if_inet6 | jc --proc -p + [ + { + "address": "fe80000000000000020c29fffea4e315", + "index": "02", + "prefix": "40", + "scope": "20", + "flags": "80", + "name": "ens33" + }, + { + "address": "00000000000000000000000000000001", + "index": "01", + "prefix": "80", + "scope": "10", + "flags": "80", + "name": "lo" + } + ] + + + +### 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 a54a0c90..b60efebd 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -124,6 +124,7 @@ parsers = [ 'proc-net-arp', 'proc-net-dev', 'proc-net-dev-mcast', + 'proc-net-if-inet6', 'ps', 'route', 'rpm-qi', diff --git a/jc/parsers/proc_net_if_inet6.py b/jc/parsers/proc_net_if_inet6.py new file mode 100644 index 00000000..700e8ba0 --- /dev/null +++ b/jc/parsers/proc_net_if_inet6.py @@ -0,0 +1,122 @@ +"""jc - JSON Convert `/proc/net/if_inet6` file parser + +Usage (cli): + + $ cat /proc/net/if_inet6 | jc --proc + +or + + $ jc /proc/net/if_inet6 + +or + + $ cat /proc/net/if_inet6 | jc --proc-net-if-inet6 + +Usage (module): + + import jc + result = jc.parse('proc', proc_net_if_inet6_file) + +or + + import jc + result = jc.parse('proc_net_if_inet6', proc_net_if_inet6_file) + +Schema: + + [ + { + "address": string, + "index": string, + "prefix": string, + "scope": string, + "flags": string, + "name": string + } + ] + +Examples: + + $ cat /proc/net/if_inet6 | jc --proc -p + [ + { + "address": "fe80000000000000020c29fffea4e315", + "index": "02", + "prefix": "40", + "scope": "20", + "flags": "80", + "name": "ens33" + }, + { + "address": "00000000000000000000000000000001", + "index": "01", + "prefix": "80", + "scope": "10", + "flags": "80", + "name": "lo" + } + ] +""" +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/net/if_inet6` 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): + + header = 'address index prefix scope flags name\n' + data = header + data + raw_output = simple_table_parse(data.splitlines()) + + return raw_output if raw else _process(raw_output) diff --git a/tests/fixtures/linux-proc/net_if_inet6.json b/tests/fixtures/linux-proc/net_if_inet6.json new file mode 100644 index 00000000..8b1ca114 --- /dev/null +++ b/tests/fixtures/linux-proc/net_if_inet6.json @@ -0,0 +1 @@ +[{"address":"fe80000000000000020c29fffea4e315","index":"02","prefix":"40","scope":"20","flags":"80","name":"ens33"},{"address":"00000000000000000000000000000001","index":"01","prefix":"80","scope":"10","flags":"80","name":"lo"}] diff --git a/tests/test_proc_net_if_inet6.py b/tests/test_proc_net_if_inet6.py new file mode 100644 index 00000000..32e04fc9 --- /dev/null +++ b/tests/test_proc_net_if_inet6.py @@ -0,0 +1,44 @@ +import os +import unittest +import json +from typing import Dict +import jc.parsers.proc_net_if_inet6 + +THIS_DIR = os.path.dirname(os.path.abspath(__file__)) + + +class MyTests(unittest.TestCase): + f_in: Dict = {} + f_json: Dict = {} + + @classmethod + def setUpClass(cls): + fixtures = { + 'proc_net_if_inet6': ( + 'fixtures/linux-proc/net_if_inet6', + 'fixtures/linux-proc/net_if_inet6.json') + } + + for file, filepaths in fixtures.items(): + with open(os.path.join(THIS_DIR, filepaths[0]), 'r', encoding='utf-8') as a, \ + open(os.path.join(THIS_DIR, filepaths[1]), 'r', encoding='utf-8') as b: + cls.f_in[file] = a.read() + cls.f_json[file] = json.loads(b.read()) + + + def test_proc_net_if_inet6_nodata(self): + """ + Test 'proc_net_if_inet6' with no data + """ + self.assertEqual(jc.parsers.proc_net_if_inet6.parse('', quiet=True), []) + + def test_proc_net_if_inet6(self): + """ + Test '/proc/net/if_inet6' + """ + self.assertEqual(jc.parsers.proc_net_if_inet6.parse(self.f_in['proc_net_if_inet6'], quiet=True), + self.f_json['proc_net_if_inet6']) + + +if __name__ == '__main__': + unittest.main()