Example #1
0
	def __init__(self, name, diagramData = None, doc = None) :
		self.layers = {}
		self.name = name

		if not diagramData :
			self.diagram = dia.new(self.name + ".dia")
			self.data = self.diagram.data
			self.display = self.diagram.display()
		else :
			for diagram in dia.diagrams() :
				if diagram.data == diagramData :
					self.diagram = diagram
			self.data = diagramData
			try:
				self.display = self.diagram.displays[0]
			except:
				self.display = self.diagram.display()
		if doc:
			self.add_document(doc)
		else:
			self.doc = edmx.Document()
			root = self.doc.ChildElement(edmx.Edmx)
			dataservices = root.ChildElement(edmx.DataServices)
			version = dataservices.DataServiceVersion()
			if not version :
				dataservices.SetAttribute("DataServiceVersion","2.0")
		self.add_handlers()
Example #2
0
	def setUp(self):
		'called multiple times, before every test method'
		self.schema = self.files["metadata"].root.DataServices.Schema[0]
		self.diagram = dia.new("metadata")
		schemaName = self.schema.GetAttribute(('~','Namespace'))
		self.layer = self.diagram.data.add_layer(schemaName)
		self.logPoint()
Example #3
0
def newDiagram(modelName ) :
    
#   diagram = dia.active_display().diagram

    diagram = dia.new( modelName)
    data = diagram.data
#   layer = data.active_layer
    display = diagram.display()
        
    return data
Example #4
0
	def createCuonObject(self,st,cx,cy):


		diagram = dia.new("Cuon Objects.dia")
		data = diagram.data
		#layer = data.active_layer
		layer = data.add_layer ('Network')
		print layer
		o, h1, h2 = dia.get_object_type(st).create (cx, cy)
		print 'o = ',  o
		w = o.bounding_box.right - o.bounding_box.left
		h = o.bounding_box.bottom - o.bounding_box.top
		o.move (cx, cy)               
		print 'move',  cx,  cy
		layer.add_object (o)
		layer.update_extents()
		data.update_extents()
		print 'data updated'
		if diagram :
			print 'show diagram'
			diagram.display()
			diagram.flush()
Example #5
0
def allprops_cb(data, flags):

    # copied from otypes.py
    if data:
        diagram = None  # we may be running w/o GUI
    else:
        diagram = dia.new("All Object Properties.dia")
        data = diagram.data
    layer = data.active_layer

    props_by_name = {}
    otypes = dia.registered_types()
    name_type_clashes = []

    for oname in otypes:
        try:
            obj, h1, h2 = dia.get_object_type(oname).create(0, 0)
        except:
            print "Huh?", oname
            continue
        prop_keys = obj.properties.keys()
        for k in prop_keys:
            p = obj.properties[k]
            if props_by_name.has_key(k):
                # check if it is the same type
                p0, names = props_by_name[k]
                try:
                    if p0.type != p.type:
                        # construct a unique name
                        uname = p.name + "<" + p.type + ">"
                        if props_by_name.has_key(uname):
                            props_by_name[uname][1].append(oname)
                        else:
                            props_by_name[uname] = (p, [oname])
                            name_type_clashes.append(oname + "::" + p.name +
                                                     " as " + p0.type +
                                                     " and " + p.type)
                    else:
                        # remember the origin of the property
                        props_by_name[k][1].append(oname)
                except KeyError:
                    print oname, "::", k, p, "?"
            else:
                props_by_name[k] = (p, [oname])
        obj.destroy(
        )  # unsave delete, any method call will fail/crash afterweards
        obj = None

    grid = {}  # one type per row
    dx = 1.0
    dy = 5.0
    ot = dia.get_object_type("UML - Class")

    props_keys = props_by_name.keys()
    # alpha-numeric sorting by type; after by number of users
    props_keys.sort(
        lambda a, b: len(props_by_name[b][1]) - len(props_by_name[a][1]))
    props_keys.sort(
        lambda a, b: cmp(props_by_name[a][0].type, props_by_name[b][0].type))

    almost_all = 98 * len(otypes) / 100  # 98 %

    for pname in props_keys:

        p, names = props_by_name[pname]

        x = 0.0
        y = 0.0
        if grid.has_key(p.type):
            x, y = grid[p.type]
        else:
            x = 0.0
            y = len(grid.keys()) * dy
        o, h1, h2 = ot.create(x, y)
        o.properties["name"] = pname
        o.properties["template"] = 1
        o.properties["templates"] = [(p.type, '')]
        # coloring depending on use
        if len(names) > almost_all:
            o.properties["fill_colour"] = "lightgreen"
        elif len(names) > 100:
            o.properties["fill_colour"] = "lightblue"
        elif len(names) > 2:
            o.properties["fill_colour"] = "lightcyan"
        elif len(names) > 1:
            o.properties["fill_colour"] = "lightyellow"
        # if there is only one user show it
        if len(names) == 1:
            o.properties["comment"] = names[0]
            o.properties["visible_comments"] = 1
            o.properties["comment_line_length"] = 60
        else:
            o.properties["comment"] = string.join(names, "; ")
            o.properties["visible_comments"] = 0
            o.properties["comment_line_length"] = 60

        # store position for next in row
        x += (dx + o.properties["elem_width"].value)
        grid[p.type] = (x, y)

        layer.add_object(o)
    layer.update_extents()
    data.update_extents()
    if diagram:
        diagram.display()
        diagram.flush()
    if len(name_type_clashes) > 0:
        dia.message(
            0, "One name, one type?!\n" + string.join(name_type_clashes, "\n"))
    return data
