MCPcopy Create free account
hub / github.com/FreeOpcUa/python-opcua / StructGenerator

Class StructGenerator

opcua/common/structures.py:140–238  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

138
139
140class StructGenerator(object):
141 def __init__(self):
142 self.model = []
143
144 def make_model_from_string(self, xml):
145 obj = objectify.fromstring(xml)
146 self._make_model(obj)
147
148 def make_model_from_file(self, path):
149 obj = objectify.parse(path)
150 root = obj.getroot()
151 self._make_model(root)
152
153 def _make_model(self, root):
154 enums = {}
155 for child in root.iter("{*}EnumeratedType"):
156 intenum = EnumType(child.get("Name"))
157 for xmlfield in child.iter("{*}EnumeratedValue"):
158 name = xmlfield.get("Name")
159 value = xmlfield.get("Value")
160 enumvalue = EnumeratedValue(name, value)
161 intenum.fields.append(enumvalue)
162 enums[child.get("Name")] = value
163 self.model.append(intenum)
164
165 for child in root.iter("{*}StructuredType"):
166 struct = Struct(child.get("Name"))
167 array = False
168 for xmlfield in child.iter("{*}Field"):
169 name = xmlfield.get("Name")
170 if name.startswith("NoOf"):
171 array = True
172 continue
173 field = Field(_clean_name(name))
174 field.uatype = xmlfield.get("TypeName")
175 if ":" in field.uatype:
176 field.uatype = field.uatype.split(":")[1]
177 field.uatype = _clean_name(field.uatype)
178 field.value = get_default_value(field.uatype, enums)
179 if array:
180 field.array = True
181 field.value = []
182 array = False
183 struct.fields.append(field)
184 self.model.append(struct)
185
186 def save_to_file(self, path, register=False):
187 _file = open(path, "w+")
188 self._make_header(_file)
189 for struct in self.model:
190 _file.write(struct.get_code())
191 if register:
192 _file.write(self._make_registration())
193 _file.close()
194
195 def _make_registration(self):
196 code = "\n\n"
197 for struct in self.model:

Callers 4

test_custom_structsMethod · 0.90
load_type_definitionsFunction · 0.85

Calls

no outgoing calls

Tested by 3

test_custom_structsMethod · 0.72