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