mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-17 01:32:37 +02:00
tweak regex to support arbitrary 'detail' data
This commit is contained in:
@ -1,5 +1,7 @@
|
|||||||
"""jc - JSON CLI output utility `finger` command output parser
|
"""jc - JSON CLI output utility `finger` command output parser
|
||||||
|
|
||||||
|
Supports `-s` output option. Does not support the `-l` detail option.
|
||||||
|
|
||||||
Usage (cli):
|
Usage (cli):
|
||||||
|
|
||||||
$ finger | jc --finger
|
$ finger | jc --finger
|
||||||
@ -15,7 +17,7 @@ Usage (module):
|
|||||||
|
|
||||||
Compatibility:
|
Compatibility:
|
||||||
|
|
||||||
'linux', 'darwin', 'cygwin', 'aix', 'freebsd'
|
'linux', 'darwin', 'cygwin', freebsd'
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
|
|
||||||
@ -51,7 +53,7 @@ class info():
|
|||||||
# details = 'enter any other details here'
|
# details = 'enter any other details here'
|
||||||
|
|
||||||
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
# compatible options: linux, darwin, cygwin, win32, aix, freebsd
|
||||||
compatible = ['linux', 'darwin', 'cygwin', 'aix', 'freebsd']
|
compatible = ['linux', 'darwin', 'cygwin', 'freebsd']
|
||||||
magic_commands = ['finger']
|
magic_commands = ['finger']
|
||||||
|
|
||||||
|
|
||||||
@ -77,12 +79,22 @@ def process(proc_data):
|
|||||||
"tty": string,
|
"tty": string,
|
||||||
"idle": string, # null if empty
|
"idle": string, # null if empty
|
||||||
"login_time": string,
|
"login_time": string,
|
||||||
"details": string
|
"details": string,
|
||||||
|
"tty_writeable": boolean
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
"""
|
"""
|
||||||
|
for entry in proc_data:
|
||||||
|
if 'tty' in entry:
|
||||||
|
entry['tty_writeable'] = True
|
||||||
|
if '*' in entry['tty']:
|
||||||
|
entry['tty'] = entry['tty'].replace('*', '')
|
||||||
|
entry['tty_writeable'] = False
|
||||||
|
|
||||||
|
if 'idle' in entry:
|
||||||
|
if entry['idle'] == '-':
|
||||||
|
entry['idle'] = None
|
||||||
|
|
||||||
# rebuild output for added semantic information
|
|
||||||
return proc_data
|
return proc_data
|
||||||
|
|
||||||
|
|
||||||
@ -127,17 +139,18 @@ def parse(data, raw=False, quiet=False):
|
|||||||
raw_output = jc.parsers.universal.sparse_table_parse(first_half)
|
raw_output = jc.parsers.universal.sparse_table_parse(first_half)
|
||||||
|
|
||||||
# use regex to get login datetime and 'other' data
|
# use regex to get login datetime and 'other' data
|
||||||
pattern = re.compile(r'([A-Z][a-z]{2}\s+\d{1,2}\s+\d\d:\d\d)(\s+\S+)?')
|
pattern = re.compile(r'([A-Z][a-z]{2}\s+\d{1,2}\s+)(\d\d:\d\d|\d{4})(\s?.+)?$')
|
||||||
|
|
||||||
# remove header row from list
|
# remove header row from list
|
||||||
second_half.pop(0)
|
second_half.pop(0)
|
||||||
|
|
||||||
for index, line in enumerate(second_half):
|
for index, line in enumerate(second_half):
|
||||||
dt = re.search(pattern, line)
|
dt = re.search(pattern, line)
|
||||||
if dt.group(1):
|
if dt:
|
||||||
raw_output[index]['login_time'] = dt.group(1).strip()
|
if dt.group(1) and dt.group(2):
|
||||||
if dt.group(2):
|
raw_output[index]['login_time'] = dt.group(1).strip() + ' ' + dt.group(2).strip()
|
||||||
raw_output[index]['details'] = dt.group(2).strip()
|
if dt.group(3):
|
||||||
|
raw_output[index]['details'] = dt.group(3).strip()
|
||||||
|
|
||||||
if raw:
|
if raw:
|
||||||
return raw_output
|
return raw_output
|
||||||
|
Reference in New Issue
Block a user