mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-13 01:20:24 +02:00
add proc-pid-statm parser and tests
This commit is contained in:
78
docs/parsers/proc_pid_statm.md
Normal file
78
docs/parsers/proc_pid_statm.md
Normal file
@ -0,0 +1,78 @@
|
||||
[Home](https://kellyjonbrazil.github.io/jc/)
|
||||
<a id="jc.parsers.proc_pid_statm"></a>
|
||||
|
||||
# jc.parsers.proc\_pid\_statm
|
||||
|
||||
jc - JSON Convert `/proc/<pid>/statm` file parser
|
||||
|
||||
Usage (cli):
|
||||
|
||||
$ cat /proc/1/statm | jc --proc
|
||||
|
||||
or
|
||||
|
||||
$ jc /proc/1/statm
|
||||
|
||||
or
|
||||
|
||||
$ cat /proc/1/statm | jc --proc-pid_statm
|
||||
|
||||
Usage (module):
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc', proc_pid_statm_file)
|
||||
|
||||
or
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc_pid_statm', proc_pid_statm_file)
|
||||
|
||||
Schema:
|
||||
|
||||
{
|
||||
"size": integer,
|
||||
"resident": integer,
|
||||
"shared": integer,
|
||||
"text": integer,
|
||||
"lib": integer,
|
||||
"data": integer,
|
||||
"dt": integer
|
||||
}
|
||||
|
||||
Examples:
|
||||
|
||||
$ cat /proc/1/statm | jc --proc -p
|
||||
{
|
||||
"size": 42496,
|
||||
"resident": 3313,
|
||||
"shared": 2169,
|
||||
"text": 202,
|
||||
"lib": 0,
|
||||
"data": 5180,
|
||||
"dt": 0
|
||||
}
|
||||
|
||||
<a id="jc.parsers.proc_pid_statm.parse"></a>
|
||||
|
||||
### parse
|
||||
|
||||
```python
|
||||
def parse(data: str, raw: bool = False, quiet: bool = False) -> 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:
|
||||
|
||||
Dictionary. Raw or processed structured data.
|
||||
|
||||
### Parser Information
|
||||
Compatibility: linux
|
||||
|
||||
Version 1.0 by Kelly Brazil (kellyjonbrazil@gmail.com)
|
@ -119,6 +119,7 @@ parsers = [
|
||||
'proc-pid-numa-maps',
|
||||
'proc-pid-smaps',
|
||||
'proc-pid-stat',
|
||||
'proc-pid-statm',
|
||||
'ps',
|
||||
'route',
|
||||
'rpm-qi',
|
||||
|
119
jc/parsers/proc_pid_statm.py
Normal file
119
jc/parsers/proc_pid_statm.py
Normal file
@ -0,0 +1,119 @@
|
||||
"""jc - JSON Convert `/proc/<pid>/statm` file parser
|
||||
|
||||
Usage (cli):
|
||||
|
||||
$ cat /proc/1/statm | jc --proc
|
||||
|
||||
or
|
||||
|
||||
$ jc /proc/1/statm
|
||||
|
||||
or
|
||||
|
||||
$ cat /proc/1/statm | jc --proc-pid_statm
|
||||
|
||||
Usage (module):
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc', proc_pid_statm_file)
|
||||
|
||||
or
|
||||
|
||||
import jc
|
||||
result = jc.parse('proc_pid_statm', proc_pid_statm_file)
|
||||
|
||||
Schema:
|
||||
|
||||
{
|
||||
"size": integer,
|
||||
"resident": integer,
|
||||
"shared": integer,
|
||||
"text": integer,
|
||||
"lib": integer,
|
||||
"data": integer,
|
||||
"dt": integer
|
||||
}
|
||||
|
||||
Examples:
|
||||
|
||||
$ cat /proc/1/statm | jc --proc -p
|
||||
{
|
||||
"size": 42496,
|
||||
"resident": 3313,
|
||||
"shared": 2169,
|
||||
"text": 202,
|
||||
"lib": 0,
|
||||
"data": 5180,
|
||||
"dt": 0
|
||||
}
|
||||
"""
|
||||
from typing import Dict
|
||||
import jc.utils
|
||||
|
||||
|
||||
class info():
|
||||
"""Provides parser metadata (version, author, etc.)"""
|
||||
version = '1.0'
|
||||
description = '`/proc/<pid>/statm` file parser'
|
||||
author = 'Kelly Brazil'
|
||||
author_email = 'kellyjonbrazil@gmail.com'
|
||||
compatible = ['linux']
|
||||
hidden = True
|
||||
|
||||
|
||||
__version__ = info.version
|
||||
|
||||
|
||||
def _process(proc_data: Dict) -> Dict:
|
||||
"""
|
||||
Final processing to conform to the schema.
|
||||
|
||||
Parameters:
|
||||
|
||||
proc_data: (Dictionary) raw structured data to process
|
||||
|
||||
Returns:
|
||||
|
||||
Dictionary. Structured to conform to the schema.
|
||||
"""
|
||||
return proc_data
|
||||
|
||||
|
||||
def parse(
|
||||
data: str,
|
||||
raw: bool = False,
|
||||
quiet: bool = False
|
||||
) -> 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:
|
||||
|
||||
Dictionary. Raw or processed structured data.
|
||||
"""
|
||||
jc.utils.compatibility(__name__, info.compatible, quiet)
|
||||
jc.utils.input_type_check(data)
|
||||
|
||||
raw_output: Dict = {}
|
||||
|
||||
if jc.utils.has_data(data):
|
||||
|
||||
size, resident, shared, text, lib, data_, dt = data.split()
|
||||
|
||||
raw_output = {
|
||||
'size': int(size),
|
||||
'resident': int(resident),
|
||||
'shared': int(shared),
|
||||
'text': int(text),
|
||||
'lib': int(lib),
|
||||
'data': int(data_),
|
||||
'dt': int(dt)
|
||||
}
|
||||
|
||||
return raw_output if raw else _process(raw_output)
|
7
man/jc.1
7
man/jc.1
@ -1,4 +1,4 @@
|
||||
.TH jc 1 2022-09-24 1.21.2 "JSON Convert"
|
||||
.TH jc 1 2022-09-25 1.21.2 "JSON Convert"
|
||||
.SH NAME
|
||||
\fBjc\fP \- JSON Convert JSONifies the output of many CLI tools, file-types, and strings
|
||||
.SH SYNOPSIS
|
||||
@ -580,6 +580,11 @@ PLIST file parser
|
||||
\fB--proc-pid-stat\fP
|
||||
`/proc/<pid>/stat` file parser
|
||||
|
||||
.TP
|
||||
.B
|
||||
\fB--proc-pid-statm\fP
|
||||
`/proc/<pid>/statm` file parser
|
||||
|
||||
.TP
|
||||
.B
|
||||
\fB--ps\fP
|
||||
|
1
tests/fixtures/linux-proc/pid_statm.json
vendored
Normal file
1
tests/fixtures/linux-proc/pid_statm.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"size":42496,"resident":3313,"shared":2169,"text":202,"lib":0,"data":5180,"dt":0}
|
44
tests/test_proc_pid_statm.py
Normal file
44
tests/test_proc_pid_statm.py
Normal file
@ -0,0 +1,44 @@
|
||||
import os
|
||||
import unittest
|
||||
import json
|
||||
from typing import Dict
|
||||
import jc.parsers.proc_pid_statm
|
||||
|
||||
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_pid_statm': (
|
||||
'fixtures/linux-proc/pid_statm',
|
||||
'fixtures/linux-proc/pid_statm.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_pid_statm_nodata(self):
|
||||
"""
|
||||
Test 'proc_pid_statm' with no data
|
||||
"""
|
||||
self.assertEqual(jc.parsers.proc_pid_statm.parse('', quiet=True), {})
|
||||
|
||||
def test_proc_pid_statm(self):
|
||||
"""
|
||||
Test '/proc/<pid>/statm'
|
||||
"""
|
||||
self.assertEqual(jc.parsers.proc_pid_statm.parse(self.f_in['proc_pid_statm'], quiet=True),
|
||||
self.f_json['proc_pid_statm'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Reference in New Issue
Block a user