-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONToDrawioXML.py
More file actions
148 lines (119 loc) · 3.86 KB
/
JSONToDrawioXML.py
File metadata and controls
148 lines (119 loc) · 3.86 KB
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
import argparse
import textwrap
import logging
import datetime
import json
import os
import re
import sys
from pathlib import Path
from jinja2 import Template
from operator import itemgetter
from MauroAPIInterface import MauroAPIInterface
#####################################################################
## Argument Parsing
#####################################################################
help_text = "Turns a JSON file to a Draw.io entity diagram"
epilog_text = '''Outputs an XML object to a filepath.
'''
ap = argparse.ArgumentParser(
description=help_text,
epilog=textwrap.dedent(epilog_text)
)
# Log-related arguments
ap.add_argument(
'-l',
'--log-level',
action='store',
nargs=1, # '?' is optional 1
default='INFO',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help='Sets File logging to specified level (default INFO). Overridden by --verbose.'
)
ap.add_argument(
'-v',
'--verbose',
action='store_true',
help='Sets File logging to DEBUG (default info), and StdOut logging to INFO (default none). Overrides --log-level.'
)
ap.add_argument(
'-p',
'--log-path',
action='store',
default='logs/',
help="Sets destination path for file logs. Default 'logs/'."
)
# Input related arguments
ap.add_argument(
'-i',
'--input-file',
action='store',
required=True,
help="The filename of the JSON to be converted."
)
# Output related arguments
ap.add_argument(
'-o',
'--output-file',
action='store',
default='new_model.xml',
help="Sets filename of output file."
)
args = ap.parse_args()
#####################################################################
## Log Handling
#####################################################################
# create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) #Sets the level that is passed to the handlers. The handlers then determine what is passed onward.
# Create handlers
now_string = datetime.datetime.now().strftime("%Y%m%d-%H%M%S%f")
if not args.log_path.endswith('/'):
args.log_path = args.log_path + '/'
f_handler = logging.FileHandler(args.log_path + 'JSONToDrawioXML_' + now_string + '.log', encoding='utf-8')
strloglevel = args.log_level
intloglevel = getattr(logging, strloglevel.upper())
file_logging_level = intloglevel
if args.verbose:
file_logging_level = logging.DEBUG
f_handler.setLevel(file_logging_level)
# Create formatters and add it to handlers
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
f_handler.setFormatter(logging.Formatter(log_format))
# Add handlers to the logger. Nothing logs prior to now.
logger.addHandler(f_handler)
if args.verbose:
c_handler = logging.StreamHandler()
c_handler.setLevel(logging.INFO)
c_handler.setFormatter(logging.Formatter(log_format))
logger.addHandler(c_handler)
#####################################################################
## Handle exiting
#####################################################################
def crit_and_die(message):
logger.critical(message)
logger.critical('Execution ends')
sys.exit(message)
def err_and_die(err, message):
logger.exception(err)
logger.critical(message)
logger.critical('Execution ends')
sys.exit(err)
#####################################################################
## Actual program
#####################################################################
logger.info('Execution begins')
print("fish")
srcpath = ""
template_filename = "drawio_entities_diagram.tem"
data_filename = args.input_file
template = Path(os.path.join(srcpath, template_filename)).read_text()
with open(os.path.join(srcpath, data_filename)) as jsonfile:
data = json.load(jsonfile)
logger.debug(str(data))
j2_template = Template(template)
rendered_template_string = j2_template.render(data)
output_filename = args.output_file
with open(output_filename, "w") as text_file:
text_file.write(rendered_template_string)
logger.info("Execution ends")