xmlclass/xmlclass.py
author lbessard
Mon, 14 Jan 2008 17:35:32 +0100
changeset 147 5ef987c1b927
parent 125 394d9f168258
child 151 aaa80b48bead
permissions -rw-r--r--
Bug on array initial value parsing fixed
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
Regular expression models for extracting dates and times from a string
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    33
"""
116
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    34
time_model = re.compile('([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]*)?)(?:Z)?')
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    35
date_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})((?:[\-\+][0-9]{2}:[0-9]{2})|Z)?')
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    36
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]*)?)((?:[\-\+][0-9]{2}:[0-9]{2})|Z)?')
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    37
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    38
class xml_timezone(tzinfo):
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    39
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    40
    def SetOffset(self, offset):
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    41
        if offset == "Z":
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    42
            self.__offset = timedelta(minutes = 0)
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    43
            self.__name = "UTC"
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    44
        else:
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    45
            sign = {"-" : -1, "+" : 1}[offset[0]]
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    46
            hours, minutes = [int(val) for val in offset[1:].split(":")]
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    47
            self.__offset = timedelta(minutes = sign * (hours * 60 + minutes))
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    48
            self.__name = ""
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    49
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    50
    def utcoffset(self, dt):
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    51
        return self.__offset
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    52
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    53
    def tzname(self, dt):
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    54
        return self.__name
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    55
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    56
    def dst(self, dt):
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    57
        return ZERO
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    58
90
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 88
diff changeset
    59
XSD_INTEGER_TYPES = ["integer","nonPositiveInteger","negativeInteger","long",
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 88
diff changeset
    60
    "int","short","byte","nonNegativeInteger","unsignedLong","unsignedInt",
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 88
diff changeset
    61
    "unsignedShort","unsignedByte","positiveInteger"]
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 88
diff changeset
    62
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 88
diff changeset
    63
XSD_STRING_TYPES = ["string","normalizedString","token","anyURI","NMTOKEN","language"]
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 88
diff changeset
    64
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    65
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    66
This function calculates the number of whitespace for indentation
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    67
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    68
def getIndent(indent, balise):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    69
    first = indent * 2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    70
    second = first + len(balise) + 1
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    71
    return "\t".expandtabs(first), "\t".expandtabs(second)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    72
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    73
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    74
Function that extracts data from a node
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    75
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    76
def GetAttributeValue(attr):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    77
    if len(attr.childNodes) == 1:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    78
        return attr.childNodes[0].data.encode()
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    79
    else:
67
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 58
diff changeset
    80
        text = ""
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 58
diff changeset
    81
        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
    82
            if node.nodeName != "#text":
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 58
diff changeset
    83
                text += node.data.encode()
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 58
diff changeset
    84
        return text
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    85
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    86
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    87
Function that computes value from a python type (Only Boolean are critical because
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    88
there is no uppercase in plcopen)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    89
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    90
def ComputeValue(value):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    91
    if type(value) == BooleanType:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    92
        if value:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    93
            return "true"
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    94
        else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    95
            return "false"
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    96
    else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    97
        return str(value)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    98
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    99
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   100
Function that extracts a value from a string following the xsd type given
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   101
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   102
def GetComputedValue(attr_type, value):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   103
    type_compute = attr_type[4:].replace("[]", "")
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   104
    if type_compute == "boolean":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   105
         if value == "true":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   106
             return True
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   107
         elif value == "false":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   108
             return False
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   109
         else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   110
            raise ValueError, "\"%s\" is not a valid boolean!"%value
90
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 88
diff changeset
   111
    elif type_compute in XSD_INTEGER_TYPES:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   112
        return int(value)
90
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 88
diff changeset
   113
    elif type_compute in ["decimal", "float", "double"]:
86
4f1dbdb0bed2 Bug on xmlclass XML file attributes generation fixed
lbessard
parents: 85
diff changeset
   114
        computed_value = float(value)
4f1dbdb0bed2 Bug on xmlclass XML file attributes generation fixed
lbessard
parents: 85
diff changeset
   115
        if computed_value % 1 == 0:
4f1dbdb0bed2 Bug on xmlclass XML file attributes generation fixed
lbessard
parents: 85
diff changeset
   116
            return int(computed_value)
4f1dbdb0bed2 Bug on xmlclass XML file attributes generation fixed
lbessard
parents: 85
diff changeset
   117
        return computed_value
90
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 88
diff changeset
   118
    elif type_compute in XSD_STRING_TYPES:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   119
        return value
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   120
    elif type_compute == "time":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   121
        result = time_model.match(value)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   122
        if result:
24
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   123
            values = result.groups()
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   124
            time_values = [int(v) for v in values[:2]]
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   125
            seconds = float(values[2])
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   126
            time_values.extend([int(seconds), int((seconds % 1) * 1000000)])
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   127
            return time(*time_values)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   128
        else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   129
            raise ValueError, "\"%s\" is not a valid time!"%value
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   130
    elif type_compute == "date":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   131
        result = date_model.match(value)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   132
        if result:
116
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
   133
            values = result.groups()
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
   134
            date_values = [int(v) for v in values[:3]]
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
   135
            if values[3] is not None:
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
   136
                tz = xml_timezone()
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
   137
                tz.SetOffset(values[3])
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
   138
                date_values.append(tz)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   139
            return date(*date_values)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   140
        else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   141
            raise ValueError, "\"%s\" is not a valid date!"%value
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   142
    elif type_compute == "dateTime":
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   143
        result = datetime_model.match(value)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   144
        if result:
24
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   145
            values = result.groups()
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   146
            datetime_values = [int(v) for v in values[:5]]
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   147
            seconds = float(values[5])
364320323b4d Adding support for date and time data types
lbessard
parents: 2
diff changeset
   148
            datetime_values.extend([int(seconds), int((seconds % 1) * 1000000)])
116
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
   149
            if values[6] is not None:
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
   150
                tz = xml_timezone()
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
   151
                tz.SetOffset(values[6])
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
   152
                datetime_values.append(tz)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   153
            return datetime(*datetime_values)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   154
        else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   155
            raise ValueError, "\"%s\" is not a valid datetime!"%value
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   156
    else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   157
        print "Can't affect: %s"%type_compute
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   158
        return None
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   159
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   160
def GetInitialValueFunction(value):
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   161
    def GetInitialValue():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   162
        return value
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   163
    return GetInitialValue
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   164
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   165
"""
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   166
Class that generate class from an XML Tree 
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   167
"""
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   168
class ClassFactory:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   169
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   170
    def __init__(self, xsd_tree):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   171
        self.XML_Tree = xsd_tree
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   172
        
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   173
        # Dictionary for stocking Classes and Types definitions created from the XML tree
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   174
        self.XMLClassDefinitions = {}
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   175
        
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   176
        # Dictionaries for stocking Classes and Types generated
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   177
        self.ComputedClasses = {}
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   178
        self.ComputedTypes = {}
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   179
        self.AlreadyComputed = {}
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   180
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   181
    """
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   182
    This function recursively creates a definition of the classes and their attributes
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   183
    for plcopen from the xsd file of plcopen opened in a DOM model
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   184
    """
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   185
    def GenerateXSDClasses(self, tree, parent, sequence = False):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   186
        attributes = {}
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   187
        inheritance = []
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   188
        if sequence:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   189
            order = []
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   190
        # The lists of attributes and inheritance of the node are generated from the childrens 
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   191
        for node in tree.childNodes:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   192
            # We make fun of #text elements and all other tags that don't are xsd tags
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   193
            if node.nodeName != "#text" and node.nodeName.startswith("xsd:"):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   194
                recursion = False
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   195
                name = node.nodeName[4:].encode()
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   196
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   197
                # This tags defines an attribute of the class
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   198
                if name in ["element", "attribute"]:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   199
                    nodename = GetAttributeValue(node._attrs["name"])
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   200
                    default = None
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   201
                    if "type" in node._attrs:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   202
                        nodetype = GetAttributeValue(node._attrs["type"])
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   203
                        if nodetype.startswith("xsd"):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   204
                            nodetype = nodetype.replace("xsd", "bse")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   205
                        elif nodetype.startswith("ppx"):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   206
                            nodetype = nodetype.replace("ppx", "cls")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   207
                    else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   208
                        # The type of attribute is defines in the child tree so we generate a new class
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   209
                        # No name is defined so we create one from nodename and parent class name
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   210
                        # (because some different nodes can have the same name)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   211
                        if parent:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   212
                            classname = "%s_%s"%(parent, nodename)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   213
                        else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   214
                            classname = nodename
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   215
                        if len(node.childNodes) > 0:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   216
                            self.GenerateXSDClasses(node, classname)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   217
                            nodetype = "cls:%s"%classname
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   218
                        else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   219
                            nodetype = classname
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   220
                    if name == "attribute":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   221
                        if "use" in node._attrs:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   222
                            use = GetAttributeValue(node._attrs["use"])
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   223
                        else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   224
                            use = "optional"
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   225
                        if "default" in node._attrs:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   226
                            default = GetAttributeValue(node._attrs["default"])
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   227
                    elif name == "element":
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   228
                        # If a tag can be written more than one time we define a list attribute
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   229
                        if "maxOccurs" in node._attrs and GetAttributeValue(node._attrs["maxOccurs"]) == "unbounded":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   230
                            nodetype = "%s[]"%nodetype
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   231
                        if "minOccurs" in node._attrs and GetAttributeValue(node._attrs["minOccurs"]) == "0":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   232
                            use = "optional"
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   233
                        else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   234
                            use = "required"
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   235
                    attributes[nodename] = (nodetype, name, use, default)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   236
                    if sequence:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   237
                        order.append(nodename)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   238
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   239
                # This tag defines a new class
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   240
                elif name == "complexType" or name == "simpleType":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   241
                    if "name" in node._attrs:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   242
                        classname = GetAttributeValue(node._attrs["name"])
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   243
                        super, attrs = self.GenerateXSDClasses(node, classname)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   244
                    else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   245
                        classname = parent
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   246
                        super, attrs = self.GenerateXSDClasses(node, classname.split("_")[-1])
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   247
                    # When all attributes and inheritances have been extracted, the
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   248
                    # values are added in the list of classes to create
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   249
                    if self.XMLClassDefinitions.get(classname, None) == None:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   250
                        self.XMLClassDefinitions[classname] = (super, attrs)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   251
                    elif self.XMLClassDefinitions[classname] != (super, attrs):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   252
                        print "A different class has already got %s for name"%classname
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   253
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   254
                # This tag defines an attribute that can have different types
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   255
                elif name == "choice":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   256
                    super, attrs = self.GenerateXSDClasses(node, parent)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   257
                    
84
0369ad49e67f Bug on choice element in xmlclass fixed
jon
parents: 83
diff changeset
   258
                    choices = {}
0369ad49e67f Bug on choice element in xmlclass fixed
jon
parents: 83
diff changeset
   259
                    for attr, values in attrs.items():
0369ad49e67f Bug on choice element in xmlclass fixed
jon
parents: 83
diff changeset
   260
                        if attr == "ref":
0369ad49e67f Bug on choice element in xmlclass fixed
jon
parents: 83
diff changeset
   261
                            choices[attr] = values
0369ad49e67f Bug on choice element in xmlclass fixed
jon
parents: 83
diff changeset
   262
                        else:
0369ad49e67f Bug on choice element in xmlclass fixed
jon
parents: 83
diff changeset
   263
                            choices[attr] = values[0]
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   264
                    if "maxOccurs" in node._attrs and GetAttributeValue(node._attrs["maxOccurs"]) == "unbounded":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   265
                        attributes["multichoice_content"] = choices
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   266
                        if sequence:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   267
                            order.append("multichoice_content")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   268
                    else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   269
                        attributes["choice_content"] = choices
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   270
                        if sequence:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   271
                            order.append("choice_content")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   272
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   273
                # This tag defines the order in which class attributes must be written
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   274
                # in plcopen xml file. We have to store this order like an attribute
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   275
                elif name in "sequence":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   276
                    super, attrs, order = self.GenerateXSDClasses(node, parent, True)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   277
                    if "maxOccurs" in node._attrs and GetAttributeValue(node._attrs["maxOccurs"]) == "unbounded":
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   278
                        for attr, (attr_type, xml_type, write_type, default) in attrs.items():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   279
                            attrs[attr] = ("%s[]"%attr_type, xml_type, write_type, default)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   280
                    if "minOccurs" in node._attrs and GetAttributeValue(node._attrs["minOccurs"]) == "0":
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   281
                        for attr, (attr_type, xml_type, write_type, default) in attrs.items():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   282
                            attrs[attr] = (attr_type, xml_type, "optional", default)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   283
                    inheritance.extend(super)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   284
                    attributes.update(attrs)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   285
                    attributes["order"] = order
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   286
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   287
                # This tag defines of types
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   288
                elif name == "group":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   289
                    if "name" in node._attrs:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   290
                        nodename = GetAttributeValue(node._attrs["name"])
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   291
                        super, attrs = self.GenerateXSDClasses(node, None)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   292
                        self.XMLClassDefinitions[nodename] = (super, {"group":attrs["choice_content"]})
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   293
                    elif "ref" in node._attrs:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   294
                        if "ref" not in attributes:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   295
                            attributes["ref"] = [GetAttributeValue(node._attrs["ref"])]
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   296
                        else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   297
                            attributes["ref"].append(GetAttributeValue(node._attrs["ref"]))
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   298
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   299
                # This tag define a base class for the node
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   300
                elif name == "extension":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   301
                    super = GetAttributeValue(node._attrs["base"])
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   302
                    if super.startswith("xsd"):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   303
                        super = super.replace("xsd", "bse")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   304
                    elif super.startswith("ppx"):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   305
                        super = super.replace("ppx", "cls")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   306
                    inheritance.append(super[4:])
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   307
                    recursion = True
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   308
                    
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   309
                # This tag defines a restriction on the type of attribute
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   310
                elif name == "restriction":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   311
                    basetype = GetAttributeValue(node._attrs["base"])
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   312
                    if basetype.startswith("xsd"):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   313
                        basetype = basetype.replace("xsd", "bse")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   314
                    elif basetype.startswith("ppx"):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   315
                        basetype = basetype.replace("ppx", "cls")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   316
                    attributes["basetype"] = basetype
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   317
                    recursion = True
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   318
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   319
                # This tag defines an enumerated type
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   320
                elif name == "enumeration":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   321
                    if "enum" not in attributes:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   322
                        attributes["enum"] = [GetAttributeValue(node._attrs["value"])]
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   323
                    else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   324
                        attributes["enum"].append(GetAttributeValue(node._attrs["value"]))
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   325
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   326
                # This tags defines a restriction on a numerical value
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   327
                elif name in ["minInclusive","maxInclusive"]:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   328
                    if "limit" not in attributes:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   329
                        attributes["limit"] = {}
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   330
                    if name == "minInclusive":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   331
                        attributes["limit"]["min"] = eval(GetAttributeValue(node._attrs["value"]))
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   332
                    elif name == "maxInclusive":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   333
                        attributes["limit"]["max"] = eval(GetAttributeValue(node._attrs["value"]))
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   334
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   335
                # This tag are not important but their childrens are. The childrens are then parsed. 
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   336
                elif name in ["complexContent", "schema"]:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   337
                    recursion = True
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   338
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   339
                # We make fun of xsd documentation
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   340
                elif name in ["annotation"]:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   341
                    pass
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   342
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   343
                else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   344
                    # Unable this line to print XSD element that is not yet supported 
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   345
                    #print name
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   346
                    self.GenerateXSDClasses(node, parent)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   347
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   348
                # Parse the childrens of node
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   349
                if recursion:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   350
                    super, attrs = self.GenerateXSDClasses(node, parent)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   351
                    inheritance.extend(super)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   352
                    attributes.update(attrs)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   353
        
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   354
        # if sequence tag have been found, order is returned
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   355
        if sequence:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   356
            return inheritance, attributes, order
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   357
        else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   358
            return inheritance, attributes
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   359
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   360
    """
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   361
    Funtion that returns the Python type and default value for a given type
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   362
    """
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   363
    def GetTypeInitialValue(self, attr_type, default = None):
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   364
        type_compute = attr_type[4:].replace("[]", "")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   365
        if attr_type.startswith("bse:"):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   366
            if type_compute == "boolean":
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   367
                if default:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   368
                    def GetBooleanInitialValue():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   369
                        return default == "true"
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   370
                    return BooleanType, GetBooleanInitialValue
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   371
                else:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   372
                    return BooleanType, lambda:False
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   373
            elif type_compute in ["unsignedLong","long","integer"]:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   374
                if default:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   375
                    def GetIntegerInitialValue():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   376
                        return int(default)
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   377
                    return IntType, GetIntegerInitialValue
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   378
                else:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   379
                    return IntType, lambda:0
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   380
            elif type_compute == "decimal":
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   381
                if default:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   382
                    def GetFloatInitialValue():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   383
                        return float(default)
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   384
                    return FloatType, GetFloatInitialValue
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   385
                else:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   386
                    return FloatType, lambda:0.
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   387
            elif type_compute in ["string","anyURI","NMTOKEN"]:
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   388
                if default:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   389
                    def GetStringInitialValue():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   390
                        return default
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   391
                    return StringType, GetStringInitialValue
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   392
                else:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   393
                    return StringType, lambda:""
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   394
            elif type_compute == "time":
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   395
                if default:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   396
                    def GetTimeInitialValue():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   397
                        result = time_model.match(value)
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   398
                        if result:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   399
                            values = result.groups()
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   400
                            time_values = [int(v) for v in values[:2]]
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   401
                            seconds = float(values[2])
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   402
                            time_values.extend([int(seconds), int((seconds % 1) * 1000000)])
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   403
                            return time(*time_values)
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   404
                        return time(0,0,0,0)
92
76d5001393df Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 90
diff changeset
   405
                    return time, GetTimeInitialValue
76d5001393df Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 90
diff changeset
   406
                else:
76d5001393df Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 90
diff changeset
   407
                    return time, lambda:time(0,0,0,0)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   408
            elif type_compute == "date":
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   409
                if default:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   410
                    def GetDateInitialValue():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   411
                        result = date_model.match(value)
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   412
                        if result:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   413
                            date_values = [int(v) for v in result.groups()]
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   414
                            return date(*date_values)
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   415
                        return date(1,1,1)
92
76d5001393df Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 90
diff changeset
   416
                    return date, GetDateInitialValue
76d5001393df Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 90
diff changeset
   417
                else:
76d5001393df Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 90
diff changeset
   418
                    return date, lambda:date(1,1,1)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   419
            elif type_compute == "dateTime":
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   420
                if default:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   421
                    def GetDateTimeInitialValue():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   422
                        result = datetime_model.match(value)
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   423
                        if result:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   424
                            values = result.groups()
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   425
                            datetime_values = [int(v) for v in values[:5]]
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   426
                            seconds = float(values[5])
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   427
                            datetime_values.extend([int(seconds), int((seconds % 1) * 1000000)])
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   428
                            return datetime(*datetime_values)
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   429
                        return datetime(1,1,1,0,0,0,0)
92
76d5001393df Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 90
diff changeset
   430
                    return datetime, GetDateTimeInitialValue
76d5001393df Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 90
diff changeset
   431
                else:
76d5001393df Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 90
diff changeset
   432
                    return datetime, lambda:datetime(1,1,1,0,0,0,0)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   433
            elif type_compute == "language":
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   434
                if default:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   435
                    def GetStringInitialValue():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   436
                        return default
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   437
                    return StringType, GetStringInitialValue
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   438
                else:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   439
                    return StringType, lambda:"en-US"
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   440
            else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   441
                print "Can't affect: %s"%type_compute
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   442
        elif attr_type.startswith("cls:"):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   443
            if self.XMLClassDefinitions.get(type_compute, None) != None:
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   444
                def GetClassInitialValue():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   445
                    if self.XMLClassDefinitions.get(type_compute, None) != None:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   446
                        obj = self.ComputedClasses[type_compute]()
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   447
                        if default:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   448
                            obj.setValue(default)
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   449
                        return obj
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   450
                    return None
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   451
                return self.XMLClassDefinitions[type_compute], GetClassInitialValue
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   452
        return None, lambda:None
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   453
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   454
    """
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   455
    Funtion that returns the Python type and default value for a given type
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   456
    """
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   457
    def GetInitialValues(self, value_types):
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   458
        initial_values = {}
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   459
        for name, value_type in value_types.items():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   460
            result = self.GetTypeInitialValue(value_type)
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   461
            if result:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   462
                initial_values[name] = result[1]
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   463
        return initial_values
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   464
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   465
    """
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   466
    Methods that generate the classes
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   467
    """
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   468
    def CreateClasses(self):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   469
        self.GenerateXSDClasses(self.XML_Tree, None)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   470
        for classname in self.XMLClassDefinitions.keys():
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   471
            self.CreateClass(classname)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   472
        for classname in self.XMLClassDefinitions.keys():
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   473
            self.MarkUsedClasses(classname)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   474
        return self.ComputedClasses, self.ComputedTypes
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   475
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   476
    def CreateClass(self, classname):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   477
        # Checks that classe haven't been generated yet
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   478
        if not self.AlreadyComputed.get(classname, False) and classname in self.XMLClassDefinitions:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   479
            self.AlreadyComputed[classname] = True
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   480
            inheritance, attributes = self.XMLClassDefinitions[classname]
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   481
            #print classname, inheritance, attributes
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   482
            members = {}
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   483
            bases = []
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   484
            
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   485
            # If inheritance classes haven't been generated
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   486
            for base in inheritance:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   487
                self.CreateClass(base)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   488
                bases.append(self.ComputedClasses[base])
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   489
            
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   490
            # Checks that all attribute types are available 
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   491
            for attribute, type_attribute in attributes.items():
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   492
                if attribute == "group":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   493
                    self.ComputedTypes[classname] = type_attribute
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   494
                elif attribute in ["choice_content","multichoice_content"]:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   495
                    element_types = {}
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   496
                    for attr, value in type_attribute.items():
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   497
                        if attr == "ref":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   498
                            for ref in value:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   499
                                self.CreateClass(ref[4:])
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   500
                                element_types.update(self.ComputedTypes[ref[4:]])
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   501
                        else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   502
                            element_types[attr] = value
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   503
                    members[attribute] = element_types
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   504
                else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   505
                    members[attribute] = type_attribute
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   506
                    if attribute == "enum":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   507
                        self.ComputedTypes["%s_enum"%classname] = type_attribute
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   508
                    elif attribute not in ["limit", "order"]:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   509
                        if type_attribute[0].startswith("cls:"):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   510
                            type_compute = type_attribute[0][4:].replace("[]","")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   511
                            self.CreateClass(type_compute)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   512
            if "group" not in attributes:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   513
                bases = tuple(bases)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   514
                classmembers = {"IsBaseClass" : True}
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   515
                initialValues = {}
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   516
                for attr, values in members.items():
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   517
                    if attr in ["order", "basetype"]:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   518
                        pass
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   519
                    
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   520
                    # Class is a enumerated type
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   521
                    elif attr == "enum":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   522
                        value_type, initial = self.GetTypeInitialValue(members["basetype"])
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   523
                        initialValues["value"] = GetInitialValueFunction(values[0])
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   524
                        classmembers["value"] = values[0]
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   525
                        classmembers["setValue"] = generateSetEnumMethod(values, value_type)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   526
                        classmembers["getValue"] = generateGetMethod("value")
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   527
                        classmembers["getValidValues"] = generateGetChoicesMethod(values)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   528
                    
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   529
                    # Class is a limited type
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   530
                    elif attr == "limit":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   531
                        value_type, initial = self.GetTypeInitialValue(members["basetype"])
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   532
                        if "min" in values:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   533
                            initial = max(initial, values["min"])
92
76d5001393df Adding support for refreshing PLCOpenEditor block list
lbessard
parents: 90
diff changeset
   534
                        elif "max" in values:
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   535
                            initial = min(initial, values["max"])
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   536
                        initialValues["value"] = GetInitialValueFunction(initial)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   537
                        classmembers["value"] = initial
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   538
                        classmembers["setValue"] = generateSetLimitMethod(values, value_type)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   539
                        classmembers["getValue"] = generateGetMethod("value")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   540
                    
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   541
                    # Class has an attribute that can have different value types
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   542
                    elif attr == "choice_content":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   543
                        classmembers["content"] = None
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   544
                        initialValues["content"] = lambda:None
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   545
                        classmembers["deleteContent"] = generateDeleteMethod("content")
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   546
                        classmembers["addContent"] = generateAddChoiceMethod(values, self.GetInitialValues(values))
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   547
                        classmembers["setContent"] = generateSetChoiceMethod(values)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   548
                        classmembers["getContent"] = generateGetMethod("content")
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   549
                        classmembers["getChoices"] = generateGetChoicesMethod(values)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   550
                    elif attr == "multichoice_content":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   551
                        classmembers["content"] = []
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   552
                        initialValues["content"] = lambda:[]
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   553
                        classmembers["appendContent"] = generateAppendChoiceMethod(values)
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   554
                        classmembers["appendContentByType"] = generateAppendChoiceByTypeMethod(values, self.GetInitialValues(values))
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   555
                        classmembers["insertContent"] = generateInsertChoiceMethod(values)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   556
                        classmembers["removeContent"] = generateRemoveMethod("content")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   557
                        classmembers["countContent"] = generateCountMethod("content")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   558
                        classmembers["setContent"] = generateSetMethod("content", ListType)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   559
                        classmembers["getContent"] = generateGetMethod("content")
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   560
                        classmembers["getChoices"] = generateGetChoicesMethod(values)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   561
                    
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   562
                    # It's an attribute of the class
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   563
                    else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   564
                        attrname = attr[0].upper()+attr[1:]
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   565
                        attr_type, xml_type, write_type, default = values
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   566
                        value_type, initial = self.GetTypeInitialValue(attr_type, default)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   567
                        # Value of the attribute is a list
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   568
                        if attr_type.endswith("[]"):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   569
                            classmembers[attr] = []
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   570
                            initialValues[attr] = lambda:[]
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   571
                            classmembers["append"+attrname] = generateAppendMethod(attr, value_type)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   572
                            classmembers["insert"+attrname] = generateInsertMethod(attr, value_type)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   573
                            classmembers["remove"+attrname] = generateRemoveMethod(attr)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   574
                            classmembers["count"+attrname] = generateCountMethod(attr)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   575
                            classmembers["set"+attrname] = generateSetMethod(attr, ListType)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   576
                        else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   577
                            if write_type == "optional":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   578
                                classmembers[attr] = None
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   579
                                initialValues[attr] = lambda:None
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   580
                                classmembers["add"+attrname] = generateAddMethod(attr, initial)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   581
                                classmembers["delete"+attrname] = generateDeleteMethod(attr)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   582
                            else:
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   583
                                classmembers[attr] = initial()
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   584
                                initialValues[attr] = initial
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   585
                            classmembers["set"+attrname] = generateSetMethod(attr, value_type)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   586
                        classmembers["get"+attrname] = generateGetMethod(attr)
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   587
                classmembers["__init__"] = generateInitMethod(bases, initialValues)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   588
                classmembers["loadXMLTree"] = generateLoadXMLTree(bases, members, self.ComputedClasses)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   589
                classmembers["generateXMLText"] = generateGenerateXMLText(bases, members)
85
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   590
                classmembers["getElementAttributes"] = generateGetElementAttributes(members, self.ComputedClasses)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   591
                classmembers["getElementInfos"] = generateGetElementInfos(members, self.ComputedClasses)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   592
                classmembers["setElementValue"] = generateSetElementValue(members)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   593
                classmembers["singleLineAttributes"] = True
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   594
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   595
                self.ComputedClasses[classname] = classobj(classname, bases, classmembers)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   596
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   597
    def MarkUsedClasses(self, classname):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   598
        # Checks that classe haven't been generated yet
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   599
        if classname in self.XMLClassDefinitions:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   600
            inheritance, attributes = self.XMLClassDefinitions[classname]
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   601
            
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   602
            # If inheritance classes haven't been generated
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   603
            for base in inheritance:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   604
                if base in self.ComputedClasses:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   605
                    self.ComputedClasses[base].IsBaseClass = False
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   606
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   607
            # Checks that all attribute types are available 
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   608
            for attribute, type_attribute in attributes.items():
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   609
                if attribute in ["choice_content","multichoice_content"]:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   610
                    element_types = {}
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   611
                    for attr, value in type_attribute.items():
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   612
                        if attr == "ref":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   613
                            for ref in value:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   614
                                element_types.update(self.ComputedTypes[ref[4:]])
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   615
                        else:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   616
                            element_types[attr] = value
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   617
                    for type_name in element_types.values():
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   618
                        type_compute = type_name[4:].replace("[]","")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   619
                        if type_compute in self.ComputedClasses:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   620
                            self.ComputedClasses[type_compute].IsBaseClass = False
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   621
                elif attribute != "group":
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   622
                    if attribute not in ["enum", "limit", "order"]:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   623
                        if type_attribute[0].startswith("cls:"):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   624
                            type_compute = type_attribute[0][4:].replace("[]","")
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   625
                            if type_compute in self.ComputedClasses:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   626
                                self.ComputedClasses[type_compute].IsBaseClass = False
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   627
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   628
    """
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   629
    Methods that print the classes generated
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   630
    """
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   631
    def PrintClasses(self):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   632
        for classname, xmlclass in self.ComputedClasses.items():
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   633
            print "%s : %s"%(classname, str(xmlclass))
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   634
        
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   635
    def PrintClassNames(self):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   636
        classnames = self.XMLClassDefinitions.keys()
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   637
        classnames.sort()
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   638
        for classname in classnames:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   639
            print classname
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   640
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   641
"""
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   642
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
   643
