Skip to content
This repository has been archived by the owner on Jan 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #262 from dephell/feature/colored-json-output
Browse files Browse the repository at this point in the history
Colored JSON output
  • Loading branch information
orsinium committed Oct 8, 2019
2 parents 3799996 + 3331132 commit ad4d69c
Show file tree
Hide file tree
Showing 27 changed files with 107 additions and 21 deletions.
18 changes: 14 additions & 4 deletions dephell/actions/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from functools import reduce
from typing import Optional

# external
from pygments import formatters, highlight, lexers


def _each(value):
if isinstance(value, list):
Expand Down Expand Up @@ -82,20 +85,27 @@ def getitem(value, key):
return value[key]


def make_json(data, key: str = None, sep: Optional[str] = '-') -> str:
def _jsonify(data, colors: bool = False) -> str:
json_params = dict(indent=2, sort_keys=True, ensure_ascii=False)
dumped = json.dumps(data, **json_params)
if not colors:
return dumped
return highlight(dumped, lexers.JsonLexer(), formatters.TerminalFormatter())


def make_json(data, key: str = None, sep: Optional[str] = '-', colors: bool = True) -> str:
# print all config
if not key:
return json.dumps(data, **json_params) # type: ignore
return _jsonify(data=data, colors=colors)

if sep is None:
return json.dumps(data[key], **json_params) # type: ignore
return _jsonify(data=data[key], colors=colors)

keys = key.replace('.', sep).split(sep)
value = reduce(getitem, keys, data)
# print config section
if isinstance(value, (dict, list)):
return json.dumps(value, **json_params) # type: ignore
return _jsonify(data=value, colors=colors)

# print one value
return str(value)
6 changes: 5 additions & 1 deletion dephell/commands/deps_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def __call__(self) -> bool:
))

if data:
print(make_json(data=data, key=self.config.get('filter')))
print(make_json(
data=data,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return False

self.logger.info('dependencies has no known vulnerabilities (yet)')
Expand Down
6 changes: 5 additions & 1 deletion dephell/commands/deps_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ def __call__(self) -> bool:
))

if data:
print(make_json(data=data, key=self.config.get('filter')))
print(make_json(
data=data,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return False

self.logger.info('all packages is up-to-date')
Expand Down
7 changes: 6 additions & 1 deletion dephell/commands/deps_licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,10 @@ def __call__(self) -> bool:
else:
licenses['Unknown'].add(dep.name)
licenses = {name: sorted(deps) for name, deps in licenses.items()}
print(make_json(data=licenses, key=self.config.get('filter'), sep=None))
print(make_json(
data=licenses,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
sep=None,
))
return True
6 changes: 5 additions & 1 deletion dephell/commands/deps_outdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ def __call__(self) -> bool:
))

