This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
267 lines (217 loc) · 8.16 KB
/
core.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#core.py
import importlib
import re
"""
"""
class App:
"""
Description:
Arguments:
Output:
Note:
"""
def __init__(self, config_path):
self.metadata = None
self.plugins = None
self.handles = None
self.workflow = None
self.current_sequence = None
config = self.read_config(config_path)
self.set_metadata(config["metadata"])
self.set_plugins(config["plugins"])
self.set_handles(config["handles"])
self.set_workflow(config["workflow"])
self.all_plugins_required_metadata_check()
"""
Description:
Arguments:
Output:
Note:
"""
def read_config(self, config_path):
config = {
"metadata":[],
"plugins":[],
"handles":[],
"workflow":[]
}
current_field = None
current_line = None
with open(config_path) as handle:
for line in handle:
current_line = line.rstrip("\n")
#Comments and blank lines ignore
if current_line == "": continue
if current_line[0] == "#": continue
#Filling the config object with the content of the configuration file
if current_field:
assert current_field in ["metadata", "plugins", "handles", "workflow"]
#Checking if we are at the end of a field
if re.match(r"^(<\/)(\w+)(>)$", current_line):
current_field = None
else:
config[current_field].append(current_line)
else:
assert re.match(r"^(<)(\w+)(>)$", current_line)
assert current_line[1:-1] in ["metadata", "plugins", "handles", "workflow"]
current_field = current_line[1:-1]
handle.close()
return config
"""
Description:
- converts the array containing the metadata into a dictionnary by splitting
each string containing the metadata key and metadata value into an item with
the key and value.
Arguments:
- array: list of strings
Output:
- dictionnary of strings
Note:
"""
def set_metadata(self, array:list):
def convert(value):
temp = value.split(",")
if len(temp) > 1:
if temp[1] == "int":
return int(temp[0])
return value
self.metadata = {element.split(":")[0]:convert(element.split(":")[1]) for element in array}
"""
Description:
- converts the array containing the plugins parameters (name, package) into a
dictionnary by splitting each string containing the plugin key, the plugin name
and the plugin package into an item with the key and the callable plugin.
Store the result in the self.metadata variable.
Arguments:
- array: list of strings
Output:
- dictionnary of plugin objects
Note:
"""
def set_plugins(self, array:list):
self.plugins = {element.split(":")[0]:importlib.import_module(element.split(":")[1].split(",")[0],element.split(":")[1].split(",")[1]).Plugin() for element in array}
"""
Description:
- converts the array containing the handles parameters into a dictionnary by
splitting each string containing the handle key, the handle converter and the
file path into an item with the key and the converted as a data frame handle.
Store the result in the self.plugin variable.
Arguments:
- array: list of strings
Output:
- dictionnary of data frames
Note:
"""
def set_handles(self, array):
self.handles = {element.split(":")[0]:self.plugins[element.split(":")[1].split(",")[0]].process(element.split(":")[1].split(",")[1]) for element in array}
"""
Description:
- converts the array containing the workflow into a recursive automaton where
task are described by a tuple containing the required plugin, the handle where
the data is found and a list of elements to call.
Store the result in the self.handles variable.
Arguments:
- array: list of strings
Output (assigned):
- array: list of tuples (recursive)
Note:
"""
def set_workflow(self, array):
temp = self.refactor_workflow(array)
temp = self.merge_workflow(temp)
self.workflow = self.convert_workflow_task(temp[0])
"""
Description:
- converts strings into a tuple containing the level of the task, the rest of the
string and an empty array.
Arguments:
- array list of strings
Output:
- array: list of tuples
Note:
"""
def refactor_workflow(self, array):
for i in range(len(array)):
element = array[i]
level = len(re.search(r"^(-)+", element).group())
array[i] = (level, element[level:], [])
return array
"""
Description:
- place the tasks in their parent (level-1) tasks array
Arguments:
- array: list of tuples
Output:
- array: list of tuples (recursive)
Note:
"""
def merge_workflow(self, array):
array.insert(0, (0,None,[]))
max_level = max(array, key = lambda element: element[0])[0]
for i in range(max_level, -1, -1):
for j in range(len(array)):
element = array[j]
if element[0] == i:
for k in range(1, j+1):
if (
array[j-k] is not None and
array[j-k][0] == i-1
):
array[j-k][2].append(element)
array[j] = None
break
array = [element for element in array if element != None]
return array
"""
Description:
- recursively converts the tuples containing the task string into a tuple containing
a reference to the application, the plugin key, the handle key and an array of
subtasks.
Arguments:
- array: list of tuples (recursive)
Output:
- array: list of tuples (recusrive)
Note:
"""
def convert_workflow_task(self, task):
if task[1]:
return (
self,
task[1].split(",")[0],
task[1].split(",")[2] if len(task[1].split(",")) > 2 else "default",
task[1].split(",")[1],
[self.convert_workflow_task(sub_task) for sub_task in task[2]]
)
return [self.convert_workflow_task(sub_task) for sub_task in task[2]]
"""
Description:
Arguments:
Output:
Note:
"""
def all_plugins_required_metadata_check(self):
for key, plugin in self.plugins.items():
if not plugin.required_metadata_check(self):
raise InvalidConfigurationError(f"{key} plugin could not find required metadata")
"""
Description:
Arguments:
Output:
Note:
"""
def run(self):
for app, key_plugin, *args in self.workflow:
app.plugins[key_plugin].process(app, *args)
"""
"""
class InvalidConfigurationError(Exception):
def __init__(self, *args):
if args:
self.message = args[0]
else:
self.message = None
def __str__(self):
if self.message:
return f"InvalidConfigurationError, {self.message}"
else:
return "InvalidConfigurationError has been raised"