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

Commit

Permalink
Merge branch 'list-command'
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Apr 2, 2019
2 parents 951ffa9 + 45f1c2f commit 47288d2
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 29 deletions.
3 changes: 3 additions & 0 deletions dephell/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .jail_list import JailListCommand
from .jail_remove import JailRemoveCommand
from .package_install import PackageInstallCommand
from .package_list import PackageListCommand
from .package_show import PackageShowCommand
from .venv_create import VenvCreateCommand
from .venv_destroy import VenvDestroyCommand
Expand All @@ -39,6 +40,7 @@
'JailInstallCommand',
'JailListCommand',
'JailRemoveCommand',
'PackageListCommand',
'PackageInstallCommand',
'PackageShowCommand',
'VenvCreateCommand',
Expand Down Expand Up @@ -68,6 +70,7 @@
'jail remove': JailRemoveCommand,
# 'jail update': ...,
'package install': PackageInstallCommand,
'package list': PackageListCommand,
'package show': PackageShowCommand,
'venv create': VenvCreateCommand,
'venv destroy': VenvDestroyCommand,
Expand Down
25 changes: 3 additions & 22 deletions dephell/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# app
from ..config import config
from .helpers import getitem


class BaseCommand:
Expand Down Expand Up @@ -53,30 +54,10 @@ def get_value(cls, data, key, sep: Optional[str] = '-'):
return json.dumps(data[key], indent=2, sort_keys=True)

keys = key.replace('.', sep).split(sep)
value = reduce(cls._getitem, keys, data)
value = reduce(getitem, keys, data)
# print config section
if type(value) is dict:
if isinstance(value, (dict, list)):
return json.dumps(value, indent=2, sort_keys=True)

# print one value
return value

@staticmethod
def _getitem(value, key):
if key in ('reverse()', 'reversed()'):
return value[::-1]
if key == 'first()':
return value[0]
if key == 'last()':
return value[-1]
if key == 'min()':
return min(value)
if key == 'max()':
return max(value)
if key == 'type()':
return type(value).__name__
if key in ('len()', 'length()'):
return len(value)
if key.isdigit():
key = int(key)
return value[key]
2 changes: 1 addition & 1 deletion dephell/commands/deps_licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ def __call__(self):
else:
licenses['Unknown'].add(dep.name)
licenses = {name: sorted(deps) for name, deps in licenses.items()}
print(self.get_value(data=licenses, key=self.config['filter'], sep=None))
print(self.get_value(data=licenses, key=self.config.get('filter'), sep=None))
return True
47 changes: 47 additions & 0 deletions dephell/commands/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections import defaultdict

# external
from dephell_pythons import Python, Pythons

Expand All @@ -6,6 +8,51 @@
from ..config import Config


def _each(value):
if isinstance(value, list):
new_value = defaultdict(list)
for line in value:
for name, field in line.items():
new_value[name].append(field)
return dict(new_value)

new_value = []
for line in zip(*value.values()):
new_value.append(dict(zip(value.keys(), line)))
return new_value


FILTERS = {
'each()': _each,
'first()': lambda v: v[0],
'last()': lambda v: v[-1],
'len()': lambda v: len(v),
'max()': lambda v: max(v),
'min()': lambda v: min(v),
'reverse()': lambda v: v[::-1],
'type()': lambda v: type(v).__name__,
'zip()': lambda v: list(map(list, zip(*v))),

# aliases
'#': _each,
'latest()': lambda v: v[-1],
'length()': lambda v: len(v),
'reversed()': lambda v: v[::-1],
}


def getitem(value, key):
filter = FILTERS.get(key)
if filter is not None:
return filter(value)
if '+' in key:
keys = key.split('+')
return {key: value[key] for key in keys}
if key.isdigit():
key = int(key)
return value[key]


def get_python(config: Config) -> Python:
pythons = Pythons()

Expand Down
2 changes: 1 addition & 1 deletion dephell/commands/inspect_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ def get_parser(cls):
return parser

def __call__(self):
print(self.get_value(data=self.config._data, key=self.config['filter']))
print(self.get_value(data=self.config._data, key=self.config.get('filter')))
2 changes: 1 addition & 1 deletion dephell/commands/inspect_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ def __call__(self):
lib=str(venv.lib_path),
python=str(venv.python_path),
))
print(self.get_value(data=data, key=self.config['filter']))
print(self.get_value(data=data, key=self.config.get('filter')))
6 changes: 4 additions & 2 deletions dephell/commands/jail_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ def __call__(self) -> bool:
return False
paths = list(venv.lib_path.glob('{}*.*-info'.format(name)))
if not paths:
self.logger.critical('cannot locate dist-info for installed package')
return False
paths = list(venv.lib_path.glob('{}*.*-info'.format(name.replace('-', '_'))))
if not paths:
self.logger.critical('cannot locate dist-info for installed package')
return False
path = paths[0] / 'entry_points.txt'
if not path.exists():
self.logger.error('cannot find any entrypoints for package')
Expand Down
2 changes: 1 addition & 1 deletion dephell/commands/jail_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ def __call__(self) -> bool:
if venv_path.match(venvs_path):
entrypoints[venv_path.name].append(entrypoint.name)

print(self.get_value(data=dict(entrypoints), key=self.config['filter'], sep=None))
print(self.get_value(data=dict(entrypoints), key=self.config.get('filter'), sep=None))
return True
54 changes: 54 additions & 0 deletions dephell/commands/package_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# built-in
from argparse import ArgumentParser
from pathlib import Path

# app
from ..config import builders
from ..converters import InstalledConverter
from ..repositories import WareHouseRepo
from ..venvs import VEnvs
from .base import BaseCommand


class PackageListCommand(BaseCommand):
@classmethod
def get_parser(cls):
parser = ArgumentParser(
prog='dephell package list',
description='Show all installed packages',
)
builders.build_config(parser)
builders.build_output(parser)
builders.build_api(parser)
builders.build_other(parser)
return parser

def __call__(self):
venvs = VEnvs(path=self.config['venv'])
venv = venvs.get(Path(self.config['project']), env=self.config.env)
path = None
if venv.exists():
path = venv.lib_path
else:
self.logger.warning('venv not found, package version will be shown for global python lib')

converter = InstalledConverter()
root = converter.load(path)

repo = WareHouseRepo()
data = []
for dep in root.dependencies:
releases = repo.get_releases(dep)
data.append(dict(
name=dep.name,
version=dict(
latest=str(releases[0].version),
installed=str(dep.constraint).lstrip('='),
),
description=dep.description,

license=getattr(dep.license, 'id', dep.license),
links=dep.links,
authors=[str(author) for author in dep.authors],
))
print(self.get_value(data=data, key=self.config.get('filter')))
2 changes: 1 addition & 1 deletion dephell/commands/package_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ def __call__(self):
links=dep.links,
authors=[str(author) for author in dep.authors],
)
print(self.get_value(data=data, key=self.config['filter']))
print(self.get_value(data=data, key=self.config.get('filter')))

0 comments on commit 47288d2

Please sign in to comment.