if data:
print(make_json(data=data, key=self.config.get('filter')))
print(make_json(
data=data,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return False

self.logger.info('all dependencies is up-to-date')
Expand Down
6 changes: 5 additions & 1 deletion dephell/commands/deps_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ def __call__(self) -> bool:
latest=str(dep.groups.releases[0].version),
dependencies=[subdep.name for subdep in dep.dependencies],
))
print(make_json(result, key=self.config.get('filter')))
print(make_json(
data=result,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return True

if self.args.type == 'graph':
Expand Down
6 changes: 5 additions & 1 deletion dephell/commands/docker_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ def get_parser(cls) -> ArgumentParser:

def __call__(self) -> bool:
container = get_docker_container(config=self.config)
print(make_json(data=container.tags, key=self.config.get('filter')))
print(make_json(
data=container.tags,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return True
6 changes: 5 additions & 1 deletion dephell/commands/inspect_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ def get_parser(cls) -> ArgumentParser:
return parser

def __call__(self) -> bool:
print(make_json(data=self.config['auth'], key=self.config.get('filter')))
print(make_json(
data=self.config['auth'],
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return True
6 changes: 5 additions & 1 deletion dephell/commands/inspect_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ def get_parser(cls) -> ArgumentParser:
def __call__(self) -> bool:
config = self.config._data.copy()
del config['auth'] # do not show credentials
print(make_json(data=config, key=self.config.get('filter')))
print(make_json(
data=config,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return True
6 changes: 5 additions & 1 deletion dephell/commands/inspect_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@ def __call__(self) -> bool:
versions=versions,
cache=format_size(get_path_size(Path(self.config['cache']['path']))),
)
print(make_json(data=data, key=self.config.get('filter')))
print(make_json(
data=data,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return True
6 changes: 5 additions & 1 deletion dephell/commands/inspect_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@ def __call__(self) -> bool:
lib_size=format_size(get_path_size(venv.lib_path)),
python=str(venv.python_path),
))
print(make_json(data=data, key=self.config.get('filter')))
print(make_json(
data=data,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return True
7 changes: 6 additions & 1 deletion dephell/commands/jail_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ def __call__(self) -> bool:
if venv_path.match(venvs_path):
entrypoints[venv_path.name].append(entrypoint.name)

print(make_json(data=dict(entrypoints), key=self.config.get('filter'), sep=None))
print(make_json(
data=dict(entrypoints),
key=self.config.get('filter'),
colors=not self.config['nocolors'],
sep=None,
))
return True
6 changes: 5 additions & 1 deletion dephell/commands/package_downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ def __call__(self) -> bool:
systems=get_downloads_by_category(category='systems', name=name),
)

print(make_json(data=data, key=self.config.get('filter')))
print(make_json(
data=data,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return True
6 changes: 5 additions & 1 deletion dephell/commands/package_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@ def __call__(self) -> bool:
authors=[str(author) for author in dep.authors],
updated=str(releases[0].time.date()),
))
print(make_json(data=data, key=self.config.get('filter')))
print(make_json(
data=data,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return True
6 changes: 5 additions & 1 deletion dephell/commands/package_releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@ def __call__(self) -> bool:
if not data:
self.logger.error('no releases')
return False
print(make_json(data=data, key=self.config.get('filter')))
print(make_json(
data=data,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return True
6 changes: 5 additions & 1 deletion dephell/commands/package_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ def __call__(self) -> bool:
if not results:
self.logger.error('no results')
return False
print(make_json(data=results, key=self.config.get('filter')))
print(make_json(
data=results,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return True
6 changes: 5 additions & 1 deletion dephell/commands/package_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,9 @@ def __call__(self) -> bool:
size=format_size(sum(get_path_size(place) for place in local_places)),
))

print(make_json(data=data, key=self.config.get('filter')))
print(make_json(
data=data,
key=self.config.get('filter'),
colors=not self.config['nocolors'],
))
return True
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ jinja2 = "*"
m2r = "*"
packaging = "*"
pip = ">=18.0"
pygments = "*"
pyyaml = "*" # TODO: replace by ruamel.yaml when migrate from poetry format to flit
requests = "*"
setuptools = "*"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
'fissix; python_version >= "3.6"',
'fissix-py35; python_version < "3.6"', 'html5lib', 'jinja2', 'm2r',
'packaging', 'pip>=18.0', 'pyyaml', 'requests', 'setuptools', 'tomlkit',
'yaspin'
'yaspin','Pygments'
],
extras_require={
'full': ['aiofiles', 'autopep8', 'colorama', 'graphviz', 'yapf'],
Expand Down
1 change: 1 addition & 0 deletions tests/test_commands/test_deps_licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def test_deps_licenses_command(temp_path: Path, capsys):
'from': dict(format='pip', path=str(reqs_path)),
'level': 'WARNING',
'silent': True,
'nocolors': True,
})

command = DepsLicensesCommand(argv=[], config=config)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_commands/test_deps_outdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_deps_outdated_command_file(temp_path: Path, capsys):
'from': dict(format='piplock', path=str(reqs_path)),
'level': 'WARNING',
'silent': True,
'nocolors': True,
})

command = DepsOutdatedCommand(argv=[], config=config)
Expand Down Expand Up @@ -49,6 +50,7 @@ def test_deps_outdated_command_venv(temp_path: Path, capsys):
'venv': str(venv_path),
'level': 'WARNING',
'silent': True,
'nocolors': True,
})

command = DepsOutdatedCommand(argv=[], config=config)
Expand Down
1 change: 1 addition & 0 deletions tests/test_commands/test_deps_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_deps_tree_command(temp_path: Path, capsys):
config.attach({
'level': 'WARNING',
'silent': True,
'nocolors': True,
})

command = DepsTreeCommand(argv=['--type=json', 'autopep8==1.4.3'], config=config)
Expand Down
1 change: 1 addition & 0 deletions tests/test_commands/test_inspect_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def test_inspect_config_command(temp_path: Path, capsys):
'level': 'WARNING',
'silent': True,
'project': 'nani',
'nocolors': True,
})
command = InspectConfigCommand(argv=[], config=config)
result = command()
Expand Down
1 change: 1 addition & 0 deletions tests/test_commands/test_inspect_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_inspect_venv_command(temp_path: Path, capsys):
config.attach({
'project': str(temp_path),
'venv': str(temp_path),
'nocolors': True,
})

command = InspectVenvCommand(argv=[], config=config)
Expand Down
1 change: 1 addition & 0 deletions tests/test_commands/test_package_downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_package_downloads_command(capsys):
config.attach({
'level': 'WARNING',
'silent': True,
'nocolors': True,
})

command = PackageDownloadsCommand(argv=['DJANGO'], config=config)
Expand Down
1 change: 1 addition & 0 deletions tests/test_commands/test_package_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_package_search_command(capsys):
config.attach({
'level': 'WARNING',
'silent': True,
'nocolors': True,
})

command = PackageSearchCommand(argv=['textdistance'], config=config)
Expand Down
1 change: 1 addition & 0 deletions tests/test_commands/test_package_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_package_show_command(capsys):
config.attach({
'level': 'WARNING',
'silent': True,
'nocolors': True,
})

command = PackageShowCommand(argv=['textdistance'], config=config)
Expand Down

0 comments on commit ad4d69c

Please sign in to comment.