mydoit.py 1.57 KB
Newer Older
Matthieu Boileau's avatar
Matthieu Boileau committed
1
2
3
4
"""Define some Doit customizations"""

from doit.cmd_help import Help
from doit.doit_cmd import DoitMain
Matthieu Boileau's avatar
Matthieu Boileau committed
5
from doit.cmd_base import NamespaceTaskLoader
Matthieu Boileau's avatar
Matthieu Boileau committed
6
7
8
9
10
11
12
13
14
15
import inspect


class MyDoitMain(DoitMain):
    BIN_NAME = globals()['__package__']


class MyHelp(Help):
    """Extend doit Help class to add a get_usage() function"""

Matthieu Boileau's avatar
Matthieu Boileau committed
16
17
    @staticmethod
    def get_usage(cmds):
Matthieu Boileau's avatar
Matthieu Boileau committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
        """return doit "usage" (basic help) instructions"""
        msg = "\n"
        for cmd_name in sorted(cmds.keys()):
            cmd = cmds[cmd_name]
            msg += "  {:16s}      {}\n".format(cmd_name, cmd.doc_purpose)
        msg += "\n"
        for line in [
                "  {}                  show doit's help\n",
                "  {} task             show help on task dictionary fields\n",
                "  {} <command>        show command usage\n",
                "  {} <task-name>      show task usage\n"]:
            msg += line.format("help")
        return msg


class MyDoit(DoitMain):
    """Extend DoitMain class to add a get_help() function"""

    def get_help(self):
        """return help usage as string"""

        # get list of available commands
        sub_cmds = self.get_cmds()
Matthieu Boileau's avatar
Matthieu Boileau committed
41
42
43
44
        doit_help = MyHelp(config=self.config,
                           bin_name='',
                           cmds=sub_cmds)
        return doit_help.get_usage(sub_cmds.to_dict())
Matthieu Boileau's avatar
Matthieu Boileau committed
45
46


Matthieu Boileau's avatar
Matthieu Boileau committed
47
class ClassTaskLoader(NamespaceTaskLoader):
Matthieu Boileau's avatar
Matthieu Boileau committed
48
49
    """Implementation of a loader of tasks from a Class namespace"""

Matthieu Boileau's avatar
Matthieu Boileau committed
50
51
52
    def __init__(self, classname):
        super().__init__()
        self.namespace = dict(inspect.getmembers(classname))