329
|
1
|
|
2 class TaskError(Exception):
|
|
3 pass
|
|
4
|
|
5
|
|
6 class Task:
|
|
7 """ Task that can run, and depend on other tasks """
|
|
8 def __init__(self, name):
|
|
9 self.name = name
|
|
10 self.subtasks = []
|
|
11 self.completed = False
|
|
12 self.dependencies = []
|
|
13
|
|
14 def run(self):
|
|
15 raise NotImplementedError("Implement this abstract method!")
|
|
16
|
|
17 def fire(self):
|
|
18 assert all(t.completed for t in self.dependencies)
|
|
19 self.run()
|
|
20 self.completed = True
|
|
21
|
|
22 def addSubTask(self, tsk):
|
|
23 self.subtasks.append(tsk)
|
|
24 return tsk
|
|
25
|
|
26 def addDependency(self, task):
|
|
27 self.dependencies.append(task)
|
|
28 return task
|
|
29
|
332
|
30 def __repr__(self):
|
|
31 return 'Task "{}"'.format(self.name)
|
|
32
|
329
|
33
|
|
34 class TaskRunner:
|
332
|
35 """ Basic task runner that can run some tasks in sequence """
|
329
|
36 def __init__(self):
|
|
37 self.task_list = []
|
332
|
38
|
329
|
39 def add_task(self, task):
|
|
40 self.task_list.append(task)
|
|
41
|
|
42 def run_tasks(self):
|
|
43 try:
|
|
44 for t in self.task_list:
|
|
45 #print('Running {}'.format(t.name))
|
|
46 t.fire()
|
|
47 except TaskError as e:
|
|
48 print('Error: {}'.format(e))
|
|
49 return 1
|
|
50 return 0
|
332
|
51
|
|
52 def display(self):
|
|
53 """ Display task how they would be run """
|
|
54 for task in self.task_list:
|
|
55 print(task)
|