mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
add proc-net-unix parser and tests
This commit is contained in:
100
docs/parsers/proc_net_unix.md
Normal file
100
docs/parsers/proc_net_unix.md
Normal file
@ -0,0 +1,100 @@
|
||||
[Home](https://kellyjonbrazil.github.io/jc/)
|
||||
<a id="jc.parsers.proc_net_unix"></a>
|
||||
|
||||
# jc.parsers.proc\_net\_unix
|
||||
|
||||
jc - JSON Convert `/proc/net/unix` file parser
|
||||
|
||||
Usage (cli):
|
||||
|
||||
$ cat /proc/net/unix | jc --proc
|
||||
|
||||
or
|
||||
|
||||
$ jc /proc/net/unix
|
||||
|
||||
or
|
||||
|
||||
$ cat /proc/net/unix | jc --proc-net-unix
|
||||
|
||||
Usage (module):
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc', proc_net_unix_file)
|
||||
|
||||
or
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc_net_unix', proc_net_unix_file)
|
||||
|
||||
Schema:
|
||||
|
||||
[
|
||||
{
|
||||
"Num": string,
|
||||
"RefCount": string,
|
||||
"Protocol": string,
|
||||
"Flags": string,
|
||||
"Type": string,
|
||||
"St": string,
|
||||
"Inode": integer,
|
||||
"Path": string
|
||||
}
|
||||
]
|
||||
|
||||
Examples:
|
||||
|
||||
$ cat /proc/net/unix | jc --proc -p
|
||||
[
|
||||
{
|
||||
"Num": "ffff9b61ac49c400:",
|
||||
"RefCount": "00000002",
|
||||
"Protocol": "00000000",
|
||||
"Flags": "00010000",
|
||||
"Type": "0001",
|
||||
"St": "01",
|
||||
"Inode": 42776,
|
||||
"Path": "/var/snap/lxd/common/lxd/unix.socket"
|
||||
},
|
||||
...
|
||||
]
|
||||
|
||||
$ cat /proc/net/unix | jc --proc -p -r
|
||||
[
|
||||
{
|
||||
"Num": "ffff9b61ac49c400:",
|
||||
"RefCount": "00000002",
|
||||
"Protocol": "00000000",
|
||||
"Flags": "00010000",
|
||||
"Type": "0001",
|
||||
"St": "01",
|
||||
"Inode": "42776",
|
||||
"Path": "/var/snap/lxd/common/lxd/unix.socket"
|
||||
},
|
||||
...
|
||||
]
|
||||
|
||||
<a id="jc.parsers.proc_net_unix.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)
|
@ -133,6 +133,7 @@ parsers = [
|
||||
'proc-net-packet',
|
||||
'proc-net-protocols',
|
||||
'proc-net-route',
|
||||
'proc-net-unix',
|
||||
'ps',
|
||||
'route',
|
||||
'rpm-qi',
|
||||
|
139
jc/parsers/proc_net_unix.py
Normal file
139
jc/parsers/proc_net_unix.py
Normal file
@ -0,0 +1,139 @@
|
||||
"""jc - JSON Convert `/proc/net/unix` file parser
|
||||
|
||||
Usage (cli):
|
||||
|
||||
$ cat /proc/net/unix | jc --proc
|
||||
|
||||
or
|
||||
|
||||
$ jc /proc/net/unix
|
||||
|
||||
or
|
||||
|
||||
$ cat /proc/net/unix | jc --proc-net-unix
|
||||
|
||||
Usage (module):
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc', proc_net_unix_file)
|
||||
|
||||
or
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc_net_unix', proc_net_unix_file)
|
||||
|
||||
Schema:
|
||||
|
||||
[
|
||||
{
|
||||
"Num": string,
|
||||
"RefCount": string,
|
||||
"Protocol": string,
|
||||
"Flags": string,
|
||||
"Type": string,
|
||||
"St": string,
|
||||
"Inode": integer,
|
||||
"Path": string
|
||||
}
|
||||
]
|
||||
|
||||
Examples:
|
||||
|
||||
$ cat /proc/net/unix | jc --proc -p
|
||||
[
|
||||
{
|
||||
"Num": "ffff9b61ac49c400:",
|
||||
"RefCount": "00000002",
|
||||
"Protocol": "00000000",
|
||||
"Flags": "00010000",
|
||||
"Type": "0001",
|
||||
"St": "01",
|
||||
"Inode": 42776,
|
||||
"Path": "/var/snap/lxd/common/lxd/unix.socket"
|
||||
},
|
||||
...
|
||||
]
|
||||
|
||||
$ cat /proc/net/unix | jc --proc -p -r
|
||||
[
|
||||
{
|
||||
"Num": "ffff9b61ac49c400:",
|
||||
"RefCount": "00000002",
|
||||
"Protocol": "00000000",
|
||||
"Flags": "00010000",
|
||||
"Type": "0001",
|
||||
"St": "01",
|
||||
"Inode": "42776",
|
||||
"Path": "/var/snap/lxd/common/lxd/unix.socket"
|
||||
},
|
||||
...
|
||||
]
|
||||
"""
|
||||
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/unix` 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 = {'Inode'}
|
||||
|
||||
for entry in proc_data:
|
||||
for key, val in entry.items():
|
||||
if key in int_list:
|
||||
entry[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):
|
||||
|
||||
raw_output = simple_table_parse(data.splitlines())
|
||||
|
||||
return raw_output if raw else _process(raw_output)
|
5
man/jc.1
5
man/jc.1
@ -650,6 +650,11 @@ PLIST file parser
|
||||
\fB--proc-net-route\fP
|
||||
`/proc/net/route` file parser
|
||||
|
||||
.TP
|
||||
.B
|
||||
\fB--proc-net-unix\fP
|
||||
`/proc/net/unix` file parser
|
||||
|
||||
.TP
|
||||
.B
|
||||
\fB--ps\fP
|
||||
|
1
tests/fixtures/linux-proc/net_unix.json
vendored
Normal file
1
tests/fixtures/linux-proc/net_unix.json
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -163,6 +163,9 @@ class MyTests(unittest.TestCase):
|
||||
'proc_net_route': (
|
||||
'fixtures/linux-proc/net_route',
|
||||
'fixtures/linux-proc/net_route.json'),
|
||||
'proc_net_unix': (
|
||||
'fixtures/linux-proc/net_unix',
|
||||
'fixtures/linux-proc/net_unix.json'),
|
||||
|
||||
'proc_pid_fdinfo': (
|
||||
'fixtures/linux-proc/pid_fdinfo',
|
||||
|
44
tests/test_proc_net_unix.py
Normal file
44
tests/test_proc_net_unix.py
Normal file
@ -0,0 +1,44 @@
|
||||
import os
|
||||
import unittest
|
||||
import json
|
||||
from typing import Dict
|
||||
import jc.parsers.proc_net_unix
|
||||
|
||||
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_unix': (
|
||||
'fixtures/linux-proc/net_unix',
|
||||
'fixtures/linux-proc/net_unix.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_unix_nodata(self):
|
||||
"""
|
||||
Test 'proc_net_unix' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.proc_net_unix.parse('', quiet=True), [])
|
||||
|
||||
def test_proc_net_unix(self):
|
||||
"""
|
||||
Test '/proc/net/unix'
|
||||
"""
|
||||
self.assertEqual(jc.parsers.proc_net_unix.parse(self.f_in['proc_net_unix'], quiet=True),
|
||||
self.f_json['proc_net_unix'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Reference in New Issue
Block a user