diff --git a/docs/parsers/proc_pid_statm.md b/docs/parsers/proc_pid_statm.md new file mode 100644 index 00000000..b1254631 --- /dev/null +++ b/docs/parsers/proc_pid_statm.md @@ -0,0 +1,78 @@ +[Home](https://kellyjonbrazil.github.io/jc/) + + +# jc.parsers.proc\_pid\_statm + +jc - JSON Convert `/proc//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 + } + + + +### 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) diff --git a/jc/lib.py b/jc/lib.py index b5ac480b..b08b5649 100644 --- a/jc/lib.py +++ b/jc/lib.py @@ -119,6 +119,7 @@ parsers = [ 'proc-pid-numa-maps', 'proc-pid-smaps', 'proc-pid-stat', + 'proc-pid-statm', 'ps', 'route', 'rpm-qi', diff --git a/jc/parsers/proc_pid_statm.py b/jc/parsers/proc_pid_statm.py new file mode 100644 index 00000000..bf817ef6 --- /dev/null +++ b/jc/parsers/proc_pid_statm.py @@ -0,0 +1,119 @@ +"""jc - JSON Convert `/proc//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//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) diff --git a/man/jc.1 b/man/jc.1 index fdccc75d..e320a915 100644 --- a/man/jc.1 +++ b/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//stat` file parser +.TP +.B +\fB--proc-pid-statm\fP +`/proc//statm` file parser + .TP .B \fB--ps\fP diff --git a/tests/fixtures/linux-proc/pid_statm.json b/tests/fixtures/linux-proc/pid_statm.json new file mode 100644 index 00000000..f19cabe4 --- /dev/null +++ b/tests/fixtures/linux-proc/pid_statm.json @@ -0,0 +1 @@ +{"size":42496,"resident":3313,"shared":2169,"text":202,"lib":0,"data":5180,"dt":0} diff --git a/tests/test_proc_pid_statm.py b/tests/test_proc_pid_statm.py new file mode 100644 index 00000000..4b22219d --- /dev/null +++ b/tests/test_proc_pid_statm.py @@ -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//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()