-
Notifications
You must be signed in to change notification settings - Fork 2
/
doit_yaml.py
81 lines (59 loc) · 2.05 KB
/
doit_yaml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""load doit tasks from a YAML file"""
import yaml
from doit.task import dict_to_task, Task
from doit.cmd_base import TaskLoader
from doitpy.pyflakes import Pyflakes
def listify(val):
"""if not a list convert value to a one-element list"""
return val if isinstance(val, list) else [val]
def conv_task(data):
"""convert a yaml-dict to Task"""
if isinstance(data, str):
name = data
actions = [data]
else:
assert isinstance(data, dict)
name = data['name']
actions = listify(data['cmd'])
yield Task(name, actions=actions)
def conv_pyflakes(data):
"""convert a yaml entry into a Pyflakes task"""
if isinstance(data, str):
pattern = '*' in data
module = data
else:
assert isinstance(data, dict)
pattern = False
module = data['file']
flakes = Pyflakes()
list_taskd = flakes.tasks(module) if pattern else [flakes(module)]
for taskd in list_taskd:
taskd['name'] = 'pyflakes:{}'.format(taskd['name'])
yield dict_to_task(taskd)
class YAML_Loader(TaskLoader):
# convert a yaml-dict to Task
CONV = {
'task': conv_task,
'pyflakes': conv_pyflakes,
}
def yaml2dict(self):
"""convert YAML file into dict"""
with open('dodo.yaml') as fp:
return yaml.safe_load(fp)
def entry2tasks(self, entry):
"""convert a dict from YAML into tasks using appropriate converter"""
assert len(entry) == 1
conv_name, val = entry.items()[0]
conv = self.CONV.get(conv_name, None)
assert conv is not None, '{} has no converter'.format(conv_name)
return (conv(item) for item in listify(val))
def load_tasks(self, cmd, opt_values, pos_args):
"""implement TaskLoader API"""
data = self.yaml2dict()
task_list = []
for entry in data:
for task_gen in self.entry2tasks(entry):
for task in task_gen:
task_list.append(task)
config = {'verbosity': 2}
return task_list, config