跳转到主要内容

通过使用setuptools入口点,轻松使您的应用程序可扩展,您或其他人可以使用。

项目描述

Safdie

通过使用setuptools入口点,轻松使您的应用程序可扩展,您或其他人可以使用。

  • 免费软件:MIT许可

在过去几年中,我为大约十个命令行应用程序编写了大致相同的模块系统,到现在我已经找到了一个非常灵活和有用的模式。在这里,我将它打包到一个模块中,这样我们每次有新项目时都可以避免重新发明它。

安装

pip install safdie

您还可以使用以下方法安装开发版本


pip install https://github.com/coddingtonbear/safdie/archive/master.zip

快速入门

下面的示例不是很实用,但它展示了如何完全使用这个功能。

  1. 将您的命令作为 safdie.BaseCommand 的子类创建,并编写所需的任何命令类
# Module Path: my_app.commands
from safdie import BaseCommand

class MyCommand(BaseCommand):
    def handle(self):
        print("Do whatever you need to do here")
  1. 创建您程序的主要命令行函数
# Module Path: my_app.cli
from safdie import SafdieRunner, BaseCommand

def main():
    # This will look up the command and run it's `handle` function.
    SafdieRunner("myapp.commands").run()
  1. 在setuptools入口点中,声明您的命令行入口点和每个命令的入口点
   setup(
       ...
       entrypoints={
           "console_scripts": [
               "my_command_line_app = my_app.cli:main",
           ],
           "myapp.commands": {
               "somecommand = my_app.commands:MyCommand",
           }
       }
   )
  1. 使用 python setup.py install 安装您的应用程序

现在您可以通过运行 my_command_line_app somecommand 来执行您的函数。

技巧

添加参数

也许你想要为你的应用程序添加一个命令行标志;你可以通过继承 SafdieRunner 并定义一个对 add_arguments 的覆盖来实现,如下所示

from argparse import ArgumentParser
from safdie import SafdieRunner


class MyRunner(SafdieRunner):
    def add_arguments(self, parser: ArgumentParser) -> None:
        parser.add_argument("--something", action="store_true")


def main():
    MyRunner("myapp.commands").run()

自定义你的参数解析器

默认情况下,Safdie 会为你生成一个新的参数解析器,但也许你想要使用 Gooey 或其他 Argparse 兼容的解析器?你可以通过指定 parser_class 命令行参数来提供用于生成参数解析器的类。

from gooey import GooeyParser, Gooey
from safdie import SafdieRunner


@Gooey
def main():
    SafdieRunner("myapp.commands", parser_class=GooeyParser).run()

在执行命令之前做一些事情

也许你想要在解析参数和执行命令之间可选地启动一个调试器?

import argparse
from safdie import SafdieRunner
from typing import Any, Dict, Iterable


class MyRunner(SafdieRunner):
    def add_arguments(self, parser: ArgumentParser) -> None:
        parser.add_argument("--debugger", action="store_true')

    def handle(
        self,
        args: argparse.Namespace,
        init_args: Iterable[Any],
        init_kwargs: Dict[str, Any],
        handle_args: Iterable[Any],
        handle_kwargs: Dict[str, Any],
    ) -> Any:
        if args.debugger:
            import debugpy

            debugpy.listen(("0.0.0.0", 5678))
            debugpy.wait_for_client()

        super().handle(
            args,
            init_args,
            init_kwargs,
            handle_args,
            handle_kwargs
        )


def main():
    MyRunner("myapp.commands").run()

使用你自己的命令子类

在下面的例子中,你有你自己的命令子类,它在初始化时需要额外的参数。虽然下面的例子只使用了额外的参数来传递给 __init__,你也可以将额外的参数传递给 handle。请参阅源代码获取更多详细信息。

# Module Path: my_app.commands
from safdie import BaseCommand


class MyAppCommandBase(BaseCommand):
    def __init__(self, some_additional_init_param, *args, **kwargs):
        # Do something with `some_additional_init_param
        super().__init__(*args, **kwargs)


class MyCommand(MyAppBaseCommand):
    def handle(self):
        print("Do whatever you need to do here")
from typing import Any, Dict, Iterable

from safdie import SafdieRunner

from .commands import MyAppCommandBase


class MyRunner(SafdieRunner):
    def handle(
        self,
        args: argparse.Namespace,
        init_args: Iterable[Any],
        init_kwargs: Dict[str, Any],
        handle_args: Iterable[Any],
        handle_kwargs: Dict[str, Any],
    ) -> Any:
        some_value_i_want_to_pass = "Arbitrary"

        init_kwargs['some_additional_init_param'] = (
            some_value_i_want_to_pass
        )

        super().handle(
            args,
            init_args,
            init_kwargs,
            handle_args,
            handle_kwargs
        )

def main():
    MyRunner("myapp.commands", cmd_class=MyAppCommandBase).run()

为什么这个名字叫 'Safdie'?

你可能至少见过一些名为 Habitat 67 的著名建筑的图片。Moshe Safdie 就是设计它的人。

项目详情


下载文件

下载适合你平台的应用程序文件。如果你不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

safdie-2.0.1.tar.gz (12.0 kB 查看哈希值)

上传时间

构建分布

safdie-2.0.1-py2.py3-none-any.whl (7.5 kB 查看哈希值)

上传时间 Python 2 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面