python - 如何重命名命名空间结构命令

标签 python fabric

您如何控制自定义 Fabric 命令的公开方式?

我将我的命令组织在各种包中,例如:

mydomain
    __init__.py
    db.py
        @task
        def create()...

        @task()
        def dump()...

        @task
        def shell()...

当我运行 fab --list 时,我看到 Fabric 公开了以 mydomain 为前缀的命令:

mydomain.db.create
mydomain.db.dump
mydomain.db.shell

如何让 Fabric 删除 mydomain 前缀,同时仍将任务组织在我的自定义命名空间中?我试过在 @task(name=...) 中指定名称,但这对命名空间没有影响。

最佳答案

我有以下结构,它没有以包名称为前缀。

fab --version
Fabric 1.8.0
Paramiko 1.12.0

rootdir/
     fabfile.py
     mydomain/
             __init__.py
             db.py
             other.py

fab文件.py

from fabric.api import task
from mydomain import db, other

@task
def boom():
    print "boom"

数据库.py

from fabric.api import task

@task
def create():
    print "create"

@task()
def dump():
    print "dump"

其他.py

from fabric.api import task

@task
def do_stuff():
    print "doing stuff"

fab --list 输出

fab --list
Available commands:

    boom
    db.create
    db.dump
    other.do_stuff

我注意到如果我导入

import mydomain.db
import mydomain.other

那么输出是:

fab --list
Available commands:

    boom
    mydomain.db.create
    mydomain.db.dump
    mydomain.other.do_stuff

所以看看你是如何导入你的包的,这似乎是导致它的原因

关于python - 如何重命名命名空间结构命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16740893/

相关文章:

python - 如果字段包含特定列中的值,则将字段移动到新列

django - 为什么 Fabric fabfile 看不到 Django 设置?

织物厂 -f : Can't find any collection named 'fabfile'

python - 将字符串列表传递给结构任务

python - fexpect 破坏结构脚本

python - 解决 pygame 三角函数错误

python - 如何使用漂亮的汤在 XHTML 中提取没有样式键的内联 CSS 样式

python - 如果对象存在则从对象获取属性

python - 如何在 matplotlib 条形图中添加颜色?

python - key 错误 : 'PATH' when running docker-compose up from Fabric