Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decorator to mark a class method as a task generator #330

Closed
wants to merge 10 commits into from
18 changes: 18 additions & 0 deletions doit/cmd_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,24 @@ def setup(self, opt_values):
)))


class TaskGenerator():
"""parent class for a class to have task generator."""
@staticmethod
def doit_task(func):
"""decorator to mark a class method as a task generator."""
func.__doit__ = None
return func

def doit_tasks(self):
"""Get all the doit tasks as a dict."""
tasks = {}
for member_name, member in inspect.getmembers(
self, predicate=inspect.ismethod):
if hasattr(member, '__doit__'):
tasks['task_' + member_name] = member
return tasks


def get_loader(config, task_loader=None, cmds=None):
"""get task loader and configure it

Expand Down
18 changes: 18 additions & 0 deletions tests/test_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from doit.cmd_base import TaskGenerator


class Runner(TaskGenerator):

@TaskGenerator.doit_task
def work(self):
return {
'actions': None,
}


class TestGenerator(object):
def test_tasks(self):
runner = Runner()
tasks = runner.doit_tasks()
assert 'task_work' in tasks
assert runner.work() == {'actions': None}