diff --git a/README.md b/README.md index b2ab7eb3..4c08409c 100755 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ jc PARSER [OPTIONS] - `--lsof` enables the `lsof` parser - `--mount` enables the `mount` parser - `--netstat` enables the `netstat` parser +- `--pip-list` enables the `pip list` parser - `--ps` enables the `ps` parser - `--route` enables the `route` parser - `--ss` enables the `ss` parser @@ -1100,6 +1101,26 @@ $ sudo netstat -apee | jc --netstat -p }, ... ] +``` +### pip list +``` +$ pip list | jc --pip-list -p +[ + { + "package": "ansible", + "version": "2.8.5" + }, + { + "package": "antlr4-python3-runtime", + "version": "4.7.2" + }, + { + "package": "asn1crypto", + "version": "0.24.0" + }, + ... +] + ``` ### ps ``` diff --git a/docgen.sh b/docgen.sh index 258d5ed7..7337bada 100755 --- a/docgen.sh +++ b/docgen.sh @@ -23,6 +23,7 @@ pydocmd simple jc.parsers.lsmod+ > ../docs/parsers/lsmod.md pydocmd simple jc.parsers.lsof+ > ../docs/parsers/lsof.md pydocmd simple jc.parsers.mount+ > ../docs/parsers/mount.md pydocmd simple jc.parsers.netstat+ > ../docs/parsers/netstat.md +pydocmd simple jc.parsers.pip_list+ > ../docs/parsers/pip_list.md pydocmd simple jc.parsers.ps+ > ../docs/parsers/ps.md pydocmd simple jc.parsers.route+ > ../docs/parsers/route.md pydocmd simple jc.parsers.ss+ > ../docs/parsers/ss.md diff --git a/docs/parsers/crontab.md b/docs/parsers/crontab.md index 0c366b0c..3260c8eb 100644 --- a/docs/parsers/crontab.md +++ b/docs/parsers/crontab.md @@ -7,7 +7,7 @@ Usage: Compatibility: - 'linux', 'aix', 'freebsd' + 'linux', 'darwin', 'aix', 'freebsd' Examples: diff --git a/docs/parsers/pip_list.md b/docs/parsers/pip_list.md new file mode 100644 index 00000000..6b2d46d7 --- /dev/null +++ b/docs/parsers/pip_list.md @@ -0,0 +1,75 @@ +# jc.parsers.pip_list +jc - JSON CLI output utility pip-list Parser + +Usage: + + specify --pip-list as the first argument if the piped input is coming from pip list + +Compatibility: + + 'linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd' + +Examples: + + $ pip list | jc --pip-list -p + [ + { + "package": "ansible", + "version": "2.8.5" + }, + { + "package": "antlr4-python3-runtime", + "version": "4.7.2" + }, + { + "package": "asn1crypto", + "version": "0.24.0" + }, + ... + ] + +## info +```python +info(self, /, *args, **kwargs) +``` + +## process +```python +process(proc_data) +``` + +Final processing to conform to the schema. + +Parameters: + + proc_data: (dictionary) raw structured data to process + +Returns: + + dictionary structured data with the following schema: + + [ + { + "package": string, + "version": string, + "location": string + } + ] + +## parse +```python +parse(data, raw=False, quiet=False) +``` + +Main text parsing function + +Parameters: + + data: (string) text data to parse + raw: (boolean) output preprocessed JSON if True + quiet: (boolean) suppress warning messages if True + +Returns: + + dictionary raw or processed structured data + diff --git a/jc/cli.py b/jc/cli.py index ec7b647b..bd491310 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -26,6 +26,7 @@ import jc.parsers.lsmod import jc.parsers.lsof import jc.parsers.mount import jc.parsers.netstat +import jc.parsers.pip_list import jc.parsers.ps import jc.parsers.route import jc.parsers.ss @@ -58,6 +59,7 @@ parser_map = { '--lsof': jc.parsers.lsof, '--mount': jc.parsers.mount, '--netstat': jc.parsers.netstat, + '--pip-list': jc.parsers.pip_list, '--ps': jc.parsers.ps, '--route': jc.parsers.route, '--ss': jc.parsers.ss, diff --git a/jc/parsers/pip_list.py b/jc/parsers/pip_list.py new file mode 100644 index 00000000..8fd92f1d --- /dev/null +++ b/jc/parsers/pip_list.py @@ -0,0 +1,105 @@ +"""jc - JSON CLI output utility pip-list Parser + +Usage: + + specify --pip-list as the first argument if the piped input is coming from pip list + +Compatibility: + + 'linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd' + +Examples: + + $ pip list | jc --pip-list -p + [ + { + "package": "ansible", + "version": "2.8.5" + }, + { + "package": "antlr4-python3-runtime", + "version": "4.7.2" + }, + { + "package": "asn1crypto", + "version": "0.24.0" + }, + ... + ] +""" +import jc.utils +import jc.parsers.universal + + +class info(): + version = '1.0' + description = 'pip-list parser' + author = 'Kelly Brazil' + author_email = 'kellyjonbrazil@gmail.com' + + # compatible options: linux, darwin, cygwin, win32, aix, freebsd + compatible = ['linux', 'darwin', 'cygwin', 'win32', 'aix', 'freebsd'] + + +def process(proc_data): + """ + Final processing to conform to the schema. + + Parameters: + + proc_data: (dictionary) raw structured data to process + + Returns: + + dictionary structured data with the following schema: + + [ + { + "package": string, + "version": string, + "location": string + } + ] + """ + # no further processing + return proc_data + + +def parse(data, raw=False, quiet=False): + """ + Main text parsing function + + Parameters: + + data: (string) text data to parse + raw: (boolean) output preprocessed JSON if True + quiet: (boolean) suppress warning messages if True + + Returns: + + dictionary raw or processed structured data + """ + if not quiet: + jc.utils.compatibility(__name__, info.compatible) + + raw_output = [] + + linedata = data.splitlines() + + # Clear any blank lines + cleandata = list(filter(None, linedata)) + + # clear separator line + for i, line in reversed(list(enumerate(cleandata))): + if line.find('---') != -1: + cleandata.pop(i) + + cleandata[0] = cleandata[0].lower() + + if cleandata: + raw_output = jc.parsers.universal.simple_table_parse(cleandata) + + if raw: + return raw_output + else: + return process(raw_output)