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

add proc_net_arp parser and tests

This commit is contained in:
Kelly Brazil
2022-09-25 12:09:01 -07:00
parent ef8c688fa1
commit d9c7dde174
6 changed files with 248 additions and 0 deletions

View File

@ -0,0 +1,81 @@
[Home](https://kellyjonbrazil.github.io/jc/)
<a id="jc.parsers.proc_net_arp"></a>
# jc.parsers.proc\_net\_arp
jc - JSON Convert `/proc/net/arp` file parser
Usage (cli):
$ cat /proc/net/arp | jc --proc
or
$ jc /proc/net/arp
or
$ cat /proc/net/arp | jc --proc-net-arp
Usage (module):
import jc
result = jc.parse('proc', proc_net_arp_file)
or
import jc
result = jc.parse('proc_net_arp', proc_net_arp_file)
Schema:
[
{
"IP_address": string,
"HW_type": string,
"Flags": string,
"HW_address": string,
"Mask": string,
"Device": string
}
]
Examples:
$ cat /proc/net/arp | jc --proc -p
[
{
"IP_address": "192.168.71.254",
"HW_type": "0x1",
"Flags": "0x2",
"HW_address": "00:50:56:f3:2f:ae",
"Mask": "*",
"Device": "ens33"
},
...
]
<a id="jc.parsers.proc_net_arp.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

@ -121,6 +121,7 @@ parsers = [
'proc-pid-stat', 'proc-pid-stat',
'proc-pid-statm', 'proc-pid-statm',
'proc-pid-status', 'proc-pid-status',
'proc-net-arp',
'ps', 'ps',
'route', 'route',
'rpm-qi', 'rpm-qi',

116
jc/parsers/proc_net_arp.py Normal file
View File

@ -0,0 +1,116 @@
"""jc - JSON Convert `/proc/net/arp` file parser
Usage (cli):
$ cat /proc/net/arp | jc --proc
or
$ jc /proc/net/arp
or
$ cat /proc/net/arp | jc --proc-net-arp
Usage (module):
import jc
result = jc.parse('proc', proc_net_arp_file)
or
import jc
result = jc.parse('proc_net_arp', proc_net_arp_file)
Schema:
[
{
"IP_address": string,
"HW_type": string,
"Flags": string,
"HW_address": string,
"Mask": string,
"Device": string
}
]
Examples:
$ cat /proc/net/arp | jc --proc -p
[
{
"IP_address": "192.168.71.254",
"HW_type": "0x1",
"Flags": "0x2",
"HW_address": "00:50:56:f3:2f:ae",
"Mask": "*",
"Device": "ens33"
},
...
]
"""
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/arp` 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 = 'IP_address HW_type Flags HW_address Mask Device'
data_splitlines = data.splitlines()
data_splitlines[0] = header
raw_output = simple_table_parse(data_splitlines)
return raw_output if raw else _process(raw_output)

View File

@ -590,6 +590,11 @@ PLIST file parser
\fB--proc-pid-status\fP \fB--proc-pid-status\fP
`/proc/<pid>/status` file parser `/proc/<pid>/status` file parser
.TP
.B
\fB--proc-net-arp\fP
`/proc/net/arp` file parser
.TP .TP
.B .B
\fB--ps\fP \fB--ps\fP

View File

@ -0,0 +1 @@
[{"IP_address":"192.168.71.254","HW_type":"0x1","Flags":"0x2","HW_address":"00:50:56:f3:2f:ae","Mask":"*","Device":"ens33"},{"IP_address":"192.168.71.2","HW_type":"0x1","Flags":"0x2","HW_address":"00:50:56:f7:4a:fc","Mask":"*","Device":"ens33"},{"IP_address":"192.168.71.1","HW_type":"0x1","Flags":"0x2","HW_address":"a6:83:e7:d2:a9:65","Mask":"*","Device":"ens33"}]

View File

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