source: mauRepo/xmi2sqlalchemy/trunk/src/main/resources/velocity/python/pythonClass.vm @ 8156

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/xmi2sqlalchemy/trunk/src/main/resources/velocity/python/pythonClass.vm@8156
Revision 8156, 6.6 KB checked in by mnagni, 8 years ago (diff)

Improves the hash and corrects a bug in eq methods

  • Property svn:executable set to *
Line 
1##########################################################################
2## pythonClass.vm
3## The form expects the following context attributes
4## classModel - an instance of ClassModel class
5## imports    - a Map<String, Set<String>> defining the modules to import
6## stereotype - an instance of UMLClass
7##########################################################################
8
9#set ($object = "object")
10#set ($newline = '\n')
11#macro( superclass $cm, $stereotype)
12#set ($superclasses = $cm.parents)
13#if($superclasses.size() == 0 && !$stereotype)
14$object#elseif($stereotype && $superclasses.size() == 0)
15${stereotype.name}#elseif($superclasses.size() > 0)
16#foreach( $classModel in $vh.getOrderedParents($cm))
17${vh.getMappedClass(${classModel.associatedClass})}#if($foreach.hasNext),#end
18#end
19#end
20#end
21
22#macro( asEnumeration $asEnumeration)
23#foreach($item in $asEnumeration.simpleKeyValues)
24${tab}en_${vh.transformToPythonName($item.key)} = "${item.value}"
25#end
26#end
27
28#macro( asCodeList $asCodeList)
29#foreach($item in $asCodeList.simpleKeyValues)
30${tab}cl_${vh.transformToPythonName($item.key)} = "${item.value}"
31#end
32#end
33
34#macro( asUnion $classModel)
35${tab}types = {#unionDictionary($classModel.attributeModel)}
36#end
37
38#macro (unionDictionary $attributeModel)
39#foreach($item in $attributeModel)
40'${item.name}':${vh.getMappedClass(${item.associatedType.associatedClass})}#if($foreach.hasNext),#end 
41#end
42#end
43
44
45#macro( importing $toImport)
46#foreach( $key in $toImport.keySet())   
47from $key import #foreach( $item in $toImport.get($key)) $item#if($foreach.hasNext),#end #end
48
49#end
50#end
51
52#macro (getter $attr)
53#set ( $methodName = "$display.capitalize( $attr.name )")
54${tab}@property
55${tab}def ${attr.name}(self):   
56#if($attr.getParentName())
57${tab}${tab}return self._${attr.getParentName().toLowerCase()}.${attr.name}
58#elseif($attr.subTypes)
59#if($attr.hasMultiplicity())
60${tab}${tab}ret = []
61#foreach($type in $attr.subTypes)
62#set ( $assoClass = "$type.associatedClass.name.toLowerCase()")
63${tab}${tab}for item in self._${attribute.name}_$assoClass:
64${tab}${tab}${tab}ret.append(item)
65#end
66${tab}${tab}return ret
67#else
68#foreach($type in $attr.subTypes)
69#set ( $assoClass = "$type.associatedClass.name.toLowerCase()")
70${tab}${tab}if(has_value(self._${attribute.name}_$assoClass)):
71${tab}${tab}${tab}return self._${attribute.name}_$assoClass
72#end
73${tab}${tab}return #initAttributeLeft($attr)   
74#end   
75#end
76#end
77
78#macro(saSetter $type, $attr)
79#foreach($stype in $attr.subTypes)
80#if(!$stype.equals($type))
81${tab}${tab}${tab}self._${attr.name}_${stype.associatedClass.name.toLowerCase()} =#initAttributeLeft($stype)
82#end
83#end
84#end
85
86#macro(initAttributeLeft $attrL)
87#if($attrL.hasMultiplicity()) []
88#else None
89#end
90#end
91
92#macro (setter $attr)
93#set ( $methodName = "$display.capitalize( $attr.name )")
94${tab}@${attr.name}.setter
95${tab}def ${attr.name}(self, value):   
96#if($attr.getParentName())
97${tab}${tab}${tab}self._${attr.getParentName().toLowerCase()}.${attr.name} = value
98#elseif($attr.subTypes)
99#if($attr.hasMultiplicity())
100${tab}${tab}if isinstance(value, list):
101#foreach($type in $attr.subTypes)
102#set ( $assoClass = "$type.associatedClass.name")
103#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()")
104${tab}${tab}${tab}self._${attr.name}_${assoClassL}[:]
105#end
106${tab}${tab}${tab}for item in value:
107${tab}${tab}${tab}${tab}valueClass = "%s.%s" % (type(item).__module__,type(item).__name__)
108#foreach($type in $attr.subTypes)
109#set ( $assoClass = "$type.associatedClass.name")
110#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()")
111${tab}${tab}${tab}${tab}if valueClass == "${vh.getMappedModule($type.associatedClass)}.${assoClass}":
112${tab}${tab}${tab}${tab}${tab}self._${attr.name}_${assoClassL}.append(item)
113${tab}${tab}${tab}${tab}${tab}continue
114#end
115#else
116${tab}${tab}valueClass = "%s.%s" % (type(value).__module__,type(value).__name__)
117#foreach($type in $attr.subTypes)
118#set ( $assoClass = "$type.associatedClass.name")
119#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()")
120${tab}${tab}if valueClass == "${vh.getMappedModule($type.associatedClass)}.${assoClass}":
121${tab}${tab}${tab}self._${attr.name}_${assoClassL} = value
122#saSetter($type, $attr)
123#end   
124#end
125#end
126#end
127
128
129
130
131#macro ( dogettersetter $attributes)
132#foreach($attribute in $attributes)
133#if($attribute.getParentName() || $attribute.subTypes)
134${vh.showAttribute($attribute)}
135#getter($attribute)
136
137#setter($attribute)     
138#end
139#end
140#end
141
142#macro ( enlistKeys $attributes)
143#foreach($attribute in $attributes)
144self.${attribute.name}#if($foreach.hasNext), #end
145#end
146#end
147
148#macro( subTypes $attribute)
149#foreach($type in $attribute.subTypes)
150${tab}${tab}self._${attribute.name}_${type.associatedClass.name.toLowerCase()} =#initAttributeLeft($attribute)
151#end
152#end
153
154#macro( attributes $attributes, $parents)
155#if($parents)
156#foreach( $parent in $parents)
157${tab}${tab}self._${parent.associatedClass.name.toLowerCase()} = ${vh.getMappedClass($parent.associatedClass)}()       
158#end
159#end
160
161#foreach($attribute in $attributes)
162#if($attribute.subTypes)
163#subTypes($attribute)
164#else
165${tab}${tab}self.${attribute.name} =#initAttributeLeft($attribute)
166#end
167#end
168#end
169
170
171#importing($imports)
172from ascore.utils import has_value, collectionProofHash
173
174'''
175Created on $date
176
177@author: mnagni
178'''
179
180class ${classModel.associatedClass.name}(#superclass($classModel, $stereotype)):
181${tab}'''
182${tab}${tab}Represents a data entity defined in a UML diagram and supposed to
183${tab}${tab}be persisted in a relational database.
184 
185${tab}${tab}This class has been genererated automatically using the Apache Velocity project.
186${tab}'''
187#if($classModel.isEnumeration())
188#asEnumeration($classModel)
189#elseif ($classModel.isCodeList())
190#asCodeList($classModel)
191#elseif ($classModel.isUnion())
192#asUnion($classModel)
193#else
194${tab}'''
195${tab}${tab}Please note that any access to the inner attributes should be done using
196${tab}${tab}the given get/set methods and NOT accessing them directly.
197${tab}'''
198${tab}def __init__(self):
199#end
200#if (!($classModel.isEnumeration() || $classModel.isCodeList() || $classModel.isUnion()))
201#attributes($attributes, $classModel.parents)
202${tab}${tab}super(${classModel.associatedClass.name}, self).__init__()
203#dogettersetter($attributes)
204
205${tab}def __key(self):
206${tab}${tab}return (#enlistKeys($attributes))
207
208${tab}def __eq__(self, y):
209${tab}${tab}if type(self) != type(y):
210${tab}${tab}${tab}return False
211${tab}${tab}'''
212${tab}${tab}Cannot compare classes which do not define a "__key" attribute
213${tab}${tab}'''
214${tab}${tab}if hasattr(self, '_%s__key' % (type(self).__name__)) and hasattr(y, '_%s__key' % (type(y).__name__)):
215${tab}${tab}${tab}return self.__key() == y.__key()
216${tab}${tab}return id(self) == id(y)
217
218${tab}def __hash__(self):
219${tab}${tab}return collectionProofHash(self.__key())
220#end
Note: See TracBrowser for help on using the repository browser.