xmlclass/xmlclass.py
author etisserant
Tue, 21 Aug 2007 08:55:59 +0200
changeset 75 82d371634f15
parent 67 3a1b0afdaf84
child 76 5bac3213fea1
permissions -rw-r--r--
Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
     1
#!/usr/bin/env python
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
     3
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
     5
#based on the plcopen standard. 
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
     6
#
58
39cd981ff242 Changing file headers
lbessard
parents: 24
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
     8
#
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
     9
#See COPYING file for copyrights details.
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    10
#
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    15
#
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
58
39cd981ff242 Changing file headers
lbessard
parents: 24
diff changeset
    19
#General Public License for more details.
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    20
#
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    24
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    25
from xml.dom import minidom
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    26
import sys,re
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    27
from types import *
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    28
from datetime import *
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
    29
from new import classobj
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    30
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    31
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    32
Time and date definitions
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    33
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    34
TimeType = time(0,0,0).__class__
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    35
DateType = date(1,1,1).__class__
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    36
DateTimeType = datetime(1,1,1,0,0,0).__class__
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    37
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    38
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    39
Regular expression models for extracting dates and times from a string
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    40
"""
24
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
    41
time_model = re.compile('([0-9]{2}):([0-9]{2}):([0-9]{2}(?:.[0-9]*)?)')
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    42
date_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})')
24
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
    43
datetime_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})[ T]([0-9]{2}):([0-9]{2}):([0-9]{2}(?:.[0-9]*)?)')
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    44
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    45
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    46
Dictionaries for stocking Classes and Types created from XML
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    47
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    48
XMLClasses = {}
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    49
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    50
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    51
This function calculates the number of whitespace for indentation
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    52
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    53
def getIndent(indent, balise):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    54
    first = indent * 2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    55
    second = first + len(balise) + 1
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    56
    return "\t".expandtabs(first), "\t".expandtabs(second)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    57
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    58
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    59
This function opens the xsd file and generate the classes from the xml tree
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    60
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    61
def GenerateClassesFromXSD(filename):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    62
    xsdfile = open(filename, 'r')
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    63
    Generate_xsd_classes(minidom.parse(xsdfile), None)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    64
    xsdfile.close()
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    65
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    66
"""
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
    67
This function generate the classes from the xsd given as a string
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
    68
