单击时包含子模块

Include submodules on click

我正在尝试对我的第一个 Click CLI 应用程序进行一种递归调用。 重点是让子命令与第一个相关联,因此,我试图将其全部分开 files/modules 以提高其可维护性。

我现在有 directory:

root
|-commands
|-project
|---__init__
|---command1
|---command2
|-database
|---__init__
|---command1
|---command2

这是我的主文件:

import click
from commands.project import project
from commands.database import database


@click.group(help="Main command")
def main():
    pass


main.add_command(project)
main.add_command(database)

我的项目__init__文件:

from commands.project.command1 import *
from commands.project.command2 import *
import click


@click.group(help="Projects")
def project():
    pass


project.add_command(command1)
project.add_command(command2)

我的 commands.project.command1 文件:

import click


@click.command()
def command1():
    """
    Execute all the steps required to update the project.
    """
    pass

这里的要点是,每次我想添加一个新的子命令,我需要:

  1. 将包含所有代码的 .py 文件添加到命令中,在相应的 subcommand/submodule 文件夹中(很明显!)

  2. __init__ 文件中添加 import 语句

  3. 将此新命令与其父命令相关联(project/database,在本例中)

有什么方法可以执行 circular/dynamic 加载以避免第 2 步和第 3 步?


编辑

在尝试了 Stephen Rauch 方法后,它成功地包含了所有提供的文件,但是 none 的命令只能与 - 一起使用函数名称(例如:update-project -> update_project).

root
|-commands
|-project
|---update
|---install_project
|-database
|---command_one
|---command_two

main.py

# main command ----------------------------------------------------------- ###
@click.group(help="CLI tool!", context_settings=dict(max_content_width=120))
def main():
    pass


# PROJECT command group -------------------------------------------------------- ###
@main.group(cls=group_from_folder("commands/project"),
            short_help="Project installation and upgrade utils.",
            help="Project installation and upgrade.")
def project():
    pass

commands/project/install_project.py

import click    

@click.command(name="install-project",
               help="This options allows you to easily install project",
               short_help="Install a brand new project")
@click.pass_context
def install_project(ctx):

CLI 结果main project --help(注意 install_project 而不是 install-project 子命令)

Usage: main project [OPTIONS] COMMAND [ARGS]...

  Project installation and upgrade.

Options:
  --help  Show this message and exit.

Commands:
  install_project                     Install a brand new project one

修改示例from here,可以去掉第二步和第三步。我建议通过闭包为每个文件夹创建自定义 class。这完全消除了命令文件夹中 __init__.py 的需要。此外,无需导入文件夹(模块)或文件夹中的命令。

自定义群组Class创建者:

import click
import os

def group_from_folder(group_folder_name):

    folder = os.path.join(os.path.dirname(__file__), group_folder_name)

    class FolderCommands(click.MultiCommand):

        def list_commands(self, ctx):
            return sorted(
                f[:-3] for f in os.listdir(folder) if f.endswith('.py'))

        def get_command(self, ctx, name):
            namespace = {}
            command_file = os.path.join(folder, name + '.py')
            with open(command_file) as f:
                code = compile(f.read(), command_file, 'exec')
                eval(code, namespace, namespace)
            return namespace[name.replace('-', '_').lower()]

    return FolderCommands

使用自定义 Class:

要使用自定义 class,首先将命令(按照问题中的结构)放入文件夹中。然后使用 cls 参数修饰组命令,并传递一个自定义的 class,它被初始化指向包含命令的文件夹。

@cli.group(cls=group_from_folder('project'))
def group():
    "command for grouping"

测试代码:

@click.group()
def cli():
    "My awesome script"


@cli.group(cls=group_from_folder('group'))
def group():
    "command for grouping"


if __name__ == "__main__":
    commands = (
        'group command-test',
        'group',
        'group --help',
        '',
    )

    import sys, time

    time.sleep(1)
    print('Click Version: {}'.format(click.__version__))
    print('Python Version: {}'.format(sys.version))
    for cmd in commands:
        try:
            time.sleep(0.1)
            print('-----------')
            print('> ' + cmd)
            time.sleep(0.1)
            cli(cmd.split())

        except BaseException as exc:
            if str(exc) != '0' and \
                    not isinstance(exc, (click.ClickException, SystemExit)):
                raise

文件group/command-test.py

import click


@click.command('command-test')
def command_test():
    """
    Execute all the steps required to update the project.
    """
    click.echo('Command Test')

结果:

Click Version: 6.7
Python Version: 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
-----------
> group command-test
Command Test
-----------
> group
Usage: test.py group [OPTIONS] COMMAND [ARGS]...

  command for grouping

Options:
  --help  Show this message and exit.

Commands:
  command-test  Execute all the steps required to update the...
-----------
> group --help
Usage: test.py group [OPTIONS] COMMAND [ARGS]...

  command for grouping

Options:
  --help  Show this message and exit.

Commands:
  command-test  Execute all the steps required to update the...
-----------
> 
Usage: test.py [OPTIONS] COMMAND [ARGS]...

  My awesome script

Options:
  --help  Show this message and exit.

Commands:
  group  command for grouping

我建议您只阅读特定 Python 包中的命令,然后将其添加到您的条目组中。

假设我们有这样的结构:

|--app
   |--commands
      |--__init__.py
      |--group1
         |--__init__.py
         |--command1.py
      |--group2
         |--__init__.py
         |--command2.py
|--__init__.py
|--cli.py

那么您的命令文件需要包含一个具有指定名称的 click.Command 和一个名称为 'command' 的函数:

import click

@click.command(name="your-first-command")
def command():
    pass

每个组中的初始化文件都需要包含文档字符串,以便为您的 click.Group.

提供正确的 'help' 值

最有趣的是cli.py:

import click
import importlib
import pkgutil
import os.path


def get_commands_from_pkg(pkg) -> dict:
    pkg_obj = importlib.import_module(pkg)

    pkg_path = os.path.dirname(pkg_obj.__file__)

    commands = {}
    for module in pkgutil.iter_modules([pkg_path]):
        module_obj = importlib.import_module(f"{pkg}.{module.name}")
        if not module.ispkg:
            commands[module_obj.command.name] = module_obj.command

        else:
            commands[module.name.replace('_', '-')] = click.Group(
                context_settings={'help_option_names': ['-h', '--help']},
                help=module_obj.__doc__,
                commands=get_commands_from_pkg(f"{pkg}.{module.name}")
            )

    return commands


@click.group(context_settings={'help_option_names': ['-h', '--help']}, help="Your CLI",
             commands=get_commands_from_pkg('app.commands'))
def cli():
    pass

如您所见,我们递归创建点击组并将点击命令添加到特定组。