Ignore:
Timestamp:
17/04/12 16:47:01 (8 years ago)
Author:
mnagni
Message:

Creates cleaner python classes and more able to manage lists

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mauRepo/xmi2sqlalchemy/trunk/src/main/resources/velocity/python/pythonClass.vm

    r8156 r8243  
    22## pythonClass.vm 
    33## The form expects the following context attributes 
    4 ## classModel - an instance of ClassModel class 
     4## classModel - an instance of PythonClassModel class 
    55## imports    - a Map<String, Set<String>> defining the modules to import 
    6 ## stereotype - an instance of UMLClass 
     6## stereotype - an instance of UMLClass representing the classModel 
     7## attributes - a Set<PythonAttributeModel> 
    78########################################################################## 
    89 
     
    5859#elseif($attr.subTypes) 
    5960#if($attr.hasMultiplicity()) 
    60 ${tab}${tab}ret = [] 
     61${tab}${tab}try: 
     62${tab}${tab}${tab}ret = [] 
    6163#foreach($type in $attr.subTypes) 
    6264#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 
     65${tab}${tab}${tab}ret.extend(self._${attribute.name}_$assoClass) 
     66#end 
     67${tab}${tab}${tab}if not hasattr(self, '_${attr.name}'): 
     68${tab}${tab}${tab}${tab}self._${attr.name} = [] 
     69${tab}${tab}${tab}if self._${attr.name}: 
     70${tab}${tab}${tab}${tab}ret.extend(self._${attr.name}) 
     71${tab}${tab}${tab}del self._${attr.name}[:] 
     72${tab}${tab}${tab}self._${attr.name}.extend(set(ret)) 
     73${tab}${tab}except Exception as e: 
     74${tab}${tab}${tab}pass 
     75${tab}${tab}return self._${attr.name} 
    6776#else 
    6877#foreach($type in $attr.subTypes) 
     
    91100 
    92101#macro (setter $attr) 
    93 #set ( $methodName = "$display.capitalize( $attr.name )") 
     102#if($attr.getParentName()) 
    94103${tab}@${attr.name}.setter 
    95104${tab}def ${attr.name}(self, value):     
    96 #if($attr.getParentName()) 
    97105${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 
     106#elseif($attr.subTypes && !$attr.hasMultiplicity()) 
     107${tab}@${attr.name}.setter 
     108${tab}def ${attr.name}(self, value):     
    116109${tab}${tab}valueClass = "%s.%s" % (type(value).__module__,type(value).__name__) 
    117110#foreach($type in $attr.subTypes) 
     
    120113${tab}${tab}if valueClass == "${vh.getMappedModule($type.associatedClass)}.${assoClass}": 
    121114${tab}${tab}${tab}self._${attr.name}_${assoClassL} = value 
     115## Reinitializes all the other subTypes 
    122116#saSetter($type, $attr)  
     117#end 
    123118#end     
    124119#end 
    125 #end 
    126 #end 
    127  
    128  
     120 
     121 
     122#macro ( synchronize $attributes, $parents) 
     123${tab}def synchronize(self): 
     124##if($parents) 
     125##foreach( $parent in $parents) 
     126##${tab}${tab}self._${parent.associatedClass.name.toLowerCase()}.synchronize()   
     127##end 
     128##end 
     129#foreach($attr in $attributes) 
     130#if(!$attr.getParentName() && $attr.hasMultiplicity()) 
     131#foreach($type in $attr.subTypes) 
     132#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()") 
     133${tab}${tab}del self._${attr.name}_${assoClassL}[:] 
     134#end 
     135#end 
     136#end 
     137#foreach($attr in $attributes) 
     138#if(!$attr.getParentName() && $attr.hasMultiplicity() && $attr.subTypes) 
     139${tab}${tab}for item in self.${attr.name}:       
     140#foreach($type in $attr.subTypes) 
     141#set ( $assoClassL = "$type.associatedClass.name.toLowerCase()") 
     142${tab}${tab}${tab}valueClass = "%s.%s" % (type(item).__module__,type(item).__name__) 
     143${tab}${tab}${tab}if valueClass == "${vh.getMappedModule($type.associatedClass)}.${type.associatedClass.name}": 
     144${tab}${tab}${tab}${tab}self._${attr.name}_${assoClassL}.append(item) 
     145${tab}${tab}${tab}${tab}continue 
     146#end 
     147#end 
     148#end 
     149${tab}${tab}pass 
     150#end 
    129151 
    130152 
     
    132154#foreach($attribute in $attributes) 
    133155#if($attribute.getParentName() || $attribute.subTypes) 
    134 ${vh.showAttribute($attribute)} 
    135156#getter($attribute) 
    136157 
     
    147168 
    148169#macro( subTypes $attribute) 
     170## Generates the hidden objects attributes used by the RDBM 
     171#if($attr.getParentName()) 
     172#else 
    149173#foreach($type in $attribute.subTypes) 
    150174${tab}${tab}self._${attribute.name}_${type.associatedClass.name.toLowerCase()} =#initAttributeLeft($attribute) 
    151175#end 
     176#end 
     177## Generates the hidden dummy object oriented instance of the attribute 
     178${tab}${tab}self._${attribute.name} =#initAttributeLeft($attribute) 
    152179#end 
    153180 
     
    160187 
    161188#foreach($attribute in $attributes) 
    162 #if($attribute.subTypes) 
     189#if($attribute.subTypes && !$attribute.getParentName()) 
    163190#subTypes($attribute) 
    164191#else 
     192#if(($attribute.getParentName() && !$attribute.hasMultiplicity()) || !$attribute.getParentName()) 
    165193${tab}${tab}self.${attribute.name} =#initAttributeLeft($attribute) 
     194#end 
    166195#end 
    167196#end 
     
    202231${tab}${tab}super(${classModel.associatedClass.name}, self).__init__() 
    203232#dogettersetter($attributes) 
     233 
     234#synchronize($attributes, $classModel.parents) 
    204235 
    205236${tab}def __key(self): 
Note: See TracChangeset for help on using the changeset viewer.