-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtask.py
118 lines (82 loc) · 3.04 KB
/
task.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""This module implements the task"""
from collections.abc import Mapping
from abc import ABCMeta, abstractmethod
import flatdict
import itertools
from collections import Iterable
import flatdict
class Job:
def __init__(self):
self.start_date = None
self.end_date = None
def is_running(self):
return self.end_date is None
class Task(dict):
"""A Task
A task is defined by attributes like priority, period, usw. These attributes
are represented by key-value pairs and stored in a dict.
:param *blocks dict: A number of dicts as parameters, which are merged to
this task. This concept is called Task-Blocks. Task-Blocks are located in
the directory `taskgen.blocks`.
"""
def __init__(self, *blocks):
super().__init__({
# default values
"id" : None,
"priority" : None,
# blob values
"quota" : None,
"pkg" : None,
"config" : None,
# periodic task
"period" : None,
"numberofjobs" : 1
# "offset" : None, unused at genode side.
})
# stores all job data (added during runtime of this task)
self.jobs = []
# add inital blocks
for block in blocks:
if callable(block):
block = block()
# if not isinstance(attr, dict):
# raise ValueError("An attribute for a task must be dict.")
super().update(block)
@property
def id(self):
"""Getter of the task id attribute
This method is used by monitor implementations for finding the related
task for an event.
"""
return self['id']
@id.setter
def id(self, _id):
"""Setter for the task id
This method is used by `taskgen.taskset.TaskSet`. If a task is added to a
task-set, an unique id is assigned to the task.
"""
self['id'] = _id
def variants(self):
"""Generator for task variants
This method generates all variants of a task and is used by
`taskgen.taskset.TaskSet`.
"""
flat = flatdict.FlatDict(self)
# make everything to an iterator, except iterators. Pay attention:
# strings are wrapped with an iterator again.
iters = map(lambda x: [x] if not isinstance(x, Iterable) or
isinstance(x, str) else x, flat.itervalues())
keys = flat.keys()
for values in itertools.product(*iters):
# update dictionary with the new combined values. This is done by
# mapping all keys to their values.
flat.update(dict(zip(keys, values)))
# create new task
yield Task(flat.as_dict())
def binary(self):
"""Binary of the task
Every task has a binary. This method returns it.
:return: binary of the task
:rtype: str
"""
return self['pkg']