[dia] Added PHP to the list of languages supported by codegen.py.
- From: Steffen Macke <sdteffen src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [dia] Added PHP to the list of languages supported by codegen.py.
- Date: Tue, 8 May 2012 20:50:02 +0000 (UTC)
commit 31260b8420948700485832750ee0da7755dcab2d
Author: Steven Garcia <webwhammy gmail com>
Date: Tue May 8 22:44:55 2012 +0200
Added PHP to the list of languages supported by codegen.py.
plug-ins/python/codegen.py | 126 +++++++++++++++++++++++++++++++++++++++++---
1 files changed, 119 insertions(+), 7 deletions(-)
---
diff --git a/plug-ins/python/codegen.py b/plug-ins/python/codegen.py
index 6d439df..3face4b 100644
--- a/plug-ins/python/codegen.py
+++ b/plug-ins/python/codegen.py
@@ -28,8 +28,8 @@ class Klass :
self.parents = []
self.templates = []
self.inheritance_type = ""
- def AddAttribute(self, name, type, visibility, value, comment) :
- self.attributes.append ((name, (type, visibility, value, comment)))
+ def AddAttribute(self, name, type, visibility, value, comment, class_scope) :
+ self.attributes.append ((name, (type, visibility, value, comment, class_scope)))
def AddOperation(self, name, type, visibility, params, inheritance_type, comment, class_scope) :
self.operations.append((name,(type, visibility, params, inheritance_type, comment, class_scope)))
def SetComment(self, s) :
@@ -48,7 +48,7 @@ class ObjRenderer :
self.klasses = {}
self.arrows = []
self.filename = ""
-
+
def begin_render (self, data, filename) :
self.filename = filename
# not only reset the filename but also the other state, otherwise we would accumulate information through every export
@@ -79,7 +79,7 @@ class ObjRenderer :
# see objects/UML/umlattributes.c:umlattribute_props
#print "\t", attr[0], attr[1], attr[4]
# name, type, value, comment, visibility, abstract, class_scope
- k.AddAttribute(attr[0], attr[1], attr[4], attr[2], attr[3])
+ k.AddAttribute(attr[0], attr[1], attr[4], attr[2], attr[3], attr[6])
self.klasses[o.properties["name"].value] = k
#Connections
elif o.type.name == "UML - Association" :
@@ -167,7 +167,7 @@ class CxxRenderer(ObjRenderer) :
for so, (t, v, p, i, c, s) in k.operations :
ops[v].append((t,so,p,i,c))
vars = [[], [], [], []]
- for sa, (t, vi, va, vc) in k.attributes :
+ for sa, (t, vi, va, vc, class_scope) in k.attributes :
#TODO: use 'va'=value 'vc'=comment
vars[vi].append((t, sa))
visibilities = ("public:", "private:", "protected:", "/* implementation: */")
@@ -313,7 +313,7 @@ class PascalRenderer(ObjRenderer) :
for op_name, (op_type, op_visibility, op_params, op_inheritance, op_comment, op_class_scope) in k.operations :
ops[op_visibility].append((op_type, op_name, op_params, op_comment, op_inheritance))
vars = [[], [], [], []]
- for var_name, (var_type, var_visibility, var_value, var_comment) in k.attributes : # name, type, visibility, value, comment
+ for var_name, (var_type, var_visibility, var_value, var_comment, class_scope) in k.attributes : # name, type, visibility, value, comment
vars[var_visibility].append((var_type, var_name, var_value, var_comment))
visibilities = ("public", "private", "protected", "/* implementation */")
for v in [1,2,0,3] :
@@ -441,7 +441,7 @@ class JavaRenderer(ObjRenderer) :
f.write(" {\n")
# attributes
- for attrname, (type, visibility, value, comment) in klass.attributes :
+ for attrname, (type, visibility, value, comment, class_scope) in klass.attributes :
if comment:
f.write("\t/**\n")
for l in range(len(comment)/73+1):
@@ -516,8 +516,120 @@ class JavaRenderer(ObjRenderer) :
mainfile.close()
ObjRenderer.end_render(self)
+# PhpRenderer: export Dia UML diagram to PHP
+# Added by: Steven Garcia
+# This is similar to the Java renderer except for PHP
+# Added class scope (static) support
+class PhpRenderer(ObjRenderer) :
+ def __init__(self) :
+ ObjRenderer.__init__(self)
+
+ def end_render(self) :
+ visibilities = {0:"public", 1:"private", 2:"protected"}
+
+ mainfile = open(self.filename, "w")
+ mainfile.write("<?php\n/* Generated by dia/codegen.py\n *\n * Generated files:\n")
+
+ for name, klass in self.klasses.iteritems() :
+ # splits the classes in separate files
+ classfile = self.filename[:self.filename.rfind("/")+1] + name + ".php"
+ f = open(classfile, "w")
+ # class comment
+ f.write("<?php\n/**\n")
+ if len(klass.comment) > 1:
+ for l in range(len(klass.comment)/77+1):
+ f.write(" * %s\n" % klass.comment[l*77:(l+1)*77])
+ f.write(" * @author\n *\n */\n")
+
+ if klass.inheritance_type == "template": classtype = "interface"
+ elif klass.inheritance_type == "abstract": classtype = "abstract class"
+ else: classtype = "class"
+ f.write ("%s %s" % (classtype, name))
+ if klass.parents:
+ f.write (" extends %s" % klass.parents[0])
+ if klass.templates:
+ f.write (" implements %s" % ", ".join(klass.templates))
+ f.write("\n{\n")
+
+ # attributes
+ for attrname, (type, visibility, value, comment, class_scope) in klass.attributes :
+ if comment:
+ f.write("\t/**\n")
+ for l in range(len(comment)/73+1):
+ f.write("\t * %s\n" % comment[l*73:(l+1)*73])
+ f.write("\t */\n")
+ if visibility in visibilities:
+ vis = visibilities[visibility]
+ else: vis = ""
+ static = class_scope and "static " or ""
+ f.write("\t%s%s $%s" % (static, vis, attrname))
+ if value: f.write(" = %s" % value)
+ f.write(";\n")
+
+ # We should automatic implement abstract parent and interface methods
+ parmethods = []
+ if klass.parents:
+ parmethods = [(n,m[:3]+(1,)+m[4:]) for n,m in \
+ self.klasses[klass.parents[0]].operations if m[3] == 0]
+ for template in klass.templates:
+ parmethods.extend(self.klasses[template].operations)
+
+ for pName, pMethod in parmethods:
+ pTypes = [p[1] for p in pMethod[2]]
+ for name, pars in [(n,m[2]) for n,m in klass.operations]:
+ types = [p[1] for p in pars]
+ if pars == pMethod[2] and types == pTypes:
+ break
+ else: klass.operations.append((pName,pMethod))
+
+ for methodname, method in klass.operations :
+ # method comment
+ f.write("\t/**\n")
+ if method[4]:
+ for l in range(len(method[4])/73+1):
+ f.write("\t * %s\n" % method[4][l*73:(l+1)*73])
+ if method[2]:
+ for param in method[2]:
+ f.write("\t * @param %s\n" % param[0]) #param[0]->name
+ f.write("\t * @return %s\n\t */\n" % (method[0]=="" or method[0]))
+ # if there are no parameter names, something else should appear
+ pars = []
+ v = ord("a")
+ for name, type, value, comment, kind in method[2]:
+ #TODO: also use: kind
+ if not name:
+ name = chr(v)
+ v += 1
+ if value:
+ value = " = " + value
+ pars.append((type, '$' + name, value))
+ pars = ", ".join([type+" "+name+value for type, name, value in pars])
+
+ vis = method[1] in visibilities and visibilities[method[1]] or ""
+ return_type = method[0] == "" or method[0]
+ # TODO inheritance type is set to leaf/final by default in Dia. A normal type is need for the default
+ #inheritance_type = method[3] == 0 and "abstract " or method[3] == 2 and "final " or ""
+ inheritance_type = method[3] == 0 and "abstract " or ""
+ static = method[5] and "static " or ""
+ f.write("\t%s%s%sfunction %s(%s)" % (static, vis + ' ', inheritance_type, methodname, pars))
+ if klass.inheritance_type == "template" or method[3] == 0:
+ f.write(";\n\n")
+ else:
+ f.write("\n\t{\n\t\t// TODO Auto-generated method stub\n\t}\n\n")
+ f.write ("}\n\n")
+ f.close()
+ mainfile.write(" *\t%s\n" % classfile)
+ mainfile.write(" */\n")
+ mainfile.close()
+ ObjRenderer.end_render(self)
+
+ def fill_rect(*args):pass
+ def draw_line(*args):pass
+ def draw_string(*args):pass
+
# dia-python keeps a reference to the renderer class and uses it on demand
dia.register_export ("PyDia Code Generation (Python)", "py", PyRenderer())
dia.register_export ("PyDia Code Generation (C++)", "cxx", CxxRenderer())
dia.register_export ("PyDia Code Generation (Pascal)", "pas", PascalRenderer())
dia.register_export ("PyDia Code Generation (Java)", "java", JavaRenderer())
+dia.register_export ("PyDia Code Generation (PHP)", "php", PhpRenderer())
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]