xmlclass/xmlclass.py
author Laurent Bessard
Thu, 29 Aug 2013 00:28:39 +0200
changeset 1293 40117d02601b
parent 1291 42ea51d083ce
child 1294 f02ba5b83811
permissions -rw-r--r--
Fixed diagram editing in xmlclass refactoring
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     3
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     5
#based on the plcopen standard. 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     6
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     8
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     9
#See COPYING file for copyrights details.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    10
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    15
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    19
#General Public License for more details.
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    20
#
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    24
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    25
import os, sys
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    26
import re
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    27
import datetime
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    28
from types import *
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    29
from xml.dom import minidom
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    30
from xml.sax.saxutils import escape, unescape, quoteattr
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
    31
from lxml import etree
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    32
from new import classobj
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
    33
from collections import OrderedDict
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    34
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    35
def CreateNode(name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    36
    node = minidom.Node()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    37
    node.nodeName = name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    38
    node._attrs = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    39
    node.childNodes = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    40
    return node
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    41
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    42
def NodeRenameAttr(node, old_name, new_name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    43
    node._attrs[new_name] = node._attrs.pop(old_name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    44
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    45
def NodeSetAttr(node, name, value):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    46
    attr = minidom.Attr(name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    47
    text = minidom.Text()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    48
    text.data = value
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    49
    attr.childNodes[0] = text
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    50
    node._attrs[name] = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    51
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    52
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    53
Regular expression models for checking all kind of string values defined in XML
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    54
standard
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    55
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    56
Name_model = re.compile('([a-zA-Z_\:][\w\.\-\:]*)$')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    57
Names_model = re.compile('([a-zA-Z_\:][\w\.\-\:]*(?: [a-zA-Z_\:][\w\.\-\:]*)*)$')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    58
NMToken_model = re.compile('([\w\.\-\:]*)$')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    59
NMTokens_model = re.compile('([\w\.\-\:]*(?: [\w\.\-\:]*)*)$')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    60
QName_model = re.compile('((?:[a-zA-Z_][\w]*:)?[a-zA-Z_][\w]*)$')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    61
QNames_model = re.compile('((?:[a-zA-Z_][\w]*:)?[a-zA-Z_][\w]*(?: (?:[a-zA-Z_][\w]*:)?[a-zA-Z_][\w]*)*)$')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    62
NCName_model = re.compile('([a-zA-Z_][\w]*)$')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    63
URI_model = re.compile('((?:http://|/)?(?:[\w.-]*/?)*)$')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    64
LANGUAGE_model = re.compile('([a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*)$')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    65
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    66
ONLY_ANNOTATION = re.compile("((?:annotation )?)")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    67
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    68
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    69
Regular expression models for extracting dates and times from a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    70
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    71
time_model = re.compile('([0-9]{2}):([0-9]{2}):([0-9]{2}(?:\.[0-9]*)?)(?:Z)?$')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    72
date_model = re.compile('([0-9]{4})-([0-9]{2})-([0-9]{2})((?:[\-\+][0-9]{2}:[0-9]{2})|Z)?$')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    73
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)?$')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    74
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    75
class xml_timezone(datetime.tzinfo):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    76
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    77
    def SetOffset(self, offset):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    78
        if offset == "Z":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    79
            self.__offset = timedelta(minutes = 0)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    80
            self.__name = "UTC"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    81
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    82
            sign = {"-" : -1, "+" : 1}[offset[0]]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    83
            hours, minutes = [int(val) for val in offset[1:].split(":")]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    84
            self.__offset = timedelta(minutes=sign * (hours * 60 + minutes))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    85
            self.__name = ""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    86
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    87
    def utcoffset(self, dt):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    88
        return self.__offset
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    89
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    90
    def tzname(self, dt):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    91
        return self.__name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    92
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    93
    def dst(self, dt):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    94
        return ZERO
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    95
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    96
[SYNTAXELEMENT, SYNTAXATTRIBUTE, SIMPLETYPE, COMPLEXTYPE, COMPILEDCOMPLEXTYPE, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    97
 ATTRIBUTESGROUP, ELEMENTSGROUP, ATTRIBUTE, ELEMENT, CHOICE, ANY, TAG, CONSTRAINT,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    98
] = range(13)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    99
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   100
def NotSupportedYet(type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   101
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   102
    Function that generates a function that point out to user that datatype
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   103
    used is not supported by xmlclass yet
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   104
    @param type: data type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   105
    @return: function generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   106
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   107
    def GetUnknownValue(attr):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   108
        raise ValueError("\"%s\" type isn't supported by \"xmlclass\" yet!" % \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   109
                         type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   110
    return GetUnknownValue
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   111
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   112
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   113
This function calculates the number of whitespace for indentation
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   114
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   115
def getIndent(indent, balise):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   116
    first = indent * 2
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   117
    second = first + len(balise) + 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   118
    return u'\t'.expandtabs(first), u'\t'.expandtabs(second)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   119
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   120
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   121
def GetAttributeValue(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   122
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   123
    Function that extracts data from a tree node
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   124
    @param attr: tree node containing data to extract
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   125
    @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   126
    @return: data extracted as string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   127
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   128
    if not extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   129
        return attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   130
    if len(attr.childNodes) == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   131
        return unicode(unescape(attr.childNodes[0].data))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   132
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   133
        # content is a CDATA
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   134
        text = u''
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   135
        for node in attr.childNodes:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   136
            if not (node.nodeName == "#text" and node.data.strip() == u''):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   137
                text += unicode(unescape(node.data))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   138
        return text
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   139
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   140
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   141
def GetNormalizedString(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   142
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   143
    Function that normalizes a string according to XML 1.0. Replace  
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   144
    tabulations, line feed and carriage return by white space
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   145
    @param attr: tree node containing data to extract or data to normalize
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   146
    @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   147
    @return: data normalized as string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   148
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   149
    if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   150
        value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   151
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   152
        value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   153
    return value.replace("\t", " ").replace("\r", " ").replace("\n", " ")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   154
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   155
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   156
def GetToken(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   157
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   158
    Function that tokenizes a string according to XML 1.0. Remove any leading  
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   159
    and trailing white space and replace internal sequence of two or more 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   160
    spaces by only one white space
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   161
    @param attr: tree node containing data to extract or data to tokenize
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   162
    @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   163
    @return: data tokenized as string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   164
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   165
    return " ".join([part for part in 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   166
                     GetNormalizedString(attr, extract).split(" ")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   167
                     if part])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   168
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   169
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   170
def GetHexInteger(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   171
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   172
    Function that extracts an hexadecimal integer from a tree node or a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   173
    @param attr: tree node containing data to extract or data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   174
    @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   175
    @return: data as an integer
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   176
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   177
    if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   178
        value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   179
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   180
        value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   181
    if len(value) % 2 != 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   182
        raise ValueError("\"%s\" isn't a valid hexadecimal integer!" % value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   183
    try:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   184
        return int(value, 16)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   185
    except:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   186
        raise ValueError("\"%s\" isn't a valid hexadecimal integer!" % value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   187
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   188
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   189
def GenerateIntegerExtraction(minInclusive=None, maxInclusive=None, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   190
                              minExclusive=None, maxExclusive=None):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   191
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   192
    Function that generates an extraction function for integer defining min and
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   193
    max of integer value
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   194
    @param minInclusive: inclusive minimum
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   195
    @param maxInclusive: inclusive maximum
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   196
    @param minExclusive: exclusive minimum
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   197
    @param maxExclusive: exclusive maximum
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   198
    @return: function generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   199
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   200
    def GetInteger(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   201
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   202
        Function that extracts an integer from a tree node or a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   203
        @param attr: tree node containing data to extract or data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   204
        @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   205
        @return: data as an integer
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   206
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   207
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   208
        if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   209
            value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   210
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   211
            value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   212
        try:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   213
            # TODO: permit to write value like 1E2
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   214
            value = int(value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   215
        except:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   216
            raise ValueError("\"%s\" isn't a valid integer!" % value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   217
        if minInclusive is not None and value < minInclusive:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   218
            raise ValueError("\"%d\" isn't greater or equal to %d!" % \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   219
                             (value, minInclusive))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   220
        if maxInclusive is not None and value > maxInclusive:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   221
            raise ValueError("\"%d\" isn't lesser or equal to %d!" % \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   222
                             (value, maxInclusive))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   223
        if minExclusive is not None and value <= minExclusive:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   224
            raise ValueError("\"%d\" isn't greater than %d!" % \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   225
                             (value, minExclusive))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   226
        if maxExclusive is not None and value >= maxExclusive:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   227
            raise ValueError("\"%d\" isn't lesser than %d!" % \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   228
                             (value, maxExclusive))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   229
        return value
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   230
    return GetInteger
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   231
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   232
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   233
def GenerateFloatExtraction(type, extra_values=[]):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   234
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   235
    Function that generates an extraction function for float
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   236
    @param type: name of the type of float
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   237
    @return: function generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   238
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   239
    def GetFloat(attr, extract = True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   240
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   241
        Function that extracts a float from a tree node or a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   242
        @param attr: tree node containing data to extract or data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   243
        @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   244
        @return: data as a float
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   245
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   246
        if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   247
            value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   248
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   249
            value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   250
        if value in extra_values:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   251
            return value
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   252
        try:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   253
            return float(value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   254
        except:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   255
            raise ValueError("\"%s\" isn't a valid %s!" % (value, type))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   256
    return GetFloat
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   257
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   258
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   259
def GetBoolean(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   260
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   261
    Function that extracts a boolean from a tree node or a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   262
    @param attr: tree node containing data to extract or data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   263
    @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   264
    @return: data as a boolean
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   265
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   266
    if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   267
        value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   268
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   269
        value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   270
    if value == "true" or value == "1":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   271
        return True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   272
    elif value == "false" or value == "0":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   273
        return False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   274
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   275
        raise ValueError("\"%s\" isn't a valid boolean!" % value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   276
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   277
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   278
def GetTime(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   279
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   280
    Function that extracts a time from a tree node or a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   281
    @param attr: tree node containing data to extract or data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   282
    @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   283
    @return: data as a time
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   284
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   285
    if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   286
        value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   287
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   288
        value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   289
    result = time_model.match(value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   290
    if result:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   291
        values = result.groups()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   292
        time_values = [int(v) for v in values[:2]]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   293
        seconds = float(values[2])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   294
        time_values.extend([int(seconds), int((seconds % 1) * 1000000)])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   295
        return datetime.time(*time_values)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   296
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   297
        raise ValueError("\"%s\" isn't a valid time!" % value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   298
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   299
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   300
def GetDate(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   301
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   302
    Function that extracts a date from a tree node or a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   303
    @param attr: tree node containing data to extract or data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   304
    @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   305
    @return: data as a date
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   306
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   307
    if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   308
        value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   309
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   310
        value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   311
    result = date_model.match(value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   312
    if result:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   313
        values = result.groups()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   314
        date_values = [int(v) for v in values[:3]]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   315
        if values[3] is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   316
            tz = xml_timezone()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   317
            tz.SetOffset(values[3])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   318
            date_values.append(tz)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   319
        return datetime.date(*date_values)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   320
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   321
        raise ValueError("\"%s\" isn't a valid date!" % value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   322
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   323
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   324
def GetDateTime(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   325
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   326
    Function that extracts date and time from a tree node or a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   327
    @param attr: tree node containing data to extract or data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   328
    @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   329
    @return: data as date and time
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   330
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   331
    if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   332
        value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   333
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   334
        value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   335
    result = datetime_model.match(value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   336
    if result:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   337
        values = result.groups()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   338
        datetime_values = [int(v) for v in values[:5]]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   339
        seconds = float(values[5])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   340
        datetime_values.extend([int(seconds), int((seconds % 1) * 1000000)])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   341
        if values[6] is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   342
            tz = xml_timezone()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   343
            tz.SetOffset(values[6])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   344
            datetime_values.append(tz)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   345
        return datetime.datetime(*datetime_values)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   346
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   347
        raise ValueError("\"%s\" isn't a valid datetime!" % value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   348
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   349
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   350
def GenerateModelNameExtraction(type, model):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   351
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   352
    Function that generates an extraction function for string matching a model
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   353
    @param type: name of the data type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   354
    @param model: model that data must match
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   355
    @return: function generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   356
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   357
    def GetModelName(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   358
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   359
        Function that extracts a string from a tree node or not and check that
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   360
        string extracted or given match the model
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   361
        @param attr: tree node containing data to extract or data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   362
        @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   363
        @return: data as a string if matching
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   364
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   365
        if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   366
            value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   367
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   368
            value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   369
        result = model.match(value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   370
        if not result:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   371
            raise ValueError("\"%s\" isn't a valid %s!" % (value, type))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   372
        return value
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   373
    return GetModelName
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   374
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   375
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   376
def GenerateLimitExtraction(min=None, max=None, unbounded=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   377
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   378
    Function that generates an extraction function for integer defining min and
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   379
    max of integer value
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   380
    @param min: minimum limit value
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   381
    @param max: maximum limit value
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   382
    @param unbounded: value can be "unbounded" or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   383
    @return: function generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   384
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   385
    def GetLimit(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   386
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   387
        Function that extracts a string from a tree node or not and check that
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   388
        string extracted or given is in a list of values
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   389
        @param attr: tree node containing data to extract or data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   390
        @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   391
        @return: data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   392
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   393
        if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   394
            value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   395
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   396
            value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   397
        if value == "unbounded":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   398
            if unbounded:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   399
                return value
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   400
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   401
                raise ValueError("Member limit can't be defined to \"unbounded\"!")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   402
        try:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   403
            limit = int(value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   404
        except:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   405
            raise ValueError("\"%s\" isn't a valid value for this member limit!" % value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   406
        if limit < 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   407
            raise ValueError("Member limit can't be negative!")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   408
        elif min is not None and limit < min:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   409
            raise ValueError("Member limit can't be lower than \"%d\"!" % min)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   410
        elif max is not None and limit > max:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   411
            raise ValueError("Member limit can't be upper than \"%d\"!" % max)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   412
        return limit
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   413
    return GetLimit
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   414
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   415
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   416
def GenerateEnumeratedExtraction(type, list):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   417
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   418
    Function that generates an extraction function for enumerated values
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   419
    @param type: name of the data type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   420
    @param list: list of possible values
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   421
    @return: function generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   422
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   423
    def GetEnumerated(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   424
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   425
        Function that extracts a string from a tree node or not and check that
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   426
        string extracted or given is in a list of values
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   427
        @param attr: tree node containing data to extract or data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   428
        @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   429
        @return: data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   430
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   431
        if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   432
            value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   433
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   434
            value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   435
        if value in list:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   436
            return value
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   437
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   438
            raise ValueError("\"%s\" isn't a valid value for %s!" % \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   439
                             (value, type))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   440
    return GetEnumerated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   441
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   442
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   443
def GetNamespaces(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   444
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   445
    Function that extracts a list of namespaces from a tree node or a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   446
    @param attr: tree node containing data to extract or data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   447
    @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   448
    @return: list of namespaces
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   449
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   450
    if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   451
        value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   452
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   453
        value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   454
    if value == "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   455
        return []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   456
    elif value == "##any" or value == "##other":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   457
        namespaces = [value]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   458
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   459
        namespaces = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   460
        for item in value.split(" "):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   461
            if item == "##targetNamespace" or item == "##local":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   462
                namespaces.append(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   463
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   464
                result = URI_model.match(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   465
                if result is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   466
                    namespaces.append(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   467
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   468
                    raise ValueError("\"%s\" isn't a valid value for namespace!" % value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   469
    return namespaces
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   470
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   471
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   472
def GenerateGetList(type, list):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   473
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   474
    Function that generates an extraction function for a list of values
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   475
    @param type: name of the data type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   476
    @param list: list of possible values
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   477
    @return: function generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   478
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   479
    def GetLists(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   480
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   481
        Function that extracts a list of values from a tree node or a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   482
        @param attr: tree node containing data to extract or data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   483
        @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   484
        @return: list of values
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   485
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   486
        if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   487
            value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   488
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   489
            value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   490
        if value == "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   491
            return []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   492
        elif value == "#all":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   493
            return [value]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   494
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   495
            values = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   496
            for item in value.split(" "):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   497
                if item in list:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   498
                    values.append(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   499
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   500
                    raise ValueError("\"%s\" isn't a valid value for %s!" % \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   501
                                     (value, type))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   502
            return values
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   503
    return GetLists
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   504
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   505
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   506
def GenerateModelNameListExtraction(type, model):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   507
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   508
    Function that generates an extraction function for list of string matching
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   509
    a model
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   510
    @param type: name of the data type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   511
    @param model: model that list elements must match
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   512
    @return: function generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   513
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   514
    def GetModelNameList(attr, extract=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   515
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   516
        Function that extracts a list of string from a tree node or not and
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   517
        check that all extracted items match the model
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   518
        @param attr: tree node containing data to extract or data as a string
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   519
        @param extract: attr is a tree node or not
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   520
        @return: data as a list of string if matching 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   521
        """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   522
        if extract:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   523
            value = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   524
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   525
            value = attr
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   526
        values = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   527
        for item in value.split(" "):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   528
            result = model.match(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   529
            if result is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   530
                values.append(item)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   531
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   532
                raise ValueError("\"%s\" isn't a valid value for %s!" % \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   533
                                 (value, type))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   534
        return values
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   535
    return GetModelNameList
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   536
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   537
def GenerateAnyInfos(infos):
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   538
    
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   539
    def ExtractAny(tree):
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   540
        if infos["namespace"][0] == "##any":
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   541
            return tree.xpath("p/text()")[0]
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   542
        return tree.xpath("ns:p/text()", namespaces={"ns": infos["namespace"][0]})[0]
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   543
    
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   544
    def GenerateAny(tree, value):
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   545
        if infos["namespace"][0] == "##any":
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   546
            p = tree.xpath("p")[0]
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   547
        else:
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   548
            p = tree.xpath("ns:p", namespaces={"ns": infos["namespace"][0]})[0]
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   549
        p.text = etree.CDATA(value)
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   550
        
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   551
    def InitialAny():
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   552
        if infos["namespace"][0] == "##any":
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   553
            element_name = "p"
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   554
        else:
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   555
            element_name = "{%s}p" % infos["namespace"][0]
1293
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
   556
        p = etree.Element(element_name)
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
   557
        p.text = etree.CDATA("")
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
   558
        return p
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   559
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   560
    return {
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   561
        "type": COMPLEXTYPE, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   562
        "extract": ExtractAny,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   563
        "generate": GenerateAny,
1293
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
   564
        "initial": InitialAny,
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   565
        "check": lambda x: isinstance(x, (StringType, UnicodeType, etree.ElementBase))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   566
    }
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   567
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   568
def GenerateTagInfos(infos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   569
    def ExtractTag(tree):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   570
        if len(tree._attrs) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   571
            raise ValueError("\"%s\" musn't have attributes!" % infos["name"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   572
        if len(tree.childNodes) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   573
            raise ValueError("\"%s\" musn't have children!" % infos["name"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   574
        if infos["minOccurs"] == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   575
            return True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   576
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   577
            return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   578
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   579
    def GenerateTag(value, name=None, indent=0):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   580
        if name is not None and not (infos["minOccurs"] == 0 and value is None):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   581
            ind1, ind2 = getIndent(indent, name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   582
            return ind1 + "<%s/>\n" % name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   583
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   584
            return ""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   585
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   586
    return {
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   587
        "type": TAG, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   588
        "extract": ExtractTag,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   589
        "generate": GenerateTag,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   590
        "initial": lambda: None,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   591
        "check": lambda x: x == None or infos["minOccurs"] == 0 and value == True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   592
    }
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   593
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   594
def FindTypeInfos(factory, infos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   595
    if isinstance(infos, (UnicodeType, StringType)):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   596
        namespace, name = DecomposeQualifiedName(infos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   597
        return factory.GetQualifiedNameInfos(name, namespace)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   598
    return infos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   599
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   600
def GetElementInitialValue(factory, infos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   601
    infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   602
    if infos["minOccurs"] == 1:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   603
        element_name = factory.etreeNamespaceFormat % infos["name"]
1293
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
   604
        if infos["elmt_type"]["type"] == SIMPLETYPE:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   605
            def initial_value():
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   606
                value = etree.Element(element_name)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   607
                value.text = (infos["elmt_type"]["generate"](infos["elmt_type"]["initial"]()))
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   608
                return value
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   609
        else:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   610
            def initial_value():
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   611
                value = infos["elmt_type"]["initial"]()
1293
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
   612
                if infos["type"] != ANY:
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
   613
                    DefaultElementClass.__setattr__(value, "tag", element_name)
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
   614
                    value.init()
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   615
                return value
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   616
        return [initial_value() for i in xrange(infos["minOccurs"])]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   617
    else:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   618
        return []
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   619
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   620
def HandleError(message, raise_exception):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   621
    if raise_exception:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   622
        raise ValueError(message)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   623
    return False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   624
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   625
def CheckElementValue(factory, name, infos, value, raise_exception=True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   626
    infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   627
    if value is None and raise_exception:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   628
        if not (infos["minOccurs"] == 0 and infos["maxOccurs"] == 1):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   629
            return HandleError("Attribute '%s' isn't optional." % name, raise_exception)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   630
    elif infos["maxOccurs"] == "unbounded" or infos["maxOccurs"] > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   631
        if not isinstance(value, ListType):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   632
            return HandleError("Attribute '%s' must be a list." % name, raise_exception)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   633
        if len(value) < infos["minOccurs"] or infos["maxOccurs"] != "unbounded" and len(value) > infos["maxOccurs"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   634
            return HandleError("List out of bounds for attribute '%s'." % name, raise_exception)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   635
        if not reduce(lambda x, y: x and y, map(infos["elmt_type"]["check"], value), True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   636
            return HandleError("Attribute '%s' must be a list of valid elements." % name, raise_exception)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   637
    elif infos.has_key("fixed") and value != infos["fixed"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   638
        return HandleError("Value of attribute '%s' can only be '%s'." % (name, str(infos["fixed"])), raise_exception)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   639
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   640
        return infos["elmt_type"]["check"](value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   641
    return True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   642
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   643
def GetContentInfos(name, choices):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   644
    for choice_infos in choices:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   645
        if choices_infos["type"] == "sequence":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   646
            for element_infos in choices_infos["elements"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   647
                if element_infos["type"] == CHOICE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   648
                    if GetContentInfos(name, element_infos["choices"]):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   649
                        return choices_infos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   650
                elif element_infos["name"] == name:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   651
                    return choices_infos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   652
        elif choice_infos["name"] == name:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   653
            return choices_infos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   654
    return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   655
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   656
def ComputeContentChoices(factory, name, infos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   657
    choices = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   658
    for choice in infos["choices"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   659
        if choice["type"] == "sequence":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   660
            choice["name"] = "sequence"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   661
            for sequence_element in choice["elements"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   662
                if sequence_element["type"] != CHOICE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   663
                    element_infos = factory.ExtractTypeInfos(sequence_element["name"], name, sequence_element["elmt_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   664
                    if element_infos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   665
                        sequence_element["elmt_type"] = element_infos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   666
        elif choice["elmt_type"] == "tag":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   667
            choice["elmt_type"] = GenerateTagInfos(choice)
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
   668
            factory.AddToLookupClass(choice["name"], name, DefaultElementClass)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   669
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   670
            choice_infos = factory.ExtractTypeInfos(choice["name"], name, choice["elmt_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   671
            if choice_infos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   672
                choice["elmt_type"] = choice_infos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   673
        choices.append((choice["name"], choice))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   674
    return choices
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   675
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   676
def ExtractContentElement(factory, tree, infos, content):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   677
    infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   678
    if infos["maxOccurs"] == "unbounded" or infos["maxOccurs"] > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   679
        if isinstance(content, ListType) and len(content) > 0 and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   680
           content[-1]["name"] == tree.nodeName:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   681
            content_item = content.pop(-1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   682
            content_item["value"].append(infos["elmt_type"]["extract"](tree))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   683
            return content_item
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   684
        elif not isinstance(content, ListType) and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   685
             content is not None and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   686
             content["name"] == tree.nodeName:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   687
            return {"name": tree.nodeName, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   688
                    "value": content["value"] + [infos["elmt_type"]["extract"](tree)]}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   689
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   690
            return {"name": tree.nodeName, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   691
                    "value": [infos["elmt_type"]["extract"](tree)]}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   692
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   693
        return {"name": tree.nodeName, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   694
                "value": infos["elmt_type"]["extract"](tree)}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   695
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   696
def GenerateContentInfos(factory, name, choices):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   697
    choices_dict = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   698
    for choice_name, infos in choices:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   699
        if choice_name == "sequence":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   700
            for element in infos["elements"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   701
                if element["type"] == CHOICE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   702
                    element["elmt_type"] = GenerateContentInfos(factory, name, ComputeContentChoices(factory, name, element))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   703
                elif choices_dict.has_key(element["name"]):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   704
                    raise ValueError("'%s' element defined two times in choice" % choice_name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   705
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   706
                    choices_dict[element["name"]] = infos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   707
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   708
            if choices_dict.has_key(choice_name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   709
                raise ValueError("'%s' element defined two times in choice" % choice_name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   710
            choices_dict[choice_name] = infos
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   711
    choices_xpath = "|".join(map(lambda x: "%s:%s" % (factory.TargetNamespace, x), choices_dict.keys()))
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   712
    
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   713
    def GetContentChoicesXPath():
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   714
        return choices_xpath
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   715
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   716
    def GetContentInitial():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   717
        content_name, infos = choices[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   718
        if content_name == "sequence":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   719
            content_value = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   720
            for i in xrange(infos["minOccurs"]):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   721
                for element_infos in infos["elements"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   722
                    value = GetElementInitialValue(factory, element_infos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   723
                    if value is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   724
                        if element_infos["type"] == CHOICE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   725
                            content_value.append(value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   726
                        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   727
                            content_value.append({"name": element_infos["name"], "value": value})
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   728
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   729
            content_value = GetElementInitialValue(factory, infos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   730
        return {"name": content_name, "value": content_value}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   731
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   732
    def CheckContent(value):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   733
        if value["name"] != "sequence":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   734
            infos = choices_dict.get(value["name"], None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   735
            if infos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   736
                return CheckElementValue(factory, value["name"], infos, value["value"], False)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   737
        elif len(value["value"]) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   738
            infos = choices_dict.get(value["value"][0]["name"], None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   739
            if infos is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   740
                for choice_name, infos in choices:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   741
                    if infos["type"] == "sequence":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   742
                        for element_infos in infos["elements"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   743
                            if element_infos["type"] == CHOICE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   744
                                infos = GetContentInfos(value["value"][0]["name"], element_infos["choices"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   745
            if infos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   746
                sequence_number = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   747
                element_idx = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   748
                while element_idx < len(value["value"]):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   749
                    for element_infos in infos["elements"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   750
                        element_value = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   751
                        if element_infos["type"] == CHOICE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   752
                            choice_infos = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   753
                            if element_idx < len(value["value"]):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   754
                                for choice in element_infos["choices"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   755
                                    if choice["name"] == value["value"][element_idx]["name"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   756
                                        choice_infos = choice
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   757
                                        element_value = value["value"][element_idx]["value"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   758
                                        element_idx += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   759
                                        break
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   760
                            if ((choice_infos is not None and 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   761
                                 not CheckElementValue(factory, choice_infos["name"], choice_infos, element_value, False)) or
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   762
                                (choice_infos is None and element_infos["minOccurs"] > 0)):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   763
                                raise ValueError("Invalid sequence value in attribute 'content'")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   764
                        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   765
                            if element_idx < len(value["value"]) and element_infos["name"] == value["value"][element_idx]["name"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   766
                                element_value = value["value"][element_idx]["value"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   767
                                element_idx += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   768
                            if not CheckElementValue(factory, element_infos["name"], element_infos, element_value, False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   769
                                raise ValueError("Invalid sequence value in attribute 'content'")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   770
                    sequence_number += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   771
                if sequence_number < infos["minOccurs"] or infos["maxOccurs"] != "unbounded" and sequence_number > infos["maxOccurs"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   772
                    raise ValueError("Invalid sequence value in attribute 'content'")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   773
                return True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   774
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   775
            for element_name, infos in choices:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   776
                if element_name == "sequence":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   777
                    required = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   778
                    for element in infos["elements"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   779
                        if element["minOccurs"] > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   780
                            required += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   781
                    if required == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   782
                        return True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   783
        return False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   784
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   785
    def ExtractContent(tree, content):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   786
        infos = choices_dict.get(tree.nodeName, None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   787
        if infos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   788
            if infos["name"] == "sequence":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   789
                sequence_dict = dict([(element_infos["name"], element_infos) for element_infos in infos["elements"] if element_infos["type"] != CHOICE])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   790
                element_infos = sequence_dict.get(tree.nodeName)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   791
                if content is not None and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   792
                   content["name"] == "sequence" and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   793
                   len(content["value"]) > 0 and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   794
                   choices_dict.get(content["value"][-1]["name"]) == infos:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   795
                    return {"name": "sequence",
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   796
                            "value": content["value"] + [ExtractContentElement(factory, tree, element_infos, content["value"][-1])]}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   797
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   798
                    return {"name": "sequence",
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   799
                            "value": [ExtractContentElement(factory, tree, element_infos, None)]}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   800
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   801
                return ExtractContentElement(factory, tree, infos, content)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   802
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   803
            for choice_name, infos in choices:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   804
                if infos["type"] == "sequence":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   805
                    for element_infos in infos["elements"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   806
                        if element_infos["type"] == CHOICE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   807
                            try:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   808
                                if content is not None and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   809
                                    content["name"] == "sequence" and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   810
                                    len(content["value"]) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   811
                                    return {"name": "sequence",
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   812
                                            "value": content["value"] + [element_infos["elmt_type"]["extract"](tree, content["value"][-1])]}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   813
                                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   814
                                    return {"name": "sequence",
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   815
                                            "value": [element_infos["elmt_type"]["extract"](tree, None)]}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   816
                            except:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   817
                                pass
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   818
        raise ValueError("Invalid element \"%s\" for content!" % tree.nodeName)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   819
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   820
    def GenerateContent(value, name=None, indent=0):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   821
        text = ""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   822
        if value["name"] != "sequence":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   823
            infos = choices_dict.get(value["name"], None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   824
            if infos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   825
                infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   826
                if infos["maxOccurs"] == "unbounded" or infos["maxOccurs"] > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   827
                    for item in value["value"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   828
                        text += infos["elmt_type"]["generate"](item, value["name"], indent)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   829
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   830
                    text += infos["elmt_type"]["generate"](value["value"], value["name"], indent)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   831
        elif len(value["value"]) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   832
            infos = choices_dict.get(value["value"][0]["name"], None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   833
            if infos is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   834
                for choice_name, infos in choices:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   835
                    if infos["type"] == "sequence":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   836
                        for element_infos in infos["elements"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   837
                            if element_infos["type"] == CHOICE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   838
                                infos = GetContentInfos(value["value"][0]["name"], element_infos["choices"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   839
            if infos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   840
                sequence_dict = dict([(element_infos["name"], element_infos) for element_infos in infos["elements"]]) 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   841
                for element_value in value["value"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   842
                    element_infos = sequence_dict.get(element_value["name"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   843
                    if element_infos["maxOccurs"] == "unbounded" or element_infos["maxOccurs"] > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   844
                        for item in element_value["value"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   845
                            text += element_infos["elmt_type"]["generate"](item, element_value["name"], indent)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   846
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   847
                        text += element_infos["elmt_type"]["generate"](element_value["value"], element_infos["name"], indent)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   848
        return text
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   849
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   850
    return {
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   851
        "type": COMPLEXTYPE,
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   852
        "choices_xpath": GetContentChoicesXPath,
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   853
        "initial": GetContentInitial,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   854
        "check": CheckContent,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   855
        "extract": ExtractContent,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   856
        "generate": GenerateContent
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   857
    }
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   858
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   859
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   860
#                           Structure extraction functions
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   861
#-------------------------------------------------------------------------------
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   862
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   863
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   864
def DecomposeQualifiedName(name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   865
    result = QName_model.match(name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   866
    if not result:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   867
        raise ValueError("\"%s\" isn't a valid QName value!" % name) 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   868
    parts = result.groups()[0].split(':')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   869
    if len(parts) == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   870
        return None, parts[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   871
    return parts
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   872
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   873
def GenerateElement(element_name, attributes, elements_model, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   874
                    accept_text=False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   875
    def ExtractElement(factory, node):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   876
        attrs = factory.ExtractNodeAttrs(element_name, node, attributes)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   877
        children_structure = ""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   878
        children_infos = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   879
        children = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   880
        for child in node.childNodes:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   881
            if child.nodeName not in ["#comment", "#text"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   882
                namespace, childname = DecomposeQualifiedName(child.nodeName)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   883
                children_structure += "%s "%childname
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   884
        result = elements_model.match(children_structure)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   885
        if not result:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   886
            raise ValueError("Invalid structure for \"%s\" children!. First element invalid." % node.nodeName)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   887
        valid = result.groups()[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   888
        if len(valid) < len(children_structure):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   889
            raise ValueError("Invalid structure for \"%s\" children!. Element number %d invalid." % (node.nodeName, len(valid.split(" ")) - 1))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   890
        for child in node.childNodes:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   891
            if child.nodeName != "#comment" and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   892
               (accept_text or child.nodeName != "#text"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   893
                if child.nodeName == "#text":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   894
                    children.append(GetAttributeValue(node))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   895
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   896
                    namespace, childname = DecomposeQualifiedName(child.nodeName)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   897
                    infos = factory.GetQualifiedNameInfos(childname, namespace)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   898
                    if infos["type"] != SYNTAXELEMENT:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   899
                        raise ValueError("\"%s\" can't be a member child!" % name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   900
                    if infos["extract"].has_key(element_name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   901
                        children.append(infos["extract"][element_name](factory, child))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   902
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   903
                        children.append(infos["extract"]["default"](factory, child))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   904
        return node.nodeName, attrs, children
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   905
    return ExtractElement
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   906
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   907
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   908
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   909
Class that generate class from an XML Tree
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   910
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   911
class ClassFactory:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   912
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   913
    def __init__(self, document, filepath=None, debug=False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   914
        self.Document = document
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   915
        if filepath is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   916
            self.BaseFolder, self.FileName = os.path.split(filepath)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   917
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   918
            self.BaseFolder = self.FileName = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   919
        self.Debug = debug
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   920
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   921
        # Dictionary for stocking Classes and Types definitions created from
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   922
        # the XML tree
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   923
        self.XMLClassDefinitions = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   924
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   925
        self.DefinedNamespaces = {}
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   926
        self.NSMAP = {}
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   927
        self.Namespaces = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   928
        self.SchemaNamespace = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   929
        self.TargetNamespace = None
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   930
        self.etreeNamespaceFormat = "%s"
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   931
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   932
        self.CurrentCompilations = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   933
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   934
        # Dictionaries for stocking Classes and Types generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   935
        self.ComputeAfter = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   936
        if self.FileName is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   937
            self.ComputedClasses = {self.FileName: {}}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   938
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   939
            self.ComputedClasses = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   940
        self.ComputedClassesInfos = {}
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   941
        self.ComputedClassesLookUp = {}
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
   942
        self.EquivalentClassesParent = {}
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   943
        self.AlreadyComputed = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   944
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   945
    def GetQualifiedNameInfos(self, name, namespace=None, canbenone=False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   946
        if namespace is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   947
            if self.Namespaces[self.SchemaNamespace].has_key(name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   948
                return self.Namespaces[self.SchemaNamespace][name]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   949
            for space, elements in self.Namespaces.iteritems():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   950
                if space != self.SchemaNamespace and elements.has_key(name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   951
                    return elements[name]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   952
            parts = name.split("_", 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   953
            if len(parts) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   954
                group = self.GetQualifiedNameInfos(parts[0], namespace)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   955
                if group is not None and group["type"] == ELEMENTSGROUP:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   956
                    elements = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   957
                    if group.has_key("elements"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   958
                        elements = group["elements"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   959
                    elif group.has_key("choices"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   960
                        elements = group["choices"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   961
                    for element in elements:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   962
                        if element["name"] == parts[1]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   963
                            return element
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   964
            if not canbenone:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   965
                raise ValueError("Unknown element \"%s\" for any defined namespaces!" % name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   966
        elif self.Namespaces.has_key(namespace):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   967
            if self.Namespaces[namespace].has_key(name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   968
                return self.Namespaces[namespace][name]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   969
            parts = name.split("_", 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   970
            if len(parts) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   971
                group = self.GetQualifiedNameInfos(parts[0], namespace)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   972
                if group is not None and group["type"] == ELEMENTSGROUP:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   973
                    elements = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   974
                    if group.has_key("elements"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   975
                        elements = group["elements"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   976
                    elif group.has_key("choices"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   977
                        elements = group["choices"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   978
                    for element in elements:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   979
                        if element["name"] == parts[1]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   980
                            return element
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   981
            if not canbenone:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   982
                raise ValueError("Unknown element \"%s\" for namespace \"%s\"!" % (name, namespace))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   983
        elif not canbenone:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   984
            raise ValueError("Unknown namespace \"%s\"!" % namespace)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   985
        return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   986
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   987
    def SplitQualifiedName(self, name, namespace=None, canbenone=False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   988
        if namespace is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   989
            if self.Namespaces[self.SchemaNamespace].has_key(name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   990
                return name, None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   991
            for space, elements in self.Namespaces.items():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   992
                if space != self.SchemaNamespace and elements.has_key(name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   993
                    return name, None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   994
            parts = name.split("_", 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   995
            if len(parts) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   996
                group = self.GetQualifiedNameInfos(parts[0], namespace)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   997
                if group is not None and group["type"] == ELEMENTSGROUP:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   998
                    elements = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   999
                    if group.has_key("elements"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1000
                        elements = group["elements"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1001
                    elif group.has_key("choices"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1002
                        elements = group["choices"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1003
                    for element in elements:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1004
                        if element["name"] == parts[1]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1005
                            return part[1], part[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1006
            if not canbenone:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1007
                raise ValueError("Unknown element \"%s\" for any defined namespaces!" % name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1008
        elif self.Namespaces.has_key(namespace):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1009
            if self.Namespaces[namespace].has_key(name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1010
                return name, None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1011
            parts = name.split("_", 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1012
            if len(parts) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1013
                group = self.GetQualifiedNameInfos(parts[0], namespace)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1014
                if group is not None and group["type"] == ELEMENTSGROUP:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1015
                    elements = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1016
                    if group.has_key("elements"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1017
                        elements = group["elements"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1018
                    elif group.has_key("choices"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1019
                        elements = group["choices"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1020
                    for element in elements:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1021
                        if element["name"] == parts[1]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1022
                            return parts[1], parts[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1023
            if not canbenone:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1024
                raise ValueError("Unknown element \"%s\" for namespace \"%s\"!" % (name, namespace))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1025
        elif not canbenone:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1026
            raise ValueError("Unknown namespace \"%s\"!" % namespace)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1027
        return None, None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1028
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1029
    def ExtractNodeAttrs(self, element_name, node, valid_attrs):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1030
        attrs = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1031
        for qualified_name, attr in node._attrs.items():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1032
            namespace, name =  DecomposeQualifiedName(qualified_name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1033
            if name in valid_attrs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1034
                infos = self.GetQualifiedNameInfos(name, namespace)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1035
                if infos["type"] != SYNTAXATTRIBUTE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1036
                    raise ValueError("\"%s\" can't be a member attribute!" % name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1037
                elif name in attrs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1038
                    raise ValueError("\"%s\" attribute has been twice!" % name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1039
                elif element_name in infos["extract"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1040
                    attrs[name] = infos["extract"][element_name](attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1041
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1042
                    attrs[name] = infos["extract"]["default"](attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1043
            elif namespace == "xmlns":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1044
                infos = self.GetQualifiedNameInfos("anyURI", self.SchemaNamespace)
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1045
                value = infos["extract"](attr)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1046
                self.DefinedNamespaces[value] = name
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1047
                self.NSMAP[name] = value
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1048
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1049
                raise ValueError("Invalid attribute \"%s\" for member \"%s\"!" % (qualified_name, node.nodeName))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1050
        for attr in valid_attrs:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1051
            if attr not in attrs and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1052
               self.Namespaces[self.SchemaNamespace].has_key(attr) and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1053
               self.Namespaces[self.SchemaNamespace][attr].has_key("default"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1054
                if self.Namespaces[self.SchemaNamespace][attr]["default"].has_key(element_name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1055
                    default = self.Namespaces[self.SchemaNamespace][attr]["default"][element_name]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1056
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1057
                    default = self.Namespaces[self.SchemaNamespace][attr]["default"]["default"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1058
                if default is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1059
                    attrs[attr] = default
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1060
        return attrs
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1061
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1062
    def ReduceElements(self, elements, schema=False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1063
        result = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1064
        for child_infos in elements:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1065
            if child_infos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1066
                if child_infos[1].has_key("name") and schema:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1067
                    self.CurrentCompilations.append(child_infos[1]["name"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1068
                namespace, name = DecomposeQualifiedName(child_infos[0])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1069
                infos = self.GetQualifiedNameInfos(name, namespace)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1070
                if infos["type"] != SYNTAXELEMENT:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1071
                    raise ValueError("\"%s\" can't be a member child!" % name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1072
                element = infos["reduce"](self, child_infos[1], child_infos[2])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1073
                if element is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1074
                    result.append(element)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1075
                if child_infos[1].has_key("name") and schema:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1076
                    self.CurrentCompilations.pop(-1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1077
        annotations = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1078
        children = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1079
        for element in result:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1080
            if element["type"] == "annotation":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1081
                annotations.append(element)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1082
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1083
                children.append(element)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1084
        return annotations, children
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1085
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1086
    def AddComplexType(self, typename, infos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1087
        if not self.XMLClassDefinitions.has_key(typename):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1088
            self.XMLClassDefinitions[typename] = infos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1089
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1090
            raise ValueError("\"%s\" class already defined. Choose another name!" % typename)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1091
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1092
    def ParseSchema(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1093
        pass
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1094
    
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1095
    def AddEquivalentClass(self, name, base):
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1096
        if name != base:
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1097
            equivalences = self.EquivalentClassesParent.setdefault(self.etreeNamespaceFormat % base, {})
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1098
            equivalences[self.etreeNamespaceFormat % name] = True
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1099
        
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1100
    def AddToLookupClass(self, name, parent, typeinfos):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1101
        lookup_name = self.etreeNamespaceFormat % name
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1102
        if isinstance(typeinfos, (StringType, UnicodeType)):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1103
            self.AddEquivalentClass(name, typeinfos)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1104
            typeinfos = self.etreeNamespaceFormat % typeinfos
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1105
        lookup_classes = self.ComputedClassesLookUp.get(lookup_name)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1106
        if lookup_classes is None:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1107
            self.ComputedClassesLookUp[lookup_name] = (typeinfos, parent)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1108
        elif isinstance(lookup_classes, DictType):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1109
            lookup_classes[self.etreeNamespaceFormat % parent 
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1110
                           if parent is not None else None] = typeinfos
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1111
        else:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1112
            lookup_classes = {self.etreeNamespaceFormat % lookup_classes[1]
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1113
                              if lookup_classes[1] is not None else None: lookup_classes[0]}
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1114
            lookup_classes[self.etreeNamespaceFormat % parent
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1115
                           if parent is not None else None] = typeinfos
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1116
            self.ComputedClassesLookUp[lookup_name] = lookup_classes
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1117
    
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1118
    def ExtractTypeInfos(self, name, parent, typeinfos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1119
        if isinstance(typeinfos, (StringType, UnicodeType)):
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1120
            namespace, type_name = DecomposeQualifiedName(typeinfos)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1121
            infos = self.GetQualifiedNameInfos(type_name, namespace)
1293
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
  1122
            if name != "base":
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
  1123
                if infos["type"] == SIMPLETYPE:
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
  1124
                    self.AddToLookupClass(name, parent, DefaultElementClass)
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
  1125
                elif namespace == self.TargetNamespace:
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
  1126
                    self.AddToLookupClass(name, parent, type_name)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1127
            if infos["type"] == COMPLEXTYPE:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1128
                type_name, parent = self.SplitQualifiedName(type_name, namespace)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1129
                result = self.CreateClass(type_name, parent, infos)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1130
                if result is not None and not isinstance(result, (UnicodeType, StringType)):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1131
                    self.Namespaces[self.TargetNamespace][result["name"]] = result
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1132
                return result
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1133
            elif infos["type"] == ELEMENT and infos["elmt_type"]["type"] == COMPLEXTYPE:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1134
                type_name, parent = self.SplitQualifiedName(type_name, namespace)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1135
                result = self.CreateClass(type_name, parent, infos["elmt_type"])
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1136
                if result is not None and not isinstance(result, (UnicodeType, StringType)):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1137
                    self.Namespaces[self.TargetNamespace][result["name"]] = result
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1138
                return result
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1139
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1140
                return infos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1141
        elif typeinfos["type"] == COMPLEXTYPE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1142
            return self.CreateClass(name, parent, typeinfos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1143
        elif typeinfos["type"] == SIMPLETYPE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1144
            return typeinfos
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1145
    
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1146
    def GetEquivalentParents(self, parent):
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1147
        return reduce(lambda x, y: x + y,
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1148
            [[p] + self.GetEquivalentParents(p)
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1149
             for p in self.EquivalentClassesParent.get(parent, {}).keys()], [])
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1150
    
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1151
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1152
    Methods that generates the classes
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1153
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1154
    def CreateClasses(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1155
        self.ParseSchema()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1156
        for name, infos in self.Namespaces[self.TargetNamespace].items():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1157
            if infos["type"] == ELEMENT:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1158
                if not isinstance(infos["elmt_type"], (UnicodeType, StringType)) and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1159
                   infos["elmt_type"]["type"] == COMPLEXTYPE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1160
                    self.ComputeAfter.append((name, None, infos["elmt_type"], True))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1161
                    while len(self.ComputeAfter) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1162
                        result = self.CreateClass(*self.ComputeAfter.pop(0))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1163
                        if result is not None and not isinstance(result, (UnicodeType, StringType)):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1164
                            self.Namespaces[self.TargetNamespace][result["name"]] = result
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1165
            elif infos["type"] == COMPLEXTYPE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1166
                self.ComputeAfter.append((name, None, infos))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1167
                while len(self.ComputeAfter) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1168
                    result = self.CreateClass(*self.ComputeAfter.pop(0))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1169
                    if result is not None and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1170
                       not isinstance(result, (UnicodeType, StringType)):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1171
                        self.Namespaces[self.TargetNamespace][result["name"]] = result
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1172
            elif infos["type"] == ELEMENTSGROUP:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1173
                elements = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1174
                if infos.has_key("elements"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1175
                    elements = infos["elements"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1176
                elif infos.has_key("choices"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1177
                    elements = infos["choices"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1178
                for element in elements:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1179
                    if not isinstance(element["elmt_type"], (UnicodeType, StringType)) and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1180
                       element["elmt_type"]["type"] == COMPLEXTYPE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1181
                        self.ComputeAfter.append((element["name"], infos["name"], element["elmt_type"]))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1182
                        while len(self.ComputeAfter) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1183
                            result = self.CreateClass(*self.ComputeAfter.pop(0))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1184
                            if result is not None and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1185
                               not isinstance(result, (UnicodeType, StringType)):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1186
                                self.Namespaces[self.TargetNamespace][result["name"]] = result
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1187
        
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1188
        for name, parents in self.ComputedClassesLookUp.iteritems():
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1189
            if isinstance(parents, DictType):
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1190
                computed_classes = parents.items()
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1191
            elif parents[1] is not None:
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1192
                computed_classes = [(self.etreeNamespaceFormat % parents[1], parents[0])]
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1193
            else:
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1194
                computed_classes = []
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1195
            for parent, computed_class in computed_classes:
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1196
                for equivalent_parent in self.GetEquivalentParents(parent):
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1197
                    if not isinstance(parents, DictType):
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1198
                        parents = dict(computed_classes)
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1199
                        self.ComputedClassesLookUp[name] = parents
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1200
                    parents[equivalent_parent] = computed_class
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1201
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1202
        return self.ComputedClasses
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1203
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1204
    def CreateClass(self, name, parent, classinfos, baseclass = False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1205
        if parent is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1206
            classname = "%s_%s" % (parent, name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1207
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1208
            classname = name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1209
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1210
        # Checks that classe haven't been generated yet
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1211
        if self.AlreadyComputed.get(classname, False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1212
            if baseclass:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1213
                self.AlreadyComputed[classname].IsBaseClass = baseclass
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1214
            return self.ComputedClassesInfos.get(classname, None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1215
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1216
        # If base classes haven't been generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1217
        bases = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1218
        base_infos = classinfos.get("base", None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1219
        if base_infos is not None:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1220
            namespace, base_name = DecomposeQualifiedName(base_infos)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1221
            if namespace == self.TargetNamespace:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1222
                self.AddEquivalentClass(name, base_name)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1223
            result = self.ExtractTypeInfos("base", name, base_infos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1224
            if result is None:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1225
                namespace, base_name = DecomposeQualifiedName(base_infos)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1226
                if self.AlreadyComputed.get(base_name, False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1227
                    self.ComputeAfter.append((name, parent, classinfos))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1228
                    if self.TargetNamespace is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1229
                        return "%s:%s" % (self.TargetNamespace, classname)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1230
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1231
                        return classname
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1232
            elif result is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1233
                if self.FileName is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1234
                    classinfos["base"] = self.ComputedClasses[self.FileName].get(result["name"], None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1235
                    if classinfos["base"] is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1236
                        for filename, classes in self.ComputedClasses.iteritems():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1237
                            if filename != self.FileName:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1238
                                classinfos["base"] = classes.get(result["name"], None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1239
                                if classinfos["base"] is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1240
                                    break
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1241
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1242
                    classinfos["base"] = self.ComputedClasses.get(result["name"], None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1243
                if classinfos["base"] is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1244
                    raise ValueError("No class found for base type")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1245
                bases.append(classinfos["base"])
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1246
        bases.append(DefaultElementClass)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1247
        bases = tuple(bases)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1248
        classmembers = {"__doc__": classinfos.get("doc", ""), "IsBaseClass": baseclass}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1249
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1250
        self.AlreadyComputed[classname] = True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1251
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1252
        for attribute in classinfos["attributes"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1253
            infos = self.ExtractTypeInfos(attribute["name"], name, attribute["attr_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1254
            if infos is not None:                    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1255
                if infos["type"] != SIMPLETYPE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1256
                    raise ValueError("\"%s\" type is not a simple type!" % attribute["attr_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1257
                attrname = attribute["name"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1258
                if attribute["use"] == "optional":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1259
                    classmembers["add%s"%attrname] = generateAddMethod(attrname, self, attribute)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1260
                    classmembers["delete%s"%attrname] = generateDeleteMethod(attrname)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1261
                classmembers["set%s"%attrname] = generateSetMethod(attrname)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1262
                classmembers["get%s"%attrname] = generateGetMethod(attrname)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1263
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1264
                raise ValueError("\"%s\" type unrecognized!" % attribute["attr_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1265
            attribute["attr_type"] = infos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1266
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1267
        for element in classinfos["elements"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1268
            if element["type"] == CHOICE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1269
                elmtname = element["name"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1270
                choices = ComputeContentChoices(self, name, element)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1271
                classmembers["get%schoices"%elmtname] = generateGetChoicesMethod(element["choices"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1272
                if element["maxOccurs"] == "unbounded" or element["maxOccurs"] > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1273
                    classmembers["append%sbytype" % elmtname] = generateAppendChoiceByTypeMethod(element["maxOccurs"], self, element["choices"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1274
                    classmembers["insert%sbytype" % elmtname] = generateInsertChoiceByTypeMethod(element["maxOccurs"], self, element["choices"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1275
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1276
                    classmembers["set%sbytype" % elmtname] = generateSetChoiceByTypeMethod(self, element["choices"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1277
                infos = GenerateContentInfos(self, name, choices)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1278
            elif element["type"] == ANY:
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1279
                elmtname = element["name"] = "anyText"
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1280
                element["minOccurs"] = element["maxOccurs"] = 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1281
                infos = GenerateAnyInfos(element)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1282
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1283
                elmtname = element["name"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1284
                if element["elmt_type"] == "tag":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1285
                    infos = GenerateTagInfos(element)
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1286
                    self.AddToLookupClass(element["name"], name, DefaultElementClass)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1287
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1288
                    infos = self.ExtractTypeInfos(element["name"], name, element["elmt_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1289
            if infos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1290
                element["elmt_type"] = infos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1291
            if element["maxOccurs"] == "unbounded" or element["maxOccurs"] > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1292
                classmembers["append%s" % elmtname] = generateAppendMethod(elmtname, element["maxOccurs"], self, element)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1293
                classmembers["insert%s" % elmtname] = generateInsertMethod(elmtname, element["maxOccurs"], self, element)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1294
                classmembers["remove%s" % elmtname] = generateRemoveMethod(elmtname, element["minOccurs"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1295
                classmembers["count%s" % elmtname] = generateCountMethod(elmtname)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1296
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1297
                if element["minOccurs"] == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1298
                    classmembers["add%s" % elmtname] = generateAddMethod(elmtname, self, element)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1299
                    classmembers["delete%s" % elmtname] = generateDeleteMethod(elmtname)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1300
            classmembers["set%s" % elmtname] = generateSetMethod(elmtname)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1301
            classmembers["get%s" % elmtname] = generateGetMethod(elmtname)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1302
            
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1303
        classmembers["init"] = generateInitMethod(self, classinfos)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1304
        classmembers["getStructure"] = generateStructureMethod(classinfos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1305
        classmembers["loadXMLTree"] = generateLoadXMLTree(self, classinfos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1306
        classmembers["generateXMLText"] = generateGenerateXMLText(self, classinfos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1307
        classmembers["getElementAttributes"] = generateGetElementAttributes(self, classinfos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1308
        classmembers["getElementInfos"] = generateGetElementInfos(self, classinfos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1309
        classmembers["setElementValue"] = generateSetElementValue(self, classinfos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1310
        classmembers["singleLineAttributes"] = True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1311
        classmembers["compatibility"] = lambda x, y: None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1312
        classmembers["extraAttrs"] = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1313
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1314
        class_definition = classobj(str(classname), bases, classmembers)
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1315
        setattr(class_definition, "__getattr__", generateGetattrMethod(self, class_definition, classinfos))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1316
        setattr(class_definition, "__setattr__", generateSetattrMethod(self, class_definition, classinfos))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1317
        class_infos = {"type": COMPILEDCOMPLEXTYPE,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1318
                       "name": classname,
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1319
                       "check": generateClassCheckFunction(class_definition),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1320
                       "initial": generateClassCreateFunction(class_definition),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1321
                       "extract": generateClassExtractFunction(class_definition),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1322
                       "generate": class_definition.generateXMLText}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1323
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1324
        if self.FileName is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1325
            self.ComputedClasses[self.FileName][classname] = class_definition
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1326
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1327
            self.ComputedClasses[classname] = class_definition
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1328
        self.ComputedClassesInfos[classname] = class_infos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1329
        
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1330
        self.AddToLookupClass(name, parent, class_definition)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1331
        self.AddToLookupClass(classname, None, class_definition)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1332
            
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1333
        return class_infos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1334
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1335
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1336
    Methods that print the classes generated
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1337
    """
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1338
    def PrintClasses(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1339
        items = self.ComputedClasses.items()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1340
        items.sort()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1341
        if self.FileName is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1342
            for filename, classes in items:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1343
                print "File '%s':" % filename
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1344
                class_items = classes.items()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1345
                class_items.sort()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1346
                for classname, xmlclass in class_items:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1347
                    print "%s: %s" % (classname, str(xmlclass))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1348
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1349
            for classname, xmlclass in items:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1350
                print "%s: %s" % (classname, str(xmlclass))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1351
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1352
    def PrintClassNames(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1353
        classnames = self.XMLClassDefinitions.keys()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1354
        classnames.sort()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1355
        for classname in classnames:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1356
            print classname
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1357
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1358
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1359
Method that generate the method for checking a class instance
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1360
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1361
def generateClassCheckFunction(class_definition):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1362
    def classCheckfunction(instance):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1363
        return isinstance(instance, class_definition)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1364
    return classCheckfunction
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1365
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1366
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1367
Method that generate the method for creating a class instance
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1368
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1369
def generateClassCreateFunction(class_definition):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1370
    def classCreatefunction():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1371
        return class_definition()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1372
    return classCreatefunction
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1373
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1374
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1375
Method that generate the method for extracting a class instance
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1376
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1377
def generateClassExtractFunction(class_definition):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1378
    def classExtractfunction(node):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1379
        instance = class_definition()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1380
        instance.loadXMLTree(node)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1381
        return instance
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1382
    return classExtractfunction
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1383
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1384
def generateGetattrMethod(factory, class_definition, classinfos):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1385
    attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1386
    optional_attributes = dict([(attr["name"], True) for attr in classinfos["attributes"] if attr["use"] == "optional"])
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1387
    elements = dict([(element["name"], element) for element in classinfos["elements"]])
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1388
    
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1389
    def getattrMethod(self, name):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1390
        if attributes.has_key(name):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1391
            attribute_infos = attributes[name]
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1392
            attribute_infos["attr_type"] = FindTypeInfos(factory, attribute_infos["attr_type"])
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1393
            value = self.get(name)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1394
            if value is not None:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1395
                return attribute_infos["attr_type"]["extract"](value, extract=False)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1396
            elif attribute_infos.has_key("fixed"):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1397
                return attribute_infos["attr_type"]["extract"](attribute_infos["fixed"], extract=False)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1398
            return attribute_infos["attr_type"]["initial"]()
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1399
        
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1400
        elif elements.has_key(name):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1401
            element_infos = elements[name]
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1402
            element_infos["elmt_type"] = FindTypeInfos(factory, element_infos["elmt_type"])
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1403
            if element_infos["type"] == CHOICE:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1404
                content = self.xpath(element_infos["elmt_type"]["choices_xpath"](), namespaces=factory.NSMAP)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1405
                if element_infos["maxOccurs"] == "unbounded" or element_infos["maxOccurs"] > 1:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1406
                    return content
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1407
                elif len(content) > 0:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1408
                    return content[0]
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1409
                return None 
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1410
            elif element_infos["type"] == ANY:
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1411
                return element_infos["elmt_type"]["extract"](self)
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1412
            else:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1413
                element_name = factory.etreeNamespaceFormat % name
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1414
                if element_infos["maxOccurs"] == "unbounded" or element_infos["maxOccurs"] > 1:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1415
                    return self.findall(element_name)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1416
                else:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1417
                    return self.find(element_name)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1418
            
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1419
        elif classinfos.has_key("base"):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1420
            return classinfos["base"].__getattr__(self, name)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1421
        
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1422
        return DefaultElementClass.__getattribute__(self, name)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1423
    
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1424
    return getattrMethod
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1425
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1426
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1427
Method that generate the method for loading an xml tree by following the
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1428
attributes list defined
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1429
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1430
def generateSetattrMethod(factory, class_definition, classinfos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1431
    attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1432
    optional_attributes = dict([(attr["name"], True) for attr in classinfos["attributes"] if attr["use"] == "optional"])
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1433
    elements = OrderedDict([(element["name"], element) for element in classinfos["elements"]])
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1434
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1435
    def setattrMethod(self, name, value):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1436
        if attributes.has_key(name):
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1437
            attribute_infos = attributes[name]
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1438
            attribute_infos["attr_type"] = FindTypeInfos(factory, attribute_infos["attr_type"])
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1439
            if optional_attributes.get(name, False):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1440
                default = attribute_infos.get("default", None)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1441
                if value is None or value == default:
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1442
                    self.attrib.pop(name, None)
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1443
                    return
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1444
            elif attribute_infos.has_key("fixed"):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1445
                return
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1446
            return self.set(name, attribute_infos["attr_type"]["generate"](value))
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1447
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1448
        elif elements.has_key(name):
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1449
            element_infos = elements[name]
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1450
            element_infos["elmt_type"] = FindTypeInfos(factory, element_infos["elmt_type"])
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1451
            if element_infos["type"] == ANY:
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1452
                element_infos["elmt_type"]["generate"](self, value)
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1453
            
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1454
            else:
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1455
                element_xpath = ("%s:%s" % (factory.TargetNamespace, name)
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1456
                                 if name != "content"
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1457
                                 else elements["content"]["elmt_type"]["choices_xpath"]())
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1458
                
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1459
                for element in self.xpath(element_xpath, namespaces=factory.NSMAP):
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1460
                    self.remove(element)
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1461
                
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1462
                if value is not None:
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1463
                    previous_elements_xpath = "|".join(map(
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1464
                        lambda x: "%s:%s" % (factory.TargetNamespace, x)
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1465
                                  if x != "content"
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1466
                                  else elements["content"]["elmt_type"]["choices_xpath"](),
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1467
                        elements.keys()[elements.keys().index(name)]))
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1468
                    
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1469
                    insertion_point = len(self.xpath(previous_elements_xpath, namespaces=factory.NSMAP))
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1470
                    
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1471
                    if not isinstance(value, ListType):
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1472
                        value = [value]
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1473
                        
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1474
                    for element in reversed(value):
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1475
                        self.insert(insertion_point, element)
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1476
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1477
        elif classinfos.has_key("base"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1478
            return classinfos["base"].__setattr__(self, name, value)
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1479
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1480
        elif class_definition.__dict__.has_key(name):
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1481
            return DefaultElementClass.__setattr__(self, name, value)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1482
        
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1483
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1484
            raise AttributeError("'%s' can't have an attribute '%s'." % (self.__class__.__name__, name))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1485
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1486
    return setattrMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1487
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1488
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1489
Method that generate the method for generating the xml tree structure model by 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1490
following the attributes list defined
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1491
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1492
def ComputeMultiplicity(name, infos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1493
    if infos["minOccurs"] == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1494
        if infos["maxOccurs"] == "unbounded":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1495
            return "(?:%s)*" % name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1496
        elif infos["maxOccurs"] == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1497
            return "(?:%s)?" % name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1498
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1499
            return "(?:%s){,%d}" % (name, infos["maxOccurs"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1500
    elif infos["minOccurs"] == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1501
        if infos["maxOccurs"] == "unbounded":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1502
            return "(?:%s)+" % name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1503
        elif infos["maxOccurs"] == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1504
            return "(?:%s)" % name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1505
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1506
            return "(?:%s){1,%d}" % (name, infos["maxOccurs"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1507
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1508
        if infos["maxOccurs"] == "unbounded":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1509
            return "(?:%s){%d,}" % (name, infos["minOccurs"], name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1510
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1511
            return "(?:%s){%d,%d}" % (name, infos["minOccurs"], 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1512
                                       infos["maxOccurs"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1513
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1514
def GetStructure(classinfos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1515
    elements = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1516
    for element in classinfos["elements"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1517
        if element["type"] == ANY:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1518
            infos = element.copy()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1519
            infos["minOccurs"] = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1520
            elements.append(ComputeMultiplicity("#text |#cdata-section |\w* ", infos))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1521
        elif element["type"] == CHOICE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1522
            choices = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1523
            for infos in element["choices"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1524
                if infos["type"] == "sequence":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1525
                    structure = "(?:%s)" % GetStructure(infos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1526
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1527
                    structure = "%s " % infos["name"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1528
                choices.append(ComputeMultiplicity(structure, infos))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1529
            elements.append(ComputeMultiplicity("|".join(choices), element))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1530
        elif element["name"] == "content" and element["elmt_type"]["type"] == SIMPLETYPE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1531
            elements.append("(?:#text |#cdata-section )?")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1532
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1533
            elements.append(ComputeMultiplicity("%s " % element["name"], element))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1534
    if classinfos.get("order", True) or len(elements) == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1535
        return "".join(elements)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1536
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1537
        raise ValueError("XSD structure not yet supported!")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1538
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1539
def generateStructureMethod(classinfos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1540
    def getStructureMethod(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1541
        structure = GetStructure(classinfos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1542
        if classinfos.has_key("base"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1543
            return classinfos["base"].getStructure(self) + structure
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1544
        return structure
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1545
    return getStructureMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1546
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1547
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1548
Method that generate the method for loading an xml tree by following the
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1549
attributes list defined
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1550
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1551
def generateLoadXMLTree(factory, classinfos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1552
    attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1553
    elements = dict([(element["name"], element) for element in classinfos["elements"]])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1554
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1555
    def loadXMLTreeMethod(self, tree, extras=[], derived=False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1556
        self.extraAttrs = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1557
        self.compatibility(tree)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1558
        if not derived:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1559
            children_structure = ""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1560
            for node in tree.childNodes:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1561
                if not (node.nodeName == "#text" and node.data.strip() == "") and node.nodeName != "#comment":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1562
                    children_structure += "%s " % node.nodeName
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1563
            structure_pattern = self.getStructure()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1564
            if structure_pattern != "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1565
                structure_model = re.compile("(%s)$" % structure_pattern)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1566
                result = structure_model.match(children_structure)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1567
                if not result:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1568
                    raise ValueError("Invalid structure for \"%s\" children!." % tree.nodeName)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1569
        required_attributes = dict([(attr["name"], True) for attr in classinfos["attributes"] if attr["use"] == "required"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1570
        if classinfos.has_key("base"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1571
            extras.extend([attr["name"] for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1572
            classinfos["base"].loadXMLTree(self, tree, extras, True)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1573
        for attrname, attr in tree._attrs.iteritems():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1574
            if attributes.has_key(attrname):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1575
                attributes[attrname]["attr_type"] = FindTypeInfos(factory, attributes[attrname]["attr_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1576
                object.__setattr__(self, attrname, attributes[attrname]["attr_type"]["extract"](attr))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1577
            elif not classinfos.has_key("base") and not attrname in extras and not self.extraAttrs.has_key(attrname):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1578
                self.extraAttrs[attrname] = GetAttributeValue(attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1579
            required_attributes.pop(attrname, None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1580
        if len(required_attributes) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1581
            raise ValueError("Required attributes %s missing for \"%s\" element!" % (", ".join(["\"%s\""%name for name in required_attributes]), tree.nodeName))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1582
        first = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1583
        for node in tree.childNodes:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1584
            name = node.nodeName
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1585
            if name == "#text" and node.data.strip() == "" or name == "#comment":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1586
                continue
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1587
            elif elements.has_key(name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1588
                elements[name]["elmt_type"] = FindTypeInfos(factory, elements[name]["elmt_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1589
                if elements[name]["maxOccurs"] == "unbounded" or elements[name]["maxOccurs"] > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1590
                    if first.get(name, True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1591
                        object.__setattr__(self, name, [elements[name]["elmt_type"]["extract"](node)])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1592
                        first[name] = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1593
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1594
                        getattr(self, name).append(elements[name]["elmt_type"]["extract"](node))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1595
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1596
                    object.__setattr__(self, name, elements[name]["elmt_type"]["extract"](node))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1597
            elif elements.has_key("text"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1598
                if elements["text"]["maxOccurs"] == "unbounded" or elements["text"]["maxOccurs"] > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1599
                    if first.get("text", True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1600
                        object.__setattr__(self, "text", [elements["text"]["elmt_type"]["extract"](node)])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1601
                        first["text"] = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1602
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1603
                        getattr(self, "text").append(elements["text"]["elmt_type"]["extract"](node))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1604
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1605
                    object.__setattr__(self, "text", elements["text"]["elmt_type"]["extract"](node))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1606
            elif elements.has_key("content"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1607
                if name in ["#cdata-section", "#text"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1608
                    if elements["content"]["elmt_type"]["type"] == SIMPLETYPE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1609
                        object.__setattr__(self, "content", elements["content"]["elmt_type"]["extract"](node.data, False))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1610
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1611
                    content = getattr(self, "content")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1612
                    if elements["content"]["maxOccurs"] == "unbounded" or elements["content"]["maxOccurs"] > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1613
                        if first.get("content", True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1614
                            object.__setattr__(self, "content", [elements["content"]["elmt_type"]["extract"](node, None)])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1615
                            first["content"] = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1616
                        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1617
                            content.append(elements["content"]["elmt_type"]["extract"](node, content))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1618
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1619
                        object.__setattr__(self, "content", elements["content"]["elmt_type"]["extract"](node, content))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1620
    return loadXMLTreeMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1621
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1622
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1623
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1624
Method that generates the method for generating an xml text by following the
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1625
attributes list defined
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1626
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1627
def generateGenerateXMLText(factory, classinfos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1628
    def generateXMLTextMethod(self, name, indent=0, extras={}, derived=False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1629
        ind1, ind2 = getIndent(indent, name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1630
        if not derived:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1631
            text = ind1 + u'<%s' % name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1632
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1633
            text = u''
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1634
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1635
        first = True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1636
        
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1637
        if not classinfos.has_key("base"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1638
            extras.update(self.extraAttrs)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1639
            for attr, value in extras.iteritems():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1640
                if not first and not self.singleLineAttributes:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1641
                    text += u'\n%s' % (ind2)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1642
                text += u' %s=%s' % (attr, quoteattr(value))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1643
                first = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1644
            extras.clear()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1645
        for attr in classinfos["attributes"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1646
            if attr["use"] != "prohibited":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1647
                attr["attr_type"] = FindTypeInfos(factory, attr["attr_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1648
                value = getattr(self, attr["name"], None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1649
                if value != None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1650
                    computed_value = attr["attr_type"]["generate"](value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1651
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1652
                    computed_value = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1653
                if attr["use"] != "optional" or (value != None and \
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1654
                   computed_value != attr.get("default", attr["attr_type"]["generate"](attr["attr_type"]["initial"]()))):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1655
                    if classinfos.has_key("base"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1656
                        extras[attr["name"]] = computed_value
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1657
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1658
                        if not first and not self.singleLineAttributes:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1659
                            text += u'\n%s' % (ind2)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1660
                        text += ' %s=%s' % (attr["name"], quoteattr(computed_value))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1661
                    first = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1662
        if classinfos.has_key("base"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1663
            first, new_text = classinfos["base"].generateXMLText(self, name, indent, extras, True)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1664
            text += new_text
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1665
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1666
            first = True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1667
        for element in classinfos["elements"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1668
            element["elmt_type"] = FindTypeInfos(factory, element["elmt_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1669
            value = getattr(self, element["name"], None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1670
            if element["minOccurs"] == 0 and element["maxOccurs"] == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1671
                if value is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1672
                    if first:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1673
                        text += u'>\n'
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1674
                        first = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1675
                    text += element["elmt_type"]["generate"](value, element["name"], indent + 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1676
            elif element["minOccurs"] == 1 and element["maxOccurs"] == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1677
                if first:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1678
                    text += u'>\n'
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1679
                    first = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1680
                if element["name"] == "content" and element["elmt_type"]["type"] == SIMPLETYPE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1681
                    text += element["elmt_type"]["generate"](value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1682
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1683
                    text += element["elmt_type"]["generate"](value, element["name"], indent + 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1684
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1685
                if first and len(value) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1686
                    text += u'>\n'
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1687
                    first = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1688
                for item in value:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1689
                    text += element["elmt_type"]["generate"](item, element["name"], indent + 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1690
        if not derived:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1691
            if first:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1692
                text += u'/>\n'
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1693
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1694
                text += ind1 + u'</%s>\n' % (name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1695
            return text
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1696
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1697
            return first, text
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1698
    return generateXMLTextMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1699
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1700
def gettypeinfos(name, facets):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1701
    if facets.has_key("enumeration") and facets["enumeration"][0] is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1702
        return facets["enumeration"][0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1703
    elif facets.has_key("maxInclusive"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1704
        limits = {"max" : None, "min" : None}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1705
        if facets["maxInclusive"][0] is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1706
            limits["max"] = facets["maxInclusive"][0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1707
        elif facets["maxExclusive"][0] is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1708
            limits["max"] = facets["maxExclusive"][0] - 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1709
        if facets["minInclusive"][0] is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1710
            limits["min"] = facets["minInclusive"][0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1711
        elif facets["minExclusive"][0] is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1712
            limits["min"] = facets["minExclusive"][0] + 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1713
        if limits["max"] is not None or limits["min"] is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1714
            return limits
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1715
    return name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1716
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1717
def generateGetElementAttributes(factory, classinfos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1718
    def getElementAttributes(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1719
        attr_list = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1720
        if classinfos.has_key("base"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1721
            attr_list.extend(classinfos["base"].getElementAttributes(self))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1722
        for attr in classinfos["attributes"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1723
            if attr["use"] != "prohibited":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1724
                attr_params = {"name" : attr["name"], "use" : attr["use"], 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1725
                    "type" : gettypeinfos(attr["attr_type"]["basename"], attr["attr_type"]["facets"]),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1726
                    "value" : getattr(self, attr["name"], "")}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1727
                attr_list.append(attr_params)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1728
        return attr_list
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1729
    return getElementAttributes
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1730
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1731
def generateGetElementInfos(factory, classinfos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1732
    attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1733
    elements = dict([(element["name"], element) for element in classinfos["elements"]])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1734
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1735
    def getElementInfos(self, name, path=None, derived=False):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1736
        attr_type = "element"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1737
        value = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1738
        use = "required"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1739
        children = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1740
        if path is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1741
            parts = path.split(".", 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1742
            if attributes.has_key(parts[0]):
1179
3e7bd88fcff7 Fixed inconsistency in value display when setting integer value for float parameter
Laurent Bessard
parents: 1124
diff changeset
  1743
                if len(parts) != 1:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1744
                    raise ValueError("Wrong path!")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1745
                attr_type = gettypeinfos(attributes[parts[0]]["attr_type"]["basename"], 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1746
                                         attributes[parts[0]]["attr_type"]["facets"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1747
                value = getattr(self, parts[0], "")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1748
            elif elements.has_key(parts[0]):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1749
                if elements[parts[0]]["elmt_type"]["type"] == SIMPLETYPE:
1179
3e7bd88fcff7 Fixed inconsistency in value display when setting integer value for float parameter
Laurent Bessard
parents: 1124
diff changeset
  1750
                    if len(parts) != 1:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1751
                        raise ValueError("Wrong path!")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1752
                    attr_type = gettypeinfos(elements[parts[0]]["elmt_type"]["basename"], 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1753
                                             elements[parts[0]]["elmt_type"]["facets"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1754
                    value = getattr(self, parts[0], "")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1755
                elif parts[0] == "content":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1756
                    return self.content["value"].getElementInfos(self.content["name"], path)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1757
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1758
                    attr = getattr(self, parts[0], None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1759
                    if attr is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1760
                        raise ValueError("Wrong path!")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1761
                    if len(parts) == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1762
                        return attr.getElementInfos(parts[0])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1763
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1764
                        return attr.getElementInfos(parts[0], parts[1])
1179
3e7bd88fcff7 Fixed inconsistency in value display when setting integer value for float parameter
Laurent Bessard
parents: 1124
diff changeset
  1765
            elif elements.has_key("content"):
3e7bd88fcff7 Fixed inconsistency in value display when setting integer value for float parameter
Laurent Bessard
parents: 1124
diff changeset
  1766
                if len(parts) > 0:
3e7bd88fcff7 Fixed inconsistency in value display when setting integer value for float parameter
Laurent Bessard
parents: 1124
diff changeset
  1767
                    return self.content["value"].getElementInfos(name, path)
3e7bd88fcff7 Fixed inconsistency in value display when setting integer value for float parameter
Laurent Bessard
parents: 1124
diff changeset
  1768
            elif classinfos.has_key("base"):
3e7bd88fcff7 Fixed inconsistency in value display when setting integer value for float parameter
Laurent Bessard
parents: 1124
diff changeset
  1769
                classinfos["base"].getElementInfos(name, path)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1770
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1771
                raise ValueError("Wrong path!")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1772
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1773
            if not derived:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1774
                children.extend(self.getElementAttributes())
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1775
            if classinfos.has_key("base"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1776
                children.extend(classinfos["base"].getElementInfos(self, name, derived=True)["children"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1777
            for element_name, element in elements.items():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1778
                if element["minOccurs"] == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1779
                    use = "optional"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1780
                if element_name == "content" and element["type"] == CHOICE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1781
                    attr_type = [(choice["name"], None) for choice in element["choices"]]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1782
                    if self.content is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1783
                        value = ""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1784
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1785
                        value = self.content["name"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1786
                        if self.content["value"] is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1787
                            if self.content["name"] == "sequence":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1788
                                choices_dict = dict([(choice["name"], choice) for choice in element["choices"]])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1789
                                sequence_infos = choices_dict.get("sequence", None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1790
                                if sequence_infos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1791
                                    children.extend([item.getElementInfos(infos["name"]) for item, infos in zip(self.content["value"], sequence_infos["elements"])])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1792
                            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1793
                                children.extend(self.content["value"].getElementInfos(self.content["name"])["children"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1794
                elif element["elmt_type"]["type"] == SIMPLETYPE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1795
                    children.append({"name": element_name, "require": element["minOccurs"] != 0, 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1796
                        "type": gettypeinfos(element["elmt_type"]["basename"], 
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1797
                                             element["elmt_type"]["facets"]),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1798
                        "value": getattr(self, element_name, None)})
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1799
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1800
                    instance = getattr(self, element_name, None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1801
                    if instance is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1802
                        instance = element["elmt_type"]["initial"]()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1803
                    children.append(instance.getElementInfos(element_name))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1804
        return {"name": name, "type": attr_type, "value": value, "use": use, "children": children}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1805
    return getElementInfos
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1806
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1807
def generateSetElementValue(factory, classinfos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1808
    attributes = dict([(attr["name"], attr) for attr in classinfos["attributes"] if attr["use"] != "prohibited"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1809
    elements = dict([(element["name"], element) for element in classinfos["elements"]])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1810
    
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1811
    def setElementValue(self, path, value):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1812
        if path is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1813
            parts = path.split(".", 1)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1814
            if attributes.has_key(parts[0]):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1815
                if len(parts) != 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1816
                    raise ValueError("Wrong path!")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1817
                if attributes[parts[0]]["attr_type"]["basename"] == "boolean":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1818
                    setattr(self, parts[0], value)
1017
2925d6e49893 Fixed bug when setting empty value to optional non-string parameters
Laurent Bessard
parents: 814
diff changeset
  1819
                elif attributes[parts[0]]["use"] == "optional" and value == "":
1022
ec30c12b1d67 Fixed bug when connecting and URI_location is empty
Laurent Bessard
parents: 1017
diff changeset
  1820
                    if attributes[parts[0]].has_key("default"):
ec30c12b1d67 Fixed bug when connecting and URI_location is empty
Laurent Bessard
parents: 1017
diff changeset
  1821
                        setattr(self, parts[0], 
ec30c12b1d67 Fixed bug when connecting and URI_location is empty
Laurent Bessard
parents: 1017
diff changeset
  1822
                            attributes[parts[0]]["attr_type"]["extract"](
ec30c12b1d67 Fixed bug when connecting and URI_location is empty
Laurent Bessard
parents: 1017
diff changeset
  1823
                                attributes[parts[0]]["default"], False))
ec30c12b1d67 Fixed bug when connecting and URI_location is empty
Laurent Bessard
parents: 1017
diff changeset
  1824
                    else:
ec30c12b1d67 Fixed bug when connecting and URI_location is empty
Laurent Bessard
parents: 1017
diff changeset
  1825
                        setattr(self, parts[0], None)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1826
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1827
                    setattr(self, parts[0], attributes[parts[0]]["attr_type"]["extract"](value, False))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1828
            elif elements.has_key(parts[0]):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1829
                if elements[parts[0]]["elmt_type"]["type"] == SIMPLETYPE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1830
                    if len(parts) != 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1831
                        raise ValueError("Wrong path!")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1832
                    if elements[parts[0]]["elmt_type"]["basename"] == "boolean":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1833
                        setattr(self, parts[0], value)
1017
2925d6e49893 Fixed bug when setting empty value to optional non-string parameters
Laurent Bessard
parents: 814
diff changeset
  1834
                    elif attributes[parts[0]]["minOccurs"] == 0 and value == "":
2925d6e49893 Fixed bug when setting empty value to optional non-string parameters
Laurent Bessard
parents: 814
diff changeset
  1835
                        setattr(self, parts[0], None)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1836
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1837
                        setattr(self, parts[0], elements[parts[0]]["elmt_type"]["extract"](value, False))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1838
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1839
                    instance = getattr(self, parts[0], None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1840
                    if instance is None and elements[parts[0]]["minOccurs"] == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1841
                        instance = elements[parts[0]]["elmt_type"]["initial"]()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1842
                        setattr(self, parts[0], instance)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1843
                    if instance != None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1844
                        if len(parts) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1845
                            instance.setElementValue(parts[1], value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1846
                        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1847
                            instance.setElementValue(None, value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1848
            elif elements.has_key("content"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1849
                if len(parts) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1850
                    self.content["value"].setElementValue(path, value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1851
            elif classinfos.has_key("base"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1852
                classinfos["base"].setElementValue(self, path, value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1853
        elif elements.has_key("content"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1854
            if value == "":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1855
                if elements["content"]["minOccurs"] == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1856
                    self.setcontent(None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1857
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1858
                    raise ValueError("\"content\" element is required!")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1859
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1860
                self.setcontentbytype(value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1861
    return setElementValue
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1862
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1863
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1864
Methods that generates the different methods for setting and getting the attributes
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1865
"""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1866
def generateInitMethod(factory, classinfos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1867
    def initMethod(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1868
        self.extraAttrs = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1869
        if classinfos.has_key("base"):
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1870
            classinfos["base"].init(self)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1871
        for attribute in classinfos["attributes"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1872
            attribute["attr_type"] = FindTypeInfos(factory, attribute["attr_type"])
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1873
            if attribute["use"] == "required":
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1874
                self.set(attribute["name"], attribute["attr_type"]["generate"](attribute["attr_type"]["initial"]()))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1875
        for element in classinfos["elements"]:
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1876
            if element["type"] != CHOICE:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1877
                element_name = (
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1878
                    etree.QName(factory.NSMAP["xhtml"], "p")
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1879
                    if element["type"] == ANY
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1880
                    else factory.etreeNamespaceFormat % element["name"])
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1881
                initial = GetElementInitialValue(factory, element)
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  1882
                if initial is not None:
1293
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
  1883
                    map(self.append, initial)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1884
    return initMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1885
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1886
def generateSetMethod(attr):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1887
    def setMethod(self, value):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1888
        setattr(self, attr, value)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1889
    return setMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1890
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1891
def generateGetMethod(attr):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1892
    def getMethod(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1893
        return getattr(self, attr, None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1894
    return getMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1895
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1896
def generateAddMethod(attr, factory, infos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1897
    def addMethod(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1898
        if infos["type"] == ATTRIBUTE:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1899
            infos["attr_type"] = FindTypeInfos(factory, infos["attr_type"])
1293
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
  1900
            if not infos.has_key("default"):
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
  1901
                setattr(self, attr, infos["attr_type"]["initial"]())
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1902
        elif infos["type"] == ELEMENT:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1903
            infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
1293
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
  1904
            value = infos["elmt_type"]["initial"]()
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
  1905
            DefaultElementClass.__setattr__(value, "tag", factory.etreeNamespaceFormat % attr)
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
  1906
            setattr(self, attr, value)
40117d02601b Fixed diagram editing in xmlclass refactoring
Laurent Bessard
parents: 1291
diff changeset
  1907
            value.init()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1908
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1909
            raise ValueError("Invalid class attribute!")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1910
    return addMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1911
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1912
def generateDeleteMethod(attr):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1913
    def deleteMethod(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1914
        setattr(self, attr, None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1915
    return deleteMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1916
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1917
def generateAppendMethod(attr, maxOccurs, factory, infos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1918
    def appendMethod(self, value):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1919
        infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1920
        attr_list = getattr(self, attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1921
        if maxOccurs == "unbounded" or len(attr_list) < maxOccurs:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1922
            if len(attr_list) == 0:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1923
                setattr(self, attr, [value])
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1924
            else:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1925
                attr_list[-1].addnext(value)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1926
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1927
            raise ValueError("There can't be more than %d values in \"%s\"!" % (maxOccurs, attr))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1928
    return appendMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1929
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1930
def generateInsertMethod(attr, maxOccurs, factory, infos):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1931
    def insertMethod(self, index, value):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1932
        infos["elmt_type"] = FindTypeInfos(factory, infos["elmt_type"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1933
        attr_list = getattr(self, attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1934
        if maxOccurs == "unbounded" or len(attr_list) < maxOccurs:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1935
            if len(attr_list) == 0:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1936
                setattr(self, attr, [value])
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1937
            elif index == 0:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1938
                attr_list[0].addprevious(value)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1939
            else:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1940
                attr_list[min(index - 1, len(attr_list) - 1)].addnext(value)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1941
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1942
            raise ValueError("There can't be more than %d values in \"%s\"!" % (maxOccurs, attr))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1943
    return insertMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1944
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1945
def generateGetChoicesMethod(choice_types):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1946
    def getChoicesMethod(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1947
        return [choice["name"] for choice in choice_types]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1948
    return getChoicesMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1949
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1950
def generateSetChoiceByTypeMethod(factory, choice_types):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1951
    choices = dict([(choice["name"], choice) for choice in choice_types])
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1952
    def setChoiceMethod(self, content_type):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1953
        if not choices.has_key(content_type):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1954
            raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1955
        choices[content_type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"])
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1956
        new_content = choices[content_type]["elmt_type"]["initial"]()
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1957
        self.content = new_content
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1958
        return new_content
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1959
    return setChoiceMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1960
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1961
def generateAppendChoiceByTypeMethod(maxOccurs, factory, choice_types):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1962
    choices = dict([(choice["name"], choice) for choice in choice_types])
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1963
    def appendChoiceMethod(self, content_type):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1964
        if not choices.has_key(content_type):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1965
            raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1966
        choices[content_type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"])
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1967
        if maxOccurs == "unbounded" or len(self.content) < maxOccurs:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1968
            new_element = choices[content_type]["elmt_type"]["initial"]()
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1969
            self.appendcontent(new_element)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1970
            return new_element
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1971
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1972
            raise ValueError("There can't be more than %d values in \"content\"!" % maxOccurs)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1973
    return appendChoiceMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1974
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1975
def generateInsertChoiceByTypeMethod(maxOccurs, factory, choice_types):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1976
    choices = dict([(choice["name"], choice) for choice in choice_types])
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1977
    def insertChoiceMethod(self, index, content_type):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1978
        if not choices.has_key(content_type):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1979
            raise ValueError("Unknown \"%s\" choice type for \"content\"!" % content_type)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1980
        choices[type]["elmt_type"] = FindTypeInfos(factory, choices[content_type]["elmt_type"])
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1981
        if maxOccurs == "unbounded" or len(self.content) < maxOccurs:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1982
            new_element = choices[content_type]["elmt_type"]["initial"]()
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1983
            self.insertcontent(index, new_element)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1984
            return new_element
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1985
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1986
            raise ValueError("There can't be more than %d values in \"content\"!" % maxOccurs)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1987
    return insertChoiceMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1988
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1989
def generateRemoveMethod(attr, minOccurs):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1990
    def removeMethod(self, index):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1991
        attr_list = getattr(self, attr)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1992
        if len(attr_list) > minOccurs:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  1993
            self.remove(attr_list[index])
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1994
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1995
            raise ValueError("There can't be less than %d values in \"%s\"!" % (minOccurs, attr))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1996
    return removeMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1997
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1998
def generateCountMethod(attr):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1999
    def countMethod(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  2000
        return len(getattr(self, attr))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  2001
    return countMethod
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  2002
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  2003
"""
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2004
This function generate a xml parser from a class factory
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2005
"""
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2006
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2007
class DefaultElementClass(etree.ElementBase):
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  2008
    
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  2009
    def init(self):
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  2010
        pass
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2011
    
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2012
    def getLocalTag(self):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2013
        return etree.QName(self.tag).localname
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2014
        
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2015
    def tostring(self):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2016
        return etree.tostring(self, pretty_print=True)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2017
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2018
class XMLElementClassLookUp(etree.PythonElementClassLookup):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2019
    
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  2020
    def __init__(self, classes, *args, **kwargs):
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2021
        etree.PythonElementClassLookup.__init__(self, *args, **kwargs)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2022
        self.LookUpClasses = classes
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2023
    
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2024
    def GetElementClass(self, element_tag, parent_tag=None, default=DefaultElementClass):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2025
        element_class = self.LookUpClasses.get(element_tag, (default, None))
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2026
        if not isinstance(element_class, DictType):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2027
            if isinstance(element_class[0], (StringType, UnicodeType)):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2028
                return self.GetElementClass(element_class[0], default=default)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2029
            return element_class[0]
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2030
        
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2031
        element_with_parent_class = element_class.get(parent_tag, default)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2032
        if isinstance(element_with_parent_class, (StringType, UnicodeType)):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2033
            return self.GetElementClass(element_with_parent_class, default=default)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2034
        return element_with_parent_class
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2035
        
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2036
    def lookup(self, document, element):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2037
        parent = element.getparent()
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2038
        return self.GetElementClass(element.tag, 
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2039
            parent.tag if parent is not None else None)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2040
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2041
class XMLClassParser(etree.XMLParser):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2042
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2043
    def __init__(self, namespaces, default_namespace_format, base_class, *args, **kwargs):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2044
        etree.XMLParser.__init__(self, *args, **kwargs)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2045
        self.DefaultNamespaceFormat = default_namespace_format
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2046
        self.NSMAP = namespaces
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2047
        targetNamespace = etree.QName(default_namespace_format % "d").namespace
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2048
        if targetNamespace is not None:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2049
            self.RootNSMAP = {
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2050
                name if targetNamespace != uri else None: uri
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2051
                for name, uri in namespaces.iteritems()}
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2052
        else:
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2053
            self.RootNSMAP = namespaces
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2054
        self.BaseClass = base_class
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2055
    
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2056
    def set_element_class_lookup(self, class_lookup):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2057
        etree.XMLParser.set_element_class_lookup(self, class_lookup)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2058
        self.ClassLookup = class_lookup
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2059
    
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2060
    def CreateRoot(self):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2061
        if self.BaseClass is not None:
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  2062
            root = self.makeelement(
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2063
                self.DefaultNamespaceFormat % self.BaseClass[0],
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2064
                nsmap=self.RootNSMAP)
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  2065
            root.init()
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  2066
            return root
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2067
        return None
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2068
    
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2069
    def GetElementClass(self, element_tag, parent_tag=None):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2070
        return self.ClassLookup.GetElementClass(
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2071
            self.DefaultNamespaceFormat % element_tag, 
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2072
            self.DefaultNamespaceFormat % parent_tag 
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2073
            if parent_tag is not None else parent_tag, 
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2074
            None)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2075
    
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2076
    def CreateElement(self, element_tag, parent_tag=None):
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2077
        new_element = self.GetElementClass(element_tag, parent_tag)()
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2078
        DefaultElementClass.__setattr__(new_element, "tag", self.DefaultNamespaceFormat % element_tag)
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  2079
        new_element.init()
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2080
        return new_element
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2081
    
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2082
def GenerateParser(factory, xsdstring):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  2083
    ComputedClasses = factory.CreateClasses()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  2084
    if factory.FileName is not None and len(ComputedClasses) == 1:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2085
        ComputedClasses = ComputedClasses[factory.FileName]
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2086
        BaseClass = [(name, XSDclass) for name, XSDclass in ComputedClasses.items() if XSDclass.IsBaseClass]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  2087
    else:
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2088
        BaseClass = []
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2089
    UpdateXMLClassGlobals(ComputedClasses)
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2090
    
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2091
    parser = XMLClassParser(
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2092
        factory.NSMAP,
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2093
        factory.etreeNamespaceFormat,
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2094
        BaseClass[0] if len(BaseClass) == 1 else None,
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2095
        schema = etree.XMLSchema(etree.fromstring(xsdstring)),
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2096
        strip_cdata = False, remove_blank_text=True)
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  2097
    class_lookup = XMLElementClassLookUp(factory.ComputedClassesLookUp)
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2098
    parser.set_element_class_lookup(class_lookup)
1291
42ea51d083ce Second stage of xmlclass refactoring using lxml , project are loaded and displayed successfully
Laurent Bessard
parents: 1290
diff changeset
  2099
    
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2100
    return parser
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  2101
1124
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1022
diff changeset
  2102
def UpdateXMLClassGlobals(classes):
b1705000eba1 Fixed support for defining python runtime code using sections like in c_ext
Laurent Bessard
parents: 1022
diff changeset
  2103
    globals().update(classes)
1290
13ee5f4ab612 First stage of xmlclass refactoring using lxml
Laurent Bessard
parents: 1179
diff changeset
  2104