-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathj2templator.py
executable file
·501 lines (416 loc) · 16.4 KB
/
j2templator.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/usr/bin/env python3
import argparse
import logging
import os
import shlex
import jinja2
import yaml
APP = "j2templator"
GITHUBURL = "https://github.com/derifgig/j2templator"
VERSION = "v0.5b"
DEFAULT_LOGGING_LEVEL = logging.INFO
CFG = []
# field names in YAML config file
cf_name = "name"
cf_template = "template"
cf_check_ingore_file_absent = "check_ingore_file_absent"
cf_output_path = "output_path"
cf_output_path_create = "output_path_create"
cf_output_file_name_template = "output_file_name_template"
cf_output_file_mode = "output_file_mode"
cf_input_data_file = "input_data_file"
cf_input_data_type = "input_data_type"
cf_additional_data_file = "additional_data_file"
cf_additional_data = "additional_data"
cf_mode = "mode"
class CustomFormatter(logging.Formatter):
grey = "\x1b[38;20m"
magenta = "\x1b[35;20m"
yellow = "\x1b[33;20m"
red = "\x1b[31;20m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
format = "%(message)s"
FORMATS = {
logging.DEBUG: yellow + format + reset,
logging.INFO: grey + format + reset,
logging.WARNING: magenta + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset,
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
# end of class CustomFormatter
def parse_arguments():
parser = argparse.ArgumentParser(description=f"{APP} {VERSION} {GITHUBURL}")
parser.add_argument(
"-c", "--config", required=True, help="Config file in YML format"
)
parser.add_argument(
"-d",
"--debug",
action="store_true",
required=False,
default=False,
help="Debug mode",
)
parser.add_argument(
"-k",
"--check",
action="store_true",
required=False,
default=False,
help="Only check config, no more",
)
return parser.parse_args()
def check_file_exist(filename):
try:
result = os.path.exists(filename)
return result
except IOError:
return False
def check_config_file(config_file):
required_fields = [
cf_template,
cf_output_path,
cf_output_file_name_template,
cf_input_data_file,
]
files = [
cf_template,
cf_input_data_file,
cf_additional_data_file,
]
mode_available_values = ["all", "one"]
input_data_type = ["yml", "txt"]
logger.debug("Validation config file: %s" % config_file)
if not check_file_exist(config_file):
logger.error(f"Config file not found: {config_file}")
return False
try:
stream = open(config_file, "r")
except IOError:
logger.error("Error reading from file: %s" % config_file)
return False
try:
config_data = yaml.safe_load(stream)
logger.debug("Data type in config file recognized: %s" % type(config_data))
is_items_valid = True
for item_index in range(len(config_data)):
item_config = config_data[item_index]
item_name = item_config[cf_name] if cf_name in item_config else ""
item_prefix = "check [%s] (%s)" % (item_index, item_name)
logger.debug("%s : Checking config item" % item_prefix)
# checking for required fields
is_required_fields = True
for i in required_fields:
if i not in item_config:
logger.error("%s : No required field: [%s]" % (item_prefix, i))
is_required_fields = False
if is_required_fields:
logger.debug("%s : All required fields presents." % item_prefix)
else:
is_items_valid = False
check_ingore_file_absent = False
if cf_check_ingore_file_absent in item_config:
check_ingore_file_absent = item_config[cf_check_ingore_file_absent]
if check_ingore_file_absent:
logger.warning("%s : Absent files will be ignored" % item_prefix)
source_files_exist = True
for i in files:
if i in item_config:
if not check_file_exist(item_config[i]):
if check_ingore_file_absent:
logger.warning(
"%s : Source file not found: [%s]"
% (item_prefix, item_config[i])
)
else:
logger.error(
"%s : Source file not found: [%s]"
% (item_prefix, item_config[i])
)
source_files_exist = False
if not source_files_exist:
logger.error("%s : SKIPPED" % (item_prefix))
continue
# add default values
if cf_name not in item_config:
item_config[cf_name] = ""
if cf_mode not in item_config:
item_config[cf_mode] = "all"
else:
if item_config[cf_mode] not in mode_available_values:
logger.error(
"%s : Unknown mode [%s]" % (item_prefix, item_config[cf_mode])
)
logger.error("%s : SKIPPED" % (item_prefix))
continue
# Default data type - yml
if cf_input_data_type not in item_config:
item_config[cf_input_data_type] = "yml"
else:
if item_config[cf_input_data_type] not in input_data_type:
logger.error(
"%s : Unknown input data type [%s]"
% (item_prefix, item_config[cf_input_data_type])
)
logger.error("%s : SKIPPED" % (item_prefix))
continue
if cf_output_path_create not in item_config:
item_config[cf_output_path_create] = False
if cf_output_file_mode in item_config:
if not isinstance(item_config[cf_output_file_mode], str):
item_config[cf_output_file_mode] = str(
item_config[cf_output_file_mode]
)
# add new item to work
logger.info("%s : OK" % item_prefix)
CFG.append(item_config)
#
if not is_items_valid:
logger.error("Can not continue with this config file")
return False
except yaml.YAMLError as exc:
logger.error("Error parsing YAML data from config file")
return False
# normal final of procedure
return True
def doit():
if len(CFG) == 0:
logger.error("No valid data in config. Nothing to do")
return False
for item_index in range(len(CFG)):
item = CFG[item_index]
item_prefix = "exec [%s] (%s)" % (item_index, item[cf_name])
logger.info(
"%s : Started. Mode: %s Output path: %s"
% (item_prefix, item[cf_mode], item[cf_output_path])
)
if not check_file_exist(item[cf_template]):
logger.error(
"%s : Template file not found: %s." % (item_prefix, item[cf_template])
)
continue
try:
with open(item[cf_template]) as f:
j2_template = jinja2.Template(f.read())
except jinja2.TemplateSyntaxError as err:
logger.error(
"%s : Template syntax error in file %s: %s"
% (item_prefix, item[cf_template], err.message)
)
return False
except IOError:
logger.error(
"%s : Error reading template file: %s"
% (item_prefix, item[cf_template])
)
return False
finally:
f.close()
# Reading input data
match item[cf_input_data_type]:
case "yml":
try:
content_data_file = open(item[cf_input_data_file], "r")
content_data = yaml.safe_load(content_data_file)
except IOError:
logger.error(
"%s : Error in parsing YML file: %s"
% (item_prefix, item[cf_output_path])
)
return False
case "txt":
content_data = []
try:
content_data_file = open(item[cf_input_data_file], "r")
for line in content_data_file:
li = line.strip()
if len(li) == 0:
continue
if not li.startswith("#"):
content_data.append(shlex.split(li))
except IOError:
logger.error(
"%s : Error reading file: %s"
% (item_prefix, item[cf_input_data_file])
)
return False
if len(content_data) == 0:
logger.error(
"%s : No data in file: %s"
% (item_prefix, item[cf_input_data_file])
)
return False
# Debug
logger.debug(f"content data: {content_data}")
# by default additional data not present
additional_data = {}
additional_data_from_config = {}
additional_data_from_file = {}
#
if cf_additional_data in item:
additional_data_from_config = item[cf_additional_data]
logger.debug(f"additional_data from config: {additional_data_from_config}")
# if additional_data_file present in config
if cf_additional_data_file in item:
if check_file_exist(item[cf_additional_data_file]):
# Reading additional data
try:
logger.debug(
f'Reading additional data file" {item[cf_additional_data_file]}'
)
additional_data_file = open(item[cf_additional_data_file], "r")
additional_data_from_file = yaml.safe_load(additional_data_file)
logger.debug(
f"additional_data from file: {additional_data_from_file}"
)
except IOError:
logger.error(
"%s : Error reading additional data file: %s"
% (item_prefix, item[cf_additional_data_file])
)
return False
finally:
additional_data_file.close()
# Merging additional data
additional_data = {**additional_data_from_file, **additional_data_from_config}
logger.debug(f"additional_data: {additional_data}")
# Checking for existing OUTPUT directory
try:
result = os.path.isdir(item[cf_output_path])
if not result:
if item[cf_output_path_create]:
logger.info(
"%s : Creation directory: %s"
% (item_prefix, item[cf_output_path])
)
os.makedirs(item[cf_output_path], exist_ok=True)
else:
logger.error(
"%s : Output directory not found: %s"
% (item_prefix, item[cf_output_path])
)
continue
except IOError:
logger.error(
"%s : Output directory checking error: %s"
% (item_prefix, item[cf_output_path])
)
return False
# Working ...
match item[cf_mode]:
case "all":
# generate path
j2_template_output_path = jinja2.Template(item[cf_output_path])
output_path = j2_template_output_path.render(
content=content_data, ad=additional_data
)
# generate file name
j2_template_output_file = jinja2.Template(
item[cf_output_file_name_template]
)
output_file_name = j2_template_output_file.render(
content=content_data, ad=additional_data
)
output_file_name = output_path + "/" + output_file_name
# render, and save the results to file
logger.info("%s : Output file: %s" % (item_prefix, output_file_name))
try:
with open(output_file_name, "w") as fh:
fh.write(
j2_template.render(content=content_data, ad=additional_data)
)
except jinja2.TemplateError as err:
logger.error(
"%s : Template error : %s" % (item_prefix, err.message)
)
except IOError:
logger.error(
"%s : Writing file IOError : %s"
% (item_prefix, output_file_name)
)
continue
finally:
fh.close()
# set file mode
if cf_output_file_mode in item:
os.chmod(output_file_name, int(item[cf_output_file_mode], 8))
case "one":
logger.debug("Data type of content: %s" % type(content_data))
for data_index in range(len(content_data)):
item_content = content_data[data_index]
# generate path
j2_template_output_path = jinja2.Template(item[cf_output_path])
output_path = j2_template_output_path.render(
item=item_content, ad=additional_data
)
# generate file name
j2_template_output_file = jinja2.Template(
item[cf_output_file_name_template]
)
output_file_name = j2_template_output_file.render(
item=item_content, ad=additional_data
)
output_file_name = output_path + "/" + output_file_name
# checking for existing files, it's no problem
if check_file_exist(output_file_name):
logger.info(
"%s : Overwriting output file: %s"
% (item_prefix, output_file_name)
)
else:
logger.info(
"%s : Output file: %s" % (item_prefix, output_file_name)
)
try:
with open(output_file_name, "w") as fh:
fh.write(
j2_template.render(
content=item_content, ad=additional_data
)
)
except IOError:
logger.error(
"%s : Writing file IOError : %s"
% (item_prefix, output_file_name)
)
continue
finally:
fh.close()
case _:
logging.error(f"Unknown mode value {item[cf_mode]}")
# end of match
logger.info("%s : Done" % item_prefix)
return True
def main():
args = parse_arguments()
logger.info(f"Started {APP} {VERSION} {GITHUBURL}")
if args.debug:
logger.setLevel(logging.DEBUG)
logger.info("Config file: %s" % args.config)
check_config_result = check_config_file(args.config)
# if only check config
if args.check:
return check_config_result
else:
if not check_config_result:
return 1
#
if not doit():
return 1
#
logger.info("Finished")
# Define global items
logger = logging.getLogger(APP)
logging_level = DEFAULT_LOGGING_LEVEL
logger.setLevel(logging_level)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setFormatter(CustomFormatter())
logger.addHandler(ch)
if __name__ == "__main__":
main()