"""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
    69
def GenerateClassesFromXSDstring(xsdstring):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
    70
    Generate_xsd_classes(minidom.parseString(xsdstring), None)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
    71
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
    72
"""
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    73
This function recursively creates a definition of the classes and their attributes
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    74
for plcopen from the xsd file of plcopen opened in a DOM model
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    75
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    76
def Generate_xsd_classes(tree, parent, sequence = False):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    77
    attributes = {}
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    78
    inheritance = []
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    79
    if sequence:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    80
        order = []
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    81
    # The lists of attributes and inheritance of the node are generated from the childrens 
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    82
    for node in tree.childNodes:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    83
        # We make fun of #text elements and all other tags that don't are xsd tags
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    84
        if node.nodeName != "#text" and node.nodeName.startswith("xsd:"):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    85
            recursion = False
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    86
            name = node.nodeName[4:].encode()
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    87
            
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    88
            # This tags defines an attribute of the class
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    89
            if name in ["element", "attribute"]:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    90
                nodename = GetAttributeValue(node._attrs["name"])
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    91
                if "type" in node._attrs:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    92
                    nodetype = GetAttributeValue(node._attrs["type"])
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    93
                    if nodetype.startswith("xsd"):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    94
                        nodetype = nodetype.replace("xsd", "bse")
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    95
                    elif nodetype.startswith("ppx"):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    96
                        nodetype = nodetype.replace("ppx", "cls")
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    97
                else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    98
                    # The type of attribute is defines in the child tree so we generate a new class
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    99
                    # No name is defined so we create one from nodename and parent class name
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   100
                    # (because some different nodes can have the same name)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   101
                    if parent:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   102
                        classname = "%s_%s"%(parent, nodename)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   103
                    else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   104
                        classname = nodename
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   105
                    Generate_xsd_classes(node, classname)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   106
                    nodetype = "cls:%s"%classname
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   107
                if name == "attribute":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   108
                    if "use" in node._attrs:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   109
                        use = GetAttributeValue(node._attrs["use"])
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   110
                    else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   111
                        use = "optional"
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   112
                if name == "element":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   113
                    # If a tag can be written more than one time we define a list attribute
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   114
                    if "maxOccurs" in node._attrs and GetAttributeValue(node._attrs["maxOccurs"]) == "unbounded":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   115
                        nodetype = "%s[]"%nodetype
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   116
                    if "minOccurs" in node._attrs and GetAttributeValue(node._attrs["minOccurs"]) == "0":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   117
                        use = "optional"
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   118
                    else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   119
                        use = "required"
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   120
                attributes[nodename] = (nodetype, name, use)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   121
                if sequence:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   122
                    order.append(nodename)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   123
            
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   124
            # This tag defines a new class
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   125
            elif name == "complexType" or name == "simpleType":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   126
                if "name" in node._attrs:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   127
                    classname = GetAttributeValue(node._attrs["name"])
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   128
                    super, attrs = Generate_xsd_classes(node, classname)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   129
                else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   130
                    classname = parent
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   131
                    super, attrs = Generate_xsd_classes(node, classname.split("_")[-1])
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   132
                # When all attributes and inheritances have been extracted, the
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   133
                # values are added in the list of classes to create
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   134
                if classname not in XMLClasses:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   135
                    XMLClasses[classname] = (super, attrs)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   136
                elif XMLClasses[classname] != (super, attrs):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   137
                    print "A different class has already got %s for name"%classname
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   138
            
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   139
            # This tag defines an attribute that can have different types
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   140
            elif name == "choice":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   141
                super, attrs = Generate_xsd_classes(node, parent)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   142
                if "ref" in attrs.keys():
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   143
                    choices = attrs
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   144
                else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   145
                    choices = {}
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   146
                    for attr, (attr_type, xml_type, write_type) in attrs.items():
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   147
                        choices[attr] = attr_type
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   148
                if "maxOccurs" in node._attrs and GetAttributeValue(node._attrs["maxOccurs"]) == "unbounded":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   149
                    attributes["multichoice_content"] = choices
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   150
                    if sequence:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   151
                        order.append("multichoice_content")
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   152
                else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   153
                    attributes["choice_content"] = choices
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   154
                    if sequence:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   155
                        order.append("choice_content")
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   156
            
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   157
            # This tag defines the order in which class attributes must be written
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   158
            # in plcopen xml file. We have to store this order like an attribute
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   159
            elif name in "sequence":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   160
                super, attrs, order = Generate_xsd_classes(node, parent, True)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   161
                if "maxOccurs" in node._attrs and GetAttributeValue(node._attrs["maxOccurs"]) == "unbounded":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   162
                    for attr, (attr_type, xml_type, write_type) in attrs.items():
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   163
                        attrs[attr] = ("%s[]"%attr_type, xml_type, write_type)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   164
                if "minOccurs" in node._attrs and GetAttributeValue(node._attrs["minOccurs"]) == "0":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   165
                    for attr, (attr_type, xml_type, write_type) in attrs.items():
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   166
                        attrs[attr] = (attr_type, xml_type, "optional")
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   167
                inheritance.extend(super)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   168
                attributes.update(attrs)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   169
                attributes["order"] = order
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   170
            
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   171
            # This tag defines of types
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   172
            elif name == "group":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   173
                if "name" in node._attrs:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   174
                    nodename = GetAttributeValue(node._attrs["name"])
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   175
                    super, attrs = Generate_xsd_classes(node, None)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   176
                    XMLClasses[nodename] = (super, {"group":attrs["choice_content"]})
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   177
                elif "ref" in node._attrs:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   178
                    if "ref" not in attributes:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   179
                        attributes["ref"] = [GetAttributeValue(node._attrs["ref"])]
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   180
                    else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   181
                        attributes["ref"].append(GetAttributeValue(node._attrs["ref"]))
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   182
            
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   183
            # This tag define a base class for the node
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   184
            elif name == "extension":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   185
                super = GetAttributeValue(node._attrs["base"])
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   186
                if super.startswith("xsd"):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   187
                    super = super.replace("xsd", "bse")
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   188
                elif super.startswith("ppx"):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   189
                    super = super.replace("ppx", "cls")
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   190
                inheritance.append(super[4:])
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   191
                recursion = True
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   192
                
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   193
            # This tag defines a restriction on the type of attribute
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   194
            elif name == "restriction":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   195
                basetype = GetAttributeValue(node._attrs["base"])
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   196
                if basetype.startswith("xsd"):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   197
                    basetype = basetype.replace("xsd", "bse")
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   198
                elif basetype.startswith("ppx"):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   199
                    basetype = basetype.replace("ppx", "cls")
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   200
                attributes["basetype"] = basetype
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   201
                recursion = True
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   202
            
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   203
            # This tag defines an enumerated type
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   204
            elif name == "enumeration":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   205
                if "enum" not in attributes:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   206
                    attributes["enum"] = [GetAttributeValue(node._attrs["value"])]
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   207
                else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   208
                    attributes["enum"].append(GetAttributeValue(node._attrs["value"]))
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   209
            
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   210
            # This tags defines a restriction on a numerical value
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   211
            elif name in ["minInclusive","maxInclusive"]:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   212
                if "limit" not in attributes:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   213
                    attributes["limit"] = {}
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   214
                if name == "minInclusive":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   215
                    attributes["limit"]["min"] = eval(GetAttributeValue(node._attrs["value"]))
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   216
                elif name == "maxInclusive":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   217
                    attributes["limit"]["max"] = eval(GetAttributeValue(node._attrs["value"]))
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   218
            
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   219
            # This tag are not important but their childrens are. The childrens are then parsed. 
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   220
            elif name in ["complexContent", "schema"]:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   221
                recursion = True
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   222
            
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   223
            # We make fun of xsd documentation
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   224
            elif name in ["annotation"]:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   225
                pass
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   226
            
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   227
            else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   228
                #print name
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   229
                Generate_xsd_classes(node, parent)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   230
            
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   231
            # Parse the childrens of node
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   232
            if recursion:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   233
                super, attrs = Generate_xsd_classes(node, parent)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   234
                inheritance.extend(super)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   235
                attributes.update(attrs)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   236
    
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   237
    # if sequence tag have been found, order is returned
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   238
    if sequence:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   239
        return inheritance, attributes, order
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   240
    else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   241
        return inheritance, attributes
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   242
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   243
Function that extracts data from a node
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   244
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   245
def GetAttributeValue(attr):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   246
    if len(attr.childNodes) == 1:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   247
        return attr.childNodes[0].data.encode()
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   248
    else:
