mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-13 01:20:24 +02:00
add proc-net-igmp6 parser and tests
This commit is contained in:
125
docs/parsers/proc_net_igmp6.md
Normal file
125
docs/parsers/proc_net_igmp6.md
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
[Home](https://kellyjonbrazil.github.io/jc/)
|
||||||
|
<a id="jc.parsers.proc_net_igmp6"></a>
|
||||||
|
|
||||||
|
# jc.parsers.proc\_net\_igmp6
|
||||||
|
|
||||||
|
jc - JSON Convert `/proc/net/igmp6` file parser
|
||||||
|
|
||||||
|
Usage (cli):
|
||||||
|
|
||||||
|
$ cat /proc/net/igmp6 | jc --proc
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
$ jc /proc/net/igmp6
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
$ cat /proc/net/igmp6 | jc --proc-net-if-inet6
|
||||||
|
|
||||||
|
Usage (module):
|
||||||
|
|
||||||
|
import jc
|
||||||
|
result = jc.parse('proc', proc_net_igmp6_file)
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
import jc
|
||||||
|
result = jc.parse('proc_net_igmp6', proc_net_igmp6_file)
|
||||||
|
|
||||||
|
Schema:
|
||||||
|
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"index": integer,
|
||||||
|
"name": string,
|
||||||
|
"address": string,
|
||||||
|
"users": integer,
|
||||||
|
"group": string,
|
||||||
|
"reporters": integer
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
$ cat /proc/net/igmp6 | jc --proc -p
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"index": 1,
|
||||||
|
"name": "lo",
|
||||||
|
"address": "ff020000000000000000000000000001",
|
||||||
|
"users": 1,
|
||||||
|
"group": "0000000C",
|
||||||
|
"reporters": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"index": 1,
|
||||||
|
"name": "lo",
|
||||||
|
"address": "ff010000000000000000000000000001",
|
||||||
|
"users": 1,
|
||||||
|
"group": "00000008",
|
||||||
|
"reporters": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"index": 2,
|
||||||
|
"name": "ens33",
|
||||||
|
"address": "ff0200000000000000000001ffa4e315",
|
||||||
|
"users": 1,
|
||||||
|
"group": "00000004",
|
||||||
|
"reporters": 0
|
||||||
|
},
|
||||||
|
...
|
||||||
|
]
|
||||||
|
|
||||||
|
$ cat /proc/net/igmp6 | jc --proc -p -r
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"index": "1",
|
||||||
|
"name": "lo",
|
||||||
|
"address": "ff020000000000000000000000000001",
|
||||||
|
"users": "1",
|
||||||
|
"group": "0000000C",
|
||||||
|
"reporters": "0"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"index": "1",
|
||||||
|
"name": "lo",
|
||||||
|
"address": "ff010000000000000000000000000001",
|
||||||
|
"users": "1",
|
||||||
|
"group": "00000008",
|
||||||
|
"reporters": "0"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"index": "2",
|
||||||
|
"name": "ens33",
|
||||||
|
"address": "ff0200000000000000000001ffa4e315",
|
||||||
|
"users": "1",
|
||||||
|
"group": "00000004",
|
||||||
|
"reporters": "0"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
<a id="jc.parsers.proc_net_igmp6.parse"></a>
|
||||||
|
|
||||||
|
### 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)
|
@ -126,6 +126,7 @@ parsers = [
|
|||||||
'proc-net-dev-mcast',
|
'proc-net-dev-mcast',
|
||||||
'proc-net-if-inet6',
|
'proc-net-if-inet6',
|
||||||
'proc-net-igmp',
|
'proc-net-igmp',
|
||||||
|
'proc-net-igmp6',
|
||||||
'ps',
|
'ps',
|
||||||
'route',
|
'route',
|
||||||
'rpm-qi',
|
'rpm-qi',
|
||||||
|
166
jc/parsers/proc_net_igmp6.py
Normal file
166
jc/parsers/proc_net_igmp6.py
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
"""jc - JSON Convert `/proc/net/igmp6` file parser
|
||||||
|
|
||||||
|
Usage (cli):
|
||||||
|
|
||||||
|
$ cat /proc/net/igmp6 | jc --proc
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
$ jc /proc/net/igmp6
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
$ cat /proc/net/igmp6 | jc --proc-net-if-inet6
|
||||||
|
|
||||||
|
Usage (module):
|
||||||
|
|
||||||
|
import jc
|
||||||
|
result = jc.parse('proc', proc_net_igmp6_file)
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
import jc
|
||||||
|
result = jc.parse('proc_net_igmp6', proc_net_igmp6_file)
|
||||||
|
|
||||||
|
Schema:
|
||||||
|
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"index": integer,
|
||||||
|
"name": string,
|
||||||
|
"address": string,
|
||||||
|
"users": integer,
|
||||||
|
"group": string,
|
||||||
|
"reporters": integer
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
$ cat /proc/net/igmp6 | jc --proc -p
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"index": 1,
|
||||||
|
"name": "lo",
|
||||||
|
"address": "ff020000000000000000000000000001",
|
||||||
|
"users": 1,
|
||||||
|
"group": "0000000C",
|
||||||
|
"reporters": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"index": 1,
|
||||||
|
"name": "lo",
|
||||||
|
"address": "ff010000000000000000000000000001",
|
||||||
|
"users": 1,
|
||||||
|
"group": "00000008",
|
||||||
|
"reporters": 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"index": 2,
|
||||||
|
"name": "ens33",
|
||||||
|
"address": "ff0200000000000000000001ffa4e315",
|
||||||
|
"users": 1,
|
||||||
|
"group": "00000004",
|
||||||
|
"reporters": 0
|
||||||
|
},
|
||||||
|
...
|
||||||
|
]
|
||||||
|
|
||||||
|
$ cat /proc/net/igmp6 | jc --proc -p -r
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"index": "1",
|
||||||
|
"name": "lo",
|
||||||
|
"address": "ff020000000000000000000000000001",
|
||||||
|
"users": "1",
|
||||||
|
"group": "0000000C",
|
||||||
|
"reporters": "0"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"index": "1",
|
||||||
|
"name": "lo",
|
||||||
|
"address": "ff010000000000000000000000000001",
|
||||||
|
"users": "1",
|
||||||
|
"group": "00000008",
|
||||||
|
"reporters": "0"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"index": "2",
|
||||||
|
"name": "ens33",
|
||||||
|
"address": "ff0200000000000000000001ffa4e315",
|
||||||
|
"users": "1",
|
||||||
|
"group": "00000004",
|
||||||
|
"reporters": "0"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
"""
|
||||||
|
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/igmp6` 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 = {'index', 'users', 'reporters'}
|
||||||
|
|
||||||
|
for item in proc_data:
|
||||||
|
for key, val in item.items():
|
||||||
|
if key in int_list:
|
||||||
|
item[key] = int(val)
|
||||||
|
|
||||||
|
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 = 'index name address users group reporters\n'
|
||||||
|
data = header + data
|
||||||
|
raw_output = simple_table_parse(data.splitlines())
|
||||||
|
|
||||||
|
return raw_output if raw else _process(raw_output)
|
15
man/jc.1
15
man/jc.1
@ -605,6 +605,21 @@ PLIST file parser
|
|||||||
\fB--proc-net-dev-mcast\fP
|
\fB--proc-net-dev-mcast\fP
|
||||||
`/proc/net/dev_mcast` file parser
|
`/proc/net/dev_mcast` file parser
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B
|
||||||
|
\fB--proc-net-if-inet6\fP
|
||||||
|
`/proc/net/if_inet6` file parser
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B
|
||||||
|
\fB--proc-net-igmp\fP
|
||||||
|
`/proc/net/igmp` file parser
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B
|
||||||
|
\fB--proc-net-igmp6\fP
|
||||||
|
`/proc/net/igmp6` file parser
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.B
|
.B
|
||||||
\fB--ps\fP
|
\fB--ps\fP
|
||||||
|
1
tests/fixtures/linux-proc/net_igmp6.json
vendored
Normal file
1
tests/fixtures/linux-proc/net_igmp6.json
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
[{"index":1,"name":"lo","address":"ff020000000000000000000000000001","users":1,"group":"0000000C","reporters":0},{"index":1,"name":"lo","address":"ff010000000000000000000000000001","users":1,"group":"00000008","reporters":0},{"index":2,"name":"ens33","address":"ff0200000000000000000001ffa4e315","users":1,"group":"00000004","reporters":0},{"index":2,"name":"ens33","address":"ff020000000000000000000000000001","users":2,"group":"0000000C","reporters":0},{"index":2,"name":"ens33","address":"ff010000000000000000000000000001","users":1,"group":"00000008","reporters":0}]
|
44
tests/test_proc_net_igmp6.py
Normal file
44
tests/test_proc_net_igmp6.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
import json
|
||||||
|
from typing import Dict
|
||||||
|
import jc.parsers.proc_net_igmp6
|
||||||
|
|
||||||
|
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_igmp6': (
|
||||||
|
'fixtures/linux-proc/net_igmp6',
|
||||||
|
'fixtures/linux-proc/net_igmp6.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_igmp6_nodata(self):
|
||||||
|
"""
|
||||||
|
Test 'proc_net_igmp6' with no data
|
||||||
|
"""
|
||||||
|
self.assertEqual(jc.parsers.proc_net_igmp6.parse('', quiet=True), [])
|
||||||
|
|
||||||
|
def test_proc_net_igmp6(self):
|
||||||
|
"""
|
||||||
|
Test '/proc/net/igmp6'
|
||||||
|
"""
|
||||||
|
self.assertEqual(jc.parsers.proc_net_igmp6.parse(self.f_in['proc_net_igmp6'], quiet=True),
|
||||||
|
self.f_json['proc_net_igmp6'])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
Reference in New Issue
Block a user