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

Merge pull request #25 from kellyjonbrazil/dev

Dev v1.7.4
This commit is contained in:
Kelly Brazil
2020-02-19 07:05:29 -08:00
committed by GitHub
26 changed files with 35413 additions and 14 deletions

View File

@ -77,7 +77,7 @@ or
```
COMMAND | jc [OPTIONS] PARSER
```
Alternatively, the "magic" syntax can be used by prepending `jc` to the command to be converted. Options can be passed to `jc` immediately before the command is given.
Alternatively, the "magic" syntax can be used by prepending `jc` to the command to be converted. Options can be passed to `jc` immediately before the command is given. (Note: command aliases are not supported)
```
jc [OPTIONS] COMMAND
```

View File

@ -1,5 +1,8 @@
jc changelog
20200219 v1.7.4
- Updated ls parser to support multiple directories, globbing, and -R (recursive)
20200211 v1.7.3
- Add alternative 'magic' syntax: e.g. `jc ls -al`
- Options can now be condensed (e.g. -prq is equivalant to -p -r -q)

View File

@ -7,7 +7,8 @@ Usage:
ls options supported:
- None
- la
- laR
--time-style=full-iso
- 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.
@ -165,6 +166,7 @@ Returns:
"filename": string,
"flags": string,
"links": integer,
"parent": string,
"owner": string,
"group": string,
"size": integer,

View File

@ -13,7 +13,7 @@ import jc.utils
class info():
version = '1.7.3'
version = '1.7.4'
description = 'jc cli output JSON conversion tool'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'

View File

@ -6,7 +6,8 @@ Usage:
ls options supported:
- None
- la
- laR
--time-style=full-iso
- 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.
@ -144,7 +145,7 @@ import jc.utils
class info():
version = '1.0'
version = '1.1'
description = 'ls command parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
@ -174,6 +175,7 @@ def process(proc_data):
"filename": string,
"flags": string,
"links": integer,
"parent": string,
"owner": string,
"group": string,
"size": integer,
@ -216,22 +218,40 @@ def parse(data, raw=False, quiet=False):
linedata = data.splitlines()
# Delete first line if it starts with 'total'
# Delete first line if it starts with 'total 1234'
if linedata:
if linedata[0].find('total') == 0:
if re.match('^total [0-9]+', linedata[0]):
linedata.pop(0)
# Clear any blank lines
cleandata = list(filter(None, linedata))
parent = ''
next_is_parent = False
if cleandata:
# Look for parent line if glob or -R is used
if not re.match('^[-dclpsbDCMnP?]([-r][-w][-xsS]){2}([-r][-w][-xtT])[+]?', linedata[0]) \
and linedata[0].endswith(':'):
parent = linedata.pop(0)[:-1]
# Pop following total line
linedata.pop(0)
if linedata:
# Check if -l was used to parse extra data
if re.match('^[-dclpsbDCMnP?]([-r][-w][-xsS]){2}([-r][-w][-xtT])[+]?', cleandata[0]):
for entry in cleandata:
if re.match('^[-dclpsbDCMnP?]([-r][-w][-xsS]){2}([-r][-w][-xtT])[+]?', linedata[0]):
for entry in linedata:
output_line = {}
parsed_line = entry.split(maxsplit=8)
if not re.match('^[-dclpsbDCMnP?]([-r][-w][-xsS]){2}([-r][-w][-xtT])[+]?', entry) \
and entry.endswith(':'):
parent = entry[:-1]
continue
if re.match('^total [0-9]+', entry):
continue
if entry == '':
continue
# split filenames and links
filename_field = parsed_line[8].split(' -> ')
@ -241,6 +261,9 @@ def parse(data, raw=False, quiet=False):
if len(filename_field) > 1:
output_line['link_to'] = filename_field[1]
if parent:
output_line['parent'] = parent
output_line['flags'] = parsed_line[0]
output_line['links'] = parsed_line[1]
output_line['owner'] = parsed_line[2]
@ -249,9 +272,23 @@ def parse(data, raw=False, quiet=False):
output_line['date'] = ' '.join(parsed_line[5:8])
raw_output.append(output_line)
else:
for entry in cleandata:
for entry in linedata:
output_line = {}
if entry == '':
next_is_parent = True
continue
if next_is_parent:
parent = entry[:-1]
next_is_parent = False
continue
output_line['filename'] = entry
if parent:
output_line['parent'] = parent
raw_output.append(output_line)
if raw:

View File

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

1
tests/fixtures/centos-7.7/ls-R.json vendored Normal file

File diff suppressed because one or more lines are too long

5089
tests/fixtures/centos-7.7/ls-R.out vendored Normal file

File diff suppressed because it is too large Load Diff

1
tests/fixtures/centos-7.7/ls-alR.json vendored Normal file

File diff suppressed because one or more lines are too long

4997
tests/fixtures/centos-7.7/ls-alR.out vendored Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