67
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 58
diff changeset
   249
        text = ""
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 58
diff changeset
   250
        for node in attr.childNodes:
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 58
diff changeset
   251
            if node.nodeName != "#text":
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 58
diff changeset
   252
                text += node.data.encode()
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 58
diff changeset
   253
        return text
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   254
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   255
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   256
Funtion that returns the Python type and default value for a given type
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   257
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   258
def GetTypeInitialValue(attr_type):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   259
    type_compute = attr_type[4:].replace("[]", "")
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   260
    if attr_type.startswith("bse:"):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   261
        if type_compute == "boolean":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   262
            return BooleanType, "False"
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   263
        elif type_compute in ["decimal","unsignedLong","long","integer"]:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   264
            return IntType, "0"
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   265
        elif type_compute in ["string","anyURI","NMTOKEN"]:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   266
            return StringType, "\"\""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   267
        elif type_compute == "time":
24
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   268
            return TimeType, "time(0,0,0,0)"
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   269
        elif type_compute == "date":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   270
            return DateType, "date(1,1,1)"
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   271
        elif type_compute == "dateTime":
24
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   272
            return DateTimeType, "datetime(1,1,1,0,0,0,0)"
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   273
        elif type_compute == "language":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   274
            return StringType, "\"en-US\""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   275
        else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   276
            print "Can't affect: %s"%type_compute
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   277
    elif attr_type.startswith("cls:"):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   278
        if type_compute in XMLClasses:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   279
            return XMLClasses[type_compute],"%s()"%type_compute
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   280
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   281
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   282
Function that computes value from a python type (Only Boolean are critical because
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   283
there is no uppercase in plcopen)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   284
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   285
def ComputeValue(value):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   286
    if type(value) == BooleanType:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   287
        if value:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   288
            return "true"
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   289
        else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   290
            return "false"
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   291
    else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   292
        return str(value)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   293
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   294
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   295
Function that extracts a value from a string following the xsd type given
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   296
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   297
def GetComputedValue(attr_type, value):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   298
    type_compute = attr_type[4:].replace("[]", "")
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   299
    if type_compute == "boolean":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   300
         if value == "true":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   301
             return True
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   302
         elif value == "false":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   303
             return False
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   304
         else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   305
            raise ValueError, "\"%s\" is not a valid boolean!"%value
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   306
    elif type_compute in ["decimal","unsignedLong","long","integer"]:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   307
        return int(value)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   308
    elif type_compute in ["string","anyURI","NMTOKEN","language"]:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   309
        return value
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   310
    elif type_compute == "time":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   311
        result = time_model.match(value)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   312
        if result:
