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

Commit

Permalink
Merge branch 'show-command'
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Apr 1, 2019
2 parents 863af7e + fd57d71 commit 8a9aa32
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 24 deletions.
9 changes: 6 additions & 3 deletions dephell/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
from .inspect_config import InspectConfigCommand
from .inspect_gadget import InspectGadgetCommand
from .inspect_venv import InspectVenvCommand
from .install import InstallCommand
from .jail_install import JailInstallCommand
from .jail_list import JailListCommand
from .jail_remove import JailRemoveCommand
from .package_install import PackageInstallCommand
from .package_show import PackageShowCommand
from .venv_create import VenvCreateCommand
from .venv_destroy import VenvDestroyCommand
from .venv_run import VenvRunCommand
Expand All @@ -35,10 +36,11 @@
'InspectConfigCommand',
'InspectGadgetCommand',
'InspectVenvCommand',
'InstallCommand',
'JailInstallCommand',
'JailListCommand',
'JailRemoveCommand',
'PackageInstallCommand',
'PackageShowCommand',
'VenvCreateCommand',
'VenvDestroyCommand',
'VenvRunCommand',
Expand All @@ -61,11 +63,12 @@
'inspect config': InspectConfigCommand,
'inspect venv': InspectVenvCommand,
'inspect gadget': InspectGadgetCommand,
'install': InstallCommand,
'jail install': JailInstallCommand,
'jail list': JailListCommand,
'jail remove': JailRemoveCommand,
# 'jail update': ...,
'package install': PackageInstallCommand,
'package show': PackageShowCommand,
'venv create': VenvCreateCommand,
'venv destroy': VenvDestroyCommand,
'venv run': VenvRunCommand,
Expand Down
29 changes: 24 additions & 5 deletions dephell/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os.path
from functools import reduce
from logging import getLogger
from operator import getitem
from typing import Optional

# app
Expand Down Expand Up @@ -44,20 +43,40 @@ def validate(self):
print(self.config.format_errors())
return is_valid

@staticmethod
def get_value(data, key, sep: Optional[str] = '-'):
@classmethod
def get_value(cls, data, key, sep: Optional[str] = '-'):
# print all config
if not key:
return json.dumps(data, indent=2, sort_keys=True)

if sep is None:
return json.dumps(data[key], indent=2, sort_keys=True)

keys = key.split(sep)
value = reduce(getitem, keys, data)
keys = key.replace('.', sep).split(sep)
value = reduce(cls._getitem, keys, data)
# print config section
if type(value) is dict:
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]
3 changes: 1 addition & 2 deletions dephell/commands/deps_licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def get_parser(cls):
builders.build_api(parser)
builders.build_output(parser)
builders.build_other(parser)
parser.add_argument('key', nargs='*')
return parser

def __call__(self):
Expand Down Expand Up @@ -54,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=' '.join(self.args.key), sep=None))
print(self.get_value(data=licenses, key=self.config['filter'], sep=None))
return True
3 changes: 1 addition & 2 deletions dephell/commands/generate_authors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# built-in
import subprocess
from argparse import ArgumentParser, REMAINDER
from argparse import ArgumentParser
from pathlib import Path

# app
Expand All @@ -18,7 +18,6 @@ def get_parser(cls):
builders.build_config(parser)
builders.build_output(parser)
builders.build_other(parser)
parser.add_argument('name', nargs=REMAINDER, help='package to install')
return parser

def __call__(self):
Expand Down
3 changes: 1 addition & 2 deletions dephell/commands/generate_editorconfig.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# built-in
from argparse import ArgumentParser, REMAINDER
from argparse import ArgumentParser
from pathlib import Path

# external
Expand Down Expand Up @@ -48,7 +48,6 @@ def get_parser(cls):
builders.build_config(parser)
builders.build_output(parser)
builders.build_other(parser)
parser.add_argument('name', nargs=REMAINDER, help='package to install')
return parser