1919
tests/fixtures/centos-7.7/ls-glob.out vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -35,6 +35,11 @@ jobs > jobs.out
ls / > ls.out
ls -al / > ls-al.out
ls -alh / > ls-alh.out
ls -R /usr > ls-R.out
ls -alR /usr > ls-alR.out
ls /usr/* > ls-glob.out
lsblk > lsblk.out
lsblk -o +KNAME,FSTYPE,LABEL,UUID,PARTLABEL,PARTUUID,RA,MODEL,SERIAL,STATE,OWNER,GROUP,MODE,ALIGNMENT,MIN-IO,OPT-IO,PHY-SEC,LOG-SEC,ROTA,SCHED,RQ-SIZE,DISC-ALN,DISC-GRAN,DISC-MAX,DISC-ZERO,WSAME,WWN,RAND,PKNAME,HCTL,TRAN,REV,VENDOR > lsblk-allcols.out
lsmod > lsmod.out

1
tests/fixtures/osx-10.14.6/ls-R.json vendored Normal file

File diff suppressed because one or more lines are too long

5018
tests/fixtures/osx-10.14.6/ls-R.out vendored Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

4997
tests/fixtures/osx-10.14.6/ls-alR.out vendored Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

1831
tests/fixtures/osx-10.14.6/ls-glob.out vendored Normal file

File diff suppressed because it is too large Load Diff

1
tests/fixtures/ubuntu-18.04/ls-R.json vendored Normal file

File diff suppressed because one or more lines are too long

5001
tests/fixtures/ubuntu-18.04/ls-R.out vendored Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

5026
tests/fixtures/ubuntu-18.04/ls-alR.out vendored Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

1357
tests/fixtures/ubuntu-18.04/ls-glob.out vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -46,6 +46,33 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-alh.out'), 'r') as f:
self.osx_10_14_6_ls_alh = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-R.out'), 'r') as f:
self.centos_7_7_ls_R = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-R.out'), 'r') as f:
self.ubuntu_18_4_ls_R = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-R.out'), 'r') as f:
self.osx_10_14_6_ls_R = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-alR.out'), 'r') as f:
self.centos_7_7_ls_alR = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-alR.out'), 'r') as f:
self.ubuntu_18_4_ls_alR = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-alR.out'), 'r') as f:
self.osx_10_14_6_ls_alR = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-glob.out'), 'r') as f:
self.centos_7_7_ls_glob = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-glob.out'), 'r') as f:
self.ubuntu_18_4_ls_glob = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-glob.out'), 'r') as f:
self.osx_10_14_6_ls_glob = f.read()
# output
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls.json'), 'r') as f:
self.centos_7_7_ls_json = json.loads(f.read())
@ -83,6 +110,33 @@ class MyTests(unittest.TestCase):
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-alh.json'), 'r') as f:
self.osx_10_14_6_ls_alh_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-R.json'), 'r') as f:
self.centos_7_7_ls_R_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-R.json'), 'r') as f:
self.ubuntu_18_4_ls_R_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-R.json'), 'r') as f:
self.osx_10_14_6_ls_R_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-alR.json'), 'r') as f:
self.centos_7_7_ls_alR_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-alR.json'), 'r') as f:
self.ubuntu_18_4_ls_alR_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-alR.json'), 'r') as f:
self.osx_10_14_6_ls_alR_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/ls-glob.json'), 'r') as f:
self.centos_7_7_ls_glob_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/ls-glob.json'), 'r') as f:
self.ubuntu_18_4_ls_glob_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/ls-glob.json'), 'r') as f:
self.osx_10_14_6_ls_glob_json = json.loads(f.read())
def test_ls_centos_7_7(self):
"""
Test plain 'ls /' on Centos 7.7
@ -155,6 +209,60 @@ class MyTests(unittest.TestCase):
"""
self.assertEqual(jc.parsers.ls.parse(self.osx_10_14_6_ls_alh, quiet=True), self.osx_10_14_6_ls_alh_json)
def test_ls_R_centos_7_7(self):
"""
Test 'ls -R /usr' on Centos 7.7
"""
self.assertEqual(jc.parsers.ls.parse(self.centos_7_7_ls_R, quiet=True), self.centos_7_7_ls_R_json)
def test_ls_R_ubuntu_18_4(self):
"""
Test 'ls -R /usr' on Ubuntu 18.4
"""
self.assertEqual(jc.parsers.ls.parse(self.ubuntu_18_4_ls_R, quiet=True), self.ubuntu_18_4_ls_R_json)
def test_ls_R_osx_10_14_6(self):
"""
Test 'ls -R /usr' on OSX 10.14.6
"""
self.assertEqual(jc.parsers.ls.parse(self.osx_10_14_6_ls_R, quiet=True), self.osx_10_14_6_ls_R_json)
def test_ls_alR_centos_7_7(self):
"""
Test 'ls -alR /usr' on Centos 7.7
"""
self.assertEqual(jc.parsers.ls.parse(self.centos_7_7_ls_alR, quiet=True), self.centos_7_7_ls_alR_json)
def test_ls_alR_ubuntu_18_4(self):
"""
Test 'ls -alR /usr' on Ubuntu 18.4
"""
self.assertEqual(jc.parsers.ls.parse(self.ubuntu_18_4_ls_alR, quiet=True), self.ubuntu_18_4_ls_alR_json)
def test_ls_alR_osx_10_14_6(self):
"""
Test 'ls -alR /usr' on OSX 10.14.6
"""
self.assertEqual(jc.parsers.ls.parse(self.osx_10_14_6_ls_alR, quiet=True), self.osx_10_14_6_ls_alR_json)
def test_ls_glob_centos_7_7(self):
"""
Test 'ls /usr/*' on Centos 7.7
"""
self.assertEqual(jc.parsers.ls.parse(self.centos_7_7_ls_glob, quiet=True), self.centos_7_7_ls_glob_json)
def test_ls_glob_ubuntu_18_4(self):
"""
Test 'ls /usr/*' on Ubuntu 18.4
"""
self.assertEqual(jc.parsers.ls.parse(self.ubuntu_18_4_ls_glob, quiet=True), self.ubuntu_18_4_ls_glob_json)
def test_ls_glob_osx_10_14_6(self):
"""
Test 'ls /usr/*' on OSX 10.14.6
"""
self.assertEqual(jc.parsers.ls.parse(self.osx_10_14_6_ls_glob, quiet=True), self.osx_10_14_6_ls_glob_json)
if __name__ == '__main__':
unittest.main()