Example #6
0
def autodoc_cb(module_name):
    diagram_name = module_name + '.dia'
    diagram = dia.new(diagram_name)
    data = diagram.data
    display = diagram.display()

    layer = data.active_layer

    oType = dia.get_object_type('UML - Class')

    module = __import__(module_name)
    theDir = dir(module)
    # 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 module.' + s)
            #print s, o
            if is_a:
                theTypes[s] = o
                break
        if not theTypes.has_key(s):
            theTypes[s] = eval('module.' + s)
    # add UML classes for every object in dir
    #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('module.' + s + '.__doc__')
        is_a = eval('type(module.' + s + ') is types.BuiltinMethodType')
        if is_a:
            theGlobals.append((s, doc))
            continue
        o, h1, h2 = oType.create(0, 0)  # p.x, p.y
        if doc:
            o.properties['comment'] = doc
        layer.add_object(o)
        # 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:
                    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, 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
            if stereotype != '':
                o.properties['stereotype'] = stereotype
Example #7
0
def otypes_cb(data, flags) :

	if data:
		diagram = None # we may be running w/o GUI
	else :
		diagram = dia.new("Object Types.dia")
		data = diagram
	layer = data.active_layer

	otypes = dia.registered_types()
	keys = otypes.keys()
	keys.sort()

	# property keys w/o overlap
	object_props = ["obj_pos", "obj_bb", "meta"]
	element_props = ["elem_corner", "elem_width", "elem_height"]
	orthconn_props = ["orth_points", "orth_orient", "orth_autoroute"]
	shape_props = ["flip_horizontal", "flip_vertical"]
	# the following are not exclusuve to any objects type
	line_props = ["line_width", "line_style", "line_colour"]
	fill_props = ["fill_colour", "show_background"]
	text_props = ["text_colour", "text_font", "text_height", "text"] # "text_alignment", "text_pos"

	packages = {}
	for s in keys :
		kt = string.split(s, " - ")
		if len(kt) == 2 :
			if len(kt[0]) == 0 :
				sp = "<unnamed>"
			else :
				sp = kt[0]
			st = kt[1]
		else :
			sp = "<broken>"
			st = kt[0]
		if packages.has_key(sp) :
			packages[sp].append(st)
		else :
			packages[sp] = [st]

	dtp = dia.get_object_type("UML - LargePackage")
	dtc = dia.get_object_type("UML - Class")
	cy = 0
	maxy = 0
	maxx = 0

	for sp in packages.keys() :
		pkg = packages[sp]
		op, h1, h2 = dtp.create(0.0, cy + 1.0)
		op.properties["name"] = sp
		layer.add_object(op)
		cx = 0
		for st in pkg :
			if st == "Group" :
				continue # too special to handle
			oc, h3, h4 = dtc.create(cx + 1.0, cy + 4.0)
			oc.properties["name"] = st
			attrs = []
			# we detect inheritance by common props
			n_object = 0
			n_element = 0
			n_orthconn = 0
			n_shape = 0
			n_line = 0
			n_fill = 0
			n_text = 0
			if otypes.has_key(st) :
				o_real, h5, h6 = dia.get_object_type(st).create(0,0)
			elif otypes.has_key(sp + " - " + st) :
				o_real, h5, h6 = dia.get_object_type(sp + " - " + st).create(0,0)
			else :
				o_real = None
				print "Failed to create object", sp, st
			formal_params = []
			if not o_real is None :
				for p in o_real.properties.keys() :
					if p in object_props : n_object = n_object + 1
					elif p in orthconn_props : n_orthconn = n_orthconn + 1
					elif p in element_props : n_element = n_element + 1
					elif p in shape_props : n_shape = n_shape + 1
					elif p in line_props : n_line = n_line + 1
					elif p in text_props : n_text = n_text + 1
					elif p in fill_props : n_fill = n_fill + 1
					else : # don't replicate common props
						attrs.append((p, o_real.properties[p].type, '', '', 0, 0, 0))
				if n_line == len(line_props) :
					formal_params.append(('Line', ''))
				else : # need to add the incomplete set
					for pp in line_props :
						if o_real.properties.has_key(pp) :
							attrs.append((pp, o_real.properties[pp].type, '', '', 0, 0, 0))
				if n_fill == len(fill_props) :
					formal_params.append(('Fill', ''))
				else :
					for pp in fill_props :
						if o_real.properties.has_key(pp) :
							attrs.append((pp, o_real.properties[pp].type, '', '', 0, 0, 0))
				if n_text == len(text_props) :
					formal_params.append(('Text', ''))
				else :
					for pp in text_props :
						if o_real.properties.has_key(pp) :
							attrs.append((pp, o_real.properties[pp].type, '', '', 0, 0, 0))
			if n_orthconn == len(orthconn_props) :
				oc.properties["stereotype"] = "OrthConn"
				oc.properties["fill_colour"] = "light blue"
			elif n_shape == len(shape_props) :
				oc.properties["stereotype"] = "Shape"
				oc.properties["fill_colour"] = "light cyan"
			elif n_element == len(element_props) :
				oc.properties["stereotype"] = "Element"
				oc.properties["fill_colour"] = "light yellow"
			elif n_object == len(object_props) :
				oc.properties["stereotype"] = "Object"
			else :
				print "Huh?", st
				oc.properties["fill_colour"] = "red"
			oc.properties["attributes"] = attrs
			if len(formal_params) > 0 :
				oc.properties["template"] = 1
				oc.properties["templates"] = formal_params
			layer.add_object(oc)
			# XXX: there really should be a way to safely delete an object. This one will crash:
			# - when the object got added somewhere
			# - any object method gets called afterwards
			if not o_real is None :
				o_real.destroy()
				del o_real
			cx = oc.bounding_box.right
			if maxy < oc.bounding_box.bottom :
				maxy = oc.bounding_box.bottom
			if maxx < cx :
				maxx = cx
			# wrapping too long lines
			if cx > 300 :
				cx = 0
				cy = maxy
		h = op.handles[7]
		# adjust the package size to fit the objects
		op.move_handle(h,(maxx + 1.0, maxy + 1.0), 0, 0)
		cy = maxy + 2.0
		maxx = 0 # every package a new size
	data.update_extents()
	if diagram :
		diagram.display()
		diagram.flush()
	# make it work standalone
	return data
