pyinvoke / invoke

Pythonic task management & command execution.
http://pyinvoke.org
BSD 2-Clause "Simplified" License
4.31k stars 365 forks source link

Easier 'self-importing' / building hybrid namespaces #95

Open bitprophet opened 10 years ago

bitprophet commented 10 years ago

Use case

One's local tasks.py marries some imported task modules (e.g. from Invocations) and some custom local-to-project tasks. It has to be done like this right now:

from invocations import docs, test
from invoke import task, Collection

@task
def mylocaltask():
    pass

ns = Collection(docs, test, mylocaltask)

This isn't awful on the face of it, but it means that one has to remember to edit that ns line every time one changes the contents of the local module. This is extremely error-prone (I say this via experience...) and thus frustrating.

Solutions

Make it easier to add "self" to a Collection so one may state something like:

ns = Collection(docs, test, __self__)

Or more realistically:

# Inspects dict contents for values which are Tasks, similar to Collection.from_module
ns = Collection.from_dict(locals())
ns.add_collection(docs)
ns.add_collection(test)

Alternately/additionally:


Related to #59.

bitprophet commented 8 years ago

Another option besides using locals() + a dict constructor, would be simply reusing the module constructor with sys.modules[__name__]. Probably nice.


Still have to solve the actual API for this for a few cases: