@@ -34,6 +34,13 @@ def __init__(self, name):
3434 self .code = ""
3535
3636 def get_code (self ):
37+ if not self .fields :
38+ return """
39+
40+ class {}(object):
41+ pass
42+
43+ """ .format (self .name )
3744 self ._make_constructor ()
3845 self ._make_from_binary ()
3946 self ._make_to_binary ()
@@ -42,9 +49,10 @@ def get_code(self):
4249 def _make_constructor (self ):
4350 self .code = """
4451
52+
4553class {0}(object):
4654 '''
47- {0} autogenerated from xml
55+ {0} structure autogenerated from xml
4856 '''
4957 def __init__(self, data=None):
5058 if data is not None:
@@ -56,7 +64,7 @@ def __init__(self, data=None):
5664
5765 def _make_from_binary (self ):
5866 self .code += '''
59- @staticmethod
67+ @staticmethod
6068 def from_binary(data):
6169 return {}(data=data)
6270
@@ -72,7 +80,9 @@ def _binary_init(self, data):
7280 if field .array :
7381 self .code += '''
7482 length = ua.ua_binary.Primitives.Int32.unpack(data)
75- if length != -1:
83+ if length == -1:
84+ self.{0} = None
85+ else:
7686 self.{0} = [ua.{1}.from_binary(data) for _ in range(length)]
7787''' .format (field .name , field .uatype )
7888 else :
@@ -131,11 +141,9 @@ def _make_model(self):
131141 array = True
132142 continue
133143 field = Field (name )
134- uatype = xmlfield .get ("TypeName" )
135- if uatype .startswith ("opc" ):
136- field .uatype = uatype [4 :]
137- else :
138- field .uatype = uatype [3 :]
144+ field .uatype = xmlfield .get ("TypeName" )
145+ if ":" in field .uatype :
146+ field .uatype = field .uatype .split (":" )[1 ]
139147 field .value = get_default_value (field .uatype )
140148 if array :
141149 field .array = True
@@ -159,8 +167,6 @@ def get_structures(self):
159167 exec (struct .get_code (), ld )
160168 return ld
161169
162-
163-
164170 def _make_header (self ):
165171 self ._file .write ("""
166172'''
@@ -171,18 +177,18 @@ def _make_header(self):
171177import uuid
172178
173179from opcua import ua
174-
175-
176180""" )
177181
178182
179183
180184
181185if __name__ == "__main__" :
182186 import sys
187+ from IPython import embed
183188 sys .path .insert (0 , "." ) # necessary for import in current dir
184189
185- xmlpath = "/home/olivier/python-opcua/schemas/example.bsd"
190+ #xmlpath = "schemas/Opc.Ua.Types.bsd"
191+ xmlpath = "schemas/example.bsd"
186192 c = StructGenerator (xmlpath , "structures.py" )
187193 c .run ()
188194 import structures as s
0 commit comments