1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

add w parser

This commit is contained in:
Kelly Brazil
2019-10-24 16:06:55 -07:00
parent fa7466022b
commit f101d881a1
4 changed files with 78 additions and 3 deletions

View File

@ -75,6 +75,7 @@ jc [parser] [options]
- `--ps` enables the `ps` parser - `--ps` enables the `ps` parser
- `--route` enables the `route` parser - `--route` enables the `route` parser
- `--uname` enables the `uname -a` parser - `--uname` enables the `uname -a` parser
- `--w` enables the `w` parser
### Options ### Options
- `-p` specifies whether to pretty format the JSON output - `-p` specifies whether to pretty format the JSON output
@ -1042,6 +1043,32 @@ $ uname -a | jc --uname -p
"kernel_version": "#74-Ubuntu SMP Tue Sep 17 17:06:04 UTC 2019" "kernel_version": "#74-Ubuntu SMP Tue Sep 17 17:06:04 UTC 2019"
} }
``` ```
### w
```
$ w | jc --w -p
[
{
"USER": "root",
"TTY": "ttyS0",
"FROM": "-",
"LOGIN@": "Mon20",
"IDLE": "2:27",
"JCPU": "10.61s",
"PCPU": "10.53s",
"WHAT": "-bash"
},
{
"USER": "root",
"TTY": "pts/0",
"FROM": "192.168.71.1",
"LOGIN@": "22:58",
"IDLE": "2.00s",
"JCPU": "0.04s",
"PCPU": "0.00s",
"WHAT": "w"
}
]
```
## Contributions ## Contributions
Feel free to add/improve code or parsers! Feel free to add/improve code or parsers!

View File

@ -21,10 +21,11 @@ import jc.parsers.netstat
import jc.parsers.ps import jc.parsers.ps
import jc.parsers.route import jc.parsers.route
import jc.parsers.uname import jc.parsers.uname
import jc.parsers.w
def helptext(): def helptext():
print('Usage: jc [parser] [options]\n', file=sys.stderr) print('Usage: jc PARSER [OPTIONS]\n', file=sys.stderr)
print('Parsers:', file=sys.stderr) print('Parsers:', file=sys.stderr)
print(' --df df parser', file=sys.stderr) print(' --df df parser', file=sys.stderr)
print(' --env env parser', file=sys.stderr) print(' --env env parser', file=sys.stderr)
@ -40,7 +41,8 @@ def helptext():
print(' --netstat netstat parser', file=sys.stderr) print(' --netstat netstat parser', file=sys.stderr)
print(' --ps ps parser', file=sys.stderr) print(' --ps ps parser', file=sys.stderr)
print(' --route route parser', file=sys.stderr) print(' --route route parser', file=sys.stderr)
print(' --uname uname parser\n', file=sys.stderr) print(' --uname uname parser', file=sys.stderr)
print(' --w w parser\n', file=sys.stderr)
print('Options:', file=sys.stderr) print('Options:', file=sys.stderr)
print(' -p pretty print output\n', file=sys.stderr) print(' -p pretty print output\n', file=sys.stderr)
print('Example:', file=sys.stderr) print('Example:', file=sys.stderr)
@ -106,6 +108,9 @@ def main():
elif '--uname' in sys.argv: elif '--uname' in sys.argv:
result = jc.parsers.uname.parse(data) result = jc.parsers.uname.parse(data)
elif '--w' in sys.argv:
result = jc.parsers.w.parse(data)
else: else:
print('jc: missing or incorrect arguments\n', file=sys.stderr) print('jc: missing or incorrect arguments\n', file=sys.stderr)
helptext() helptext()

43
jc/parsers/w.py Normal file
View File

@ -0,0 +1,43 @@
"""jc - JSON CLI output utility w Parser
Usage:
specify --w as the first argument if the piped input is coming from w
Example:
$ w | jc --w -p
[
{
"USER": "root",
"TTY": "ttyS0",
"FROM": "-",
"LOGIN@": "Mon20",
"IDLE": "2:27",
"JCPU": "10.61s",
"PCPU": "10.53s",
"WHAT": "-bash"
},
{
"USER": "root",
"TTY": "pts/0",
"FROM": "192.168.71.1",
"LOGIN@": "22:58",
"IDLE": "2.00s",
"JCPU": "0.04s",
"PCPU": "0.00s",
"WHAT": "w"
}
]
"""
def parse(data):
# code adapted from Conor Heine at:
# https://gist.github.com/cahna/43a1a3ff4d075bcd71f9d7120037a501
cleandata = data.splitlines()[1:]
headers = [h for h in ' '.join(cleandata[0].strip().split()).split() if h]
raw_data = map(lambda s: s.strip().split(None, len(headers) - 1), cleandata[1:])
return [dict(zip(headers, r)) for r in raw_data]

View File

@ -5,7 +5,7 @@ with open('README.md', 'r') as f:
setuptools.setup( setuptools.setup(
name='jc', name='jc',
version='0.9.1', version='1.0.1',
author='Kelly Brazil', author='Kelly Brazil',
author_email='kellyjonbrazil@gmail.com', author_email='kellyjonbrazil@gmail.com',
description='This tool serializes the output of popular command line tools to structured JSON output.', description='This tool serializes the output of popular command line tools to structured JSON output.',