轻松处理要执行的命令列表并支持撤销。
项目描述
pycommands
轻松处理要执行的命令列表并支持撤销。
使用方法
安装
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 查看哈希值)
关闭
pycommands-0.1.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 07e6c6e8a02dffb4414470654c5811df1daace75eaf06ff659af9840ff300d61 |
|
MD5 | b40c7dc32dbbfbea47fc88099eeda127 |
|
BLAKE2b-256 | cc80e0552c5cd413fb37f8c1e7ddfd0bbfcef06a89f24b043b0afc95f5401a58 |
关闭
pycommands-0.1.0-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 3822c15a7dea8179e7a273d0d2250f1e64c5b49dd4d8ea6ef4b8a6cc9c553c4c |
|
MD5 | b1cb380e1fc637a9b9b85ba840544cab |
|
BLAKE2b-256 | 945b729ec06f3d8284f4c00852cc250d5a687384a64b7b1fb653f88aba6ac618 |