Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli commands rework/click #227

Merged
merged 9 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@



# this file will be generated by `setuptools_scm`
/nichtparasoup/VERSION


# Distribution / packaging
/nichtparasoup-*/

Expand Down
3 changes: 0 additions & 3 deletions nichtparasoup/.gitignore

This file was deleted.

5 changes: 0 additions & 5 deletions nichtparasoup/__main__.py

This file was deleted.

16 changes: 0 additions & 16 deletions nichtparasoup/_internals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,5 @@ def _message_exception(exception: BaseException, file: Optional[TextIO] = None)
_message('{}: {}'.format(exception_name, exception), file=file)


def _confirm(prompt: str, default: bool = False) -> Optional[bool]:
return_values = {
'y': True,
'yes': True,
'': default,
'n': False,
'no': False,
}
options = 'Y/n' if default else 'y/N'
try:
value = input('{} [{}]: '.format(prompt, options)).strip().lower()
return return_values[value]
except (KeyboardInterrupt, EOFError, KeyError):
return None


def _type_module_name_str(t: Type[Any]) -> str:
return '{}:{}'.format(t.__module__, t.__name__)
65 changes: 62 additions & 3 deletions nichtparasoup/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,63 @@
"""Subpackage containing all of nichtparasoup's command line interface related code
"""
__all__ = ['main']

# This file intentionally does not import submodules
from os.path import dirname
from sys import version_info

from click import group, version_option

import nichtparasoup
# from nichtparasoup.cli.completion import main as completion # @FIXME removed until fixed -- see module itself
from nichtparasoup.commands.imagecrawler_desc import main as imagecrawler_desc
from nichtparasoup.commands.imagecrawler_list import main as imagecrawler_list
from nichtparasoup.commands.server_config_check import main as server_config_check
from nichtparasoup.commands.server_config_dump_defaults import main as server_config_dump_defaults
from nichtparasoup.commands.server_run import main as server_run

VERSION_STRING = '%(version)s from {location} (python {py_version})'.format(
location=dirname(nichtparasoup.__file__),
py_version='{}.{}'.format(version_info.major, version_info.minor)
)


@group(name='nichtparasoup')
@version_option(version=nichtparasoup.__version__, message=VERSION_STRING)
def main() -> None: # pragma: no cover
"""Nichtparasoup
"""
pass


# @FIXME removed until fixed -- see module itself
# main.add_command(completion, name='completion')


@main.group(name='server')
def server() -> None: # pragma: no cover
"""Manage server.
"""
pass


server.add_command(server_run, name='run')


@server.group(name='config')
def server_config() -> None: # pragma: no cover
"""Manage server configs.
"""
pass


server_config.add_command(server_config_check, name='check')
server_config.add_command(server_config_dump_defaults, name='dump-defaults')


@main.group(name='imagecrawler')
def imagecrawler() -> None: # pragma: no cover
"""Manage imagecrawlers.
"""
pass


imagecrawler.add_command(imagecrawler_list, name='list')
imagecrawler.add_command(imagecrawler_desc, name='desc')
4 changes: 4 additions & 0 deletions nichtparasoup/cli/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

from . import main

main()
37 changes: 0 additions & 37 deletions nichtparasoup/cli/commands/__init__.py

This file was deleted.

23 changes: 0 additions & 23 deletions nichtparasoup/cli/commands/completion.py

This file was deleted.

51 changes: 0 additions & 51 deletions nichtparasoup/cli/commands/config.py

This file was deleted.

65 changes: 0 additions & 65 deletions nichtparasoup/cli/commands/imagecrawler.py

This file was deleted.

42 changes: 0 additions & 42 deletions nichtparasoup/cli/commands/run.py

This file was deleted.

26 changes: 26 additions & 0 deletions nichtparasoup/cli/completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
__all__ = ["main"]

from typing import Optional

from click import Choice, command, echo, option
from click_completion import Shell, get_code, init # type: ignore

# click's builtin completion is insufficient and partially broken.
# completes loaded sub commands as 'main' instead of the correct name
init()
# @FIXME unfortunately there is no proper completion for files
# @FIXME and since there is no support for manual `autocomplete` callbacks, i cannot patch it in ...
# - see https://github.com/click-contrib/click-completion/pull/27

ShellChoice = Choice(Shell.__members__.keys())


@command(name='completion')
@option('--shell', type=ShellChoice, default=None, metavar='SHELL',
help='Override auto-detection. Values: {}.'.format(', '.join(ShellChoice.choices)))
def main(shell: Optional[Shell]) -> None: # pragma: no cover
"""Emit completion code for the shell.

Enables the shell to auto-complete nichtparasoup commands and options.
"""
echo(get_code(shell=shell, prog_name='nichtparasoup'))
21 changes: 0 additions & 21 deletions nichtparasoup/cli/main.py

This file was deleted.

Loading