Python项目的命令行管理器 + 交互式Shell
项目描述
免费软件:ISC许可证
特性
使用 管理,您可以为Python项目添加一个 命令行管理器,并且它还包含一个带有iPython支持的交互式Shell。
您只需执行 init 命令初始化项目目录(创建manage.yml文件)
$ pip install manage
$ cd /my_project_root_folder
$ manage init
creating manage.yml....
文件 manage.yml 描述了 manage 命令如何发现您的应用程序模块和自定义命令,并定义了哪些对象应该加载到 shell 中。
Shell
默认情况下,包含命令 manage shell
,它是一个具有一些可配置选项的简单 Python REPL 控制台。
您可以更改横幅消息以显示任何您想要的,例如:“欢迎来到我的 shell!”并且您还可以指定一些要自动导入到 shell 上下文中的对象,以便您进入 shell 时已经可以访问您项目中的常用对象。
此外,您还可以指定要运行的自定义函数或基于字符串的代码块,这对于初始化和配置对象非常有用。
控制台
manage shell
可以通过传递选项启动不同的控制台:
manage shell --ipython
- 这是默认的(如果已安装 ipython)manage shell --ptpython
manage shell --bpython
manage shell --python
- 这是默认的 Python 控制台,包括自动补全支持。(如果没有安装其他选项,则为默认值)
您可以使用 manage 的第一个功能来自定义将自动加载到 shell 中的对象,从而在每次需要通过控制台与您的应用程序进行交互时,无需导入和初始化大量内容。
使用以下方式编辑 manage.yml:
project_name: My Awesome Project
help_text: |
This is the {project_name} interactive shell!
shell:
console: bpython
readline_enabled: false # MacOS has no readline completion support
banner:
enabled: true
message: 'Welcome to {project_name} shell!'
auto_import:
display: true
objects:
my_system.config.settings:
my_system.my_module.MyClass:
my_system.my_module.OtherClass:
as: NiceClass
sys.path:
as: sp
init:
insert:
args:
- 0
- /path/to/be/added/automatically/to/sys/path
init_script: |
from my_system.config import settings
print("Initializing settings...")
settings.configure()
然后上述 manage.yaml 将为您提供一个类似这样的 shell:
$ manage shell
Initializing settings...
Welcome to My Awesome Project shell!
Auto imported: ['sp', 'settings', 'MyClass', 'NiceCLass']
>>> NiceClass. <tab> # autocomplete enabled
观看演示
更多示例请查看
https://github.com/rochacbruno/manage/tree/master/examples/
著名的 naval fate 示例(用于 docopt 和 click)在:
https://github.com/rochacbruno/manage/tree/master/examples/naval/
使用 manage 的项目
Quokka CMS(一个基于 Flask 的 CMS)正在使用 manage
Red Hat Satellite QE 测试框架(robottelo)正在使用 manage
自定义命令
有时您需要在项目中添加自定义命令,例如:添加用户到您系统的命令
$ manage create_user --name=Bruno --passwd=1234 Creating the user...
manage 提供了不同的方式来定义自定义命令,您可以使用项目中模块中定义的 click commands,您也可以在项目的任何位置使用 function_commands,如果确实需要,可以在 manage.yml 文件中定义 inline_commands。
1. 使用自定义 click_commands 模块(单个文件)
假设您在应用程序中有一个命令模块,您在那里编写自定义命令,manage 将加载它
# myproject/commands.py
import click
@click.command()
@click.option('--name')
@click.option('--passwd')
def create_user(name, passwd):
"""Create a new user"""
click.echo('Creating the user...')
mysystem.User.create(name, password)
现在您转到您的 manage.yml 或 .manage.yml 并指定您的自定义命令模块。
click_commands:
- module: commands
现在您运行 manage –help
$ manage --help
...
Commands:
create_user Create a new user
debug Shows the parsed manage file
init Initialize a manage shell in current...
shell Runs a Python shell with context
使用 click_commands 包(多个文件)
通常会有不同的文件来保存您的命令,因此您可能更喜欢拥有一个 commands/ 包和一些 python 模块在其中保存命令。
# myproject/commands/user.py
import click
@click.command()
@click.option('--name')
@click.option('--passwd')
def create_user(name, passwd):
"""Create a new user"""
click.echo('Creating the user...')
mysystem.User.create(name, password)
# myproject/commands/system.py
import click
@click.command()
def clear_cache():
"""Clear the system cache"""
click.echo('The cache will be erased...')
mysystem.cache.clear()
现在您想将所有这些命令添加到您的 manage 中,通过编辑 manage 文件来实现。
click_commands:
- module: commands
现在您运行 manage –help 并您将看到来自这两个模块的命令。
$ manage --help
...
Commands:
create_user Create a new user
clear_cache Clear the system cache
debug Shows the parsed manage file
init Initialize a manage shell in current...
shell Runs a Python shell with context
自定义 click_command 名称
有时命令的名称与函数的名称不同,因此您可以自定义它。
click_commands:
- module: commands.system
config:
clear_cache:
name: reset_cache
help_text: This resets the cache
- module: commands.user
config:
create_user:
name: new_user
help_text: This creates new user
具有不同的命名空间
如果您觉得自定义名称工作量太大,只是试图处理命名冲突,您可以使用命名空间命令。
namespaced: true
click_commands:
- module: commands
现在您运行 manage –help 并您可以看到同一模块中的所有命令都将以 modulename_ 命名空间。
$ manage --help
...
Commands:
user_create_user Create a new user
system_clear_cache Clear the system cache
debug Shows the parsed manage file
init Initialize a manage shell in current...
shell Runs a Python shell with context
您甚至可以分别为每个模块单独自定义命名空间。
click_commands:
- module: commands.system
namespace: sys
- module: commands.user
namespace: user
现在运行 manage –help,您将看到同一模块中的所有命令都将具有命名空间。
$ manage --help
...
Commands:
user_create_user Create a new user
sys_clear_cache Clear the system cache
debug Shows the parsed manage file
init Initialize a manage shell in current...
shell Runs a Python shell with context
2. 在 manage 文件中直接定义您的内联命令
有时您的命令非常简单,您不想(或不能)创建自定义模块,因此您可以直接将所有命令放入 yaml 文件中。
inline_commands:
- name: clear_cache
help_text: Executes inline code to clear the cache
context:
- sys
- pprint
options:
--days:
default: 100
code: |
pprint.pprint({'clean_days': days, 'path': sys.path})
现在运行 manage –help
$ manage --help
...
Commands:
clear_cache Executes inline code to clear the cache
debug Shows the parsed manage file
init Initialize a manage shell in current...
shell Runs a Python shell with context
并且您可以使用以下方式运行:
$ manage clear_cache --days 15
3. 使用通用函数作为命令
如果您已经定义了一些函数(任何可调用的函数)。
# my_system.functions.py
def create_user(name, password):
print("Creating user %s" % name)
function_commands:
- function: my_system.functions.create_user
name: new_user
help_text: Create new user
options:
--name:
required: true
--password:
required: true
现在运行 manage –help
$ manage --help
...
Commands:
new_user Create new user
debug Shows the parsed manage file
init Initialize a manage shell in current...
shell Runs a Python shell with context
$ manage new_user --name=Bruno --password=1234
Creating user Bruno
进一步说明
您可能会问:“这有什么用?”,您不需要获取单独的包并在 yaml 中配置一切,只需使用 iPython 即可。此外,IPython 的配置具有更多选项和功能。
所以我这么说:不错!如果您不喜欢,就不使用它!
致谢
这受到了 Django 的 manage.py 命令 的启发。
这是基于 click。
该软件包使用 Cookiecutter 和 audreyr/cookiecutter-pypackage 项目模板创建的。
类似项目
Cobra 是 Go 语言的 manage,网址为 https://github.com/spf13/cobra。
历史
0.1.15 (2021-02-07)
修复 yaml 消息。
修复 pypi 中的描述。
0.1.12 (2016-08-15)
Readline 是可选的,因为与 MacOS 兼容。
0.1.11 (2016-08-15)
子模块导入
多个 'as' 名称
0.1.10 (2016-07-04)
Bpython 已添加
0.1.9 (2016-07-03)
Bpython 已添加
0.1.8 (2016-07-03)
修复 Python 3 字典问题
0.1.7 (2016-06-26)
修复 py3 中的 exec 错误
0.1.6 (2016-06-26)
修复 exec 错误
0.1.5 (2016-06-25)
添加对命令收集器的支持(请参阅 quokka cms)
0.1.4 (2016-06-22)
修复缺少的依赖项
0.1.3 (2016-06-19)
支持 function_commands
0.1.2 (2016-06-17)
支持 inline_commands
0.1.1 (2016-06-14)
支持自定义和隐藏 manage_file
0.1.0 (2016-06-09)
首次发布在 PyPI 上。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源分发
构建分发
manage-0.1.15.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 4be83a0f17a310f0f22d92114ca680bc668e8b500cd23b1ccf3440684169298c |
|
MD5 | 54aa8630091026b65af8b51ffd58bd7a |
|
BLAKE2b-256 | c52f143352da3516decf82d4e44300981f56dd1370eb0745d6bdc43d31b828ef |