跳转到主要内容

轻松处理要执行的命令列表并支持撤销。

项目描述

pycommands

CircleCI Codecov

轻松处理要执行的命令列表并支持撤销。

使用方法

安装

pycommands可在PyPI上找到

pip install pycommands

基本用法

命令定义

from commands.base import BaseCommand
from commands.exceptions import CommandException
from commands.invoker import Invoker


class Command1(BaseCommand):
    def build(self):
        return "touch content.txt"

    def build_undo(self):
        return "rm content.txt"


class Command2(BaseCommand):
    def build(self):
        return "mv content.txt content-replaced.txt"

    def build_undo(self):
        return "mv content-replaced.txt content.txt"


class InvalidCommand(BaseCommand):
    def build(self):
        raise CommandException()

使用成功命令的简单默认执行

from commands.invoker import Invoker


invoker = Invoker()

invoker.execute([
    Command1(),
    Command2(),
], run_undo=False)

# output
running command: touch content.txt
running command: mv content.txt content-replaced.txt


# If a invoker.undo() is called then all commands undo operation will be done in the LIFO order.

invoker.undo()

# output
running undo command: mv content-replaced.txt content.txt
running undo command: rm content.txt


# If a invoker.execute() is called with run_undo as True, then the undo operation will be done always
# that a command raise CommandException

invoker = Invoker()

invoker.execute([
    Command1(),
    Command2(),
    InvalidCommand(),
], run_undo=True)

# output
running command: touch content.txt
running command: mv content.txt content-replaced.txt
running command: touch content.txt
running command: mv content.txt content-replaced.txt
running undo command: mv content-replaced.txt content.txt
running undo command: rm content.txt

如何贡献

我们欢迎各种形式的贡献,例如

  • 代码(通过提交拉取请求)
  • 文档改进
  • 错误报告和功能请求

为本地开发设置

我们使用pipenv来管理依赖项,因此请确保您已安装它。

创建环境

/<project-path>/pipenv install --python=3

激活环境

/<project-path>/pipenv shell

安装pre-commit钩子

pre-commit install

通过调用pytest运行测试

pytest

就是这样!您已经准备好进行开发了

项目详情


下载文件

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

源分发

pycommands-0.1.0.tar.gz (4.0 kB 查看哈希值)

上传时间

构建分发

pycommands-0.1.0-py3-none-any.whl (6.8 kB 查看哈希值)

上传于 Python 3

由以下支持

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