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