Skip to content

Commit

Permalink
Merge pull request #13 from raimon49/release-format-json-option
Browse files Browse the repository at this point in the history
Release format json option
  • Loading branch information
raimon49 authored Oct 12, 2018
2 parents 0c8f15b + 98055b9 commit ea24992
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
language: python
python:
- "2.7"
- "3.3"
- "3.4"
- "3.5"
- "3.6"
- "3.7-dev"
install:
- pip install -r dev-requirements.txt
script:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## CHANGELOG

### 1.8.0

* Implement new option `--format-json`
* Dropped support Python 3.3

### 1.7.1

* Fix bug
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Dump the software license list of Python packages installed with pip.
* [Option: format\-rst](#option-format-rst)
* [Option: format\-confluence](#option-format-confluence)
* [Option: format\-html](#option-format-html)
* [Option: format\-json](#option-format-json)
* [More Information](#more-information)
* [License](#license)
* [Dependencies](#dependencies)
Expand Down Expand Up @@ -230,6 +231,32 @@ When executed with the `--format-html` option, you can output list in HTML table
</table>
```

### Option: format-json

When executed with the `--format-json` option, you can output list in JSON format
easily allowing post-processing

```json
[
{
"Author": "Django Software Foundation",
"License": "BSD",
"Name": "Django",
"URL": "https://www.djangoproject.com/",
"Version": "2.0.2"
},
{
"Author": "Stuart Bishop",
"License": "MIT",
"Name": "pytz",
"URL": "http://pythonhosted.org/pytz",
"Version": "2017.3"
}
]

```


### More Information

Other, please make sure to execute the `--help` option.
Expand Down
43 changes: 26 additions & 17 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,46 @@
#
# pip-compile --output-file dev-requirements.txt dev-requirements.in
#
apipkg==1.4 # via execnet
atomicwrites==1.1.5 # via pytest
attrs==18.1.0 # via pytest
autopep8==1.3.5
certifi==2018.4.16 # via requests
apipkg==1.5 # via execnet
atomicwrites==1.2.1 # via pytest
attrs==18.2.0 # via pytest
autopep8==1.4
bleach==3.0.2 # via readme-renderer
certifi==2018.8.24 # via requests
cffi==1.11.5 # via cmarkgfm
chardet==3.0.4 # via requests
click==6.7 # via pip-tools
click==7.0 # via pip-tools
cmarkgfm==0.4.2 # via readme-renderer
codecov==2.0.15
coverage==4.5.1 # via codecov, pytest-cov
docutils==0.14
execnet==1.5.0 # via pytest-cache
first==2.0.1 # via pip-tools
funcsigs==1.0.2 # via pytest
future==0.16.0 # via readme-renderer
idna==2.7 # via requests
more-itertools==4.2.0 # via pytest
pip-tools==2.0.2
more-itertools==4.3.0 # via pytest
pathlib2==2.3.2 # via pytest
pip-tools==3.1.0
pkginfo==1.4.2 # via twine
pluggy==0.6.0 # via pytest
pluggy==0.7.1 # via pytest
ptable==0.9.2
py==1.5.4 # via pytest
py==1.7.0 # via pytest
pycodestyle==2.4.0 # via autopep8, pytest-pycodestyle
pycparser==2.19 # via cffi
pygments==2.2.0 # via readme-renderer
pypandoc==1.4
pytest-cache==1.0 # via pytest-pycodestyle
pytest-cov==2.5.1
pytest-cov==2.6.0
pytest-pycodestyle==1.0.6
pytest-runner==4.2
pytest==3.6.2 # via pytest-cache, pytest-cov, pytest-pycodestyle
pytest==3.8.2 # via pytest-cache, pytest-cov, pytest-pycodestyle
readme-renderer==22.0 # via twine
requests-toolbelt==0.8.0 # via twine
requests==2.19.1 # via codecov, requests-toolbelt, twine
six==1.11.0 # via more-itertools, pip-tools, pytest
tqdm==4.23.4 # via twine
twine==1.11.0
scandir==1.9.0 # via pathlib2
six==1.11.0 # via bleach, more-itertools, pathlib2, pip-tools, pytest, readme-renderer
tqdm==4.26.0 # via twine
twine==1.12.1
urllib3==1.23 # via requests
wheel==0.31.1
webencodings==0.5.1 # via bleach
wheel==0.32.1
40 changes: 38 additions & 2 deletions piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
HEADER as RULE_HEADER, NONE as RULE_NONE)

__pkgname__ = 'pip-licenses'
__version__ = '1.7.1'
__version__ = '1.8.0'
__author__ = 'raimon'
__license__ = 'MIT License'
__summary__ = ('Dump the software license list of '
Expand Down Expand Up @@ -140,12 +140,42 @@ def get_pkg_info(pkg):
return table


class JsonPrettyTable(PrettyTable):
"""PrettyTable-like class exporting to JSON"""

def _format_row(self, row, options):
resrow = {}
for (field, value) in zip(self._field_names, row):
if field not in options["fields"]:
continue

resrow[field] = value

return resrow

def get_string(self, **kwargs):
# import included here in order to limit dependencies
# if not interested in JSON output,
# then the dependency is not required
import json

options = self._get_options(kwargs)
rows = self._get_rows(options)
formatted_rows = self._format_rows(rows, options)

lines = []
for row in formatted_rows:
lines.append(row)

return json.dumps(lines, indent=2, sort_keys=True)


def factory_styled_table_with_args(args):
table = PrettyTable()
table.field_names = FIELD_NAMES
table.align = 'l'
table.border = (args.format_markdown or args.format_rst or
args.format_confluence)
args.format_confluence or args.format_json)
table.header = True

if args.format_markdown:
Expand All @@ -157,6 +187,8 @@ def factory_styled_table_with_args(args):
elif args.format_confluence:
table.junction_char = '|'
table.hrules = RULE_NONE
elif args.format_json:
table = JsonPrettyTable(table.field_names)

return table

Expand Down Expand Up @@ -260,6 +292,10 @@ def create_parser():
action='store_true',
default=False,
help='dump as html style')
parser.add_argument('--format-json',
action='store_true',
default=False,
help='dump as json')

return parser

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,17 @@ def read_file(filename):
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Topic :: System :: Systems Administration',
'Topic :: System :: System Shells',
],
keywords='pip pypi package license check',
py_modules=['piplicenses'],
license=LICENSE,
python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*',
install_requires=['PTable'],
setup_requires=[
'pytest-runner',
Expand Down
8 changes: 8 additions & 0 deletions test_piplicenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,13 @@ def test_format_html(self):

self.assertIn('<table>', output_string)

def test_format_json(self):
format_json_args = ['--format-json', '--with-authors']
args = self.parser.parse_args(format_json_args)
output_string = create_output_string(args)

self.assertIn('"Author":', output_string)
self.assertNotIn('"URL":', output_string)

def tearDown(self):
pass

0 comments on commit ea24992

Please sign in to comment.