xmlclass/xmlclass.py
author lbessard
Tue, 22 Jan 2008 10:57:41 +0100
changeset 151 aaa80b48bead
parent 125 394d9f168258
child 153 f0e8e7f58a5a
permissions -rw-r--r--
Adding support for the new version of xmlclass
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
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    26
import sys, re, datetime
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    27
from types import *
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
    28
from new import classobj
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    29
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    30
LANGUAGES = ["en-US", "fr-FR", "en", "fr"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    31
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    32
"""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    33
Regular expression models for check all kind of string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    34
"""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    35
Name_model = re.compile('([a-zA-Z_\:][\w\.\-\:]*)$')
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    36
Names_model = re.compile('([a-zA-Z_\:][\w\.\-\:]*(?: [a-zA-Z_\:][\w\.\-\:]*)*)$')
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    37
NMToken_model = re.compile('([\w\.\-\:]*)$')
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    38
NMTokens_model = re.compile('([\w\.\-\:]*(?: [\w\.\-\:]*)*)$')
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    39
QName_model = re.compile('((?:[a-zA-Z_][\w]*:)?[a-zA-Z_][\w]*)$')
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    40
QNames_model = re.compile('((?:[a-zA-Z_][\w]*:)?[a-zA-Z_][\w]*(?: (?:[a-zA-Z_][\w]*:)?[a-zA-Z_][\w]*)*)$')
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    41
NCName_model = re.compile('([a-zA-Z_][\w]*)$')
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    42
URI_model = re.compile('((?:http://|/)?(?:[\w.]*/?)*)$')
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    43
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    44
ONLY_ANNOTATION = re.compile("((?:annotation )?)")
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    45
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    46
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    47
Regular expression models for extracting dates and times from a string
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    48
"""
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    49
time_model = re.compile('([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]*)?)(?:Z)?$')
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    50
date_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})((?:[\-\+][0-9]{2}:[0-9]{2})|Z)?$')
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    51
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)?$')
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    52
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    53
class xml_timezone(datetime.tzinfo):
116
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    54
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    55
    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
    56
        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
    57
            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
    58
            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
    59
        else:
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    60
            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
    61
            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
    62
            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
    63
            self.__name = ""
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    64
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    65
    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
    66
        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
    67
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    68
    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
    69
        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
    70
58b9b84e385f Adding support in xmlclass for timezone in datetime and for not paying attention to xml comments
lbessard
parents: 92
diff changeset
    71
    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
    72
        return ZERO
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    73
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    74
[SYNTAXELEMENT, SYNTAXATTRIBUTE, SIMPLETYPE, COMPLEXTYPE, COMPILEDCOMPLEXTYPE, 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    75
 ATTRIBUTESGROUP, ELEMENTSGROUP, ATTRIBUTE, ELEMENT, CHOICE, ANY, TAG
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    76
] = range(12)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    77
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    78
def NotSupportedYet(type):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    79
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    80
    Function that generates a function that point out to user that datatype asked
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    81
    are not yet supported by xmlclass
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    82
    @param type: data type
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    83
    @return: function generated
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    84
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    85
    def GetUnknownValue(attr):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    86
        raise ValueError, "\"%s\" type isn't supported by \"xmlclass\" yet!"%type
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    87
    return GetUnknownValue
90
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 88
diff changeset
    88
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    89
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    90
This function calculates the number of whitespace for indentation
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    91
"""
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    92
def getIndent(indent, balise):
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    93
    first = indent * 2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    94
    second = first + len(balise) + 1
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    95
    return "\t".expandtabs(first), "\t".expandtabs(second)
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
    96
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    97
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    98
def GetAttributeValue(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
    99
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   100
    Function that extracts data from a tree node
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   101
    @param attr: tree node containing data to extract
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   102
    @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   103
    @return: data extracted as string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   104
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   105
    if not extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   106
        return attr
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   107
    if len(attr.childNodes) == 1:
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   108
        return attr.childNodes[0].data.encode()
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   109
    else:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   110
        # content is a CDATA
67
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 58
diff changeset
   111
        text = ""
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 58
diff changeset
   112
        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
   113
            if node.nodeName != "#text":
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 58
diff changeset
   114
                text += node.data.encode()
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 58
diff changeset
   115
        return text
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   116
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   117
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   118
def GetNormalizedString(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   119
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   120
    Function that normalizes a string according to XML 1.0. Replace tabulations, 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   121
    line feed and carriage return by white space
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   122
    @param attr: tree node containing data to extract or data to normalize
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   123
    @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   124
    @return: data normalized as string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   125
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   126
    if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   127
        return GetAttributeValue(attr).replace("\n", " ").replace("\t", " ")
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   128
    else:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   129
        return attr.replace("\n", " ").replace("\t", " ")
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   130
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   131
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   132
def GetToken(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   133
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   134
    Function that tokenizes a string according to XML 1.0. Remove any leading and 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   135
    trailing white space and replace internal sequence of two or more spaces by 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   136
    only one white space
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   137
    @param attr: tree node containing data to extract or data to tokenize
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   138
    @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   139
    @return: data tokenized as string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   140
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   141
    return " ".join([part for part in GetNormalizedString(attr, extract).split(" ") if part])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   142
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   143
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   144
def GetHexInteger(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   145
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   146
    Function that extracts an hexadecimal integer from a tree node or a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   147
    @param attr: tree node containing data to extract or data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   148
    @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   149
    @return: data as an integer
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   150
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   151
    if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   152
        value = GetAttributeValue(attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   153
    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   154
        value = attr
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   155
    try:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   156
        return int(value, 16)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   157
    except:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   158
        raise ValueError, "\"%s\" isn't a valid hexadecimal integer!"%value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   159
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   160
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   161
def GenerateIntegerExtraction(minInclusive = None, maxInclusive = None, minExclusive = None, maxExclusive = None):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   162
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   163
    Function that generates an extraction function for integer defining min and max
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   164
    of integer value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   165
    @param minInclusive: inclusive minimum
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   166
    @param maxInclusive: inclusive maximum
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   167
    @param minExclusive: exclusive minimum
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   168
    @param maxExclusive: exclusive maximum
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   169
    @return: function generated
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   170
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   171
    def GetInteger(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   172
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   173
        Function that extracts an integer from a tree node or a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   174
        @param attr: tree node containing data to extract or data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   175
        @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   176
        @return: data as an integer
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   177
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   178
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   179
        if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   180
            value = GetAttributeValue(attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   181
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   182
            value = attr
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   183
        try:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   184
            # TODO: permit to write value like 1E2
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   185
            value = int(value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   186
        except:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   187
            raise ValueError, "\"%s\" isn't a valid integer!"%value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   188
        if minInclusive is not None and value < minInclusive:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   189
            raise ValueError, "%d isn't greater or equal to %d!"%(value, minInclusive)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   190
        if maxInclusive is not None and value > maxInclusive:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   191
            raise ValueError, "%d isn't lesser or equal to %d!"%(value, maxInclusive)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   192
        if minExclusive is not None and value <= minExclusive:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   193
            raise ValueError, "%d isn't greater than %d!"%(value, minExclusive)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   194
        if maxExclusive is not None and value >= maxExclusive:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   195
            raise ValueError, "%d isn't lesser than %d!"%(value, maxExclusive)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   196
        return value
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   197
    return GetInteger
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   198
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   199
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   200
def GenerateFloatExtraction(type, extra_values = []):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   201
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   202
    Function that generates an extraction function for float
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   203
    @param type: name of the type of float
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   204
    @return: function generated
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   205
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   206
    def GetFloat(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   207
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   208
        Function that extracts a float from a tree node or a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   209
        @param attr: tree node containing data to extract or data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   210
        @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   211
        @return: data as a float
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   212
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   213
        if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   214
            value = GetAttributeValue(attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   215
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   216
            value = attr
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   217
        try:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   218
            if value in extra_values:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   219
                return value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   220
            return float(value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   221
        except:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   222
            raise ValueError, "\"%s\" isn't a valid %s!"%(value, type)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   223
    return GetFloat
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   224
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   225
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   226
def GetBoolean(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   227
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   228
    Function that extracts a boolean from a tree node or a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   229
    @param attr: tree node containing data to extract or data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   230
    @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   231
    @return: data as a boolean
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   232
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   233
    if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   234
        value = GetAttributeValue(attr)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   235
    else:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   236
        value = attr
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   237
    if value == "true" or value == "1":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   238
        return True
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   239
    elif value == "false" or value == "0":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   240
        return False
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   241
    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   242
        raise ValueError, "\"%s\" isn't a valid boolean!"%value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   243
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   244
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   245
def GetTime(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   246
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   247
    Function that extracts a time from a tree node or a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   248
    @param attr: tree node containing data to extract or data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   249
    @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   250
    @return: data as a time
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   251
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   252
    if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   253
        result = time_model.match(GetAttributeValue(attr))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   254
    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   255
        result = time_model.match(attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   256
    if result:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   257
        values = result.groups()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   258
        time_values = [int(v) for v in values[:2]]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   259
        seconds = float(values[2])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   260
        time_values.extend([int(seconds), int((seconds % 1) * 1000000)])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   261
        return datetime.time(*time_values)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   262
    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   263
        raise ValueError, "\"%s\" is not a valid time!"%value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   264
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   265
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   266
def GetDate(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   267
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   268
    Function that extracts a date from a tree node or a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   269
    @param attr: tree node containing data to extract or data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   270
    @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   271
    @return: data as a date
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   272
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   273
    if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   274
        result = date_model.match(GetAttributeValue(attr))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   275
    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   276
        result = date_model.match(attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   277
    if result:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   278
        values = result.groups()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   279
        date_values = [int(v) for v in values[:3]]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   280
        if values[3] is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   281
            tz = xml_timezone()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   282
            tz.SetOffset(values[3])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   283
            date_values.append(tz)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   284
        return datetime.date(*date_values)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   285
    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   286
        raise ValueError, "\"%s\" is not a valid date!"%value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   287
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   288
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   289
def GetDateTime(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   290
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   291
    Function that extracts date and time from a tree node or a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   292
    @param attr: tree node containing data to extract or data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   293
    @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   294
    @return: data as date and time
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   295
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   296
    if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   297
        result = datetime_model.match(GetAttributeValue(attr))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   298
    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   299
        result = datetime_model.match(attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   300
    if result:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   301
        values = result.groups()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   302
        datetime_values = [int(v) for v in values[:5]]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   303
        seconds = float(values[5])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   304
        datetime_values.extend([int(seconds), int((seconds % 1) * 1000000)])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   305
        if values[6] is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   306
            tz = xml_timezone()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   307
            tz.SetOffset(values[6])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   308
            datetime_values.append(tz)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   309
        return datetime.datetime(*datetime_values)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   310
    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   311
        raise ValueError, "\"%s\" is not a valid datetime!"%value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   312
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   313
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   314
def GenerateModelNameExtraction(type, model):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   315
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   316
    Function that generates an extraction function for string matching a model
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   317
    @param type: name of the data type
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   318
    @param model: model that data must match
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   319
    @return: function generated
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   320
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   321
    def GetModelName(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   322
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   323
        Function that extracts a string from a tree node or not and check that
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   324
        string extracted or given match the model
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   325
        @param attr: tree node containing data to extract or data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   326
        @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   327
        @return: data as a string if matching
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   328
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   329
        if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   330
            value = GetAttributeValue(attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   331
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   332
            value = attr
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   333
        result = model.match(value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   334
        if not result:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   335
            raise ValueError, "\"%s\" is not a valid %s!"%(value, type)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   336
        return value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   337
    return GetModelName
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   338
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   339
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   340
def GenerateLimitExtraction(min = None, max = None, unbounded = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   341
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   342
    Function that generates an extraction function for integer defining min and max
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   343
    of integer value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   344
    @param min: minimum limit value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   345
    @param max: maximum limit value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   346
    @param unbounded: value can be "unbounded" or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   347
    @return: function generated
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   348
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   349
    def GetLimit(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   350
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   351
        Function that extracts a string from a tree node or not and check that
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   352
        string extracted or given is in a list of values
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   353
        @param attr: tree node containing data to extract or data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   354
        @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   355
        @return: data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   356
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   357
        if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   358
            value = GetAttributeValue(attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   359
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   360
            value = attr
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   361
        if value == "unbounded":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   362
            if unbounded:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   363
                return value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   364
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   365
                raise "\"%s\" isn't a valid value for this member limit!"%value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   366
        try:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   367
            limit = int(value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   368
        except:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   369
            raise "\"%s\" isn't a valid value for this member limit!"%value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   370
        if limit < 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   371
            raise "\"%s\" isn't a valid value for this member limit!"%value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   372
        elif min is not None and limit < min:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   373
            raise "\"%s\" isn't a valid value for this member limit!"%value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   374
        elif max is not None and limit > max:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   375
            raise "\"%s\" isn't a valid value for this member limit!"%value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   376
        return limit
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   377
    return GetLimit
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   378
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   379
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   380
def GenerateEnumeratedExtraction(type, list):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   381
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   382
    Function that generates an extraction function for enumerated values
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   383
    @param type: name of the data type
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   384
    @param list: list of possible values
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   385
    @return: function generated
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   386
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   387
    def GetEnumerated(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   388
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   389
        Function that extracts a string from a tree node or not and check that
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   390
        string extracted or given is in a list of values
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   391
        @param attr: tree node containing data to extract or data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   392
        @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   393
        @return: data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   394
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   395
        if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   396
            value = GetAttributeValue(attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   397
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   398
            value = attr
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   399
        if value in list:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   400
            return value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   401
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   402
            raise ValueError, "\"%s\" isn't a valid value for %s!"%(value, type)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   403
    return GetEnumerated
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   404
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   405
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   406
def GetNamespaces(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   407
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   408
    Function that extracts a list of namespaces from a tree node or a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   409
    @param attr: tree node containing data to extract or data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   410
    @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   411
    @return: list of namespaces
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   412
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   413
    if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   414
        value = GetAttributeValue(attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   415
    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   416
        value = attr
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   417
    if value == "":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   418
        return []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   419
    elif value == "##any" or value == "##other":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   420
        namespaces = [value]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   421
    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   422
        namespaces = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   423
        for item in value.split(" "):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   424
            if item == "##targetNamespace" or item == "##local":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   425
                namespaces.append(item)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   426
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   427
                result = URI_model.match(item)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   428
                if result is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   429
                    namespaces.append(item)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   430
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   431
                    raise ValueError, "\"%s\" isn't a valid value for namespace!"%value
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   432
    return namespaces
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   433
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   434
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   435
def GenerateGetList(type, list):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   436
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   437
    Function that generates an extraction function for a list of values
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   438
    @param type: name of the data type
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   439
    @param list: list of possible values
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   440
    @return: function generated
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   441
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   442
    def GetLists(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   443
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   444
        Function that extracts a list of values from a tree node or a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   445
        @param attr: tree node containing data to extract or data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   446
        @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   447
        @return: list of values
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   448
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   449
        if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   450
            value = GetAttributeValue(attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   451
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   452
            value = attr
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   453
        if value == "":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   454
            return []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   455
        elif value == "#all":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   456
            return [value]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   457
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   458
            values = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   459
            for item in value.split(" "):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   460
                if item in list:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   461
                    values.append(item)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   462
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   463
                    raise ValueError, "\"%s\" isn't a valid value for %s!"%(value, type)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   464
            return values
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   465
    return GetLists
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   466
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   467
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   468
def GenerateModelNameListExtraction(type, model):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   469
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   470
    Function that generates an extraction function for list of string matching a model
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   471
    @param type: name of the data type
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   472
    @param model: model that list elements must match
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   473
    @return: function generated
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   474
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   475
    def GetModelNameList(attr, extract = True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   476
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   477
        Function that extracts a list of string from a tree node or not and check 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   478
        that all the items extracted match the model
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   479
        @param attr: tree node containing data to extract or data as a string
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   480
        @param extract: attr is a tree node or not
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   481
        @return: data as a list of string if matching 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   482
        """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   483
        if extract:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   484
            value = GetAttributeValue(attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   485
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   486
            value = attr
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   487
        values = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   488
        for item in value.split(" "):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   489
            result = model.match(item)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   490
            if result is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   491
                values.append(item)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   492
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   493
                raise ValueError, "\"%s\" isn't a valid value for %s!"%(value, type)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   494
        return values
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   495
    return GetModelNameList
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   496
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   497
def GenerateAnyInfos():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   498
    def ExtractAny(tree):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   499
        return tree.data.encode()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   500
    
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   501
    def GenerateAny(value, name = None, indent = 0):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   502
        return "<![CDATA[%s]]>\n"%str(value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   503
        
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   504
    return {
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   505
        "type" : COMPLEXTYPE, 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   506
        "extract" : ExtractAny,
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   507
        "generate" : GenerateAny,
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   508
        "initial" : lambda: "",
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   509
        "check" : lambda x: isinstance(x, (StringType, UnicodeType))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   510
    }
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   511
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   512
def GenerateTagInfos(name):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   513
    def ExtractTag(tree):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   514
        if len(tree._attrs) > 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   515
            raise ValueError, "\"%s\" musn't have attributes!"%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   516
        if len(tree.childNodes) > 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   517
            raise ValueError, "\"%s\" musn't have children!"%name
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents:
diff changeset
   518
        return None
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   519
    
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   520
    def GenerateTag(value, name = None, indent = 0):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   521
        if name is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   522
            ind1, ind2 = getIndent(indent, name)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   523
            return ind1 + "<%s/>\n"%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   524
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   525
            return ""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   526
    
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   527
    return {
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   528
        "type" : TAG, 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   529
        "extract" : ExtractTag,
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   530
        "generate" : GenerateTag,
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   531
        "initial" : lambda: None,
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   532
        "check" : lambda x: x == None
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   533
    }
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   534
    
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   535
def GenerateContentInfos(factory, choices):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   536
    def GetContentInitial():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   537
        content_name, infos = choices[0]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   538
        if isinstance(infos["elmt_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   539
            namespace, name = DecomposeQualifiedName(infos["elmt_type"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   540
            infos["elmt_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   541
        if infos["maxOccurs"] == "unbounded" or infos["maxOccurs"] > 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   542
            return {"name" : content_name, "value" : map(infos["elmt_type"]["initial"], range(infos["minOccurs"]))}
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   543
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   544
            return {"name" : content_name, "value" : infos["elmt_type"]["initial"]()}
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   545
    
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   546
    def CheckContent(value):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   547
        for content_name, infos in choices:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   548
            if content_name == value["name"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   549
                if isinstance(infos["elmt_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   550
                    namespace, name = DecomposeQualifiedName(infos["elmt_type"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   551
                    infos["elmt_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   552
                if infos["maxOccurs"] == "unbounded" or infos["maxOccurs"] > 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   553
                    if isinstance(value["value"], ListType) and infos["minOccurs"] <= len(value["value"]) <= infos["maxOccurs"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   554
                        return reduce(lambda x, y: x and y, map(infos["elmt_type"]["check"], value["value"]), True)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   555
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   556
                    return infos["elmt_type"]["check"](value["value"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   557
        return False
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   558
        
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   559
    def ExtractContent(tree, content):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   560
        for content_name, infos in choices:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   561
            if content_name == tree.nodeName:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   562
                if isinstance(infos["elmt_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   563
                    namespace, name = DecomposeQualifiedName(infos["elmt_type"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   564
                    infos["elmt_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   565
                if infos["maxOccurs"] == "unbounded" or infos["maxOccurs"] > 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   566
                    if isinstance(content, ListType) and len(content) > 0 and content[-1]["name"] == content_name:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   567
                        content_item = content.pop(-1)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   568
                        content_item["value"].append(infos["elmt_type"]["extract"](tree))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   569
                        return content_item
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   570
                    elif not isinstance(content, ListType) and content is not None and content["name"] == content_name:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   571
                        return {"name" : content_name, "value" : content["value"] + [infos["elmt_type"]["extract"](tree)]}
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   572
                    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   573
                        return {"name" : content_name, "value" : [infos["elmt_type"]["extract"](tree)]}
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   574
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   575
                    return {"name" : content_name, "value" : infos["elmt_type"]["extract"](tree)}
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   576
        raise ValueError, "Invalid element \"%s\" for content!"%tree.nodeName
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   577
    
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   578
    def GenerateContent(value, name = None, indent = 0):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   579
        for content_name, infos in choices:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   580
            if content_name == value["name"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   581
                if isinstance(infos["elmt_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   582
                    namespace, name = DecomposeQualifiedName(infos["elmt_type"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   583
                    infos["elmt_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   584
                if infos["maxOccurs"] == "unbounded" or infos["maxOccurs"] > 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   585
                    text = ""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   586
                    for item in value["value"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   587
                        text += infos["elmt_type"]["generate"](item, content_name, indent)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   588
                    return text
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   589
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   590
                    return infos["elmt_type"]["generate"](value["value"], content_name, indent)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   591
        return ""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   592
        
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   593
    return {
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   594
        "initial" : GetContentInitial,
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   595
        "check" : CheckContent,
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   596
        "extract" : ExtractContent,
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   597
        "generate" : GenerateContent
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   598
    }
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   599
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   600
#-------------------------------------------------------------------------------
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   601
#                           Structure extraction functions
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   602
#-------------------------------------------------------------------------------
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   603
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   604
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   605
def DecomposeQualifiedName(name):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   606
    result = QName_model.match(name)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   607
    if not result:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   608
        raise ValueError, "\"%s\" isn't a valid QName value!"%name 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   609
    parts = result.groups()[0].split(':')
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   610
    if len(parts) == 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   611
        return None, parts[0]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   612
    return parts
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   613
    
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   614
def GenerateElement(element_name, attributes, elements_model, accept_text = False):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   615
    def ExtractElement(factory, node):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   616
        attrs = factory.ExtractNodeAttrs(element_name, node, attributes)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   617
        children_structure = ""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   618
        children_infos = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   619
        children = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   620
        for child in node.childNodes:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   621
            if child.nodeName not in ["#comment", "#text"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   622
                namespace, childname = DecomposeQualifiedName(child.nodeName)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   623
                children_structure += "%s "%childname
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   624
        result = elements_model.match(children_structure)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   625
        if not result:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   626
            raise ValueError, "Invalid structure for \"%s\" children!. First element invalid."%node.nodeName
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   627
        valid = result.groups()[0]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   628
        if len(valid) < len(children_structure):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   629
            raise ValueError, "Invalid structure for \"%s\" children!. Element number %d invalid."%(node.nodeName, len(valid.split(" ")) - 1)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   630
        for child in node.childNodes:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   631
            if child.nodeName != "#comment" and (accept_text or child.nodeName != "#text"):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   632
                if child.nodeName == "#text":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   633
                    children.append(GetAttributeValue(node))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   634
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   635
                    namespace, childname = DecomposeQualifiedName(child.nodeName)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   636
                    infos = factory.GetQualifiedNameInfos(childname, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   637
                    if infos["type"] != SYNTAXELEMENT:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   638
                        raise ValueError, "\"%s\" can't be a member child!"%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   639
                    if element_name in infos["extract"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   640
                        children.append(infos["extract"][element_name](factory, child))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   641
                    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   642
                        children.append(infos["extract"]["default"](factory, child))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   643
        return node.nodeName, attrs, children
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   644
    return ExtractElement
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   645
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   646
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   647
"""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   648
Class that generate class from an XML Tree
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
   649
"""
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   650
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
   651
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   652
    def __init__(self, document, debug = False):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   653
        self.Document = document
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   654
        self.Debug = debug
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
   655
        
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   656
        # 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
   657
        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
   658
        
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   659
        self.DefinedNamespaces = {}
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   660
        self.Namespaces = {}
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   661
        self.SchemaNamespace = None
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   662
        self.TargetNamespace = None
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   663
        
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   664
        self.CurrentCompilations = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   665
        
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
   666
        # Dictionaries for stocking Classes and Types generated
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   667
        self.ComputeAfter = []
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
        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
   669
        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
   670
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   671
    def GetQualifiedNameInfos(self, name, namespace = None, canbenone = False):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   672
        if namespace is None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   673
            if name in self.Namespaces[self.SchemaNamespace]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   674
                return self.Namespaces[self.SchemaNamespace][name]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   675
            for space, elements in self.Namespaces.items():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   676
                if space != self.SchemaNamespace and name in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   677
                    return elements[name]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   678
            parts = name.split("_", 1)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   679
            if len(parts) > 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   680
                group = self.GetQualifiedNameInfos(parts[0], namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   681
                if group is not None and group["type"] == ELEMENTSGROUP:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   682
                    elements = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   683
                    if "elements" in group:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   684
                        elements = group["elements"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   685
                    elif "choices" in group:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   686
                        elements = group["choices"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   687
                    for element in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   688
                        if element["name"] == parts[1]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   689
                            return element
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   690
            if not canbenone:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   691
                raise ValueError, "Unknown element \"%s\" for any defined namespaces!"%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   692
        elif namespace in self.Namespaces:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   693
            if name in self.Namespaces[namespace]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   694
                return self.Namespaces[namespace][name]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   695
            parts = name.split("_", 1)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   696
            if len(parts) > 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   697
                group = self.GetQualifiedNameInfos(parts[0], namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   698
                if group is not None and group["type"] == ELEMENTSGROUP:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   699
                    elements = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   700
                    if "elements" in group:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   701
                        elements = group["elements"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   702
                    elif "choices" in group:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   703
                        elements = group["choices"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   704
                    for element in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   705
                        if element["name"] == parts[1]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   706
                            return element
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   707
            if not canbenone:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   708
                raise ValueError, "Unknown element \"%s\" for namespace \"%s\"!"%(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   709
        elif not canbenone:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   710
            raise ValueError, "Unknown namespace \"%s\"!"%namespace
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   711
        return None
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   712
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   713
    def SplitQualifiedName(self, name, namespace = None, canbenone = False):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   714
        if namespace is None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   715
            if name in self.Namespaces[self.SchemaNamespace]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   716
                return name, None
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   717
            for space, elements in self.Namespaces.items():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   718
                if space != self.SchemaNamespace and name in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   719
                    return name, None
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   720
            parts = name.split("_", 1)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   721
            if len(parts) > 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   722
                group = self.GetQualifiedNameInfos(parts[0], namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   723
                if group is not None and group["type"] == ELEMENTSGROUP:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   724
                    elements = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   725
                    if "elements" in group:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   726
                        elements = group["elements"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   727
                    elif "choices" in group:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   728
                        elements = group["choices"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   729
                    for element in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   730
                        if element["name"] == parts[1]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   731
                            return part[1], part[0]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   732
            if not canbenone:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   733
                raise ValueError, "Unknown element \"%s\" for any defined namespaces!"%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   734
        elif namespace in self.Namespaces:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   735
            if name in self.Namespaces[namespace]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   736
                return name, None
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   737
            parts = name.split("_", 1)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   738
            if len(parts) > 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   739
                group = self.GetQualifiedNameInfos(parts[0], namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   740
                if group is not None and group["type"] == ELEMENTSGROUP:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   741
                    elements = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   742
                    if "elements" in group:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   743
                        elements = group["elements"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   744
                    elif "choices" in group:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   745
                        elements = group["choices"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   746
                    for element in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   747
                        if element["name"] == parts[1]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   748
                            return parts[1], parts[0]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   749
            if not canbenone:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   750
                raise ValueError, "Unknown element \"%s\" for namespace \"%s\"!"%(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   751
        elif not canbenone:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   752
            raise ValueError, "Unknown namespace \"%s\"!"%namespace
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   753
        return None, None
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   754
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   755
    def ExtractNodeAttrs(self, element_name, node, valid_attrs):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   756
        attrs = {}
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   757
        for qualified_name, attr in node._attrs.items():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   758
            namespace, name =  DecomposeQualifiedName(qualified_name)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   759
            if name in valid_attrs:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   760
                infos = self.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   761
                if infos["type"] != SYNTAXATTRIBUTE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   762
                    raise ValueError, "\"%s\" can't be a member attribute!"%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   763
                elif name in attrs:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   764
                    raise ValueError, "\"%s\" attribute has been twice!"%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   765
                elif element_name in infos["extract"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   766
                    attrs[name] = infos["extract"][element_name](attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   767
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   768
                    attrs[name] = infos["extract"]["default"](attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   769
            elif namespace == "xmlns":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   770
                infos = self.GetQualifiedNameInfos("anyURI", self.SchemaNamespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   771
                self.DefinedNamespaces[infos["extract"](attr)] = name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   772
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   773
                raise ValueError, "Invalid attribute \"%s\" for member \"%s\"!"%(qualified_name, node.nodeName)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   774
        for attr in valid_attrs:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   775
            if attr not in attrs and attr in self.Namespaces[self.SchemaNamespace] and "default" in self.Namespaces[self.SchemaNamespace][attr]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   776
                if element_name in self.Namespaces[self.SchemaNamespace][attr]["default"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   777
                    default = self.Namespaces[self.SchemaNamespace][attr]["default"][element_name]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   778
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   779
                    default = self.Namespaces[self.SchemaNamespace][attr]["default"]["default"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   780
                if default is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   781
                    attrs[attr] = default
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   782
        return attrs
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   783
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   784
    def ReduceElements(self, elements, schema=False):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   785
        result = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   786
        for child_infos in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   787
            if "name" in child_infos[1] and schema:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   788
                self.CurrentCompilations.append(child_infos[1]["name"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   789
            namespace, name = DecomposeQualifiedName(child_infos[0])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   790
            infos = self.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   791
            if infos["type"] != SYNTAXELEMENT:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   792
                raise ValueError, "\"%s\" can't be a member child!"%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   793
            result.append(infos["reduce"](self, child_infos[1], child_infos[2]))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   794
            if "name" in child_infos[1] and schema:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   795
                self.CurrentCompilations.pop(-1)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   796
        annotations = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   797
        children = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   798
        for element in result:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   799
            if element["type"] == "annotation":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   800
                annotations.append(element)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   801
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   802
                children.append(element)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   803
        return annotations, children
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   804
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   805
    def AddComplexType(self, typename, infos):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   806
        if typename not in self.XMLClassDefinitions:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   807
            self.XMLClassDefinitions[typename] = infos
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   808
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   809
            raise ValueError, "\"%s\" class already defined. Choose another name!"%typename
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   810
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   811
    def ParseSchema(self):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   812
        pass
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   813
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   814
    def ExtractTypeInfos(self, name, parent, typeinfos):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   815
        if isinstance(typeinfos, (StringType, UnicodeType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   816
            namespace, name = DecomposeQualifiedName(typeinfos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   817
            infos = self.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   818
            if infos["type"] == COMPLEXTYPE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   819
                name, parent = self.SplitQualifiedName(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   820
                result = self.CreateClass(name, parent, infos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   821
                if result is not None and not isinstance(result, (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   822
                    self.Namespaces[self.TargetNamespace][result["name"]] = result
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   823
                return result
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   824
            elif infos["type"] == ELEMENT and infos["elmt_type"]["type"] == COMPLEXTYPE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   825
                name, parent = self.SplitQualifiedName(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   826
                result = self.CreateClass(name, parent, infos["elmt_type"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   827
                if result is not None and not isinstance(result, (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   828
                    self.Namespaces[self.TargetNamespace][result["name"]] = result
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   829
                return result
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   830
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   831
                return infos
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   832
        elif typeinfos["type"] == COMPLEXTYPE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   833
            return self.CreateClass(name, parent, typeinfos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   834
        elif typeinfos["type"] == SIMPLETYPE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   835
            return typeinfos
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   836
            
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   837
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   838
    Methods that generates the classes
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   839
    """
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   840
    def CreateClasses(self):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   841
        self.ParseSchema()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   842
        for name, infos in self.Namespaces[self.TargetNamespace].items():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   843
            if infos["type"] == ELEMENT:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   844
                if not isinstance(infos["elmt_type"], (UnicodeType, StringType)) and infos["elmt_type"]["type"] == COMPLEXTYPE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   845
                    self.ComputeAfter.append((name, None, infos["elmt_type"], True))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   846
                    while len(self.ComputeAfter) > 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   847
                        result = self.CreateClass(*self.ComputeAfter.pop(0))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   848
                        if result is not None and not isinstance(result, (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   849
                            self.Namespaces[self.TargetNamespace][result["name"]] = result
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   850
            elif infos["type"] == COMPLEXTYPE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   851
                self.ComputeAfter.append((name, None, infos))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   852
                while len(self.ComputeAfter) > 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   853
                    result = self.CreateClass(*self.ComputeAfter.pop(0))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   854
                    if result is not None and not isinstance(result, (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   855
                        self.Namespaces[self.TargetNamespace][result["name"]] = result
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   856
            elif infos["type"] == ELEMENTSGROUP:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   857
                elements = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   858
                if "elements" in infos:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   859
                    elements = infos["elements"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   860
                elif "choices" in infos:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   861
                    elements = infos["choices"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   862
                for element in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   863
                    if not isinstance(element["elmt_type"], (UnicodeType, StringType)) and element["elmt_type"]["type"] == COMPLEXTYPE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   864
                        self.ComputeAfter.append((element["name"], infos["name"], element["elmt_type"]))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   865
                        while len(self.ComputeAfter) > 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   866
                            result = self.CreateClass(*self.ComputeAfter.pop(0))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   867
                            if result is not None and not isinstance(result, (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   868
                                self.Namespaces[self.TargetNamespace][result["name"]] = result
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   869
        return self.ComputedClasses
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   870
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   871
    def CreateClass(self, name, parent, classinfos, baseclass = False):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   872
        if parent is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   873
            classname = "%s_%s"%(parent, name)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   874
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   875
            classname = name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   876
        
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   877
        # Checks that classe haven't been generated yet
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   878
        if self.AlreadyComputed.get(classname, False):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   879
            if baseclass:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   880
                self.AlreadyComputed[classname].IsBaseClass = baseclass
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   881
            return None
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   882
        
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   883
        # If base classes haven't been generated
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   884
        bases = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   885
        if "base" in classinfos:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   886
            result = self.ExtractTypeInfos("base", name, classinfos["base"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   887
            if result is None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   888
                namespace, base_name = DecomposeQualifiedName(classinfos["base"])                
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   889
                if self.AlreadyComputed.get(base_name, False):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   890
                    self.ComputeAfter.append((name, parent, classinfos))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   891
                    if self.TargetNamespace is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   892
                        return "%s:%s"%(self.TargetNamespace, 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
   893
                    else:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   894
                        return classname
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   895
            elif result is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   896
                classinfos["base"] = self.ComputedClasses[result["name"]]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   897
                bases.append(self.ComputedClasses[result["name"]])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   898
        bases.append(object)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   899
        bases = tuple(bases)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   900
        classmembers = {"__doc__" : classinfos.get("doc", ""), "IsBaseClass" : baseclass}
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   901
        
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   902
        self.AlreadyComputed[classname] = True
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   903
        
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   904
        for attribute in classinfos["attributes"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   905
            infos = self.ExtractTypeInfos(attribute["name"], name, attribute["attr_type"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   906
            if infos is not None:                    
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   907
                if infos["type"] != SIMPLETYPE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   908
                    raise ValueError, "\"%s\" type is not a simple type!"%attribute["attr_type"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   909
                attrname = attribute["name"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   910
                if attribute["use"] == "optional":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   911
                    classmembers[attrname] = None
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   912
                    classmembers["add%s"%attrname] = generateAddMethod(attrname, self, attribute)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   913
                    classmembers["delete%s"%attrname] = generateDeleteMethod(attrname)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   914
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   915
                    classmembers[attrname] = infos["initial"]()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   916
                classmembers["set%s"%attrname] = generateSetMethod(attrname)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   917
                classmembers["get%s"%attrname] = generateGetMethod(attrname)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   918
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   919
                raise ValueError, "\"%s\" type unrecognized!"%attribute["attr_type"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   920
            attribute["attr_type"] = infos
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   921
            
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   922
        for element in classinfos["elements"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   923
            if element["type"] == CHOICE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   924
                elmtname = element["name"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   925
                choices = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   926
                for choice in element["choices"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   927
                    if choice["elmt_type"] == "tag":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   928
                        choice["elmt_type"] = GenerateTagInfos(choice["name"])
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
   929
                    else:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   930
                        infos = self.ExtractTypeInfos(choice["name"], name, choice["elmt_type"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   931
                        if infos is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   932
                            choice["elmt_type"] = infos
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   933
                    choices.append((choice["name"], choice))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   934
                classmembers["get%schoices"%elmtname] = generateGetChoicesMethod(element["choices"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   935
                classmembers["add%sbytype"%elmtname] = generateAddChoiceByTypeMethod(element["choices"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   936
                infos = GenerateContentInfos(self, choices)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   937
            elif element["type"] == ANY:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   938
                elmtname = element["name"] = "text"
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   939
                element["minOccurs"] = element["maxOccurs"] = 1
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   940
                infos = GenerateAnyInfos()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   941
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   942
                elmtname = element["name"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   943
                infos = self.ExtractTypeInfos(element["name"], name, element["elmt_type"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   944
            if infos is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   945
                element["elmt_type"] = infos
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   946
            if element["maxOccurs"] == "unbounded" or element["maxOccurs"] > 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   947
                classmembers[elmtname] = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   948
                classmembers["append%s"%elmtname] = generateAppendMethod(elmtname, element["maxOccurs"], self, element)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   949
                classmembers["insert%s"%elmtname] = generateInsertMethod(elmtname, element["maxOccurs"], self, element)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   950
                classmembers["remove%s"%elmtname] = generateRemoveMethod(elmtname, element["minOccurs"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   951
                classmembers["count%s"%elmtname] = generateCountMethod(elmtname)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   952
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   953
                if element["minOccurs"] == 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   954
                    classmembers[elmtname] = None
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   955
                    classmembers["add%s"%elmtname] = generateAddMethod(elmtname, self, element)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   956
                    classmembers["delete%s"%elmtname] = generateDeleteMethod(elmtname)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   957
                elif not isinstance(element["elmt_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   958
                    classmembers[elmtname] = element["elmt_type"]["initial"]()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   959
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   960
                    classmembers[elmtname] = None
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   961
            classmembers["set%s"%elmtname] = generateSetMethod(elmtname)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   962
            classmembers["get%s"%elmtname] = generateGetMethod(elmtname)
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
   963
            
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   964
        classmembers["__init__"] = generateInitMethod(self, classinfos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   965
        classmembers["__setattr__"] = generateSetattrMethod(self, classinfos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   966
        classmembers["getStructure"] = generateStructureMethod(classinfos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   967
        classmembers["loadXMLTree"] = generateLoadXMLTree(self, classinfos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   968
        classmembers["generateXMLText"] = generateGenerateXMLText(self, classinfos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   969
        classmembers["getElementAttributes"] = generateGetElementAttributes(self, classinfos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   970
        classmembers["getElementInfos"] = generateGetElementInfos(self, classinfos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   971
        classmembers["setElementValue"] = generateSetElementValue(self, classinfos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   972
        classmembers["singleLineAttributes"] = True
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   973
        
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   974
        class_definition = classobj(str(classname), bases, classmembers)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   975
        
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   976
        self.ComputedClasses[classname] = class_definition
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   977
        
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   978
        return {"type" : COMPILEDCOMPLEXTYPE,
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   979
                "name" : classname,
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   980
                "check" : generateClassCheckFunction(class_definition),
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   981
                "initial" : generateClassCreateFunction(class_definition),
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   982
                "extract" : generateClassExtractFunction(class_definition),
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
   983
                "generate" : class_definition.generateXMLText}
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
   984
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   985
    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
   986
        # 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
   987
        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
   988
            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
   989
            
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   990
            # 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
   991
            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
   992
                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
   993
                    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
   994
                
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
   995
            # 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
   996
            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
   997
                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
   998
                    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
   999
                    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
  1000
                        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
  1001
                            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
  1002
                                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
  1003
                        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
  1004
                            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
  1005
                    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
  1006
                        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
  1007
                        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
  1008
                            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
  1009
                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
  1010
                    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
  1011
                        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
  1012
                            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
  1013
                            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
  1014
                                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
  1015
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1016
    """
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1017
    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
  1018
    """
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1019
    def PrintClasses(self):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1020
        items = self.ComputedClasses.items()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1021
        items.sort()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1022
        for classname, xmlclass in items:
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
  1023
            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
  1024
        
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1025
    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
  1026
        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
  1027
        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
  1028
        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
  1029
            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
  1030
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1031
"""
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1032
Method that generate the method for checking a class instance
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1033
"""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1034
def generateClassCheckFunction(class_definition):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1035
    def classCheckfunction(instance):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1036
        return isinstance(instance, class_definition)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1037
    return classCheckfunction
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1038
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1039
"""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1040
Method that generate the method for creating a class instance
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1041
"""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1042
def generateClassCreateFunction(class_definition):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1043
    def classCreatefunction():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1044
        return class_definition()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1045
    return classCreatefunction
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1046
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1047
"""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1048
Method that generate the method for extracting a class instance
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1049
"""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1050
def generateClassExtractFunction(class_definition):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1051
    def classExtractfunction(node):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1052
        instance = class_definition()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1053
        instance.loadXMLTree(node)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1054
        return instance
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1055
    return classExtractfunction
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1056
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1057
"""
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1058
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
  1059
attributes list defined
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1060
"""
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1061
def generateSetattrMethod(factory, classinfos):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1062
    attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1063
    optional_attributes = [attr["name"] for attr in classinfos["attributes"] if attr["use"] == "optional"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1064
    elements = dict([(element["name"], element) for element in classinfos["elements"]])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1065
    
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1066
    def setattrMethod(self, name, value):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1067
        if name in attributes:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1068
            if isinstance(attributes[name]["attr_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1069
                namespace, name = DecomposeQualifiedName(infos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1070
                attributes[name]["attr_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1071
            if value is None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1072
                if name in optional_attributes:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1073
                    return object.__setattr__(self, name, None)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1074
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1075
                    raise ValueError, "Attribute '%s' isn't optional."%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1076
            elif "fixed" in attributes[name] and value != attributes[name]["fixed"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1077
                raise ValueError, "Value of attribute '%s' can only be '%s'."%(name, str(attributes[name]["fixed"]))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1078
            elif attributes[name]["attr_type"]["check"](value):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1079
                return object.__setattr__(self, name, value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1080
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1081
                raise ValueError, "Invalid value for attribute '%s'."%(name)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1082
        elif name in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1083
            if isinstance(elements[name]["elmt_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1084
                namespace, name = DecomposeQualifiedName(infos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1085
                elements[name]["elmt_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1086
            if value is None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1087
                if elements[name]["minOccurs"] == 0 and elements[name]["maxOccurs"] == 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1088
                    return object.__setattr__(self, name, None)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1089
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1090
                    raise ValueError, "Attribute '%s' isn't optional."%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1091
            elif elements[name]["maxOccurs"] == "unbounded" or elements[name]["maxOccurs"] > 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1092
                if isinstance(value, ListType) and elements[name]["minOccurs"] <= len(value) <= elements[name]["maxOccurs"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1093
                    if reduce(lambda x, y: x and y, map(elements[name]["elmt_type"]["check"], value), True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1094
                        return object.__setattr__(self, name, value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1095
                raise ValueError, "Attribute '%s' must be a list of valid elements."%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1096
            elif "fixed" in elements[name] and value != elements[name]["fixed"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1097
                raise ValueError, "Value of attribute '%s' can only be '%s'."%(name, str(elements[name]["fixed"]))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1098
            elif elements[name]["elmt_type"]["check"](value):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1099
                return object.__setattr__(self, name, value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1100
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1101
                raise ValueError, "Invalid value for attribute '%s'."%(name)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1102
        elif "base" in classinfos:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1103
            return classinfos["base"].__setattr__(self, name, value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1104
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1105
            raise AttributeError, "'%s' can't have an attribute '%s'."%(classinfos["name"], name)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1106
        
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1107
    return setattrMethod
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1108
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1109
"""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1110
Method that generate the method for generating the xml tree structure model by 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1111
following the attributes list defined
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1112
"""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1113
def ComputeMultiplicity(name, infos):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1114
    if infos["minOccurs"] == 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1115
        if infos["maxOccurs"] == "unbounded":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1116
            return "(?:%s)*"%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1117
        elif infos["maxOccurs"] == 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1118
            return "(?:%s)?"%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1119
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1120
            return "(?:%s){0, %d}"%(name, infos["maxOccurs"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1121
    elif infos["minOccurs"] == 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1122
        if infos["maxOccurs"] == "unbounded":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1123
            return "(?:%s)+"%name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1124
        elif infos["maxOccurs"] == 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1125
            return name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1126
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1127
            return "(?:%s){1, %d}"%(name, infos["maxOccurs"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1128
    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1129
        if infos["maxOccurs"] == "unbounded":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1130
            return "(?:%s){%d}(?:%s )*"%(name, infos["minOccurs"], name)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1131
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1132
            return "(?:%s){%d, %d}"%(name, infos["minOccurs"], infos["maxOccurs"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1133
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1134
def generateStructureMethod(classinfos):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1135
    elements = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1136
    for element in classinfos["elements"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1137
        if element["type"] == ANY:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1138
            elements.append(ComputeMultiplicity("(?:#cdata-section )?", element))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1139
        elif element["type"] == CHOICE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1140
            elements.append(ComputeMultiplicity(
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1141
                "|".join([ComputeMultiplicity("%s "%infos["name"], infos) for infos in element["choices"]]), 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1142
                element))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1143
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1144
            elements.append(ComputeMultiplicity("%s "%element["name"], element))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1145
    if classinfos.get("order") or len(elements) == 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1146
        structure = "".join(elements)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1147
    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1148
        raise ValueError, "XSD structure not yet supported!"
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1149
    
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1150
    def getStructureMethod(self):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1151
        if "base" in classinfos:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1152
            return classinfos["base"].getStructure(self) + structure
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1153
        return structure
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1154
    return getStructureMethod
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1155
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1156
"""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1157
Method that generate the method for loading an xml tree by following the
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1158
attributes list defined
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1159
"""
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1160
def generateLoadXMLTree(factory, classinfos):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1161
    attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1162
    elements = dict([(element["name"], element) for element in classinfos["elements"]])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1163
    
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1164
    def loadXMLTreeMethod(self, tree, extras = [], derived = False):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1165
        if not derived:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1166
            children_structure = ""
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1167
            for node in tree.childNodes:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1168
                if node.nodeName not in ["#comment", "#text"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1169
                    children_structure += "%s "%node.nodeName
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1170
            structure_model = re.compile("(%s)$"%self.getStructure())
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1171
            result = structure_model.match(children_structure)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1172
            if not result:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1173
                raise ValueError, "Invalid structure for \"%s\" children!."%tree.nodeName
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1174
        required_attributes = [attr["name"] for attr in classinfos["attributes"] if attr["use"] == "required"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1175
        if "base" in classinfos:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1176
            extras.extend([attr["name"] for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1177
            classinfos["base"].loadXMLTree(self, tree, extras, True)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1178
        for attrname, attr in tree._attrs.items():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1179
            if attrname in attributes:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1180
                if isinstance(attributes[attrname]["attr_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1181
                    namespace, name = DecomposeQualifiedName(infos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1182
                    attributes[attrname]["attr_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1183
                setattr(self, attrname, attributes[attrname]["attr_type"]["extract"](attr))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1184
            elif "base" not in classinfos and attrname not in extras:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1185
                raise ValueError, "Invalid attribute \"%s\" for \"%s\" element!"%(attrname, tree.nodeName)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1186
            if attrname in required_attributes:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1187
                required_attributes.remove(attrname)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1188
        if len(required_attributes) > 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1189
            raise ValueError, "Required attributes %s missing for \"%s\" element!"%(", ".join(["\"%s\""%name for name in required_attributes]), tree.nodeName)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1190
        first = {}
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1191
        for node in tree.childNodes:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1192
            name = node.nodeName
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1193
            if name in ["#text", "#comment"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1194
                continue
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1195
            if name in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1196
                if isinstance(elements[name]["elmt_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1197
                    namespace, name = DecomposeQualifiedName(infos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1198
                    elements[name]["elmt_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1199
                if elements[name]["maxOccurs"] == "unbounded" or elements[name]["maxOccurs"] > 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1200
                    if first.get(name, True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1201
                        setattr(self, name, [elements[name]["elmt_type"]["extract"](node)])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1202
                        first[name] = False
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1203
                    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1204
                        getattr(self, name).append(elements[name]["elmt_type"]["extract"](node))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1205
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1206
                    setattr(self, name, elements[name]["elmt_type"]["extract"](node))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1207
            elif name == "#cdata-section" and "text" in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1208
                if elements["text"]["maxOccurs"] == "unbounded" or elements["text"]["maxOccurs"] > 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1209
                    if first.get("text", True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1210
                        setattr(self, "text", [elements["text"]["elmt_type"]["extract"](node)])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1211
                        first["text"] = False
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1212
                    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1213
                        getattr(self, "text").append(elements["text"]["elmt_type"]["extract"](node))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1214
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1215
                    setattr(self, "text", elements["text"]["elmt_type"]["extract"](node))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1216
            elif "content" in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1217
                content = getattr(self, "content")
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1218
                if elements["content"]["maxOccurs"] == "unbounded" or elements["content"]["maxOccurs"] > 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1219
                    if first.get("content", True):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1220
                        setattr(self, "content", [elements["content"]["elmt_type"]["extract"](node, None)])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1221
                        first["content"] = False
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1222
                    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1223
                        content.append(elements["content"]["elmt_type"]["extract"](node, content))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1224
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1225
                    setattr(self, "content", elements["content"]["elmt_type"]["extract"](node, content))
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1226
    return loadXMLTreeMethod
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1227
        
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1228
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1229
"""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1230
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
  1231
attributes list defined
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1232
"""
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1233
def generateGenerateXMLText(factory, classinfos):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1234
    def generateXMLTextMethod(self, name, indent = 0, extras = {}, derived = False):
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1235
        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
  1236
        if not derived:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1237
            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
  1238
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1239
            text = ""
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1240
        
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1241
        first = True
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1242
        if "base" not in classinfos:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1243
            for attr, value in extras.items():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1244
                if not first and not self.singleLineAttributes:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1245
                    text += "\n%s"%(ind2)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1246
                text += " %s=\"%s\""%(attr, value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1247
                first = False
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1248
            extras.clear()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1249
        for attr in classinfos["attributes"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1250
            if attr["use"] != "prohibited":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1251
                if isinstance(attr["attr_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1252
                    namespace, name = DecomposeQualifiedName(infos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1253
                    attr["attr_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1254
                value = getattr(self, attr["name"], None)
86
4f1dbdb0bed2 Bug on xmlclass XML file attributes generation fixed
lbessard
parents: 85
diff changeset
  1255
                if value != None:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1256
                    computed_value = attr["attr_type"]["generate"](value)
86
4f1dbdb0bed2 Bug on xmlclass XML file attributes generation fixed
lbessard
parents: 85
diff changeset
  1257
                else:
4f1dbdb0bed2 Bug on xmlclass XML file attributes generation fixed
lbessard
parents: 85
diff changeset
  1258
                    computed_value = None
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1259
                if attr["use"] != "optional" or (value != None and computed_value != attr.get("default", attr["attr_type"]["generate"](attr["attr_type"]["initial"]()))):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1260
                    if "base" in classinfos:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1261
                        extras[attr["name"]] = computed_value
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1262
                    else:
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1263
                        if not first and not self.singleLineAttributes:
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1264
                            text += "\n%s"%(ind2)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1265
                        text += " %s=\"%s\""%(attr["name"], computed_value)
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1266
                    first = False
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1267
        if "base" in classinfos:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1268
            first, new_text = classinfos["base"].generateXMLText(self, name, indent, extras, True)
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1269
            text += new_text
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1270
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1271
            first = True
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1272
        for element in classinfos["elements"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1273
            if isinstance(element["elmt_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1274
                namespace, name = DecomposeQualifiedName(infos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1275
                element["elmt_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1276
            value = getattr(self, element["name"], None)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1277
            if element["minOccurs"] == 0 and element["maxOccurs"] == 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1278
                if value 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
  1279
                    if first:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1280
                        text += ">\n"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1281
                        first = False
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1282
                    text += element["elmt_type"]["generate"](value, element["name"], indent + 1)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1283
            elif element["minOccurs"] == 1 and element["maxOccurs"] == 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1284
                if first:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1285
                    text += ">\n"
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1286
                    first = False
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1287
                text += element["elmt_type"]["generate"](value, element["name"], indent + 1)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1288
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1289
                if first and len(value) > 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1290
                    text += ">\n"
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1291
                    first = False
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1292
                for item in value:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1293
                    text += element["elmt_type"]["generate"](item, element["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
  1294
        if not derived:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1295
            if first:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1296
                text += "/>\n"
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1297
            else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1298
                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
  1299
            return text
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1300
        else:
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1301
            return first, text
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1302
    return generateXMLTextMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1303
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1304
def gettypeinfos(name, facets):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1305
    if "enumeration" in facets and facets["enumeration"][0] is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1306
        return facets["enumeration"][0]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1307
    elif "maxInclusive" in facets:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1308
        limits = {"max" : None, "min" : None}
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1309
        if facets["maxInclusive"][0] is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1310
            limits["max"] = facets["maxInclusive"][0]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1311
        elif facets["maxExclusive"][0] is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1312
            limits["max"] = facets["maxExclusive"][0] - 1
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1313
        if facets["minInclusive"][0] is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1314
            limits["min"] = facets["minInclusive"][0]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1315
        elif facets["minExclusive"][0] is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1316
            limits["min"] = facets["minExclusive"][0] + 1
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1317
        if limits["max"] is not None or limits["min"] is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1318
            return limits
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1319
    return name
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1320
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1321
def generateGetElementAttributes(factory, classinfos):
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1322
    def getElementAttributes(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1323
        attr_list = []
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1324
        for attr in classinfos["attributes"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1325
            if attr["use"] != "prohibited":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1326
                attr_params = {"name" : attr["name"], "require" : attr["use"] == "required", 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1327
                    "type" : gettypeinfos(attr["attr_type"]["basename"], attr["attr_type"]["facets"]),
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1328
                    "value" : getattr(self, attr["name"], "")}
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1329
                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
  1330
        return attr_list
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1331
    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
  1332
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1333
def generateGetElementInfos(factory, classinfos):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1334
    attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1335
    elements = dict([(element["name"], element) for element in classinfos["elements"]])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1336
    
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
  1337
    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
  1338
        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
  1339
        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
  1340
        children = []
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1341
        if path is not None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1342
            parts = path.split(".", 1)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1343
            if parts[0] in attributes:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1344
                if len(parts) != 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1345
                    raise ValueError, "Wrong path!"
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1346
                attr_type = gettypeinfos(attributes[parts[0]]["attr_type"]["basename"], 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1347
                                         attributes[parts[0]]["attr_type"]["facets"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1348
                value = getattr(self, parts[0], "")
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1349
            elif parts[0] in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1350
                if element["elmt_type"]["type"] == SIMPLETYPE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1351
                    if len(parts) != 0:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1352
                        raise ValueError, "Wrong path!"
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1353
                    attr_type = gettypeinfos(elements[parts[0]]["elmt_type"]["basename"], 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1354
                                             elements[parts[0]]["elmt_type"]["facets"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1355
                    value = getattr(self, parts[0], "")
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1356
                elif parts[0] == "content":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1357
                    return self.content["value"].getElementInfos(self.content["name"], path)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1358
                elif len(parts) == 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1359
                    return attr.getElementInfos(parts[0])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1360
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1361
                    return attr.getElementInfos(parts[0], parts[1])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1362
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1363
                raise ValueError, "Wrong path!"
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1364
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1365
            children.extend(self.getElementAttributes())
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1366
            for element_name, element in elements.items():
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1367
                if element_name == "content":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1368
                    attr_type = [(choice["name"], None) for choice in element["choices"]]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1369
                    value = self.content["name"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1370
                    children.extend(self.content["value"].getElementInfos(self.content["name"])["children"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1371
                elif element["elmt_type"]["type"] == SIMPLETYPE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1372
                    children.append({"name" : element_name, "require" : element["minOccurs"] != 0, 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1373
                        "type" : gettypeinfos(element["elmt_type"]["basename"], 
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1374
                                              element["elmt_type"]["facets"]),
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1375
                        "value" : getattr(self, element_name, None)})
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1376
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1377
                    instance = getattr(self, element_name, None)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1378
                    if instance is None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1379
                        instance = elmt_type["elmt_type"]["initial"]()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1380
                    children.append(instance.getElementInfos(element_name))
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
  1381
        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
  1382
    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
  1383
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1384
def generateSetElementValue(factory, classinfos):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1385
    attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1386
    elements = dict([(element["name"], element) for element in classinfos["elements"]])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1387
    
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
  1388
    def setElementValue(self, path, value):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1389
        if "content" in elements:
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
  1390
            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
  1391
                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
  1392
            else:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1393
                self.addcontentbytype(value)
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
  1394
        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
  1395
            parts = path.split(".", 1)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1396
            if parts[0] in attributes:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1397
                if len(parts) != 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1398
                    raise ValueError, "Wrong path!"
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1399
                if attributes[parts[0]]["attr_type"]["basename"] == "boolean":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1400
                    setattr(self, parts[0], value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1401
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1402
                    setattr(self, parts[0], attributes[parts[0]]["attr_type"]["extract"](value, False))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1403
            elif parts[0] in elements:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1404
                if elements[parts[0]]["elmt_type"]["type"] == SIMPLETYPE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1405
                    if len(parts) != 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1406
                        raise ValueError, "Wrong path!"
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1407
                    if elements[parts[0]]["elmt_type"]["basename"] == "boolean":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1408
                        setattr(self, parts[0], value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1409
                    else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1410
                        setattr(self, parts[0], elements[parts[0]]["elmt_type"]["extract"](value, False))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1411
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1412
                    instance = getattr(self, parts[0], None)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1413
                    if instance != None:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1414
                        if len(parts) == 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1415
                            instance.setElementValue(None, value)
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
  1416
                        else:
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1417
                            instance.setElementValue(parts[1], value)
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
  1418
    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
  1419
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1420
"""
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1421
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
  1422
"""
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1423
def generateInitMethod(factory, classinfos):
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1424
    def initMethod(self):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1425
        if "base" in classinfos:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1426
            classinfos["base"].__init__(self)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1427
        for attribute in classinfos["attributes"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1428
            if isinstance(attribute["attr_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1429
                namespace, name = DecomposeQualifiedName(attribute["attr_type"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1430
                attribute["attr_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1431
            if attribute["use"] == "required":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1432
                setattr(self, attribute["name"], attribute["attr_type"]["initial"]())
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1433
            elif attribute["use"] == "optional":
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1434
                if "default" in attribute:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1435
                    setattr(self, attribute["name"], attribute["attr_type"]["extract"](attribute["default"], False))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1436
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1437
                    setattr(self, attribute["name"], None)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1438
        for element in classinfos["elements"]:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1439
            if isinstance(element["elmt_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1440
                namespace, name = DecomposeQualifiedName(element["elmt_type"])
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1441
                element["elmt_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1442
            if element["minOccurs"] == 0 and element["maxOccurs"] == 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1443
                if "default" in element:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1444
                    setattr(self, element["name"], element["elmt_type"]["extract"](element["default"], False))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1445
                else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1446
                    setattr(self, element["name"], None)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1447
            elif element["minOccurs"] == 1 and element["maxOccurs"] == 1:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1448
                setattr(self, element["name"], element["elmt_type"]["initial"]())
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1449
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1450
                value = []
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1451
                for i in xrange(element["minOccurs"]):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1452
                    value.append(element["elmt_type"]["initial"]())
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1453
                setattr(self, element["name"], value)
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1454
    return initMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1455
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1456
def generateSetMethod(attr):
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1457
    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
  1458
        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
  1459
    return setMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1460
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1461
def generateGetMethod(attr):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1462
    def getMethod(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1463
        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
  1464
    return getMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1465
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1466
def generateAddMethod(attr, factory, infos):
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1467
    def addMethod(self):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1468
        if infos["type"] == ATTRIBUTE:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1469
            if isinstance(infos["attr_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1470
                namespace, name = DecomposeQualifiedName(infos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1471
                infos["attr_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1472
            initial = infos["attr_type"]["initial"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1473
            extract = infos["attr_type"]["extract"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1474
        elif infos["type"] == ELEMENT:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1475
            if isinstance(infos["elmt_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1476
                namespace, name = DecomposeQualifiedName(infos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1477
                infos["elmt_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1478
            initial = infos["elmt_type"]["initial"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1479
            extract = infos["elmt_type"]["extract"]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1480
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1481
            raise ValueError, "Invalid class attribute!"
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1482
        if "default" in infos:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1483
            setattr(self, attr, extract(infos["default"], False))
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1484
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1485
            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
  1486
    return addMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1487
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1488
def generateDeleteMethod(attr):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1489
    def deleteMethod(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1490
        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
  1491
    return deleteMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1492
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1493
def generateAppendMethod(attr, maxOccurs, factory, infos):
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1494
    def appendMethod(self, value):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1495
        if isinstance(infos["elmt_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1496
            namespace, name = DecomposeQualifiedName(infos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1497
            infos["elmt_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1498
        attr_list = getattr(self, attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1499
        if maxOccurs == "unbounded" or len(attr_list) < maxOccurs:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1500
            if infos["elmt_type"]["check"](value):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1501
                attr_list.append(value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1502
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1503
                raise ValueError, "\"%s\" value isn't valid!"%attr
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1504
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1505
            raise ValueError, "There can't be more than %d values in \"%s\"!"%(maxOccurs, attr)
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1506
    return appendMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1507
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1508
def generateInsertMethod(attr, maxOccurs, factory, infos):
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1509
    def insertMethod(self, index, value):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1510
        if isinstance(infos["elmt_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1511
            namespace, name = DecomposeQualifiedName(infos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1512
            infos["elmt_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1513
        attr_list = getattr(self, attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1514
        if maxOccurs == "unbounded" or len(attr_list) < maxOccurs:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1515
            if infos["elmt_type"]["check"](value):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1516
                attr_list.insert(index, value)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1517
            else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1518
                raise ValueError, "\"%s\" value isn't valid!"%attr
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1519
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1520
            raise ValueError, "There can't be more than %d values in \"%s\"!"%(maxOccurs, attr)
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1521
    return insertMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1522
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1523
def generateGetChoicesMethod(choice_types):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1524
    def getChoicesMethod(self):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1525
        return [choice["name"] for choice in choice_types]
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1526
    return getChoicesMethod
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1527
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1528
def generateAddChoiceByTypeMethod(choice_types):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1529
    choices = dict([(choice["name"], choice) for choice in choice_types])
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1530
    def addChoiceMethod(self, name):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1531
        if name in choices:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1532
            if isinstance(choices["name"]["elmt_type"], (UnicodeType, StringType)):
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1533
                namespace, name = DecomposeQualifiedName(infos)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1534
                choices["name"]["elmt_type"] = factory.GetQualifiedNameInfos(name, namespace)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1535
            self.content = {"name" : name, "value" : choices["name"]["elmt_type"]["initial"]()}
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1536
    return addChoiceMethod
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1537
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1538
def generateRemoveMethod(attr, minOccurs):
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1539
    def removeMethod(self, index):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1540
        attr_list = getattr(self, attr)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1541
        if len(attr_list) > minOccurs:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1542
            getattr(self, attr).pop(index)
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1543
        else:
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1544
            raise ValueError, "There can't be less than %d values in \"%s\"!"%(minOccurs, attr)
75
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1545
    return removeMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1546
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1547
def generateCountMethod(attr):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1548
    def countMethod(self):
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1549
        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
  1550
    return countMethod
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1551
82d371634f15 Changed the way class are generated, using classobj from "new" module, instead of type inheritence.
etisserant
parents: 67
diff changeset
  1552
"""
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
  1553
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
  1554
"""
5bac3213fea1 xmlclass modified for allowing class definitions for multiple XSD files and indicating which classes are the base classes
lbessard
parents: 75
diff changeset
  1555
def GenerateClasses(factory, declare = False):
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1556
    ComputedClasses = factory.CreateClasses()
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1557
    #factory.PrintClasses()
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
  1558
    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
  1559
        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
  1560
            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
  1561
        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
  1562
            sys._getframe(1).f_locals[TypeName] = Type
83
180e4a0160b2 Adding support for default values
lbessard
parents: 76
diff changeset
  1563
    globals().update(ComputedClasses)
151
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1564
    return ComputedClasses
aaa80b48bead Adding support for the new version of xmlclass
lbessard
parents: 125
diff changeset
  1565