forked from tomohiro-noda/docker-uml-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dc2uml.py
270 lines (206 loc) · 6.24 KB
/
dc2uml.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
def finditem(obj, key):
if key in obj:
return obj[key]
for (k, v) in obj.items():
if isinstance(v, dict):
return finditem(v, key)
# input_yml
def open_yml(filename):
import yaml
with open(filename, 'r') as f:
dict = yaml.safe_load(f)
return dict
# pre-process_yml
def remove_restricted_str(data_dict, before, after):
data_str = str(data_dict)
if before in data_str:
modified_str = after.join(data_str.split(before))
modified_dict = eval(modified_str)
return modified_dict
# service:depends_on
def extract_service_depends_on(data_dict):
ret = {}
for (k, v) in data_dict.items():
tmp = []
items = finditem(v, 'depends_on')
if isinstance(items, list):
for i in items:
tmp.append(i)
ret.update({k: tmp})
else:
pass
return ret
def convert_service_depends_on_to_uml(data_dict):
ret = ''
for (k, v) in data_dict.items():
if v is None:
pass
elif isinstance(v, list):
for i in v:
ret = ret + '[' + k + ']' + ' --> ' + '[' + i + ']' \
+ '\n'
else:
ret = ret + '[' + k + ']' + ' --> ' + '[' + v + ']' + '\n'
return ret
# service:image
def extract_service_image(data_dict):
ret = {}
for (k, v) in data_dict.items():
tmp = []
for i in [finditem(v, 'image')]:
tmp.append(i)
ret.update({k: tmp})
return ret
def convert_service_image_to_uml(data_dict):
ret = ''
prefix = 'cloud ' + 'Repository' + ' {\n'
sufix = '}\n'
for (k, v) in data_dict.items():
if v is None:
pass
elif isinstance(v, list):
for i in v:
ret = ret + ' [' + k + ']' + ' --> ' + '[' + i + ']' \
+ '\n'
else:
ret = ret + ' [' + k + ']' + ' --> ' + '[' + v + ']' + '\n'
ret = prefix + ret + sufix
return ret
# service:networks
def extract_service_networks(data_dict):
ret = {}
for (k, v) in data_dict.items():
networks = finditem(data_dict, 'networks')
if len(networks) > 1:
for i in networks:
if i in ret:
ret[i].append(k)
else:
ret.update({i: [k]})
else:
if networks[0] in ret:
ret[networks[0]].append(k)
else:
ret.update({networks[0]: [k]})
return ret
def convert_service_networks_to_uml(data_dict):
return data_dict
# service:services
def extract_service_services(data_dict):
return data_dict
def convert_service_services_to_uml(data_dict):
ret = {}
prefix = 'component '
sufix = ' ]'
for (k, v) in data_dict.items():
tmp = prefix + k + ' [' + k + '''
---
'''
tmp = add_ports(tmp, v)
#tmp = add_aliases(tmp, v)
tmp = tmp + sufix
ret.update({k: tmp})
return ret
def add_ports(uml, data_dict):
ports_define = 'ports'
ports = finditem(data_dict, ports_define)
poarts_str = ''
try:
for i in ports:
poarts_str = poarts_str + str(i) + ','
added_str = uml + ' ' + ports_define + ':' + poarts_str + '\n'
except:
return uml
return added_str
def add_aliases(uml, data_dict):
aliases_define = 'aliases'
aliases = finditem(data_dict['networks'], aliases_define) # must modify
aliases_str = ''
for i in aliases:
aliases_str = aliases_str + str(i) + ','
added_str = uml + ' ' + aliases_define + ':' + aliases_str + '\n'
return added_str
# combine_uml
def combine_network_component_uml(networks_dict, services_dict):
ret = {}
networks_str = ''
for (k, v) in networks_dict.items():
if isinstance(v, list):
for i in v:
networks_str = networks_str + ' ' + services_dict[i] \
+ '\n'
else:
networks_str = networks_str + ' ' + services_dict[v] + '\n'
prefix = 'package ' + k + ' {\n'
sufix = '}\n'
ret.update({k: prefix + networks_str + sufix})
return ret
def combine_networks_uml(networks_uml_dict):
ret = ''
for (k, v) in networks_uml_dict.items():
ret = ret + v
return ret
def combine_uml(networks_uml, depends_on_uml, image_uml):
return networks_uml + '\n' + depends_on_uml + '\n' + image_uml
def combine_puml_with_atom_md(raw_uml):
prefix = '```plantuml\n'
sufix = '```'
return prefix + raw_uml + sufix
# output_uml
def save_md(uml, output_filename):
with open(output_filename, 'w') as f:
f.write(uml)
def main():
import sys
# ----
# input
# ----
data_dict = open_yml(sys.argv[1])
# ----
# pre-process
# ----
data_dict_preprocessed = remove_restricted_str(data_dict, '-', '_')
# data_dict_preprocessed=data_dict
# ----
# process depends_on
# ----
depends_on_data_dict = \
extract_service_depends_on(data_dict_preprocessed['services'])
depends_on_uml = \
convert_service_depends_on_to_uml(depends_on_data_dict)
# ----
# process image
# ----
# image_data_dict = \
# extract_service_image(data_dict_preprocessed['services'])
# image_uml = convert_service_image_to_uml(image_data_dict)
image_uml = ''
# ----
# process networks
# ----
networks_data_dict = \
extract_service_networks(data_dict_preprocessed['services'])
# networks_uml=convert_service_networks_to_uml(networks_data_dict)
# ----
# process services
# ----
# services_data_dict = extract_service_services(data_dict_preprocessed['services'])
services_uml = \
convert_service_services_to_uml(data_dict_preprocessed['services'
])
# ----
# combine_uml
# ----
networks_uml_dict = \
combine_network_component_uml(networks_data_dict, services_uml)
networks_uml = combine_networks_uml(networks_uml_dict)
raw_uml = combine_uml(networks_uml, depends_on_uml, image_uml)
uml = combine_puml_with_atom_md(raw_uml)
# ----
# output_uml
# ----
save_md(uml, sys.argv[2])
if __name__ == '__main__':
main()