Example #8
0
def aobjects_cb(data, flags):

    # copied from otypes.py
    if data:
        diagram = None  # we may be running w/o GUI
    else:
        diagram = dia.new("All Objects.dia")
        data = diagram
    layer = data.active_layer

    otypes = dia.registered_types()
    keys = otypes.keys()
    keys.sort()

    packages = {}
    for s in keys:
        kt = string.split(s, " - ")
        if len(kt) == 2:
            if len(kt[0]) == 0:
                sp = "<unnamed>"
            else:
                sp = kt[0]
            st = kt[1]
        else:
            sp = "<broken>"
            st = kt[0]
        if packages.has_key(sp):
            packages[sp].append(s)
        else:
            packages[sp] = [s]

    for sp in packages.keys():
        # add a layer per package
        layer = data.add_layer(sp)

        cx = 0.0
        cy = 0.0
        n = 0  # counting objects
        my = 0.0
        pkg = packages[sp]
        for st in pkg:
            if st == "Group":
                continue  # can't create empty group
            #print st
            o, h1, h2 = dia.get_object_type(st).create(cx, cy)
            # to make the resulting diagram more interesting we set every sting property with it's name
            set_object_string(o)
            w = o.bounding_box.right - o.bounding_box.left
            h = o.bounding_box.bottom - o.bounding_box.top
            o.move(cx, cy)
            cx += w * 1.5
            if h > my: my = h
            n += 1
            if n % 10 == 0:
                cx = 0.0
                cy += my * 1.5
                my = 0
            layer.add_object(o)
        layer.update_extents()
    data.update_extents()
    if diagram:
        diagram.display()
        diagram.flush()
    return data
