Add namespaces to task decorators
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
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.
Written by the indexing model from the issue text.
Description
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
- Dominant language
- Python
- Stars
- 4.8k
- Forks
- 412
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pyinvoke/invoke
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100