dia r4322 - in trunk: . plug-ins/python



Author: hans
Date: Sun Mar 15 22:15:51 2009
New Revision: 4322
URL: http://svn.gnome.org/viewvc/dia?rev=4322&view=rev

Log:
2009-03-15  Hans Breuer  <hans breuer org>

	* plug-ins/python/pydiadoc.py : work better from toolbox and filter out
	the SWIG noise when running from the other bindings, some more objects


Modified:
   trunk/ChangeLog
   trunk/plug-ins/python/pydiadoc.py

Modified: trunk/plug-ins/python/pydiadoc.py
==============================================================================
--- trunk/plug-ins/python/pydiadoc.py	(original)
+++ trunk/plug-ins/python/pydiadoc.py	Sun Mar 15 22:15:51 2009
@@ -1,5 +1,5 @@
 #    PyDia Self Documentation Series - Part I : PyDia itself
-#    Copyright (c) 2003, 2005  Hans Breuer  <hans breuer org>
+#    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
@@ -20,7 +20,7 @@
 #   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
+import sys, math, dia, types, string
 
 def distribute_objects (objs) :
 	width = 0.0
@@ -37,15 +37,25 @@
 	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 :
-		o.move (x, y)
-		x += width
+		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 autodoc_cb (data, flags) :
-	if dia.active_display () :
+	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
@@ -60,8 +70,14 @@
 	theDir = dir(dia)
 	# for reflection we need some objects ...
 	theObjects = [data, layer, oType]
-	if diagram : theObjects.append (diagram)
-	if display : theObjects.append (display)
+	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
@@ -86,22 +102,32 @@
 	#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
+	#print theTypes
 
 	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))
-			#print s, doc
 			continue
 		o, h1, h2 = oType.create (0,0) # p.x, p.y
 		if doc :
@@ -116,27 +142,47 @@
 			methods = []
 			# ... attributes
 			attributes = []
-			for m in dir(t) :
-				if m[:2] == "__" :
+			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
-				#print s + "." + m
-				#tm = eval("t." + m)
-				#print tm
 				try :
-					is_m = eval("type(t." + m + ") is types.BuiltinMethodType")
-				except IndexError :
-					is_m = None
+					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 :
 					doc = eval("t." + m + ".__doc__")
 				except :
 					doc = str(t) + "." + m
 				if is_m : # (name, type, comment, stereotype, visibility, inheritance_type, query,class_scope, params)
-					methods.append((m,'',doc,'',0,0,0,0,()))
+					methods.append((m,tt,doc,'',0,0,0,0,()))
 				else : # (name,type,value,comment,visibility,abstract,class_scope)
-					attributes.append((m,'','',doc,0,0,0))
+					attributes.append((m,tt,'',doc,0,0,0))
 			o.properties["operations"] = methods
 			o.properties["attributes"] = attributes
+			if stereotype != "" :
+				o.properties["stereotype"] = stereotype
 	# build the module object
 	o, h1, h2 = oType.create (0,0) # p.x, p.y
 	layer.add_object (o)
@@ -145,15 +191,18 @@
 	o.properties["comment"] = eval("dia.__doc__")
 	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 them
 	distribute_objects (layer.objects)
 
-	data.update_extents ()
-	if diagram and display :
+	if diagram :
 		diagram.update_extents()
 		diagram.flush()
+	# work with bindings test
+	return data
 
 dia.register_action ("HelpPydia", "PyDia Docs", 
                        "/ToolboxMenu/Help/HelpExtensionStart", 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]