diff --git a/docs/parsers/proc_net_igmp6.md b/docs/parsers/proc_net_igmp6.md index 917446f4..089950c5 100644 --- a/docs/parsers/proc_net_igmp6.md +++ b/docs/parsers/proc_net_igmp6.md @@ -15,7 +15,7 @@ or or - $ cat /proc/net/igmp6 | jc --proc-net-if-inet6 + $ cat /proc/net/igmp6 | jc --proc-net-igmp6 Usage (module): diff --git a/docs/parsers/proc_net_ipv6_route.md b/docs/parsers/proc_net_ipv6_route.md new file mode 100644 index 00000000..9c8f033f --- /dev/null +++ b/docs/parsers/proc_net_ipv6_route.md @@ -0,0 +1,89 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_net\_ipv6\_route + +jc - JSON Convert `/proc/net/ipv6_route` file parser + +Usage (cli): + + $ cat /proc/net/ipv6_route | jc --proc + +or + + $ jc /proc/net/ipv6_route + +or + + $ cat /proc/net/ipv6_route | jc --proc-net-ipv6-route + +Usage (module): + + import jc + result = jc.parse('proc', proc_net_ipv6_route_file) + +or + + import jc + result = jc.parse('proc_net_ipv6_route', proc_net_ipv6_route_file) + +Schema: + + [ + { + "dest_net": string, + "dest_prefix": string, + "source_net": string, + "source_prefix": string, + "next_hop": string, + "metric": string, + "ref_count": string, + "use_count": string, + "flags": string, + "device": string + } + ] + +Examples: + + $ cat /proc/net/ipv6_route | jc --proc -p + [ + { + "dest_net": "00000000000000000000000000000001", + "dest_prefix": "80", + "source_net": "00000000000000000000000000000000", + "source_prefix": "00", + "next_hop": "00000000000000000000000000000000", + "metric": "00000100", + "ref_count": "00000001", + "use_count": "00000000", + "flags": "00000001", + "device": "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 e4787e93..9ca3f68d 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -127,6 +127,7 @@ parsers = [ 'proc-net-if-inet6', 'proc-net-igmp', 'proc-net-igmp6', + 'proc-net-ipv6-route', 'ps', 'route', 'rpm-qi', diff --git a/jc/parsers/proc_net_igmp6.py b/jc/parsers/proc_net_igmp6.py index 8517657f..543e6afa 100644 --- a/jc/parsers/proc_net_igmp6.py +++ b/jc/parsers/proc_net_igmp6.py @@ -10,7 +10,7 @@ or or - $ cat /proc/net/igmp6 | jc --proc-net-if-inet6 + $ cat /proc/net/igmp6 | jc --proc-net-igmp6 Usage (module): diff --git a/jc/parsers/proc_net_ipv6_route.py b/jc/parsers/proc_net_ipv6_route.py new file mode 100644 index 00000000..ccc0be1a --- /dev/null +++ b/jc/parsers/proc_net_ipv6_route.py @@ -0,0 +1,123 @@ +"""jc - JSON Convert `/proc/net/ipv6_route` file parser + +Usage (cli): + + $ cat /proc/net/ipv6_route | jc --proc + +or + + $ jc /proc/net/ipv6_route + +or + + $ cat /proc/net/ipv6_route | jc --proc-net-ipv6-route + +Usage (module): + + import jc + result = jc.parse('proc', proc_net_ipv6_route_file) + +or + + import jc + result = jc.parse('proc_net_ipv6_route', proc_net_ipv6_route_file) + +Schema: + + [ + { + "dest_net": string, + "dest_prefix": string, + "source_net": string, + "source_prefix": string, + "next_hop": string, + "metric": string, + "ref_count": string, + "use_count": string, + "flags": string, + "device": string + } + ] + +Examples: + + $ cat /proc/net/ipv6_route | jc --proc -p + [ + { + "dest_net": "00000000000000000000000000000001", + "dest_prefix": "80", + "source_net": "00000000000000000000000000000000", + "source_prefix": "00", + "next_hop": "00000000000000000000000000000000", + "metric": "00000100", + "ref_count": "00000001", + "use_count": "00000000", + "flags": "00000001", + "device": "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/ipv6_route` 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 = 'dest_net dest_prefix source_net source_prefix next_hop metric ref_count use_count flags device\n' + data = header + data + raw_output = simple_table_parse(data.splitlines()) + + return raw_output if raw else _process(raw_output) diff --git a/man/jc.1 b/man/jc.1 index aa9e78f5..2aa7db1b 100644 --- a/man/jc.1 +++ b/man/jc.1 @@ -620,6 +620,11 @@ PLIST file parser \fB--proc-net-igmp6\fP `/proc/net/igmp6` file parser +.TP +.B +\fB--proc-net-ipv6-route\fP +`/proc/net/ipv6_route` file parser + .TP .B \fB--ps\fP diff --git a/tests/fixtures/linux-proc/net_ipv6_route.json b/tests/fixtures/linux-proc/net_ipv6_route.json new file mode 100644 index 00000000..aa0dc1c7 --- /dev/null +++ b/tests/fixtures/linux-proc/net_ipv6_route.json @@ -0,0 +1 @@ +[{"dest_net":"00000000000000000000000000000001","dest_prefix":"80","source_net":"00000000000000000000000000000000","source_prefix":"00","next_hop":"00000000000000000000000000000000","metric":"00000100","ref_count":"00000001","use_count":"00000000","flags":"00000001","device":"lo"},{"dest_net":"fe800000000000000000000000000000","dest_prefix":"40","source_net":"00000000000000000000000000000000","source_prefix":"00","next_hop":"00000000000000000000000000000000","metric":"00000100","ref_count":"00000001","use_count":"00000000","flags":"00000001","device":"ens33"},{"dest_net":"00000000000000000000000000000000","dest_prefix":"00","source_net":"00000000000000000000000000000000","source_prefix":"00","next_hop":"00000000000000000000000000000000","metric":"ffffffff","ref_count":"00000001","use_count":"00000000","flags":"00200200","device":"lo"},{"dest_net":"00000000000000000000000000000001","dest_prefix":"80","source_net":"00000000000000000000000000000000","source_prefix":"00","next_hop":"00000000000000000000000000000000","metric":"00000000","ref_count":"00000004","use_count":"00000000","flags":"80200001","device":"lo"},{"dest_net":"fe80000000000000020c29fffea4e315","dest_prefix":"80","source_net":"00000000000000000000000000000000","source_prefix":"00","next_hop":"00000000000000000000000000000000","metric":"00000000","ref_count":"00000002","use_count":"00000000","flags":"80200001","device":"ens33"},{"dest_net":"ff000000000000000000000000000000","dest_prefix":"08","source_net":"00000000000000000000000000000000","source_prefix":"00","next_hop":"00000000000000000000000000000000","metric":"00000100","ref_count":"00000004","use_count":"00000000","flags":"00000001","device":"ens33"},{"dest_net":"00000000000000000000000000000000","dest_prefix":"00","source_net":"00000000000000000000000000000000","source_prefix":"00","next_hop":"00000000000000000000000000000000","metric":"ffffffff","ref_count":"00000001","use_count":"00000000","flags":"00200200","device":"lo"}] diff --git a/tests/test_proc_net_ipv6_route.py b/tests/test_proc_net_ipv6_route.py new file mode 100644 index 00000000..c696fb02 --- /dev/null +++ b/tests/test_proc_net_ipv6_route.py @@ -0,0 +1,44 @@ +import os +import unittest +import json +from typing import Dict +import jc.parsers.proc_net_ipv6_route + +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_ipv6_route': ( + 'fixtures/linux-proc/net_ipv6_route', + 'fixtures/linux-proc/net_ipv6_route.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_ipv6_route_nodata(self): + """ + Test 'proc_net_ipv6_route' with no data + """ + self.assertEqual(jc.parsers.proc_net_ipv6_route.parse('', quiet=True), []) + + def test_proc_net_ipv6_route(self): + """ + Test '/proc/net/ipv6_route' + """ + self.assertEqual(jc.parsers.proc_net_ipv6_route.parse(self.f_in['proc_net_ipv6_route'], quiet=True), + self.f_json['proc_net_ipv6_route']) + + +if __name__ == '__main__': + unittest.main()