Add namespaces to task decorators

オープン
#875 コメント 1 件 リアクション 3 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python
領域
cli

調査の方向性

Start in tasks.py and inspect the @task decorator and invoke.Collection entry points. Compare the proposed namespace forms and default root-collection behavior, then verify that nested namespaces and unqualified tasks behave as described in the examples.

索引モデルが issue の本文から書いたものです。

説明

The current mechanism to create namespaces is a lot of work. I've hacked together an example tasks decorator dynamically creates namespaces as needed.

@task('ns1.ns2.ns3') # or @task(collection='ns1.ns2.ns3') or @task(namespace=...)
def do_something(c):
    pass

I'm also of the opinion that anything marked with @task in tasks.py should be added to the root collection by default.

Here's a working example. I'd be more that happy to contribute this as a feature.

import invoke
from collections import deque
namespace = invoke.Collection()

def resolve_collection(namespace_str):
    """
    Returns an existing subcollection for a path (e.g. 'a.b.c') or creates them
    and returns the "leaf" subcollection.
    """
    collection_names = deque(namespace_str.split('.'))
    curr = namespace
    while True:
        collection_name = collection_names.popleft()
        try:
            subcollection = curr.subcollection_from_path(collection_name)
        except KeyError:
            subcollection = None
        # if theres not a matching subcollection add it
        if not subcollection:
            subcollection = invoke.Collection(collection_name)
            curr.add_collection(subcollection)
        curr = subcollection 

        if not collection_names:
            return subcollection

def task(collection = namespace, **kwargs):
    """invoke.task wrapped with some extra goodies"""
    def wrapper(task_fn):
        if isinstance(collection, invoke.Collection):
            collection.add_task(invoke.task(task_fn, **kwargs))

        if isinstance(collection, str):
            resolve_collection(collection).add_task(invoke.task(task_fn, **kwargs))
    return wrapper

#
# Usage
#

@task()
def this_task_will_be_in_the_default_namespace(c):
    pass

@task(collection='docs')
def this_task_will_be_in_docs(c):
    pass
主要言語
Python
スター
4.8k
フォーク
412
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

pyinvoke/invoke のほかの issue

pyinvoke/invoke の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。