comparison python/ppci/tasks.py @ 334:6f4753202b9a

Added more recipes
author Windel Bouwman
date Thu, 13 Feb 2014 22:02:08 +0100
parents 87feb8a23b4d
children 86b02c98a717
comparison
equal deleted inserted replaced
333:dcae6574c974 334:6f4753202b9a
1
2 import logging
1 3
2 class TaskError(Exception): 4 class TaskError(Exception):
3 pass 5 pass
4 6
5 7
8 def __init__(self, name): 10 def __init__(self, name):
9 self.name = name 11 self.name = name
10 self.subtasks = [] 12 self.subtasks = []
11 self.completed = False 13 self.completed = False
12 self.dependencies = [] 14 self.dependencies = []
15 self.duration = 1
13 16
14 def run(self): 17 def run(self):
15 raise NotImplementedError("Implement this abstract method!") 18 raise NotImplementedError("Implement this abstract method!")
16 19
17 def fire(self): 20 def fire(self):
32 35
33 36
34 class TaskRunner: 37 class TaskRunner:
35 """ Basic task runner that can run some tasks in sequence """ 38 """ Basic task runner that can run some tasks in sequence """
36 def __init__(self): 39 def __init__(self):
40 self.logger = logging.getLogger('taskrunner')
37 self.task_list = [] 41 self.task_list = []
38 42
39 def add_task(self, task): 43 def add_task(self, task):
40 self.task_list.append(task) 44 self.task_list.append(task)
41 45
46 @property
47 def total_duration(self):
48 return sum(t.duration for t in self.task_list)
49
42 def run_tasks(self): 50 def run_tasks(self):
51 passed_time = 0.0
52 total_time = self.total_duration
43 try: 53 try:
44 for t in self.task_list: 54 for t in self.task_list:
45 #print('Running {}'.format(t.name)) 55 self.report_progress(passed_time / total_time, t.name)
46 t.fire() 56 t.fire()
57 passed_time += t.duration
47 except TaskError as e: 58 except TaskError as e:
48 print('Error: {}'.format(e)) 59 print('Error: {}'.format(e))
49 return 1 60 return 1
61 self.report_progress(1, 'OK')
50 return 0 62 return 0
51 63
52 def display(self): 64 def display(self):
53 """ Display task how they would be run """ 65 """ Display task how they would be run """
54 for task in self.task_list: 66 for task in self.task_list:
55 print(task) 67 print(task)
68
69 def report_progress(self, percentage, text):
70 self.logger.info('[{:3.1%}] {}'.format(percentage, text))