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

add jobs parser

This commit is contained in:
Kelly Brazil
2019-10-23 14:05:47 -07:00
parent 802f1510eb
commit 3db758764e
3 changed files with 83 additions and 0 deletions

View File

@ -1,5 +1,11 @@
jc changelog jc changelog
2019xxxx v0.9.0
- No blocking if no piped data
- Better help text
- Add jobs parser
- Clean up iptables parser code
20191022 v0.8.1 20191022 v0.8.1
- Add env parser - Add env parser
- Add df parser - Add df parser

View File

@ -11,6 +11,7 @@ import jc.parsers.env
import jc.parsers.free import jc.parsers.free
import jc.parsers.ifconfig import jc.parsers.ifconfig
import jc.parsers.iptables import jc.parsers.iptables
import jc.parsers.jobs
import jc.parsers.ls import jc.parsers.ls
import jc.parsers.lsblk import jc.parsers.lsblk
import jc.parsers.mount import jc.parsers.mount
@ -28,6 +29,7 @@ def helptext():
print(' --free free parser', file=sys.stderr) print(' --free free parser', file=sys.stderr)
print(' --ifconfig iconfig parser', file=sys.stderr) print(' --ifconfig iconfig parser', file=sys.stderr)
print(' --iptables iptables parser', file=sys.stderr) print(' --iptables iptables parser', file=sys.stderr)
print(' --jobs jobs parser', file=sys.stderr)
print(' --ls ls parser', file=sys.stderr) print(' --ls ls parser', file=sys.stderr)
print(' --lsblk lsblk parser', file=sys.stderr) print(' --lsblk lsblk parser', file=sys.stderr)
print(' --mount mount parser', file=sys.stderr) print(' --mount mount parser', file=sys.stderr)
@ -70,6 +72,9 @@ def main():
elif '--iptables' in sys.argv: elif '--iptables' in sys.argv:
result = jc.parsers.iptables.parse(data) result = jc.parsers.iptables.parse(data)
elif '--jobs' in sys.argv:
result = jc.parsers.jobs.parse(data)
elif '--ls' in sys.argv: elif '--ls' in sys.argv:
result = jc.parsers.ls.parse(data) result = jc.parsers.ls.parse(data)

72
jc/parsers/jobs.py Normal file
View File

@ -0,0 +1,72 @@
"""jc - JSON CLI output utility jobs Parser
Usage:
specify --jobs as the first argument if the piped input is coming from jobs
Examples:
"""
import string
def parse(data):
output = []
linedata = data.splitlines()
# Clear any blank lines
cleandata = list(filter(None, linedata))
if cleandata:
for entry in cleandata:
output_line = {}
remainder = []
job_number = ''
pid = ''
job_history = ''
parsed_line = entry.split(maxsplit=2)
# check if -l was used
if parsed_line[1][0] in string.digits:
pid = parsed_line.pop(1)
remainder = parsed_line.pop(1)
job_number = parsed_line.pop(0)
remainder = remainder.split(maxsplit=1)
# rebuild parsed_line
parsed_line = []
for r in remainder:
parsed_line.append(r)
parsed_line.insert(0, job_number)
# check for + or - in first field
if parsed_line[0].find('+') != -1:
job_history = 'current'
parsed_line[0] = parsed_line[0].rstrip('+')
if parsed_line[0].find('-') != -1:
job_history = 'previous'
parsed_line[0] = parsed_line[0].rstrip('-')
# clean up first field
parsed_line[0] = parsed_line[0].lstrip('[').rstrip(']')
# create list of dictionaries
output_line['job_number'] = int(parsed_line[0])
if pid:
output_line['pid'] = int(pid)
if job_history:
output_line['history'] = job_history
output_line['status'] = parsed_line[1]
output_line['command'] = parsed_line[2]
output.append(output_line)
return output