attributes list defined
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   644
"""
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   645
def generateLoadXMLTree(bases, members, classes):
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   646
    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
   647
        # 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
   648
        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
   649
            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
   650
        # 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
   651
        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
   652
            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
   653
            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
   654
            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
   655
            self.setValue(val)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   656
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   657
            
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   658
            # 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
   659
            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
   660
                if attrname in members.keys():
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   661
                    attr_type, xml_type, write_type, default = members[attrname]
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   662
                    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
   663
                    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
   664
                        # Extracts the value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   665
                        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
   666
                            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
   667
                        elif attr_type.startswith("cls:"):
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   668
                            val = classes[attr_type[4:]]()
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   669
                            val.loadXMLTree(attr)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   670
                        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
   671
            
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   672
            # 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
   673
            for node in tree.childNodes:
116
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
   674
                if node.nodeType == node.COMMENT_NODE:
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
   675
                    continue
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   676
                name = node.nodeName
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   677
                # 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
   678
                if name != "#text":
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   679
                    
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   680
                    # 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
   681
                    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
   682
                        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
   683
                        # Extracts the value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   684
                        if attr_type.startswith("bse:"):
119
564564136c79 Bug on LoadXMLTree with "choice_content" and "multichoice_content" fixed
lbessard
parents: 116
diff changeset
   685
                            val = GetComputedValue(attr_type.replace("[]",""), GetAttributeValue(node))
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   686
                        elif attr_type.startswith("cls:"):
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   687
                            val = classes[attr_type[4:].replace("[]","")]()
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   688
                            val.loadXMLTree(node)
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   689
                        else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   690
                            val = None
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   691
                        # Stock value in content attribute
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   692
                        if val is not None:
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   693
                            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
   694
                                if self.content:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   695
                                    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
   696
                                else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   697
                                    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
   698
                            else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   699
                                self.content = {"name":name,"value":val}
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   700
                        else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   701
                            self.content = {"name":name,"value":None}
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   702
                    
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   703
                    # 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
   704
                    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
   705
                        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
   706
                        # Extracts the value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   707
                        if attr_type.startswith("bse:"):
119
564564136c79 Bug on LoadXMLTree with "choice_content" and "multichoice_content" fixed
lbessard
parents: 116
diff changeset
   708
                            val = GetComputedValue(attr_type, GetAttributeValue(node))
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   709
                        elif attr_type.startswith("cls:"):
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   710
                            val = classes[attr_type[4:]]()
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   711
                            val.loadXMLTree(node)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   712
                        # 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
   713
                        if val:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   714
                            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
   715
                    
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   716
                    # 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
   717
                    elif name in members.keys():
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   718
                        attr_type, xml_type, write_type, default = members[name]
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   719
                        # Extracts the value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   720
                        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
   721
                            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
   722
                            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
   723
                                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
   724
                            else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   725
                                val = None
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   726
                        elif attr_type.startswith("cls:"):
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   727
                            val = classes[attr_type[4:].replace("[]","")]()
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   728
                            val.loadXMLTree(node)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   729
                        # 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
   730
                        if val:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   731
                            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
   732
                                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
   733
                            else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   734
                                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
   735
    return loadXMLTreeMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   736
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   737
"""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   738
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
   739
