1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-08-08 22:36:48 +02:00

process ls data

This commit is contained in:
Kelly Brazil
2019-11-05 08:30:32 -08:00
parent 8873b1bc69
commit dfe0f6e99b

View File

@ -5,7 +5,10 @@ Usage:
ls options supported: ls options supported:
- None - None
- lah - la
- h file sizes will be available in text form with -r but larger file sizes
with human readable suffixes will be converted to Null in default view
since the parser attempts to convert this field to an integer.
Examples: Examples:
@ -27,6 +30,39 @@ $ ls /usr/bin | jc --ls -p
] ]
$ ls -l /usr/bin | jc --ls -p $ ls -l /usr/bin | jc --ls -p
[
{
"filename": "apropos",
"link_to": "whatis",
"flags": "lrwxrwxrwx.",
"links": 1,
"owner": "root",
"group": "root",
"size": 6,
"date": "Aug 15 10:53"
},
{
"filename": "ar",
"flags": "-rwxr-xr-x.",
"links": 1,
"owner": "root",
"group": "root",
"size": 62744,
"date": "Aug 8 16:14"
},
{
"filename": "arch",
"flags": "-rwxr-xr-x.",
"links": 1,
"owner": "root",
"group": "root",
"size": 33080,
"date": "Aug 19 23:25"
},
...
]
$ ls -l /usr/bin | jc --ls -p -r
[ [
{ {
"filename": "apropos", "filename": "apropos",
@ -87,22 +123,49 @@ $ ls -l /usr/bin | jc --ls -p
... ...
] ]
$ ls -l /usr/bin | jc --ls | jq '.[] | select(.size|tonumber > 50000000)' $ ls -l /usr/bin | jc --ls | jq '.[] | select(.size > 50000000)'
{ {
"filename": "emacs", "filename": "emacs",
"flags": "-r-xr-xr-x", "flags": "-r-xr-xr-x",
"links": 1, "links": 1,
"owner": "root", "owner": "root",
"group": "wheel", "group": "wheel",
"size": "117164432", "size": 117164432,
"date": "May 3 22:26" "date": "May 3 2019"
} }
""" """
import re import re
def parse(data): def process(proc_data):
output = [] '''schema:
[
{
"filename": string,
"flags": string,
"links": integer,
"owner": string,
"group": string,
"size": integer,
"date": string
}
]
'''
for entry in proc_data:
int_list = ['links', 'size']
for key in int_list:
if key in entry:
try:
key_int = int(entry[key])
entry[key] = key_int
except (ValueError, TypeError):
entry[key] = None
return proc_data
def parse(data, raw=False):
raw_output = []
linedata = data.splitlines() linedata = data.splitlines()
@ -137,11 +200,14 @@ def parse(data):
output_line['group'] = parsed_line[3] output_line['group'] = parsed_line[3]
output_line['size'] = parsed_line[4] output_line['size'] = parsed_line[4]
output_line['date'] = ' '.join(parsed_line[5:8]) output_line['date'] = ' '.join(parsed_line[5:8])
output.append(output_line) raw_output.append(output_line)
else: else:
for entry in cleandata: for entry in cleandata:
output_line = {} output_line = {}
output_line['filename'] = entry output_line['filename'] = entry
output.append(output_line) raw_output.append(output_line)
return output if raw:
return raw_output
else:
return process(raw_output)