-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf_aws_iconify.py
110 lines (91 loc) · 3.84 KB
/
tf_aws_iconify.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
import os
from nltk import metrics, stem, tokenize
from test import test_text
from fuzzywuzzy import process, fuzz
stemmer = stem.PorterStemmer()
import re
import sys
import operator
from operator import itemgetter
from pprint import pprint
def shellquote(s):
return "'" + s.replace("'", "'\\''") + "'"
def get_icons():
fname = os.path.dirname(os.path.realpath(__file__)) + "/aws_icons.zip"
if not os.path.isfile(fname):
os.system(
"wget -O aws_icons.zip https://media.amazonwebservices.com/AWS-Design/Arch-Center/16.2.22_Update/AWS_Simple_Icons_EPS-SVG_v16.2.22.zip")
os.system("unzip -u aws_icons.zip -d ./aws_icons")
temp = os.walk("./aws_icons", topdown=False)
for root, dirs, files in temp:
for i in dirs:
dir = os.path.join(root, i)
os.rename(dir, os.path.join(root, i.replace("&", "").replace(" ", "")))
def abs_icon_list(path="./aws_icons/.", rename=False):
file_list = []
for root, dirs, files in os.walk(os.path.abspath(path), topdown=False):
for file in files:
abs = os.path.join(root, file)
if "__MAC" in abs:
continue
elif '.png' not in abs:
continue
else:
file_list.append(abs)
return file_list
def process_noise(data):
processed_dict = {}
for d in data:
tmp = d.rsplit("/")[-1].split(".png")[0].lower().replace("amazon", "_").replace("-", "_").replace("__",
"_").replace(
"aws", "")
processed_dict[d] = tmp
return processed_dict
def get_tf_labels():
scrubbed_tf_dict = dict()
for s in TF_GRAPH_OUTPUT.split("\n"):
if '[label' in s:
alt_label = ''
raw_label = s.split('"')[3]
label = raw_label.split(".")[0].replace("aws_", '')
og_repl = 'label = "{}"'.format(raw_label)
if 'iam' in label:
alt_label = label.replace("iam_", "security_identity_iam_")
if 'route' in label:
alt_label = label.replace('route_', 'networking_route_')
if 'vpc' in label:
alt_label = label.replace('vpc_', 'networking_vpc_')
scrubbed_tf_dict[label] = {'repl': og_repl, 'alt_label': alt_label}
return scrubbed_tf_dict
TF_GRAPH_OUTPUT = test_text
if __name__ == '__main__':
scrubbed_icon_dict = process_noise(abs_icon_list('./aws_icons/.'))
rev_scrubbed_icon_dict = dict((y, x) for x, y in scrubbed_icon_dict.iteritems())
scrubbed_tf_dict = get_tf_labels()
final_out = TF_GRAPH_OUTPUT
# pprint(scrubbed_tf_dict)
scorers = [fuzz.partial_ratio, fuzz.token_set_ratio, fuzz.ratio, fuzz.partial_token_set_ratio]
for label, v in scrubbed_tf_dict.iteritems():
tmp_repl = None
alt_label = v['alt_label']
scores = list()
for scorer in scorers:
tmp = process.extract(label, scrubbed_icon_dict.values(), limit=1, scorer=scorer)[0]
scores.append(tmp)
if alt_label:
tmp = process.extract(label, scrubbed_icon_dict.values(), limit=1, scorer=scorer)[0]
scores.append(tmp)
best_match = sorted(scores, key=lambda x: x[1], reverse=True)[0]
if best_match < 70:
continue
icon_file = rev_scrubbed_icon_dict[best_match[0]]
print final_out
if icon_file:
tmp_repl = """label = <<TABLE BORDER="0"><TR><TD><IMG SRC="{}"/></TD><TD>{}</TD></TR></TABLE>>""".format(
icon_file, label)
final_out = final_out.replace(v['repl'], tmp_repl)
os.system("printf {} | dot -Tpng > graph2.png && open graph2.png".format(shellquote(final_out)))
# final[orig] = og_repl
# print test_text
# for k,v in data.iteritems():
# print k,v