Python项目的命令行管理器和交互式Shell
项目描述
管理
特性
使用管理,你将为你的Python项目添加一个命令行管理器,并且它还包含一个具有iPython支持的交互式Shell。
你所要做的就是初始化你的项目目录(创建manage.yml文件)
$ pip install manage
$ cd /my_project_root_folder
$ manage init
creating manage.yml....
manage.yml文件描述了manage命令应该如何发现你的app模块和自定义命令,以及它定义了哪些对象应该被加载到shell中
注意: Windows用户可能需要安装适当的PyYAML版本,具体取决于你称之为操作系统的那个东西的版本,可安装的版本在:https://pypi.python.org/pypi/PyYAML 或者考虑使用Linux,并且不用担心这个问题,因为除了游戏、ps和单人游戏之外,所有东西在Linux中都能很好地工作 :)
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
观看演示
更多示例请查看: manage/tree/master/examples/
著名的 naval fate 示例(用于 docopt 和 click)位于: 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 命令,您也可以使用项目中任何地方的 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
您甚至可以为每个模块单独自定义命名空间
如果 namespaced 设置为 true,则所有命令都将被命名空间化,将其设置为 false 以单独定义
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 文件中定义您的 inline 命令
有时您的命令非常简单,您不想(或不能)有一个自定义模块,因此您可以直接在 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语言的管理工具https://github.com/spf13/cobra
历史
0.2 (2021-01-10)
- 提升版本。
0.1.15 (2021-01-10)
- 进行少量修正的Fork。
- 更新至bump2version。
0.1.12 (2016-08-15)
- 由于与MacOS兼容性,readline为可选。
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上发布。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
构建分布
pymanage-0.1.15.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 4b10e131fa6b71c63ac5c9e284f09c4d9eb646beb338ee259a1ec3818fbd275a |
|
MD5 | 9f55a59795619b70e243a3959d316be4 |
|
BLAKE2b-256 | 96d077b2b382c9ead7426ace92c4afe4be35c3904cf7ef31aeb9a5b2c71917b9 |
pymanage-0.1.15-py2.py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0acaed1e58b890cc7cb65846269c80a0a5792c43e7b8e13a7c3ecff09edd4dfb |
|
MD5 | a2c48eed1e9abac71a4e17d75d9f6541 |
|
BLAKE2b-256 | 5adb4527aebbc52cb579d8341646858a371cbd6b54c1f428e2d61e6d6c8621a5 |