1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

add proc-net-protocols parser and tests

This commit is contained in:
Kelly Brazil
2022-09-26 08:28:48 -07:00
parent 1e14425555
commit c583598257
6 changed files with 409 additions and 1 deletions

View File

@ -0,0 +1,157 @@
[Home](https://kellyjonbrazil.github.io/jc/)
<a id="jc.parsers.proc_net_protocols"></a>
# jc.parsers.proc\_net\_protocols
jc - JSON Convert `/proc/net/protocols` file parser
Usage (cli):
$ cat /proc/net/protocols | jc --proc
or
$ jc /proc/net/protocols
or
$ cat /proc/net/protocols | jc --proc-net-protocols
Usage (module):
import jc
result = jc.parse('proc', proc_net_protocols_file)
or
import jc
result = jc.parse('proc_net_protocols', proc_net_protocols_file)
Schema:
[
{
"protocol": string,
"size": integer,
"sockets": integer,
"memory": integer,
"press": string,
"maxhdr": integer,
"slab": boolean,
"module": string,
"cl": boolean,
"co": boolean,
"di": boolean,
"ac": boolean,
"io": boolean,
"in": boolean,
"de": boolean,
"sh": boolean,
"ss": boolean,
"gs": boolean,
"se": boolean,
"re": boolean,
"sp": boolean,
"bi": boolean,
"br": boolean,
"ha": boolean,
"uh": boolean,
"gp": boolean,
"em": boolean,
}
]
Examples:
$ cat /proc/net/protocols | jc --proc -p
[
{
"protocol": "AF_VSOCK",
"size": 1216,
"sockets": 0,
"memory": -1,
"press": "NI",
"maxhdr": 0,
"slab": true,
"module": "vsock",
"cl": false,
"co": false,
"di": false,
"ac": false,
"io": false,
"in": false,
"de": false,
"sh": false,
"ss": false,
"gs": false,
"se": false,
"re": false,
"sp": false,
"bi": false,
"br": false,
"ha": false,
"uh": false,
"gp": false,
"em": false
},
...
]
$ cat /proc/net/protocols | jc --proc -p -r
[
{
"protocol": "AF_VSOCK",
"size": "1216",
"sockets": "0",
"memory": "-1",
"press": "NI",
"maxhdr": "0",
"slab": "yes",
"module": "vsock",
"cl": "n",
"co": "n",
"di": "n",
"ac": "n",
"io": "n",
"in": "n",
"de": "n",
"sh": "n",
"ss": "n",
"gs": "n",
"se": "n",
"re": "n",
"sp": "n",
"bi": "n",
"br": "n",
"ha": "n",
"uh": "n",
"gp": "n",
"em": "n"
},
...
]
<a id="jc.parsers.proc_net_protocols.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)

View File

@ -131,6 +131,7 @@ parsers = [
'proc-net-netlink', 'proc-net-netlink',
'proc-net-netstat', 'proc-net-netstat',
'proc-net-packet', 'proc-net-packet',
'proc-net-protocols',
'ps', 'ps',
'route', 'route',
'rpm-qi', 'rpm-qi',

View File

@ -0,0 +1,200 @@
"""jc - JSON Convert `/proc/net/protocols` file parser
Usage (cli):
$ cat /proc/net/protocols | jc --proc
or
$ jc /proc/net/protocols
or
$ cat /proc/net/protocols | jc --proc-net-protocols
Usage (module):
import jc
result = jc.parse('proc', proc_net_protocols_file)
or
import jc
result = jc.parse('proc_net_protocols', proc_net_protocols_file)
Schema:
[
{
"protocol": string,
"size": integer,
"sockets": integer,
"memory": integer,
"press": string,
"maxhdr": integer,
"slab": boolean,
"module": string,
"cl": boolean,
"co": boolean,
"di": boolean,
"ac": boolean,
"io": boolean,
"in": boolean,
"de": boolean,
"sh": boolean,
"ss": boolean,
"gs": boolean,
"se": boolean,
"re": boolean,
"sp": boolean,
"bi": boolean,
"br": boolean,
"ha": boolean,
"uh": boolean,
"gp": boolean,
"em": boolean,
}
]
Examples:
$ cat /proc/net/protocols | jc --proc -p
[
{
"protocol": "AF_VSOCK",
"size": 1216,
"sockets": 0,
"memory": -1,
"press": "NI",
"maxhdr": 0,
"slab": true,
"module": "vsock",
"cl": false,
"co": false,
"di": false,
"ac": false,
"io": false,
"in": false,
"de": false,
"sh": false,
"ss": false,
"gs": false,
"se": false,
"re": false,
"sp": false,
"bi": false,
"br": false,
"ha": false,
"uh": false,
"gp": false,
"em": false
},
...
]
$ cat /proc/net/protocols | jc --proc -p -r
[
{
"protocol": "AF_VSOCK",
"size": "1216",
"sockets": "0",
"memory": "-1",
"press": "NI",
"maxhdr": "0",
"slab": "yes",
"module": "vsock",
"cl": "n",
"co": "n",
"di": "n",
"ac": "n",
"io": "n",
"in": "n",
"de": "n",
"sh": "n",
"ss": "n",
"gs": "n",
"se": "n",
"re": "n",
"sp": "n",
"bi": "n",
"br": "n",
"ha": "n",
"uh": "n",
"gp": "n",
"em": "n"
},
...
]
"""
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/protocols` 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', 'sockets', 'memory', 'maxhdr'}
bool_list = {'slab', 'cl', 'co', 'di', 'ac', 'io', 'in', 'de', 'sh', 'ss',
'gs', 'se', 're', 'sp', 'bi', 'br', 'ha', 'uh', 'gp', 'em'}
for item in proc_data:
for key, val in item.items():
if key in int_list:
item[key] = int(val)
if key in bool_list:
item[key] = jc.utils.convert_to_bool(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):
raw_output = simple_table_parse(data.splitlines())
return raw_output if raw else _process(raw_output)

View File

@ -1,4 +1,4 @@
.TH jc 1 2022-09-25 1.21.2 "JSON Convert" .TH jc 1 2022-09-26 1.21.2 "JSON Convert"
.SH NAME .SH NAME
\fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings \fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings
.SH SYNOPSIS .SH SYNOPSIS
@ -640,6 +640,11 @@ PLIST file parser
\fB--proc-net-packet\fP \fB--proc-net-packet\fP
`/proc/net/packet` file parser `/proc/net/packet` file parser
.TP
.B
\fB--proc-net-protocols\fP
`/proc/net/protocols` file parser
.TP .TP
.B .B
\fB--ps\fP \fB--ps\fP

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,44 @@
import os
import unittest
import json
from typing import Dict
import jc.parsers.proc_net_protocols
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_protocols': (
'fixtures/linux-proc/net_protocols',
'fixtures/linux-proc/net_protocols.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_protocols_nodata(self):
"""
Test 'proc_net_protocols' with no data
"""
self.assertEqual(jc.parsers.proc_net_protocols.parse('', quiet=True), [])
def test_proc_net_protocols(self):
"""
Test '/proc/net/protocols'
"""
self.assertEqual(jc.parsers.proc_net_protocols.parse(self.f_in['proc_net_protocols'], quiet=True),
self.f_json['proc_net_protocols'])
if __name__ == '__main__':
unittest.main()