Example #9
0
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
Example #10
0
def aobjects_cb(data, flags) :

	# copied from otypes.py
	if data :
		diagram = None # we may be running w/o GUI
	else :
		diagram = dia.new("All Objects.dia")
		data = diagram.data
	layer = data.active_layer

	otypes = dia.registered_types()
	keys = otypes.keys()
	keys.sort()

	packages = {}
	for s in keys :
		kt = string.split(s, " - ")
		if len(kt) == 2 :
			if len(kt[0]) == 0 :
				sp = "<unnamed>"
			else :
				sp = kt[0]
			st = kt[1]
		else :
			sp = "<broken>"
			st = kt[0]
		if packages.has_key(sp) :
			packages[sp].append(s)
		else :
			packages[sp] = [s] 

	for sp in packages.keys() :
		# add a layer per package
		layer = data.add_layer (sp)

		cx = 0.0
		cy = 0.0
		n = 0 # counting objects
		my = 0.0
		pkg = packages[sp]
		for st in pkg :
			if st == "Group" :
				continue # can't create empty group
			#print st
			o, h1, h2 = dia.get_object_type(st).create (cx, cy)
			# to make the resulting diagram more interesting we set every sting property with it's name
			set_object_string (o)
			w = o.bounding_box.right - o.bounding_box.left
			h = o.bounding_box.bottom - o.bounding_box.top
			o.move (cx, cy)
			cx += w * 1.5
			if h > my : my = h
			n += 1
			if n % 10 == 0 :
				cx = 0.0
				cy += my * 1.5
				my = 0
			layer.add_object (o)
		layer.update_extents()
	data.update_extents()
	if diagram :
		diagram.display()
		diagram.flush()
	return data
