Adding members to classes for automatically generate infos and set the value of an element with its path
authorlbessard
Tue, 04 Sep 2007 17:12:48 +0200
changeset 85 fd17b0e0fd7e
parent 84 0369ad49e67f
child 86 4f1dbdb0bed2
Adding members to classes for automatically generate infos and set the value of an element with its path
xmlclass/xmlclass.py
--- a/xmlclass/xmlclass.py	Tue Sep 04 17:08:12 2007 +0200
+++ b/xmlclass/xmlclass.py	Tue Sep 04 17:12:48 2007 +0200
@@ -552,7 +552,9 @@
                 classmembers["__init__"] = generateInitMethod(bases, initialValues)
                 classmembers["loadXMLTree"] = generateLoadXMLTree(bases, members, self.ComputedClasses)
                 classmembers["generateXMLText"] = generateGenerateXMLText(bases, members)
-                classmembers["getElementAttributes"] = generateGetElementAttributes(bases, members, self.ComputedClasses)
+                classmembers["getElementAttributes"] = generateGetElementAttributes(members, self.ComputedClasses)
+                classmembers["getElementInfos"] = generateGetElementInfos(members, self.ComputedClasses)
+                classmembers["setElementValue"] = generateSetElementValue(members)
                 classmembers["singleLineAttributes"] = True
                 
                 self.ComputedClasses[classname] = classobj(classname, bases, classmembers)
@@ -814,7 +816,7 @@
     return generateXMLTextMethod
 
 
-def generateGetElementAttributes(bases, members, classes):
+def generateGetElementAttributes(members, classes):
     def getElementAttributes(self):
         attr_list = []
         for attr, values in members.items():
@@ -835,7 +837,97 @@
                 attr_list.append(attr_params)
         return attr_list
     return getElementAttributes
-    
+
+def generateGetElementInfos(members, classes):
+    def getElementInfos(self, name, path = None):
+        attr_type = "element"
+        value = None
+        children = []
+        if "enum" in members:
+            attr_type = self.getValidValues()
+            value = self.value
+        elif "limit" in members:
+            attr_type = {"min" : None, "max" : None}
+            if "min" in members:
+                attr_type["min"] = members["min"]
+            if "max" in members:
+                attr_type["max"] = members["max"]
+            value = self.value
+        elif path:
+            if "choice_content" in members:
+                return self.content["value"].getElementInfos(self.content["name"], path)
+            elif "multichoice_content" not in members:
+                parts = path.split(".", 1)
+                if parts[0] in members:
+                    values = members[parts[0]]
+                    if values[1] == "attribute" and len(parts) == 1:
+                        attr = getattr(self, parts[0], None)
+                        if attr != None:
+                            if values[0].startswith("cls:"):
+                                return attr.getElementInfos(parts[0])
+                            else:
+                                attr_type = values[0][4:]
+                                value = getattr(self, attr, "")
+                    elif values[1] == "element":
+                        attr = getattr(self, parts[0], None)
+                        if attr != None:
+                            if len(parts) == 1:
+                                return attr.getElementInfos(parts[0])
+                            else:
+                                return attr.getElementInfos(parts[0], parts[1])
+        else:
+            for attr, values in members.items():
+                if attr == "order":
+                    pass
+                elif attr == "choice_content":
+                    attr_type = self.getChoices().items()
+                    value = self.content["name"]
+                    children.extend(self.content["value"].getElementInfos(self.content["name"])["children"])
+                elif attr == "multichoice_content":
+                    for element_infos in self.content:
+                        children.append(element_infos["value"].getElementInfos(element_infos["name"]))
+                elif values[1] == "attribute" and not values[0].startswith("cls:"):
+                    children.append({"name" : attr, "value" : getattr(self, attr, ""), "type" : values[0][4:], "children" : []})
+                else:
+                    element = getattr(self, attr, None)
+                    if not element:
+                        element = classes[values[0][4:]]()
+                    children.append(element.getElementInfos(attr))
+        return {"name" : name, "type" : attr_type, "value" : value, "children" : children}
+    return getElementInfos
+
+def generateSetElementValue(members):
+    def setElementValue(self, path, value):
+        if "enum" in members or "limit" in members:
+            if not path:
+                self.setValue(value)
+        elif "choice_content" in members:
+            if path:
+                self.content["value"].setElementValue(path, value)
+            else:
+                self.addContent(value)
+        else: 
+            parts = path.split(".", 1)
+            if parts[0] in members:
+                values = members[parts[0]]
+                if values[1] == "attribute" and len(parts) == 1:
+                    attr = getattr(self, parts[0], None)
+                    if attr != None:
+                        if values[0].startswith("cls:"):
+                            attr.setElementValue(None, value)
+                        elif values[0][4:] == "boolean":
+                            setattr(self, parts[0], value)
+                        else:
+                            setattr(self, parts[0], GetComputedValue(values[0], value))
+                elif values[1] == "element":
+                    attr = getattr(self, parts[0], None)
+                    if attr != None:
+                        if len(parts) == 1:
+                            attr.setElementValue(None, value)
+                        else:
+                            attr.setElementValue(parts[1], value)
+    return setElementValue
+
 """
 Methods that generates the different methods for setting and getting the attributes
 """