24
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   313
            values = result.groups()
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   314
            time_values = [int(v) for v in values[:2]]
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   315
            seconds = float(values[2])
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   316
            time_values.extend([int(seconds), int((seconds % 1) * 1000000)])
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   317
            return time(*time_values)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   318
        else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   319
            raise ValueError, "\"%s\" is not a valid time!"%value
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   320
    elif type_compute == "date":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   321
        result = date_model.match(value)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   322
        if result:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   323
            date_values = [int(v) for v in result.groups()]
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   324
            return date(*date_values)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   325
        else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   326
            raise ValueError, "\"%s\" is not a valid date!"%value
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   327
    elif type_compute == "dateTime":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   328
        result = datetime_model.match(value)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   329
        if result:
24
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   330
            values = result.groups()
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   331
            datetime_values = [int(v) for v in values[:5]]
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   332
            seconds = float(values[5])
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   333
            datetime_values.extend([int(seconds), int((seconds % 1) * 1000000)])
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   334
            return datetime(*datetime_values)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   335
        else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   336
            raise ValueError, "\"%s\" is not a valid datetime!"%value
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   337
    else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   338
        print "Can't affect: %s"%type_compute
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   339
        return None
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   340
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   341
"""
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   342
Method that generate the method for loading an xml tree by following the
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   343
attributes list defined
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   344
"""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   345
def generateLoadXMLTree(bases, members, user_classes):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   346
    def loadXMLTreeMethod(self, tree):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   347
        # If class is derived, values of inheritance classes are loaded
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   348
        for base in bases:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   349
            base.loadXMLTree(self, tree)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   350
        # Class is a enumerated or limited value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   351
        if "enum" in members.keys() or "limit" in members.keys():
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   352
            attr_value = GetAttributeValue(tree)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   353
            attr_type = members["basetype"]
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   354
            val = GetComputedValue(attr_type, attr_value)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   355
            self.setValue(val)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   356
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   357
            
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   358
            # Load the node attributes if they are defined in the list
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   359
            for attrname, attr in tree._attrs.items():
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   360
                if attrname in members.keys():
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   361
                    attr_type, xml_type, write_type = members[attrname]
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   362
                    attr_value = GetAttributeValue(attr)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   363
                    if write_type != "optional" or attr_value != "":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   364
                        # Extracts the value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   365
                        if attr_type.startswith("bse:"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   366
                            val = GetComputedValue(attr_type, attr_value)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   367
                        elif attr_type.startswith("cls:"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   368
                            val = eval("%s()"%attr_type[4:], globals().update(user_classes))
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   369
                            val.loadXMLTree(attr)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   370
                        setattr(self, attrname, val)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   371
            
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   372
            # Load the node childs if they are defined in the list
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   373
            for node in tree.childNodes:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   374
                name = node.nodeName
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   375
                # We make fun of #text elements
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   376
                if name != "#text":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   377
                    
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   378
                    # Class has an attribute that can have different value types
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   379
                    if "choice_content" in members.keys() and name in members["choice_content"].keys():
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   380
                        attr_type = members["choice_content"][name]
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   381
                        # Extracts the value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   382
                        if attr_type.startswith("bse:"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   383
                            attr_value = GetAttributeValue(node)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   384
                            if write_type != "optional" or attr_value != "":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   385
                                val = GetComputedValue(attr_type.replace("[]",""), attr_value)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   386
                            else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   387
                                val = None
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   388
                        elif attr_type.startswith("cls:"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   389
                            val = eval("%s()"%attr_type[4:].replace("[]",""), globals().update(user_classes))
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   390
                            val.loadXMLTree(node)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   391
                        # Stock value in content attribute
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   392
                        if val:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   393
                            if attr_type.endswith("[]"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   394
                                if self.content:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   395
                                    self.content["value"].append(val)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   396
                                else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   397
                                    self.content = {"name":name,"value":[val]}
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   398
                            else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   399
                                self.content = {"name":name,"value":val}
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   400
                    
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   401
                    # Class has a list of attributes that can have different value types
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   402
                    elif "multichoice_content" in members.keys() and name in members["multichoice_content"].keys():
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   403
                        attr_type = members["multichoice_content"][name]
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   404
                        # Extracts the value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   405
                        if attr_type.startswith("bse:"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   406
                            attr_value = GetAttributeValue(node)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   407
                            if write_type != "optional" or attr_value != "":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   408
                                val = GetComputedValue(attr_type, attr_value)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   409
                            else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   410
                                val = None
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   411
                        elif attr_type.startswith("cls:"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   412
                            val = eval("%s()"%attr_type[4:], globals().update(user_classes))
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   413
                            val.loadXMLTree(node)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   414
                        # Add to content attribute list
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   415
                        if val:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   416
                            self.content.append({"name":name,"value":val})
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   417
                    
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   418
                    # The node child is defined in the list
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   419
                    elif name in members.keys():
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   420
                        attr_type, xml_type, write_type = members[name]
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   421
                        # Extracts the value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   422
                        if attr_type.startswith("bse:"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   423
                            attr_value = GetAttributeValue(node)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   424
                            if write_type != "optional" or attr_value != "":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   425
                                val = GetComputedValue(attr_type.replace("[]",""), attr_value)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   426
                            else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   427
                                val = None
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   428
                        elif attr_type.startswith("cls:"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   429
                            val = eval("%s()"%attr_type[4:].replace("[]",""), globals().update(user_classes))
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   430
                            val.loadXMLTree(node)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   431
                        # Stock value in attribute
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   432
                        if val:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   433
                            if attr_type.endswith("[]"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   434
                                getattr(self, name).append(val)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   435
                            else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   436
                                setattr(self, name, val)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   437
    return loadXMLTreeMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   438
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   439
"""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   440
Method that generates the method for generating an xml text by following the
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   441
attributes list defined
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   442
"""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   443
def generateGenerateXMLText(bases, members):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   444
    def generateXMLTextMethod(self, name, indent, extras = {}, derived = False):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   445
        ind1, ind2 = getIndent(indent, name)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   446
        if not derived:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   447
            text = ind1 + "<%s"%name
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   448
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   449
            text = ""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   450
        if len(bases) > 0:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   451
            base_extras = {}
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   452
        if "order" in members.keys():
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   453
            order = members["order"]
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   454
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   455
            order = []
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   456
        if "choice_content" in members.keys() and "choice_content" not in order:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   457
            order.append("choice_content") 
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   458
        if "multichoice_content" in members.keys() and "multichoice_content" not in order:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   459
            order.append("multichoice_content") 
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   460
        size = 0
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   461
        first = True
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   462
        for attr, value in extras.items():
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   463
            if not first and not self.singleLineAttributes:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   464
                text += "\n%s"%(ind2)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   465
            text += " %s=\"%s\""%(attr, ComputeValue(value))
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   466
            first = False
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   467
        for attr, values in members.items():
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   468
            if attr in ["order","choice_content","multichoice_content"]:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   469
                pass
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   470
            elif attr in ["enum","limit"]:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   471
                if not derived:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   472
                    text += ">%s</%s>\n"%(ComputeValue(self.value),name)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   473
                else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   474
                    text += ComputeValue(self.value)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   475
                return text
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   476
            elif values[1] == "attribute":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   477
                value = getattr(self, attr, None)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   478
                if value == "":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   479
                    value = None
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   480
                if values[2] != "optional" or value != None:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   481
                    if not first and not self.singleLineAttributes:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   482
                        text += "\n%s"%(ind2)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   483
                    if values[0].startswith("cls"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   484
                        if len(bases) > 0:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   485
                            base_extras[attr] = value.getValue()
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   486
                        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   487
                            text += " %s=\"%s\""%(attr, ComputeValue(value.getValue()))
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   488
                    else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   489
                        if len(bases) > 0:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   490
                            base_extras[attr] = value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   491
                        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   492
                            text += " %s=\"%s\""%(attr, ComputeValue(value))
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   493
                    first = False
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   494
        if len(bases) > 0:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   495
            first, new_text = bases[0].generateXMLText(self, name, indent, base_extras, True)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   496
            text += new_text
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   497
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   498
            first = True
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   499
        ind3, ind4 = getIndent(indent + 1, name)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   500
        for attr in order:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   501
            value = getattr(self, attr, None)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   502
            if attr == "choice_content":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   503
                if self.content:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   504
                    if first:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   505
                        text += ">\n"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   506
                        first = False
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   507
                    value_type = members[attr][self.content["name"]]
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   508
                    if value_type.startswith("bse:"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   509
                        if value_type.endswith("[]"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   510
                            for content in self.content["value"]:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   511
                                text += ind1 + "<%s>%s</%s>\n"%(self.content["name"], ComputeValue(content), self.content["name"])
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   512
                        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   513
                            text += ind1 + "<%s>%s</%s>\n"%(self.content["name"], ComputeValue(self.content["value"]), self.content["name"])
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   514
                    elif value_type.endswith("[]"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   515
                        for content in self.content["value"]:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   516
                            text += content.generateXMLText(self.content["name"], indent + 1)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   517
                    else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   518
                        text += self.content["value"].generateXMLText(self.content["name"], indent + 1)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   519
            elif attr == "multichoice_content":
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   520
                if len(self.content) > 0:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   521
                    for element in self.content:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   522
                        if first:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   523
                            text += ">\n"
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   524
                            first = False
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   525
                        value_type = members[attr][element["name"]]
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   526
                        if value_type.startswith("bse:"):
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   527
                            text += ind1 + "<%s>%s</%s>\n"%(element["name"], ComputeValue(element["value"]), element["name"])
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   528
                        else:
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   529
                            text += element["value"].generateXMLText(element["name"], indent + 1)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   530
            elif members[attr][2] != "optional" or value != None:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   531
                if members[attr][0].endswith("[]"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   532
                    if first and len(value) > 0:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   533
                        text += ">\n"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   534
                        first = False
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   535
                    for element in value:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   536
                        if members[attr][0].startswith("bse:"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   537
                            text += ind3 + "<%s>%s</%s>\n"%(attr, ComputeValue(element), attr)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   538
                        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   539
                            text += element.generateXMLText(attr, indent + 1)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   540
                else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   541
                    if first:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   542
                        text += ">\n"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   543
                        first = False
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   544
                    if members[attr][0].startswith("bse:"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   545
                        text += ind3 + "<%s>%s</%s>\n"%(attr, ComputeValue(value), attr)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   546
                    else:
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   547
                        text += getattr(self, attr).generateXMLText(attr, indent + 1)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   548
        if not derived:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   549
            if first:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   550
                text += "/>\n"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   551
            else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   552
                text += ind1 + "</%s>\n"%(name)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   553
            return text
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   554
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   555
            return first, text
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   556
    return generateXMLTextMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   557
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   558
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   559
def generategetElementAttributes(bases, members):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   560
    def getElementAttributes(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   561
        attr_list = []
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   562
        for attr, values in members.items():
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   563
            if attr in ["order","choice_content","multichoice_content"]:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   564
                pass
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   565
            elif values[1] == "attribute":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   566
                if values[2] == "required":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   567
                    require = True
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   568
                else:
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   569
                    require = False
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   570
                attr_hash ={"name": attr,"type": values[0] ,"value": getattr(self, attr, "") ,"require": require}
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   571
                attr_list.append(attr_hash)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   572
        return attr_list
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   573
    return getElementAttributes
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   574
    
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   575
"""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   576
Methods that generates the different methods for setting and getting the attributes
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   577
"""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   578
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   579
def generateInitMethod(bases, members, user_classes):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   580
    def initMethod(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   581
        for base in bases:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   582
            base.__init__(self)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   583
        for attr, initial in members.items():
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   584
            setattr(self, attr, eval(initial, globals().update(user_classes)))
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   585
    return initMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   586
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   587
def generateSetMethod(attr, attr_type):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   588
    def setMethod(self, value):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   589
        setattr(self, attr, value)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   590
    return setMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   591
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   592
def generateSetChoiceMethod(choice_type):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   593
    def setChoiceMethod(self, name, value):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   594
        self.content = {"name":name,"value":value}
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   595
    return setChoiceMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   596
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   597
def generateSetEnumMethod(enum, attr_type):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   598
    def setEnumMethod(self, value):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   599
        if value in enum:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   600
            self.value = value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   601
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   602
            raise ValueError, "%s is not a valid value. Must be in %s"%(value, str(enum))
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   603
    return setEnumMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   604
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   605
def generateSetLimitMethod(limit, attr_type):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   606
    def setMethod(self, value):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   607
        if "min" in limit and value < limit["min"]:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   608
            raise ValueError, "%s is not a valid value. Must be greater than %d"%(value, limit["min"])
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   609
        elif "max" in limit and value > limit["max"]:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   610
            raise ValueError, "%s is not a valid value. Must be smaller than %d"%(value, limit["max"])
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   611
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   612
            self.value = value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   613
    return setMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   614
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   615
def generateGetMethod(attr):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   616
    def getMethod(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   617
        return getattr(self, attr, None)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   618
    return getMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   619
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   620
def generateAddMethod(attr, initial, user_classes):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   621
    def addMethod(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   622
        setattr(self, attr, eval(initial, globals().update(user_classes)))
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   623
    return addMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   624
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   625
def generateDeleteMethod(attr):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   626
    def deleteMethod(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   627
        setattr(self, attr, None)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   628
    return deleteMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   629
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   630
def generateAppendMethod(attr, attr_type):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   631
    def appendMethod(self, value):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   632
        getattr(self, attr).append(value)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   633
    return appendMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   634
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   635
def generateInsertMethod(attr, attr_type):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   636
    def insertMethod(self, index, value):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   637
        getattr(self, attr).insert(index, value)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   638
    return insertMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   639
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   640
def generateAppendChoiceMethod(choice_types):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   641
    def appendMethod(self, name, value):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   642
        self.content.append({"name":name,"value":value})
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   643
    return appendMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   644
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   645
def generateInsertChoiceMethod(choice_types):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   646
    def insertMethod(self, index, name, value):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   647
        self.content.insert(index, {"name":name,"value":value})
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   648
    return insertMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   649
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   650
def generateRemoveMethod(attr):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   651
    def removeMethod(self, index):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   652
        getattr(self, attr).pop(index)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   653
    return removeMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   654
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   655
def generateCountMethod(attr):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   656
    def countMethod(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   657
        return len(getattr(self, attr))
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   658
    return countMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   659
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   660
"""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   661
This is the Metaclass for PLCOpen element classes. It generates automatically
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   662
the basic useful methods for manipulate the differents attributes of the classes
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   663
"""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   664
def MetaClass(name, bases, members, user_classes):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   665
    classmembers = {}
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   666
    initialValues = {}
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   667
    for attr, values in members.items():
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   668
        if attr in ["order", "basetype"]:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   669
            pass
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   670
        
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   671
        # Class is a enumerated type
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   672
        elif attr == "enum":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   673
            value_type, initial = GetTypeInitialValue(members["basetype"])
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   674
            initialValues["value"] = "\"%s\""%values[0]
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   675
            classmembers["value"]= values[0]
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   676
            classmembers["setValue"]= generateSetEnumMethod(values, value_type)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   677
            classmembers["getValue"]= generateGetMethod("value")
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   678
        
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   679
        # Class is a limited type
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   680
        elif attr == "limit":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   681
            value_type, initial = GetTypeInitialValue(members["basetype"])
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   682
            initial = 0
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   683
            if "min" in values:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   684
                initial = max(initial, values["min"])
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   685
            if "max" in values:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   686
                initial = min(initial, values["max"])
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   687
            initialValues["value"] = "%d"%initial
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   688
            classmembers["value"]= initial
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   689
            classmembers["setValue"]= generateSetLimitMethod(values, value_type)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   690
            classmembers["getValue"]= generateGetMethod("value")
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   691
        
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   692
        # Class has an attribute that can have different value types
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   693
        elif attr == "choice_content":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   694
            classmembers["content"]= None
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   695
            initialValues["content"] = "None"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   696
            classmembers["deleteContent"]= generateDeleteMethod("content")
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   697
            classmembers["setContent"]= generateSetChoiceMethod(values)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   698
            classmembers["getContent"]= generateGetMethod("content")
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   699
        elif attr == "multichoice_content":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   700
            classmembers["content"]= []
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   701
            initialValues["content"] = "[]"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   702
            classmembers["appendContent"]= generateAppendChoiceMethod(values)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   703
            classmembers["insertContent"]= generateInsertChoiceMethod(values)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   704
            classmembers["removeContent"]= generateRemoveMethod("content")
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   705
            classmembers["countContent"]= generateCountMethod("content")
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   706
            classmembers["setContent"]= generateSetMethod("content", ListType)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   707
            classmembers["getContent"]= generateGetMethod("content")
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   708
        
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   709
        # It's an attribute of the class
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   710
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   711
            attrname = attr[0].upper()+attr[1:]
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   712
            attr_type, xml_type, write_type = values
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   713
            value_type, initial = GetTypeInitialValue(attr_type)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   714
            # Value of the attribute is a list
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   715
            if attr_type.endswith("[]"):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   716
                classmembers[attr]= []
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   717
                initialValues[attr] = "[]"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   718
                classmembers["append"+attrname] = generateAppendMethod(attr, value_type)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   719
                classmembers["insert"+attrname] = generateInsertMethod(attr, value_type)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   720
                classmembers["remove"+attrname] = generateRemoveMethod(attr)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   721
                classmembers["count"+attrname] = generateCountMethod(attr)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   722
                classmembers["set"+attrname] = generateSetMethod(attr, ListType)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   723
            else:
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   724
                if write_type == "optional":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   725
                    classmembers[attr] = None
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   726
                    initialValues[attr] = "None"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   727
                    classmembers["add"+attrname] = generateAddMethod(attr, initial, user_classes)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   728
                    classmembers["delete"+attrname] = generateDeleteMethod(attr)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   729
                else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   730
                    classmembers[attr] = initial
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   731
                    initialValues[attr] = initial
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   732
                classmembers["set"+attrname] = generateSetMethod(attr, value_type)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   733
            classmembers["get"+attrname] = generateGetMethod(attr)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   734
    classmembers["__init__"]= generateInitMethod(bases, initialValues, user_classes)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   735
    classmembers["loadXMLTree"]= generateLoadXMLTree(bases, members, user_classes)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   736
    classmembers["generateXMLText"]= generateGenerateXMLText(bases, members)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   737
    classmembers["getElementAttributes"]= generategetElementAttributes(bases, members)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   738
    classmembers["singleLineAttributes"]= True
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   739
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   740
    return classobj(name, bases, classmembers)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   741
    
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   742
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   743
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   744
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   745
Methods that generate the classes
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   746
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   747
def CreateClasses(user_classes, user_types):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   748
    for classname in XMLClasses.keys():
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   749
        CreateClass(classname, user_classes, user_types)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   750
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   751
def CreateClass(classname, user_classes, user_types):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   752
    # Checks that classe haven't been generated yet
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   753
    if classname not in user_classes and classname not in user_types and classname in XMLClasses:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   754
        inheritance, attributes = XMLClasses[classname]
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   755
        #print classe, inheritance, attributes
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   756
        members = {}
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   757
        bases = []
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   758
        
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   759
        # If inheritance classes haven't been generated
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   760
        for base in inheritance:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   761
            if base not in user_classes:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   762
                CreateClass(base, user_classes, user_types)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   763
            bases.append(user_classes[base])
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   764
        
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   765
        # Checks that all attribute types are available 
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   766
        for attribute, type_attribute in attributes.items():
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   767
            if attribute == "group":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   768
                user_types[classname] = type_attribute
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   769
            elif attribute == "ref":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   770
                user_types[classname] = {}
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   771
                for attr in type_attribute:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   772
                    if attr[4:] not in user_types:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   773
                        CreateClass(attr[4:], user_classes, user_types)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   774
                    user_types[classname].update(user_types[attr[4:]])
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   775
            elif attribute in ["choice_content","multichoice_content"]:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   776
                element_types = {}
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   777
                for attr, value in type_attribute.items():
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   778
                    if attr == "ref":
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   779
                        for ref in value:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   780
                            if ref[4:] not in user_types:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   781
                                CreateClass(ref[4:], user_classes, user_types)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   782
                            element_types.update(user_types[ref[4:]])
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   783
                    else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   784
                        element_types[attr] = value
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   785
                members[attribute] = element_types
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   786
            else:
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   787
                members[attribute] = type_attribute
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   788
                if attribute == "enum":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   789
                    user_types["%s_enum"%classname] = type_attribute
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   790
                elif attribute not in ["limit", "order"]:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   791
                    if type_attribute[0].startswith("ppx:"):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   792
                        type_compute = type_attribute[0][4:].replace("[]","")
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   793
                        if type_compute not in user_classes:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   794
                            CreateClass(type_compute, user_classes, user_types)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   795
        if "group" not in attributes.keys() and "ref" not in attributes.keys():
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   796
            cls = MetaClass(classname, tuple(bases), members, user_classes)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   797
            user_classes[classname] = cls
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   798
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   799
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   800
Methods that print the classes generated
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   801
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   802
def PrintClasses():
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   803
    for classname, xmlclass in XMLClasses.items():
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   804
        print "%s : %s\n"%(classname, str(xmlclass))
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   805
    
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   806
def PrintClassNames():
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   807
    classnames = XMLClasses.keys()
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   808
    classnames.sort()
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   809
    for classname in classnames:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   810
        print classname
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   811
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   812
def DeclareXSDClass(XSDstring):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   813
    pluginClasses = {}
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   814
    pluginTypes = {}
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   815
    GenerateClassesFromXSDstring(XSDstring)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   816
    CreateClasses(pluginClasses, pluginTypes)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   817
    
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   818
    for ClassName, Class in pluginClasses.items():
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   819
        sys._getframe(1).f_locals[ClassName] = Class