Example #11
0
def allprops_cb(data, flags) :

	# copied from otypes.py
	if data :
		diagram = None # we may be running w/o GUI
	else :
		diagram = dia.new("All Object Properties.dia")
		data = diagram.data
	layer = data.active_layer

	props_by_name = {}
	otypes = dia.registered_types()
	name_type_clashes = []

	for oname in otypes :
		try :
			obj, h1, h2 = dia.get_object_type(oname).create (0, 0)
		except :
			print "Huh?", oname
			continue
		prop_keys = obj.properties.keys()
		for k in prop_keys :
			p = obj.properties[k]
			if props_by_name.has_key(k) :
				# check if it is the same type
				p0, names = props_by_name[k]
				try :
					if p0.type != p.type :
						# construct a unique name
						uname = p.name + "<" + p.type + ">"
						if props_by_name.has_key(uname) :
							props_by_name[uname][1].append(oname)
						else :
							props_by_name[uname] = (p, [oname])
							name_type_clashes.append (oname + "::" + p.name + " as " + p0.type + " and " + p.type)
					else :
						# remember the origin of the property
						props_by_name[k][1].append(oname)
				except KeyError :
					print oname, "::", k, p, "?"
			else :
				props_by_name[k] = (p, [oname])
		obj.destroy() # unsave delete, any method call will fail/crash afterweards
		obj = None

	grid = {} # one type per row
	dx = 1.0
	dy = 5.0
	ot = dia.get_object_type("UML - Class")

	props_keys = props_by_name.keys()
	# alpha-numeric sorting by type; after by number of users
	props_keys.sort (lambda a,b : len(props_by_name[b][1]) - len(props_by_name[a][1]))
	props_keys.sort (lambda a,b : cmp(props_by_name[a][0].type, props_by_name[b][0].type))
	
	almost_all = 98 * len(otypes) / 100 # 98 %

	for pname in props_keys :
	
		p, names = props_by_name[pname]

		x = 0.0
		y = 0.0
		if grid.has_key(p.type) :
			x, y = grid[p.type]
		else :
			x = 0.0
			y = len(grid.keys()) * dy
		o, h1, h2 = ot.create (x,y)
		o.properties["name"] = pname
		o.properties["template"] = 1
		o.properties["templates"] = [(p.type, '')]
		# coloring depending on use
		if len(names) > almost_all :
			o.properties["fill_colour"] = "lightgreen"
		elif len(names) > 100 :
			o.properties["fill_colour"] = "lightblue"
		elif len(names) > 2 :
			o.properties["fill_colour"] = "lightcyan"
		elif len(names) > 1 :
			o.properties["fill_colour"] = "lightyellow"
		# if there is only one user show it
		if len(names) == 1 :
			o.properties["comment"] = names[0]
			o.properties["visible_comments"] = 1
			o.properties["comment_line_length"] = 60
		else :
			o.properties["comment"] = string.join(names, "; ")
			o.properties["visible_comments"] = 0
			o.properties["comment_line_length"] = 60

		# store position for next in row
		x += (dx + o.properties["elem_width"].value)
		grid[p.type] = (x,y)

		layer.add_object(o)
	layer.update_extents()
	data.update_extents()
	if diagram :
		diagram.display()
		diagram.flush()
	if len(name_type_clashes) > 0 :
		dia.message(0, "One name, one type?!\n" + string.join(name_type_clashes, "\n"))
	return data
Example #12
0
def autodoc_cb(module_name):
	diagram_name = module_name + '.dia'
	diagram = dia.new(diagram_name)
	data = diagram.data
	display = diagram.display()

	layer = data.active_layer

	oType = dia.get_object_type('UML - Class')		
	
	module = __import__(module_name)
	theDir = dir(module)
	# 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 module.' + s)
			#print s, o
			if is_a:
				theTypes[s] = o
				break
		if not theTypes.has_key (s):
			theTypes[s] = eval ('module.' + s)
	# add UML classes for every object in dir
	#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('module.' + s + '.__doc__')
		is_a = eval('type(module.' + s + ') is types.BuiltinMethodType')
		if is_a:
			theGlobals.append((s,doc))
			continue
		o, h1, h2 = oType.create (0,0) # p.x, p.y
		if doc:
			o.properties['comment'] = doc
		layer.add_object (o)
		# 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:
					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,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
			if stereotype != '':
				o.properties['stereotype'] = stereotype
