-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy2pumla.py
295 lines (246 loc) · 10 KB
/
py2pumla.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
#!/usr/bin/python
"""
Command Line Tool and PUMLA hook for processing
python files and producing PUMLA conformant
PlantUML models of it.
"""
__author__ = "Dr. Markus Voss (private person)"
__copyright__ = "(C) Copyright 2021 by Dr. Markus Voss (private person)"
__license__ = "GPL"
__version__ = "0.2.1"
__maintainer__ = "Dr. Markus Voss (private person)"
__status__ = "Development"
import sys
import os
from importlib.util import spec_from_file_location, module_from_spec
import inspect
pumla_path = "/Users/mvoss/Desktop/git/github/pumla"
def identifyMe():
""" information about the executed command """
print("py2pumla v0.2 - by Dr. Markus Voss")
def isInBlacklist(path, blacklist):
""" check whether the given path is contained in the
given blacklist list."""
retval = False
for e in blacklist:
if (e in path):
retval = True
return retval
def createClassPUMLCode(classelement, targetpath):
#print(classelement)
#print("class-el-name = " + classelement.__name__)
#print("class-el_mod = " + classelement.__module__)
# ltw = lines to write
ltw = "'PUMLAMR\n@startuml\n!include " + pumla_path + "/pumla_macros.puml\n!include modelrepo_json.puml\n\n"
ce_name = classelement.__name__
ce_alias = ce_name.lower() + "Class"
ltw = ltw + "!if ($PUMVarShowBody)\n"
ltw = ltw + 'class "'+ ce_name + '" as ' + ce_alias + ' <<Python>> <<class>> { \n'
#print(dir(classelement))
#print(classelement.__dict__)
for me in dir(classelement):
if ((not ("__" in me)) or (me == "__init__")):
me_name = me
#print(me)
me_raw = classelement.__dict__[me]
me_args = me_raw.__code__.co_varnames[0:(me_raw.__code__.co_argcount)]
ltw = ltw + "\t+" + me_name + str(me_args).replace("'", "").replace(",)", ")") + "\n"
ltw = ltw + "!endif\n"
ltw = ltw + "}\n\n"
ltw = ltw + "!if ($PUMVarShowDescr)\n"
for me in dir(classelement):
if ((not ("__" in me)) or (me == "__init__")):
me_raw = classelement.__dict__[me]
if not(me_raw.__doc__ == None):
ltw = ltw + "note left of " + ce_alias + "::" + me + "\n"
ltw = ltw + str(me_raw.__doc__) + "\n"
ltw = ltw + "end note\n\n"
ltw = ltw + "note bottom of " + ce_alias + "\n"
ltw = ltw + "\t" + str(classelement.__doc__) + "\n"
ltw = ltw + "end note\n"
ltw = ltw + "!endif\n"
ltw = ltw + "\n@enduml\n"
#print(ltw)
# write file here
tfn = targetpath + "/" + ce_alias + ".puml"
with open(tfn, "w") as fil:
fil.write(ltw)
return ce_alias
def createModuleFunctionsPUMLCode(module, modulefuncs, targetpath):
# todo: rework this
ltw = "'PUMLAMR\n@startuml\n!include " + pumla_path + "/pumla_macros.puml\n!include modelrepo_json.puml\n\n"
me_name = module.__name__
me_alias = me_name.lower() + "Module"
ltw = ltw + "!if ($PUMVarShowBody)\n"
modfunc_name = me_name + 'ModFuncs'
ltw = ltw + 'class "Module ' + me_name + ' Functions" as ' + modfunc_name + ' <<Python>> <<module>> {\n'
for mf in modulefuncs:
me_name = mf.__name__
me_args = mf.__code__.co_varnames[0:(mf.__code__.co_argcount)]
ltw = ltw + "\t+" + str(me_name) + str(me_args).replace("'", "").replace(",)", ")") + "\n"
ltw = ltw + "}\n\n"
ltw = ltw + "!if ($PUMVarShowDescr)\n"
for me in modulefuncs:
if ((not ("__" in me.__name__)) or (me.__name__ == "__init__")):
if not(me.__doc__ == None):
ltw = ltw + "note left of " + modfunc_name + "::" + me.__name__ + "\n"
ltw = ltw + str(me.__doc__) + "\n"
ltw = ltw + "end note\n"
ltw = ltw + "!endif\n"
ltw = ltw + "@enduml\n\n"
#print(ltw)
#write file here
tfn = targetpath + "/" + modfunc_name + ".puml"
with open(tfn, "w") as fil:
fil.write(ltw)
return modfunc_name
def createModulePUMLCore(module, filename, list_of_elementalias, mymod_func, mymod_uses, targetpath):
ltw = "'PUMLAMR\n@startuml\n!include " + pumla_path + "/pumla_macros.puml\n!include modelrepo_json.puml\n\n"
me_name = module.__name__
me_alias = me_name.lower() + "Module"
ltw = ltw + "!if ($PUMVarShowBody)\n"
ltw = ltw + 'package "' + filename + '" as ' + me_alias + ' <<Python>> { \n'
# if not(mymod_func == ""):
# ltw = ltw + '\tclass "Functions" <<Python>> <<module>> {\n'
# ltw = ltw + mymod_func
# ltw = ltw + '\t}\n\n'
for e in list_of_elementalias:
ltw = ltw + "\tPUMLAPutInternalElement(" + e + ")\n"
ltw = ltw + "}\n\n"
for el in list_of_elementalias:
relalias = "REL#" + me_alias + "_CONTAINS_" + el
ltw = ltw + 'PUMLARelation(' + me_alias + ', "..>", ' + el + ', "contains", "' + relalias + '")\n'
ltw = ltw + mymod_uses + "\n\n"
ltw = ltw + "!if ($PUMVarShowDescr)\n"
ltw = ltw + "note bottom of " + me_alias + "\n"
ltw = ltw + "\t" + str(module.__doc__) + "\n"
ltw = ltw + "end note\n"
ltw = ltw + "!endif\n"
ltw = ltw + "@enduml\n\n"
#print(ltw)
# write file here mode
tfn = targetpath + "/" + me_alias + ".puml"
with open(tfn, "w") as fil:
fil.write(ltw)
def py2pumla(fname, targetpath):
basename = os.path.basename(fname)
fullname = os.path.abspath(fname)
#print("basename = " + basename)
modulename = basename.split(".")[0]
#print("modulename = " + modulename)
#print(os.path.abspath(os.path.curdir))
sys.path.append(os.path.abspath(os.path.curdir))
sys.path.append(os.path.abspath(os.path.dirname(fullname)))
#os.chdir(os.path.commonpath()fullname.)
spec = spec_from_file_location(modulename, fullname)
mod = module_from_spec(spec)
#print("exec mod " + str(mod))
spec.loader.exec_module(mod)
d = mod.__dict__
keys = d.keys()
print("after exec")
print(keys)
mod_els = []
module_element_alias = []
module_funcs = []
if (inspect.ismodule(mod)):
#print(mod)
#print("... is a module")
pass
for e in keys:
if not "__" in e:
mod_els.append(d[e])
print(mod_els)
mymod_uses = ""
mymod_func = ""
for me in mod_els:
#print(me.__dict__.keys())
#print(dir(me))
if (inspect.isclass(me)):
#print("Class: ")
#print(me)
cl_alias = createClassPUMLCode(me, targetpath)
module_element_alias.append(cl_alias)
elif (inspect.ismodule(me)):
#print(me)
#print("... is a module used.")
# TODO: USES MODULE (import <mods>)
pass
elif (inspect.isfunction(me)):
#print(me)
if (me.__code__.co_filename == mod.__file__):
#print("... is my function.")
me_name = me.__name__
me_args = me.__code__.co_varnames
mymod_func = mymod_func + "\t\t+" + str(me_name) + str(me_args).replace("'", "").replace(",)", ")") + "\n"
module_funcs.append(me)
else:
#print("... is a function from: " + str(me.__module__))
mod_alias = str(mod.__name__).lower() + "Module"
relalias = "REL#" + mod_alias + "_USES_" + str(me.__name__) +"_FROM_" + str(me.__module__)
mymod_uses = mymod_uses + 'PUMLARelation(' + mod_alias + ', "..>", ' + str(me.__module__) + ', "uses: ' + str(me.__name__) + '", "' + relalias + '")\n'
mfc_alias = createModuleFunctionsPUMLCode(mod, module_funcs, targetpath)
if ((not (mfc_alias == None)) and (not (mfc_alias == ""))):
module_element_alias.append(mfc_alias)
createModulePUMLCore(mod, fname, module_element_alias, mymod_func, mymod_uses, targetpath)
def findPythonFiles(path):
""" find all pumla files in given path """
pythonfiles = []
blacklist = []
blacklistfilename = path + "/py2pumla_blacklist.txt"
#print(blacklistfilename)
if (os.path.isfile(blacklistfilename)):
#print("blacklist found\n")
file = open(blacklistfilename)
text = file.read()
#print(text)
file.close()
for li in text.split():
blacklist.append(path + li.strip("."))
#print(blacklist)
# walk through the file and folder structure
# and put all PUMLA files into a list
for dirpath, dirs, files in os.walk(path):
for filename in files:
if (not(isInBlacklist(dirpath, blacklist))):
#print(dirpath)
fname = os.path.join(dirpath, filename)
# a PUMLA file must end with '.puml' (see Modelling Guideline)
if fname.endswith('.py'):
pythonfiles.append(fname)
return pythonfiles
def executePumla(gendir):
#print("execute pumla")
cmd = "python3 " + pumla_path + "/pumla.py update"
oldpath = os.path.abspath(os.curdir)
os.chdir(gendir)
os.system(cmd)
os.chdir(oldpath)
def createAllElementsOverview(pumpath, targetpath):
dtxt = "@startuml\n!include modelrepo_json.puml\n"
dtxt = dtxt + "!include " + pumpath + "/pumla_macros.puml\n\n"
dtxt = dtxt + "scale 0.5\n\n"
dtxt = dtxt + "PUMLAPutAllElements()\nPUMLAPutAllStaticRelations()\n\n"
dtxt = dtxt + "@enduml\n\n"
tfn = targetpath + "/allElementsOverview.puml"
with open(tfn, "w") as fil:
fil.write(dtxt)
def parseSysArg(sysarg):
""" parses the given command line arguments """
# no parameter - default behaviour: show all pumla files in subdirs
if (len(sysarg) == 1):
pass
elif (len(sysarg) == 2):
pyfiles = findPythonFiles(sysarg[1])
pumla_gen_dir = sysarg[1] + "/_generated_p2p_pumla"
if (not(os.path.exists(pumla_gen_dir))):
os.mkdir(pumla_gen_dir)
for e in pyfiles:
py2pumla(e, pumla_gen_dir)
executePumla(pumla_gen_dir)
createAllElementsOverview(pumla_path, pumla_gen_dir)
else:
print("too much arguments... only one path to search for python files, please.")
if __name__ == "__main__":
identifyMe()
parseSysArg(sys.argv)