def __call__(self):
Expand Down
4 changes: 2 additions & 2 deletions dephell/commands/generate_license.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# built-in
from argparse import ArgumentParser
from argparse import ArgumentParser, REMAINDER
from datetime import datetime
from getpass import getuser
from pathlib import Path
Expand All @@ -22,7 +22,7 @@ def get_parser(cls):
builders.build_config(parser)
builders.build_output(parser)
builders.build_other(parser)
parser.add_argument('name', nargs=1, help='licnse name')
parser.add_argument('name', nargs=REMAINDER, help='licnse name')
return parser

def __call__(self):
Expand Down
3 changes: 1 addition & 2 deletions dephell/commands/inspect_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ def get_parser(cls):
builders.build_venv(parser)
builders.build_output(parser)
builders.build_other(parser)
parser.add_argument('key', nargs='?')
return parser

def __call__(self):
print(self.get_value(data=self.config._data, key=self.args.key))
print(self.get_value(data=self.config._data, key=self.config['filter']))
3 changes: 1 addition & 2 deletions dephell/commands/inspect_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def get_parser(cls):
builders.build_venv(parser)
builders.build_output(parser)
builders.build_other(parser)
parser.add_argument('key', nargs='?')
return parser

def __call__(self):
Expand All @@ -43,4 +42,4 @@ def __call__(self):
lib=str(venv.lib_path),
python=str(venv.python_path),
))
print(self.get_value(data=data, key=self.args.key))
print(self.get_value(data=data, key=self.config['filter']))
3 changes: 1 addition & 2 deletions dephell/commands/jail_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def get_parser(cls):
builders.build_venv(parser)
builders.build_output(parser)
builders.build_other(parser)
parser.add_argument('key', nargs='?')
return parser

def __call__(self) -> bool:
Expand All @@ -33,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.args.key, sep=None))
print(self.get_value(data=dict(entrypoints), key=self.config['filter'], sep=None))
return True
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
from .base import BaseCommand


class InstallCommand(BaseCommand):
class PackageInstallCommand(BaseCommand):
@classmethod
def get_parser(cls):
parser = ArgumentParser(
prog='dephell install',
prog='dephell package install',
description='Download and install package into project environment',
)
builders.build_config(parser)
Expand Down
61 changes: 61 additions & 0 deletions dephell/commands/package_show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# built-in
from argparse import ArgumentParser
from pathlib import Path

# app
from ..controllers import DependencyMaker
from ..config import builders
from ..converters import InstalledConverter
from ..models import RootDependency
from ..repositories import WareHouseRepo
from ..venvs import VEnvs
from .base import BaseCommand


class PackageShowCommand(BaseCommand):
@classmethod
def get_parser(cls):
parser = ArgumentParser(
prog='dephell package show',
description='Show information about package from PyPI.org.',
)
builders.build_config(parser)
builders.build_output(parser)
builders.build_api(parser)
builders.build_other(parser)
parser.add_argument('name', help='package name (and version)')
return parser

def __call__(self):
dep = DependencyMaker.from_requirement(source=RootDependency(), req=self.args.name)[0]
repo = WareHouseRepo()
releases = repo.get_releases(dep)

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)
local_version = None
for subdep in root.dependencies:
if subdep.name == dep.name:
local_version = str(subdep.constraint).lstrip('=')

data = dict(
name=dep.name,
version=dict(
latest=str(releases[0].version),
installed=local_version,
),
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['filter']))
1 change: 1 addition & 0 deletions dephell/config/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def build_output(parser):
output_group.add_argument('--nocolors', action='store_true', help='do not color output.')
output_group.add_argument('--silent', action='store_true', help='suppress any output except errors.')
output_group.add_argument('--traceback', action='store_true', help='show traceback for exceptions.')
output_group.add_argument('--filter', help='filter for JSON output.')


def build_venv(parser):
Expand Down
1 change: 1 addition & 0 deletions dephell/config/scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'format': dict(type='string', required=True, allowed=LOG_FORMATTERS),
'nocolors': dict(type='boolean', required=True),
'traceback': dict(type='boolean', required=True),
'filter': dict(type='string', required=False),

# venv
'venv': dict(type='string', required=True),
Expand Down

0 comments on commit 8a9aa32

Please sign in to comment.