attributes list defined
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   740
"""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   741
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
   742
    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
   743
        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
   744
        if not derived:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   745
            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
   746
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   747
            text = ""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   748
        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
   749
            base_extras = {}
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   750
        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
   751
            order = members["order"]
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   752
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   753
            order = []
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   754
        for attr, values in members.items():
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   755
            if attr != "order" and (attr in ("choice_content", "multichoice_content") or values[1] != "attribute"):
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   756
                if attr not in order:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   757
                    order.append(attr)
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   758
        size = 0
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   759
        first = True
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   760
        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
   761
            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
   762
                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
   763
            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
   764
            first = False
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   765
        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
   766
            if attr in ["order","choice_content","multichoice_content"]:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   767
                pass
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   768
            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
   769
                if not derived:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   770
                    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
   771
                else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   772
                    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
   773
                return text
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   774
            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
   775
                value = getattr(self, attr, None)
86
4f1dbdb0bed2 Bug on xmlclass XML file attributes generation fixed
lbessard
parents: 85
diff changeset
   776
                if value != None:
4f1dbdb0bed2 Bug on xmlclass XML file attributes generation fixed
lbessard
parents: 85
diff changeset
   777
                    if values[0].startswith("cls"):
4f1dbdb0bed2 Bug on xmlclass XML file attributes generation fixed
lbessard
parents: 85
diff changeset
   778
                        value = value.getValue()
4f1dbdb0bed2 Bug on xmlclass XML file attributes generation fixed
lbessard
parents: 85
diff changeset
   779
                    computed_value = ComputeValue(value)
4f1dbdb0bed2 Bug on xmlclass XML file attributes generation fixed
lbessard
parents: 85
diff changeset
   780
                else:
4f1dbdb0bed2 Bug on xmlclass XML file attributes generation fixed
lbessard
parents: 85
diff changeset
   781
                    computed_value = None
88
bac6435df86f Bug on optional attributes generation fixed
lbessard
parents: 87
diff changeset
   782
                if values[2] != "optional" or (value != None and computed_value != values[3]):
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   783
                    if len(bases) > 0:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   784
                        base_extras[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
                    else:
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   786
                        if not first and not self.singleLineAttributes:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   787
                            text += "\n%s"%(ind2)
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   788
                        text += " %s=\"%s\""%(attr, computed_value)
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   789
                    first = False
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   790
        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
   791
            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
   792
            text += new_text
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   793
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   794
            first = True
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   795
        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
   796
        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
   797
            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
   798
            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
   799
                if self.content:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   800
                    if first:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   801
                        text += ">\n"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   802
                        first = False
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   803
                    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
   804
                    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
   805
                        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
   806
                            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
   807
                                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
   808
                        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   809
                            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
   810
                    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
   811
                        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
   812
                            text += content.generateXMLText(self.content["name"], indent + 1)
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   813
                    elif self.content["value"] is not None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   814
                        text += self.content["value"].generateXMLText(self.content["name"], indent + 1)
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   815
                    else:
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   816
                        ind5, ind6 = getIndent(indent + 1, self.content["name"])
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 119
diff changeset
   817
                        text += ind5 + "<%s/>\n"%self.content["name"]
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   818
            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
   819
                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
   820
                    for element in self.content:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   821
                        if first:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   822
                            text += ">\n"
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   823
                            first = False
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   824
                        value_type = members[attr][element["name"]]
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   825
                        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
   826
                            text += ind1 + "<%s>%s</%s>\n"%(element["name"], ComputeValue(element["value"]), element["name"])
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   827
                        else:
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   828
                            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
   829
            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
   830
                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
   831
                    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
   832
                        text += ">\n"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   833
                        first = False
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   834
                    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
   835
                        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
   836
                            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
   837
                        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   838
                            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
   839
                else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   840
                    if first:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   841
                        text += ">\n"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   842
                        first = False
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   843
                    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
   844
                        text += ind3 + "<%s>%s</%s>\n"%(attr, ComputeValue(value), attr)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   845
                    else:
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   846
                        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
   847
        if not derived:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   848
            if first:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   849
                text += "/>\n"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   850
            else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   851
                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
   852
            return text
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   853
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   854
            return first, text
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   855
    return generateXMLTextMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   856
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   857
85
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   858
def generateGetElementAttributes(members, classes):
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   859
    def getElementAttributes(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   860
        attr_list = []
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   861
        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
   862
            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
   863
                pass
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   864
            elif values[1] == "attribute":
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   865
                attr_params = {"name": attr, "require": values[2] == "required"}
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   866
                if values[0].startswith("cls:"):
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   867
                    attr_value = getattr(self, attr, None)
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   868
                    if attr_value:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   869
                        attr_params["value"] = attr_value.getValue()
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   870
                    else:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   871
                        attr_params["value"] = ""
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   872
                    attr_params["type"] = classes[values[0][4:]]().getValidValues()
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   873
                else:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   874
                    attr_params["value"] = getattr(self, attr, "")
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   875
                    attr_params["type"] = values[0][4:]
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   876
                attr_list.append(attr_params)
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   877
        return attr_list
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   878
    return getElementAttributes
85
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   879
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   880
def generateGetElementInfos(members, classes):
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   881
    def getElementInfos(self, name, path = None):
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   882
        attr_type = "element"
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   883
        value = None
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   884
        children = []
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   885
        if "enum" in members:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   886
            attr_type = self.getValidValues()
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   887
            value = self.value
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   888
        elif "limit" in members:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   889
            attr_type = {"min" : None, "max" : None}
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   890
            if "min" in members:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   891
                attr_type["min"] = members["min"]
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   892
            if "max" in members:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   893
                attr_type["max"] = members["max"]
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   894
            value = self.value
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   895
        elif path:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   896
            if "choice_content" in members:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   897
                return self.content["value"].getElementInfos(self.content["name"], path)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   898
            elif "multichoice_content" not in members:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   899
                parts = path.split(".", 1)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   900
                if parts[0] in members:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   901
                    values = members[parts[0]]
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   902
                    if values[1] == "attribute" and len(parts) == 1:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   903
                        attr = getattr(self, parts[0], None)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   904
                        if attr != None:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   905
                            if values[0].startswith("cls:"):
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   906
                                return attr.getElementInfos(parts[0])
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   907
                            else:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   908
                                attr_type = values[0][4:]
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   909
                                value = getattr(self, attr, "")
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   910
                    elif values[1] == "element":
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   911
                        attr = getattr(self, parts[0], None)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   912
                        if attr != None:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   913
                            if len(parts) == 1:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   914
                                return attr.getElementInfos(parts[0])
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   915
                            else:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   916
                                return attr.getElementInfos(parts[0], parts[1])
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   917
        else:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   918
            for attr, values in members.items():
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   919
                if attr == "order":
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   920
                    pass
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   921
                elif attr == "choice_content":
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   922
                    attr_type = self.getChoices().items()
87
ce2607713931 *** empty log message ***
etisserant
parents: 86
diff changeset
   923
                    if self.content:
ce2607713931 *** empty log message ***
etisserant
parents: 86
diff changeset
   924
                        value = self.content["name"]
ce2607713931 *** empty log message ***
etisserant
parents: 86
diff changeset
   925
                        children.extend(self.content["value"].getElementInfos(self.content["name"])["children"])
85
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   926
                elif attr == "multichoice_content":
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   927
                    for element_infos in self.content:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   928
                        children.append(element_infos["value"].getElementInfos(element_infos["name"]))
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   929
                elif values[1] == "attribute" and not values[0].startswith("cls:"):
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   930
                    children.append({"name" : attr, "value" : getattr(self, attr, ""), "type" : values[0][4:], "children" : []})
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   931
                else:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   932
                    element = getattr(self, attr, None)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   933
                    if not element:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   934
                        element = classes[values[0][4:]]()
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   935
                    children.append(element.getElementInfos(attr))
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   936
        return {"name" : name, "type" : attr_type, "value" : value, "children" : children}
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   937
    return getElementInfos
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   938
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   939
def generateSetElementValue(members):
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   940
    def setElementValue(self, path, value):
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   941
        if "enum" in members or "limit" in members:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   942
            if not path:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   943
                self.setValue(value)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   944
        elif "choice_content" in members:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   945
            if path:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   946
                self.content["value"].setElementValue(path, value)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   947
            else:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   948
                self.addContent(value)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   949
        else: 
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   950
            parts = path.split(".", 1)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   951
            if parts[0] in members:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   952
                values = members[parts[0]]
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   953
                if values[1] == "attribute" and len(parts) == 1:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   954
                    attr = getattr(self, parts[0], None)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   955
                    if attr != None:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   956
                        if values[0].startswith("cls:"):
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   957
                            attr.setElementValue(None, value)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   958
                        elif values[0][4:] == "boolean":
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   959
                            setattr(self, parts[0], value)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   960
                        else:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   961
                            setattr(self, parts[0], GetComputedValue(values[0], value))
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   962
                elif values[1] == "element":
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   963
                    attr = getattr(self, parts[0], None)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   964
                    if attr != None:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   965
                        if len(parts) == 1:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   966
                            attr.setElementValue(None, value)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   967
                        else:
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   968
                            attr.setElementValue(parts[1], value)
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   969
    return setElementValue
fd17b0e0fd7e Adding members to classes for automatically generate infos and set the value of an element with its path
lbessard
parents: 84
diff changeset
   970
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   971
"""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   972
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
   973