Example #13
0
def otypes_cb(data, flags) :

	if data :
		diagram = None # we may be running w/o GUI
	else :
		diagram = dia.new("Object Types.dia")
		data = diagram.data
	layer = data.active_layer

	otypes = dia.registered_types()
	keys = otypes.keys()
	keys.sort()

	# property keys w/o overlap
	object_props = ["obj_pos", "obj_bb", "meta"]
	element_props = ["elem_corner", "elem_width", "elem_height"]
	orthconn_props = ["orth_points", "orth_orient", "orth_autoroute"]
	shape_props = ["flip_horizontal", "flip_vertical"]
	# the following are not exclusuve to any objects type
	line_props = ["line_width", "line_style", "line_colour"]
	fill_props = ["fill_colour", "show_background"]
	text_props = ["text_colour", "text_font", "text_height", "text"] # "text_alignment", "text_pos"

	packages = {}
	for s in keys :
		kt = string.split(s, " - ")
		if len(kt) == 2 :
			if len(kt[0]) == 0 :
				sp = "<unnamed>"
			else :
				sp = kt[0]
			st = kt[1]
		else :
			sp = "<broken>"
			st = kt[0]
		if packages.has_key(sp) :
			packages[sp].append(st)
		else :
			packages[sp] = [st] 

	dtp = dia.get_object_type("UML - LargePackage")
	dtc = dia.get_object_type("UML - Class")
	cy = 0
	maxy = 0
	maxx = 0

	for sp in packages.keys() :
		pkg = packages[sp]
		op, h1, h2 = dtp.create(0.0, cy + 1.0)
		op.properties["name"] = sp
		layer.add_object(op)
		cx = 0
		for st in pkg :
			if st == "Group" :
				continue # too special to handle
			oc, h3, h4 = dtc.create(cx + 1.0, cy + 4.0)
			oc.properties["name"] = st
			attrs = []
			# we detect inheritance by common props
			n_object = 0
			n_element = 0
			n_orthconn = 0
			n_shape = 0
			n_line = 0
			n_fill = 0
			n_text = 0
			if otypes.has_key(st) :
				o_real, h5, h6 = dia.get_object_type(st).create(0,0)
			elif otypes.has_key(sp + " - " + st) :
				o_real, h5, h6 = dia.get_object_type(sp + " - " + st).create(0,0)
			else :
				o_real = None
				print "Failed to create object", sp, st
			formal_params = []
			if not o_real is None :
				for p in o_real.properties.keys() :
					if p in object_props : n_object = n_object + 1
					elif p in orthconn_props : n_orthconn = n_orthconn + 1
					elif p in element_props : n_element = n_element + 1
					elif p in shape_props : n_shape = n_shape + 1
					elif p in line_props : n_line = n_line + 1
					elif p in text_props : n_text = n_text + 1
					elif p in fill_props : n_fill = n_fill + 1
					else : # don't replicate common props
						attrs.append((p, o_real.properties[p].type, '', '', 0, 0, 0))
				if n_line == len(line_props) :
					formal_params.append(('Line', ''))
				else : # need to add the incomplete set
					for pp in line_props : 
						if o_real.properties.has_key(pp) :
							attrs.append((pp, o_real.properties[pp].type, '', '', 0, 0, 0))
				if n_fill == len(fill_props) :
					formal_params.append(('Fill', ''))
				else :
					for pp in fill_props : 
						if o_real.properties.has_key(pp) :
							attrs.append((pp, o_real.properties[pp].type, '', '', 0, 0, 0))
				if n_text == len(text_props) :
					formal_params.append(('Text', ''))
				else :
					for pp in text_props : 
						if o_real.properties.has_key(pp) :
							attrs.append((pp, o_real.properties[pp].type, '', '', 0, 0, 0))
			if n_orthconn == len(orthconn_props) :
				oc.properties["stereotype"] = "OrthConn"
				oc.properties["fill_colour"] = "light blue"
			elif n_shape == len(shape_props) :
				oc.properties["stereotype"] = "Shape"
				oc.properties["fill_colour"] = "light cyan"
			elif n_element == len(element_props) :
				oc.properties["stereotype"] = "Element"
				oc.properties["fill_colour"] = "light yellow"
			elif n_object == len(object_props) :
				oc.properties["stereotype"] = "Object"
			else :
				print "Huh?", st
				oc.properties["fill_colour"] = "red"
			oc.properties["attributes"] = attrs
			if len(formal_params) > 0 :
				oc.properties["template"] = 1
				oc.properties["templates"] = formal_params
			layer.add_object(oc)
			# XXX: there really should be a way to safely delete an object. This one will crash:
			# - when the object got added somewhere 
			# - any object method gets called afterwards
			if not o_real is None :
				o_real.destroy()
				del o_real
			cx = oc.bounding_box.right
			if maxy < oc.bounding_box.bottom :
				maxy = oc.bounding_box.bottom
			if maxx < cx :
				maxx = cx
			# wrapping too long lines
			if cx > 300 :
				cx = 0
				cy = maxy
		h = op.handles[7]
		# adjust the package size to fit the objects
		op.move_handle(h,(maxx + 1.0, maxy + 1.0), 0, 0)
		cy = maxy + 2.0
		maxx = 0 # every package a new size
	data.update_extents()
	if diagram :
		diagram.display()
		diagram.flush()
	# make it work standalone
	return data
Example #14
0
def autodoc_cb (data, flags) :
	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

	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
		o, h1, h2 = oType.create (0,0) # p.x, p.y
		if doc :
			o.properties["comment"] = doc
		layer.add_object (o)
		# 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 :
					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,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
			if stereotype != "" :
				o.properties["stereotype"] = stereotype