jpcli 0.1.0__py3-none-any.whl → 0.1.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jpcli/main.py +19 -8
- jpcli/parsers/free_parser.py +22 -0
- {jpcli-0.1.0.dist-info → jpcli-0.1.2.dist-info}/METADATA +4 -4
- {jpcli-0.1.0.dist-info → jpcli-0.1.2.dist-info}/RECORD +8 -7
- {jpcli-0.1.0.dist-info → jpcli-0.1.2.dist-info}/LICENCE +0 -0
- {jpcli-0.1.0.dist-info → jpcli-0.1.2.dist-info}/WHEEL +0 -0
- {jpcli-0.1.0.dist-info → jpcli-0.1.2.dist-info}/entry_points.txt +0 -0
- {jpcli-0.1.0.dist-info → jpcli-0.1.2.dist-info}/top_level.txt +0 -0
jpcli/main.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
|
2
2
|
import argparse
|
3
3
|
import subprocess
|
4
|
-
from .parsers import lsmem_parser, other_command_parser
|
4
|
+
from .parsers import lsmem_parser, free_parser, other_command_parser
|
5
5
|
|
6
6
|
def run_command(command):
|
7
7
|
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
@@ -9,10 +9,10 @@ def run_command(command):
|
|
9
9
|
raise RuntimeError(f"Command '{command}' failed with error: {result.stderr.decode()}")
|
10
10
|
return result.stdout.decode()
|
11
11
|
|
12
|
-
def parse_command_output(command_output, parser_name
|
12
|
+
def parse_command_output(command_output, parser_name):
|
13
13
|
parsers = {
|
14
|
-
'': lsmem_parser.parse,
|
15
|
-
'
|
14
|
+
'lsmem': lsmem_parser.parse,
|
15
|
+
'free': free_parser.parse,
|
16
16
|
# Add other parsers here
|
17
17
|
}
|
18
18
|
if parser_name in parsers:
|
@@ -23,12 +23,23 @@ def parse_command_output(command_output, parser_name=''):
|
|
23
23
|
def main():
|
24
24
|
parser = argparse.ArgumentParser(description='JP - JSON Parser for Linux commands')
|
25
25
|
parser.add_argument('command', type=str, help='The Linux command to run')
|
26
|
-
parser.add_argument('parser_name', type=str, nargs='?', default=
|
26
|
+
parser.add_argument('parser_name', type=str, nargs='?', default=None, help='The name of the parser to use')
|
27
27
|
|
28
28
|
args = parser.parse_args()
|
29
29
|
result = jpcli(args.command, args.parser_name)
|
30
30
|
print(result)
|
31
31
|
|
32
|
-
def jpcli(command, parser_name):
|
33
|
-
|
34
|
-
|
32
|
+
def jpcli(command, parser_name=None):
|
33
|
+
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
34
|
+
if result.returncode != 0:
|
35
|
+
print(f"Error: {result.stderr}", file=sys.stderr)
|
36
|
+
sys.exit(result.returncode)
|
37
|
+
|
38
|
+
command_output = result.stdout
|
39
|
+
if parser_name is None:
|
40
|
+
parser_name = command.split()[0] # Use the first word of the command as the parser name
|
41
|
+
return parse_command_output(command_output, parser_name)
|
42
|
+
|
43
|
+
if __name__ == '__main__':
|
44
|
+
main()
|
45
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import json
|
2
|
+
|
3
|
+
def parse(command_output):
|
4
|
+
lines = command_output.strip().split('\n')
|
5
|
+
headers = lines[0].split()
|
6
|
+
memory_data = []
|
7
|
+
|
8
|
+
for line in lines[1:]:
|
9
|
+
values = line.split()
|
10
|
+
# Ensure that the length of headers and values are the same
|
11
|
+
if len(values) == len(headers):
|
12
|
+
entry = {headers[i]: values[i] for i in range(len(headers))}
|
13
|
+
else:
|
14
|
+
# Handle cases where the line might not match header length
|
15
|
+
entry = {}
|
16
|
+
for i in range(len(values)):
|
17
|
+
key = headers[i] if i < len(headers) else f'unknown_{i}'
|
18
|
+
entry[key] = values[i]
|
19
|
+
memory_data.append(entry)
|
20
|
+
|
21
|
+
return json.dumps(memory_data, indent=2)
|
22
|
+
|
@@ -1,8 +1,8 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: jpcli
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.2
|
4
4
|
Summary: A library to convert Linux command output to JSON
|
5
|
-
Home-page: https://github.com/JaimeAdanCuevas/
|
5
|
+
Home-page: https://github.com/JaimeAdanCuevas/jpcli
|
6
6
|
Author: Jaime Cuevas
|
7
7
|
Author-email: adancuevas@outlook.com
|
8
8
|
License: UNKNOWN
|
@@ -14,9 +14,9 @@ Requires-Python: >=3.6
|
|
14
14
|
Description-Content-Type: text/markdown
|
15
15
|
License-File: LICENCE
|
16
16
|
|
17
|
-
#
|
17
|
+
# jpcli
|
18
18
|
|
19
|
-
|
19
|
+
jpcli is a library that converts the output of various Linux commands to JSON format.
|
20
20
|
|
21
21
|
## Installation
|
22
22
|
|
@@ -1,14 +1,15 @@
|
|
1
1
|
jpcli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
|
-
jpcli/main.py,sha256=
|
2
|
+
jpcli/main.py,sha256=yqnw53CJ6cBFtV6X1ujy3sfKGu9I3Tp9Rrrm1gcOauU,1653
|
3
3
|
jpcli/parsers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
|
+
jpcli/parsers/free_parser.py,sha256=XymIi1eafmIElMMVxS3DmlZ-oC4dcYvKmNWDm5tCEEs,721
|
4
5
|
jpcli/parsers/lsmem_parser.py,sha256=wlOInwE_gkrarxvRp-4_tk1zCbb3RDGHyLME56AR7Go,321
|
5
6
|
jpcli/parsers/other_command_parser.py,sha256=G2so9nFluHiyuyEDJouxuN4FHkNheyIY_laDFXxE6xM,134
|
6
7
|
tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
7
8
|
tests/test_lsmem_parser.py,sha256=maT0mCSFyAHCMdRMl0NEa-FUE_2-MMDoYZS5W67PYZU,791
|
8
9
|
tests/test_other_command_parser.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
9
|
-
jpcli-0.1.
|
10
|
-
jpcli-0.1.
|
11
|
-
jpcli-0.1.
|
12
|
-
jpcli-0.1.
|
13
|
-
jpcli-0.1.
|
14
|
-
jpcli-0.1.
|
10
|
+
jpcli-0.1.2.dist-info/LICENCE,sha256=-unHZSaIlcrVEVD56Ss5dlOvdgK682a2xitz35UlKt4,1069
|
11
|
+
jpcli-0.1.2.dist-info/METADATA,sha256=8R1KypivbdKAqhfw5YPFWwu5jEEMrn0tX3qYwurHUrM,625
|
12
|
+
jpcli-0.1.2.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
|
13
|
+
jpcli-0.1.2.dist-info/entry_points.txt,sha256=6wCYs9scOVGFsZE-b2r2geNzy4uST0GgWyHmveiPSxE,43
|
14
|
+
jpcli-0.1.2.dist-info/top_level.txt,sha256=djdbp8mEzCmAEbrMOVEHNW4LIabBwnpLURp2dX_ZQbA,12
|
15
|
+
jpcli-0.1.2.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|