-
Notifications
You must be signed in to change notification settings - Fork 6
/
WfExS-config-replicator.py
405 lines (326 loc) · 12.7 KB
/
WfExS-config-replicator.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: Apache-2.0
# Copyright 2020-2024 Barcelona Supercomputing Center (BSC), Spain
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import codecs
import copy
import csv
import mimetypes
import os
import sys
from typing import (
cast,
TYPE_CHECKING,
)
import openpyxl
import xlrd2 # type:ignore
import yaml
if TYPE_CHECKING:
from typing import (
Any,
Mapping,
MutableMapping,
MutableSequence,
Optional,
Sequence,
Tuple,
Type,
Union,
)
from openpyxl.worksheet.worksheet import Worksheet
# We have preference for the C based loader and dumper, but the code
# should fallback to default implementations when C ones are not present
YAMLDumper: "Type[Union[yaml.Dumper, yaml.CDumper]]"
try:
from yaml import CDumper as YAMLDumper
except ImportError:
from yaml import Dumper as YAMLDumper
def loadWorkflowConfig(workflowConfigFilename: "str") -> "Mapping[str, Any]":
with open(workflowConfigFilename, mode="r", encoding="utf-8") as wcf:
workflow_config = yaml.safe_load(wcf)
return cast("Mapping[str, Any]", workflow_config)
def loadXLSParams(paramsFilename: "str") -> "Sequence[Mapping[str, Any]]":
paramsArray = []
wb = xlrd2.open_workbook(filename=paramsFilename)
for sheet in wb.sheets():
gotHeader = False
header: "MutableSequence[Tuple[str, int]]" = []
for row in sheet.get_rows():
if row is None:
continue
# Either get the header or the data
if gotHeader:
params: "MutableMapping[str, MutableSequence[Any]]" = dict()
for headerName, iCell in header:
theVal = row[iCell].value
params.setdefault(headerName, []).append(theVal)
paramsArray.append(params)
else:
for iCell, cell in enumerate(row):
headerName = cell.value
if headerName is not None:
if not isinstance(headerName, str):
headerName = str(headerName)
headerName = headerName.strip()
if len(headerName) > 0:
gotHeader = True
header.append((headerName, iCell))
return paramsArray
def loadXLSXParams(paramsFilename: "str") -> "Sequence[Mapping[str, Any]]":
paramsArray = []
wb = openpyxl.load_workbook(filename=paramsFilename, data_only=True, read_only=True)
sheets = wb.worksheets
for sheet_raw in sheets:
# This is needed to avoid a mypy error fired
# by latest openpyxl annotations
sheet = cast("Worksheet", sheet_raw) # type: ignore[redundant-cast]
gotHeader = False
headerMap: "MutableMapping[int,str]" = {}
for cells_in_row in sheet.iter_rows():
# Either get the header or the data
if gotHeader:
params: "MutableMapping[str, MutableSequence[Any]]" = dict()
for cell in cells_in_row:
headerName = headerMap.get(cell.col_idx)
if headerName is not None:
theVal = cell.value
params.setdefault(headerName, []).append(theVal)
paramsArray.append(params)
else:
for cell in cells_in_row:
if cell.value is not None:
headerName = str(cell.value).strip()
if len(headerName) > 0:
gotHeader = True
# The column index is 1-based
headerMap[cell.col_idx] = headerName
return paramsArray
def loadCSVParams(paramsFilename: "str") -> "Sequence[Mapping[str, Any]]":
paramsArray = []
with open(paramsFilename, mode="rb") as cR:
rawCSV = cR.read()
guessedEncoding = "iso-8859-1"
try:
decoded = rawCSV.decode("utf-8")
guessedEncoding = "utf-8"
except:
decoded = rawCSV.decode(guessedEncoding)
sep = "\t"
for testSep in (",", ";", "\t"):
if testSep in decoded:
sep = testSep
break
cR.seek(0)
readerC = codecs.getreader(encoding=guessedEncoding)
with readerC(cR) as f:
cReader = csv.reader(f, delimiter=sep)
gotHeader = False
header: MutableSequence[Tuple[str, int]] = []
for row in cReader:
# Skip commented-out lines
if row[0][0] == "#":
continue
# Either get the header or the data
if gotHeader:
params: MutableMapping[str, MutableSequence[Any]] = dict()
for headerName, iCell in header:
theVal = row[iCell]
params.setdefault(headerName, []).append(theVal)
paramsArray.append(params)
else:
for iCell, headerName in enumerate(row):
if headerName is not None:
headerName = headerName.strip()
if len(headerName) > 0:
gotHeader = True
header.append((headerName, iCell))
return paramsArray
MIME_PARSERS = {
"application/vnd.ms-excel": loadXLSParams,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": loadXLSXParams,
"text/csv": loadCSVParams,
}
def loadParamsFiles(paramsFilenames: "Sequence[str]") -> "Sequence[Mapping[str, Any]]":
"""
This method returns a list of dictionaries
being each dictionary a set of values to substitute
into the workflow configuration template
"""
paramsArray: "MutableSequence[Mapping[str, Any]]" = list()
if not mimetypes.inited:
mimetypes.init()
for paramsFilename in paramsFilenames:
if os.path.exists(paramsFilename):
guessed_mime, guessed_encoding = mimetypes.guess_type(paramsFilename)
print(
"* Processing {} ({} {})".format(
paramsFilename, guessed_mime, guessed_encoding
)
)
# If no guessed mime, bet on CSV
mime_parser = (
loadCSVParams
if guessed_mime is None
else MIME_PARSERS.get(guessed_mime)
)
if mime_parser is not None:
read_paramsArray = mime_parser(paramsFilename)
paramsArray.extend(read_paramsArray)
else:
print("\tNo handler for MIME {}. Skipping".format(guessed_mime))
return paramsArray
VALID_ROOTS_DICT = {
"params": "url",
"outputs": "preferredName",
}
VALID_ROOTS = tuple(VALID_ROOTS_DICT.keys())
def applyValuesToTemplate(
workflow_config_template: "Mapping[str, Any]", params: "Mapping[str, Any]"
) -> "Mapping[str, Any]":
"""
The parameters are set using as template the input
"""
workflow_config = cast(
"MutableMapping[str, Any]", copy.deepcopy(workflow_config_template)
)
workflow_config.setdefault("params", {})
workflow_config.setdefault("outputs", {})
for key, value in params.items():
steps = list(key.split("."))
if len(steps) < 2:
raise Exception(
"key names have to start with any of these keys, and then have the path to the parameter or output: {}".format(
VALID_ROOTS
)
)
rootStep = steps.pop(0)
leafStep = VALID_ROOTS_DICT.get(rootStep)
if leafStep is not None:
section = workflow_config[rootStep]
else:
raise Exception(
"key names have to start with any of these keys: {}".format(VALID_ROOTS)
)
lastStep = steps.pop()
for step in steps:
section = section.setdefault(step, {})
lastStepSection = section.get(lastStep)
if (
(lastStepSection is not None)
and isinstance(lastStepSection, dict)
and (lastStepSection.get("c-l-a-s-s") is not None)
):
section = lastStepSection
lastStep = leafStep
section[lastStep] = value[0] if len(value) == 1 else value
return workflow_config
def writeWorkflowConfigVariations(
workflow_config_template: "Mapping[str, Any]",
paramsArray: "Sequence[Mapping[str, Any]]",
fnameTemplate: "str",
destdir: "str" = ".",
paramSymbolTemplate: "Optional[str]" = None,
) -> "Sequence[str]":
# Creating the directory, in case it does not exist
destdir = os.path.abspath(destdir)
os.makedirs(destdir, exist_ok=True)
createdConfigurationFiles = []
paramSymbolPath = None
if paramSymbolTemplate is not None:
paramSymbolPath = paramSymbolTemplate.split(".")
for iParams, params in enumerate(paramsArray):
workflow_config = applyValuesToTemplate(workflow_config_template, params)
symbolicValue = None
if paramSymbolPath is not None:
symbolicValue = workflow_config.get("params")
for paramSymbol in paramSymbolPath:
if isinstance(symbolicValue, dict):
symbolicValue = symbolicValue.get(paramSymbol)
if symbolicValue is None:
symbolicValue = iParams
relCreatedFilename = fnameTemplate.format(symbolicValue)
createdFilename = os.path.join(destdir, relCreatedFilename)
print("* Storing updated configuration at {}".format(createdFilename))
with open(createdFilename, mode="w", encoding="utf-8") as cf:
yaml.dump(workflow_config, cf, Dumper=YAMLDumper, sort_keys=False)
createdConfigurationFiles.append(relCreatedFilename)
return createdConfigurationFiles
if __name__ == "__main__":
ap = argparse.ArgumentParser(description="WfExS config replicator")
ap.add_argument(
"-W",
"--workflow-config",
dest="workflowConfigFilename",
required=True,
help="Workflow configuration file, to be used as template",
)
app = ap.add_mutually_exclusive_group(required=True)
app.add_argument(
"-p",
"--param",
dest="inline_params",
help="Param to substitute. Repeat to tell arrays of values",
nargs=2,
metavar=("PARAM_NAME", "VALUE"),
action="append",
)
app.add_argument(
"--params-file",
dest="params_files",
help="Tabular params file with the different variations",
action="append",
)
ap.add_argument(
"--fname-template",
dest="filename_template",
help="Filename template for the created workflows",
)
ap.add_argument(
"--symbol-template",
dest="paramSymbolTemplate",
)
ap.add_argument(
"destdir",
help="Directory where all the variations of the workflow configuration file are going to be created",
nargs="?",
default=".",
)
args = ap.parse_args()
if not args.workflowConfigFilename:
print("[ERROR] Workflow config was not provided! Stopping.", file=sys.stderr)
sys.exit(1)
workflow_config_template = loadWorkflowConfig(args.workflowConfigFilename)
paramsArray = None
if args.params_files:
paramsArray = loadParamsFiles(args.params_files)
else:
params: "MutableMapping[str, Any]" = {}
paramsArray = [params]
for param, value in args.inline_params:
params.setdefault(param, []).append(value)
fnameTemplate = args.filename_template
if not fnameTemplate:
fnameTemplate = os.path.basename(args.workflowConfigFilename) + ".{}.yaml"
paramSymbolTemplate = args.paramSymbolTemplate
if paramsArray:
createdConfigurationFiles = writeWorkflowConfigVariations(
workflow_config_template,
paramsArray,
fnameTemplate,
destdir=args.destdir,
paramSymbolTemplate=paramSymbolTemplate,
)
print("These are the created files: {}".format(createdConfigurationFiles))