1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-13 01:20:24 +02:00

add proc-swaps parser

This commit is contained in:
Kelly Brazil
2022-09-16 16:04:25 -07:00
parent e171861629
commit bc816bb858
4 changed files with 229 additions and 0 deletions

View File

@ -0,0 +1,91 @@
[Home](https://kellyjonbrazil.github.io/jc/)
<a id="jc.parsers.proc_swaps"></a>
# jc.parsers.proc\_swaps
jc - JSON Convert `/proc/swaps` file parser
Usage (cli):
$ cat /proc/swaps | jc --proc
or
$ jc /proc/swaps
or
$ cat /proc/swaps | jc --proc-swaps
Usage (module):
import jc
result = jc.parse('proc', proc_swaps_file)
or
import jc
result = jc.parse('proc_swaps', proc_swaps_file)
Schema:
[
{
"filename": string,
"type": string,
"size": integer,
"used": integer,
"priority": integer
}
]
Examples:
$ cat /proc/swaps | jc --proc -p
[
{
"filename": "/swap.img",
"type": "file",
"size": 3996668,
"used": 0,
"priority": -2
},
...
]
$ cat /proc/swaps | jc --proc_swaps -p -r
[
{
"filename": "/swap.img",
"type": "file",
"size": "3996668",
"used": "0",
"priority": "-2"
},
...
]
<a id="jc.parsers.proc_swaps.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

@ -106,6 +106,7 @@ parsers = [
'proc-slabinfo',
'proc-softirqs',
'proc-stat',
'proc-swaps',
'proc-pid-numa-maps',
'ps',
'route',

132
jc/parsers/proc_swaps.py Normal file
View File

@ -0,0 +1,132 @@
"""jc - JSON Convert `/proc/swaps` file parser
Usage (cli):
$ cat /proc/swaps | jc --proc
or
$ jc /proc/swaps
or
$ cat /proc/swaps | jc --proc-swaps
Usage (module):
import jc
result = jc.parse('proc', proc_swaps_file)
or
import jc
result = jc.parse('proc_swaps', proc_swaps_file)
Schema:
[
{
"filename": string,
"type": string,
"size": integer,
"used": integer,
"priority": integer
}
]
Examples:
$ cat /proc/swaps | jc --proc -p
[
{
"filename": "/swap.img",
"type": "file",
"size": 3996668,
"used": 0,
"priority": -2
},
...
]
$ cat /proc/swaps | jc --proc_swaps -p -r
[
{
"filename": "/swap.img",
"type": "file",
"size": "3996668",
"used": "0",
"priority": "-2"
},
...
]
"""
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/swaps` 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', 'used', 'priority'}
for entry in proc_data:
for key in entry:
if key in int_list:
entry[key] = jc.utils.convert_to_int(entry[key])
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].lower()
raw_output = simple_table_parse(cleandata)
return raw_output if raw else _process(raw_output)

View File

@ -515,6 +515,11 @@ PLIST file parser
\fB--proc-stat\fP
`/proc/stat` file parser
.TP
.B
\fB--proc-swaps\fP
`/proc/swaps` file parser
.TP
.B
\fB--proc-pid-numa-maps\fP