"""
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   974
def generateInitMethod(bases, members):
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   975
    def initMethod(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   976
        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
   977
            base.__init__(self)
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   978
        for attr, initial in members.items():
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   979
            setattr(self, attr, initial())
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   980
    return initMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   981
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   982
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
   983
    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
   984
        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
   985
    return setMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   986
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   987
def generateAddChoiceMethod(choice_type, initial_values):
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   988
    def addChoiceMethod(self, name):
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   989
        if name in choice_type:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   990
            self.content = {"name" : name, "value" : initial_values[name]()}
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   991
    return addChoiceMethod
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   992
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   993
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
   994
    def setChoiceMethod(self, name, value):
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   995
        self.content = {"name" : name, "value" : value}
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   996
    return setChoiceMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
   997
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   998
def generateGetChoicesMethod(choice_type):
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
   999
    def getChoicesMethod(self):
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1000
        return choice_type
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1001
    return getChoicesMethod
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1002
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1003
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
  1004
    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
  1005
        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
  1006
            self.value = value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1007
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1008
            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
  1009
    return setEnumMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1010
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1011
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
  1012
    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
  1013
        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
  1014
            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
  1015
        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
  1016
            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
  1017
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1018
            self.value = value
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1019
    return setMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1020
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1021
def generateGetMethod(attr):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1022
    def getMethod(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1023
        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
  1024
    return getMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1025
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1026
def generateAddMethod(attr, initial):
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1027
    def addMethod(self):
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1028
        setattr(self, attr, initial())
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1029
    return addMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1030
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1031
def generateDeleteMethod(attr):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1032
    def deleteMethod(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1033
        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
  1034
    return deleteMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1035
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1036
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
  1037
    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
  1038
        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
  1039
    return appendMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1040
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1041
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
  1042
    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
  1043
        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
  1044
    return insertMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1045
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1046
def generateAppendChoiceByTypeMethod(choice_type, initial_values):
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1047
    def addChoiceMethod(self, name):
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1048
        if name in choice_type:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1049
            self.content.append({"name" : name, "value" : initial_values[name]()})
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1050
    return addChoiceMethod
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1051
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1052
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
  1053
    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
  1054
        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
  1055
    return appendMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1056
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1057
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
  1058
    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
  1059
        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
  1060
    return insertMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1061
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1062
def generateRemoveMethod(attr):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1063
    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
  1064
        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
  1065
    return removeMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1066
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1067
def generateCountMethod(attr):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1068
    def countMethod(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1069
        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
  1070
    return countMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1071
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1072
"""
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1073
This function generate the classes from a class factory
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1074
"""
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1075
def GenerateClasses(factory, declare = False):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1076
    ComputedClasses, ComputedTypes = factory.CreateClasses()
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1077
    if declare:
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1078
        for ClassName, Class in pluginClasses.items():
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1079
            sys._getframe(1).f_locals[ClassName] = Class
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1080
        for TypeName, Type in pluginTypes.items():
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1081
            sys._getframe(1).f_locals[TypeName] = Type
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1082
    globals().update(ComputedClasses)
76
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1083
    return ComputedClasses, ComputedTypes
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1084
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1085
"""
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1086
This function opens the xsd file and generate the classes from the xml tree
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1087
"""
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1088
def GenerateClassesFromXSD(filename, declare = False):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1089
    xsdfile = open(filename, 'r')
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1090
    factory = ClassFactory(minidom.parse(xsdfile))
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1091
    xsdfile.close()
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1092
    return GenerateClasses(factory, declare)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1093
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1094
"""
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1095
This function generate the classes from the xsd given as a string
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1096
"""
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1097
def GenerateClassesFromXSDstring(xsdstring, declare = False):
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1098
    factory = ClassFactory(minidom.parseString(xsdstring))
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1099
    return GenerateClasses(factory, declare)
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1100