mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-13 01:20:24 +02:00
add proc-partitions parser
This commit is contained in:
100
docs/parsers/proc_partitions.md
Normal file
100
docs/parsers/proc_partitions.md
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
[Home](https://kellyjonbrazil.github.io/jc/)
|
||||||
|
<a id="jc.parsers.proc_partitions"></a>
|
||||||
|
|
||||||
|
# jc.parsers.proc\_partitions
|
||||||
|
|
||||||
|
jc - JSON Convert `/proc/partitions` file parser
|
||||||
|
|
||||||
|
Usage (cli):
|
||||||
|
|
||||||
|
$ cat /proc/partitions | jc --proc
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
$ jc /proc/partitions
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
$ cat /proc/partitions | jc --proc-partitions
|
||||||
|
|
||||||
|
Usage (module):
|
||||||
|
|
||||||
|
import jc
|
||||||
|
result = jc.parse('proc', proc_partitions_file)
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
import jc
|
||||||
|
result = jc.parse('proc_partitions', proc_partitions_file)
|
||||||
|
|
||||||
|
Schema:
|
||||||
|
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"major": integer,
|
||||||
|
"minor": integer,
|
||||||
|
"num_blocks": integer,
|
||||||
|
"name": string
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
$ cat /proc/partitions | jc --proc -p
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"major": 7,
|
||||||
|
"minor": 0,
|
||||||
|
"num_blocks": 56896,
|
||||||
|
"name": "loop0"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"major": 7,
|
||||||
|
"minor": 1,
|
||||||
|
"num_blocks": 56868,
|
||||||
|
"name": "loop1"
|
||||||
|
},
|
||||||
|
...
|
||||||
|
]
|
||||||
|
|
||||||
|
$ cat /proc/partitions | jc --proc_partitions -p -r
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"major": "7",
|
||||||
|
"minor": "0",
|
||||||
|
"num_blocks": "56896",
|
||||||
|
"name": "loop0"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"major": "7",
|
||||||
|
"minor": "1",
|
||||||
|
"num_blocks": "56868",
|
||||||
|
"name": "loop1"
|
||||||
|
},
|
||||||
|
...
|
||||||
|
]
|
||||||
|
|
||||||
|
<a id="jc.parsers.proc_partitions.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)
|
@ -102,6 +102,7 @@ parsers = [
|
|||||||
'proc-modules',
|
'proc-modules',
|
||||||
'proc-mtrr',
|
'proc-mtrr',
|
||||||
'proc-pagetypeinfo',
|
'proc-pagetypeinfo',
|
||||||
|
'proc-partitions',
|
||||||
'proc-pid-numa-maps',
|
'proc-pid-numa-maps',
|
||||||
'ps',
|
'ps',
|
||||||
'route',
|
'route',
|
||||||
|
141
jc/parsers/proc_partitions.py
Normal file
141
jc/parsers/proc_partitions.py
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
"""jc - JSON Convert `/proc/partitions` file parser
|
||||||
|
|
||||||
|
Usage (cli):
|
||||||
|
|
||||||
|
$ cat /proc/partitions | jc --proc
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
$ jc /proc/partitions
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
$ cat /proc/partitions | jc --proc-partitions
|
||||||
|
|
||||||
|
Usage (module):
|
||||||
|
|
||||||
|
import jc
|
||||||
|
result = jc.parse('proc', proc_partitions_file)
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
import jc
|
||||||
|
result = jc.parse('proc_partitions', proc_partitions_file)
|
||||||
|
|
||||||
|
Schema:
|
||||||
|
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"major": integer,
|
||||||
|
"minor": integer,
|
||||||
|
"num_blocks": integer,
|
||||||
|
"name": string
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
$ cat /proc/partitions | jc --proc -p
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"major": 7,
|
||||||
|
"minor": 0,
|
||||||
|
"num_blocks": 56896,
|
||||||
|
"name": "loop0"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"major": 7,
|
||||||
|
"minor": 1,
|
||||||
|
"num_blocks": 56868,
|
||||||
|
"name": "loop1"
|
||||||
|
},
|
||||||
|
...
|
||||||
|
]
|
||||||
|
|
||||||
|
$ cat /proc/partitions | jc --proc_partitions -p -r
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"major": "7",
|
||||||
|
"minor": "0",
|
||||||
|
"num_blocks": "56896",
|
||||||
|
"name": "loop0"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"major": "7",
|
||||||
|
"minor": "1",
|
||||||
|
"num_blocks": "56868",
|
||||||
|
"name": "loop1"
|
||||||
|
},
|
||||||
|
...
|
||||||
|
]
|
||||||
|
"""
|
||||||
|
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/partitions` 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.
|
||||||
|
"""
|
||||||
|
for entry in proc_data:
|
||||||
|
for key in entry:
|
||||||
|
try:
|
||||||
|
entry[key] = int(entry[key])
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
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):
|
||||||
|
|
||||||
|
cleandata = list(filter(None, data.splitlines()))
|
||||||
|
cleandata[0] = cleandata[0].replace('#', 'num_')
|
||||||
|
raw_output = simple_table_parse(cleandata)
|
||||||
|
|
||||||
|
return raw_output if raw else _process(raw_output)
|
5
man/jc.1
5
man/jc.1
@ -495,6 +495,11 @@ PLIST file parser
|
|||||||
\fB--proc-pagetypeinfo\fP
|
\fB--proc-pagetypeinfo\fP
|
||||||
`/proc/pagetypeinfo` file parser
|
`/proc/pagetypeinfo` file parser
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.B
|
||||||
|
\fB--proc-partitions\fP
|
||||||
|
`/proc/partitions` file parser
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
.B
|
.B
|
||||||
\fB--proc-pid-numa-maps\fP
|
\fB--proc-pid-numa-maps\fP
|
||||||
|
Reference in New Issue
Block a user