/
pydiadoc.py
269 lines (256 loc) · 8.41 KB
/
pydiadoc.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
pydiadoc.py
# PyDia Self Documentation Series - Part I : PyDia itself
# Copyright (c) 2003, 2005 , 2009 Hans Breuer <hans@breuer.org>
#
# generates a new diagram which contains all objects
# of dir(dia). Now fills attributes and operations by using
# Python reflexion ...
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
import sys, math, dia, types, string
def distribute_objects (objs) :
width = 0.0
height = 0.0
for o in objs :
if width < o.properties["elem_width"].value :
width = o.properties["elem_width"].value
if height < o.properties["elem_height"].value :
height = o.properties["elem_height"].value
# add 20 % 'distance'
width *= 1.2
height *= 1.2
area = len (objs) * width * height
max_width = math.sqrt (area)
x = 0.0
y = 0.0
dy = 0.0 # used to pack small objects more tightly
for o in objs :
if dy + o.properties["elem_height"].value * 1.2 > height :
x += width
dy = 0.0
if x > max_width :
x = 0.0
y += height
o.move (x, y + dy)
dy += (o.properties["elem_height"].value * 1.2)
if dy > .75 * height :
x += width
dy = 0.0
if x > max_width :
x = 0.0
y += height
def find_class (data, name) :
"Find an UML-Class with the given class-name"
if not data :
return None
for la in data.layers :
for o in la.objects :
if o.type.name == 'UML - Class' and o.properties['name'].value == name :
return o
return None
def autodoc_fresh_cb (data, flags) :
return autodoc_cb(data, flags, 0)
def autodoc_update_cb (data, flags) :
return autodoc_cb(data, flags, data != None)
def autodoc_cb (data, flags, update) :
show_comments = 1 # maybe this should be selectable
if not data : # not set when called by the toolbox menu
diagram = dia.new("PyDiaObjects.dia")
# passed in data is not necessary valid - we are called from the Toolbox menu
data = diagram.data
display = diagram.display()
else :
diagram = None
display = None
layer = data.active_layer
oType = dia.get_object_type ("UML - Class")
theDir = dir(dia)
# for reflection we need some objects ...
theObjects = [data, layer, oType]
try :
theObjects.append (data.paper)
except AttributeError :
pass # no reason to fail with new bindings
if diagram :
theObjects.append (diagram)
if display :
theObjects.append (display)
# add some objects with interesting properties
#theObjects.append(dia.DiaImage())
once = 1
for s in ["Standard - Image", "Standard - BezierLine", "Standard - Text",
"UML - Class", "UML - Dependency"] :
o, h1, h2 = dia.get_object_type(s).create(0,0)
for p in o.properties.keys() :
v = o.properties[p].value
theObjects.append(v)
if type(v) is types.TupleType and len(v) > 0 :
theObjects.append(v[0])
if once :
theObjects.append(o)
theObjects.append(h1)
theObjects.append(o.bounding_box)
theObjects.append(o.connections[0])
theObjects.append(o.handles[0])
theObjects.append(o.properties)
theObjects.append(o.properties["obj_pos"])
once = 0
# o is leaked here
#print theObjects
theTypes = {}
for s in theDir :
if s == "_dia" :
continue # avoid all the messy details ;)
if theTypes.has_key(s) :
continue
for o in theObjects :
is_a = eval("type(o) is dia." + s)
#print s, o
if is_a :
theTypes[s] = o
break
if not theTypes.has_key (s) :
theTypes[s] = eval ("dia." + s)
# add UML classes for every object in dir
#print theTypes
fresh = [] # list of newly created objects
theGlobals = []
# remove all objects prefixed with '__'
for s in theDir :
if s[:2] == '__' or s[:6] == '_swig_' :
continue
if s == "__doc__" or s == "__name__" :
continue # hidden in the diagram objects but used below
doc = eval("dia." + s + ".__doc__")
is_a = eval("type(dia." + s + ") is types.BuiltinMethodType")
if is_a :
theGlobals.append((s,doc))
continue
# search the class we would create in the diagram and if found, modify rather than create
o = None
if update :
o = find_class(data, s)
h1 = h2 = None
if o :
show_comments = o.properties["visible_comments"].value
if not o :
o, h1, h2 = oType.create (0,0) # p.x, p.y
layer.add_object (o)
fresh.append(o)
if doc :
o.properties["comment"] = doc
# set the objects name
o.properties["name"] = s
# now populate the object with ...
if theTypes.has_key(s) :
t = theTypes[s]
# ... methods and ...
methods = []
# ... attributes
attributes = []
members = dir(t)
stereotype = ""
if "__getitem__" in members :
if "__len__" in members :
stereotype = "sequence"
elif "has_key" in members and "keys" in members :
stereotype = "dictionary"
for m in members :
# again ignoring underscore prefixed
if m[:2] == "__" or m == "thisown" : # ignore swig boilerplate
continue
try :
is_m = eval("callable(t." + m + ")")
except :
print "type(t." + m + ")?"
is_m = 0
doc = ""
tt = ""
if 0 : # does not work well enough, giving only sometimes 'int' and often 'property'?
try : # to detect the (return) type
if is_m :
oo = t()
tt = eval("oo." + m + "().__class__.__name__")
else :
tt = eval("t." + m + ".__class__.__name__")
except TypeError, msg :
print m, msg
except AttributeError, msg :
print m, msg # No constructor defined
try :
# try to get the member's docstring from the type
doc = eval("dia." + s + "." + m + ".__doc__")
# fallback to object?
#doc = eval("t." + m + ".__doc__")
except AttributeError :
doc = str(t) + "." + m
if is_m : # (name, type, comment, stereotype, visibility, inheritance_type, query,class_scope, params)
methods.append((m,tt,doc,'',0,0,0,0,()))
else : # (name,type,value,comment,visibility,abstract,class_scope)
attributes.append((m,tt,'',doc,0,0,0))
o.properties["operations"] = methods
o.properties["attributes"] = attributes
o.properties["comment_line_length"] = 120
o.properties["visible_comments"] = show_comments
if stereotype != "" :
o.properties["stereotype"] = stereotype
o = None
if update :
o = find_class(data, 'dia')
if o :
show_comments = o.properties["visible_comments"].value
else :
# build the module object
o, h1, h2 = oType.create (0,0) # p.x, p.y
layer.add_object (o)
# set the objects name
o.properties["name"] = "dia"
fresh.append(o)
o.properties["comment"] = eval("dia.__doc__")
o.properties["comment_line_length"] = 120
o.properties["visible_comments"] = show_comments
methods = []
for s in theGlobals :
if string.find(s[0], "swigregister") >= 0 :
continue # just noise
methods.append((s[0],'',s[1],'',0,0,0,1,()))
o.properties["operations"] = methods
# all objects got there bounding box, distribute the fresh ones
distribute_objects (fresh)
if diagram :
diagram.update_extents()
diagram.flush()
# work with bindings test
return data
def autodoc_html_cb (data, flags) :
import pydoc
import os
try :
path = os.environ["TEMP"]
except KeyError :
# with os.tmpnam() we get a RuntimeWarning so fall back to
path = "/tmp/"
os.chdir(path)
pydoc.writedoc(dia)
dia.message(0, path + os.path.sep + "dia.html saved.")
dia.register_action ("HelpPydia2", "PyDia HTML Docs",
"/ToolboxMenu/Help/HelpExtensionStart",
autodoc_html_cb)
dia.register_action ("HelpPydia", "PyDia Docs",
"/ToolboxMenu/Help/HelpExtensionStart",
autodoc_fresh_cb)
dia.register_action ("UpdatePydia", "PyDia Docs Update",
"/DisplayMenu/Help/HelpExtensionStart",
autodoc_update_cb)