Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
opcua-asyncio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nikola Balog
opcua-asyncio
Commits
4b39c912
Commit
4b39c912
authored
Dec 31, 2015
by
olivier R-D
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use python Enum in autogenerated code
parent
e6a6db32
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
139 additions
and
126 deletions
+139
-126
opcua/uaprotocol_auto.py
opcua/uaprotocol_auto.py
+109
-108
schemas/generate_protocol_python.py
schemas/generate_protocol_python.py
+30
-18
No files found.
opcua/uaprotocol_auto.py
View file @
4b39c912
This diff is collapsed.
Click to expand it.
schemas/generate_protocol_python.py
View file @
4b39c912
...
...
@@ -3,6 +3,7 @@ import generate_model as gm
IgnoredEnums
=
[
"NodeIdType"
]
IgnoredStructs
=
[
"QualifiedName"
,
"NodeId"
,
"ExpandedNodeId"
,
"FilterOperand"
,
"Variant"
,
"DataValue"
,
"LocalizedText"
,
"ExtensionObject"
]
numerics
=
(
"Int8"
,
"UInt8"
,
"Sbyte"
,
"Byte"
,
"Char"
,
"Boolean"
,
"Int16"
,
"UInt16"
,
"Int32"
,
"UInt32"
,
"Float"
,
"Int64"
,
"UInt64"
,
"Double"
)
class
CodeGenerator
(
object
):
...
...
@@ -55,6 +56,7 @@ class CodeGenerator(object):
self
.
write
(
"'''"
)
self
.
write
(
""
)
self
.
write
(
"from datetime import datetime"
)
self
.
write
(
"from enum import Enum, IntEnum"
)
self
.
write
(
""
)
self
.
write
(
"from opcua.utils import Buffer"
)
self
.
write
(
"from opcua.uatypes import *"
)
...
...
@@ -63,7 +65,7 @@ class CodeGenerator(object):
def
generate_enum_code
(
self
,
enum
):
self
.
write
(
""
)
self
.
write
(
""
)
self
.
write
(
"class {}(
object
):"
.
format
(
enum
.
name
))
self
.
write
(
"class {}(
IntEnum
):"
.
format
(
enum
.
name
))
self
.
iidx
=
1
self
.
write
(
"'''"
)
if
enum
.
doc
:
...
...
@@ -165,8 +167,8 @@ class CodeGenerator(object):
if
field
.
is_native_type
():
self
.
write_pack_uatype
(
listname
,
fname
,
field
.
uatype
)
elif
field
.
uatype
in
self
.
model
.
enum_list
:
uatype
=
self
.
model
.
get_enum
(
field
.
uatype
).
uatype
self
.
write_pack_
uatype
(
listname
,
fname
,
uatype
)
enum
=
self
.
model
.
get_enum
(
field
.
uatype
)
self
.
write_pack_
enum
(
listname
,
fname
,
enum
)
elif
field
.
uatype
in
(
"ExtensionObject"
):
self
.
write
(
"{}.append(extensionobject_to_binary({}))"
.
format
(
listname
,
fname
))
else
:
...
...
@@ -214,8 +216,10 @@ class CodeGenerator(object):
else
:
self
.
write_unpack_uatype
(
field
.
name
,
field
.
uatype
)
elif
field
.
uatype
in
self
.
model
.
enum_list
:
uatype
=
self
.
model
.
get_enum
(
field
.
uatype
).
uatype
self
.
write_unpack_uatype
(
field
.
name
,
uatype
)
#uatype = self.model.get_enum(field.uatype).uatype
#self.write_unpack_uatype(field.name, uatype)
enum
=
self
.
model
.
get_enum
(
field
.
uatype
)
self
.
write_unpack_enum
(
field
.
name
,
enum
)
else
:
if
field
.
uatype
in
(
"ExtensionObject"
):
frombinary
=
"extensionobject_from_binary(data)"
...
...
@@ -264,15 +268,25 @@ class CodeGenerator(object):
self
.
iix
=
0
def
write_unpack_uatype
(
self
,
name
,
uatype
):
def
write_unpack_enum
(
self
,
name
,
enum
):
self
.
write
(
"self.{} = {}(uatype_{}.unpack(data.read({}))[0])"
.
format
(
name
,
enum
.
name
,
enum
.
uatype
,
self
.
get_size_from_uatype
(
enum
.
uatype
)))
def
get_size_from_uatype
(
self
,
uatype
):
if
uatype
in
(
"Int8"
,
"UInt8"
,
"Sbyte"
,
"Byte"
,
"Char"
,
"Boolean"
):
size
=
1
return
1
elif
uatype
in
(
"Int16"
,
"UInt16"
):
size
=
2
return
2
elif
uatype
in
(
"Int32"
,
"UInt32"
,
"Float"
):
size
=
4
return
4
elif
uatype
in
(
"Int64"
,
"UInt64"
,
"Double"
):
size
=
8
return
8
else
:
raise
Exception
(
"Cannot get size from type {}"
.
format
(
uatype
))
def
write_unpack_uatype
(
self
,
name
,
uatype
):
if
uatype
in
numerics
:
size
=
self
.
get_size_from_uatype
(
uatype
)
self
.
write
(
"self.{} = uatype_{}.unpack(data.read({}))[0]"
.
format
(
name
,
uatype
,
size
))
elif
uatype
==
"String"
:
self
.
write
(
"self.{} = unpack_string(data)"
.
format
(
name
))
return
...
...
@@ -285,15 +299,12 @@ class CodeGenerator(object):
else
:
self
.
write
(
"self.{} = unpack_uatype('{}', data)"
.
format
(
name
,
uatype
))
return
self
.
write
(
"self.{} = uatype_{}.unpack(data.read({}))[0]"
.
format
(
name
,
uatype
,
size
))
def
write_pack_enum
(
self
,
listname
,
name
,
enum
):
self
.
write
(
"{}.append(uatype_{}.pack({}.value))"
.
format
(
listname
,
enum
.
uatype
,
name
))
def
write_pack_uatype
(
self
,
listname
,
name
,
uatype
):
if
uatype
in
(
"Int8"
,
"UInt8"
,
"Sbyte"
,
"Byte"
,
"Char"
,
"Boolean"
,
"Int16"
,
"UInt16"
,
"Int32"
,
"UInt32"
,
"Float"
,
"Int64"
,
"UInt64"
,
"Double"
):
if
uatype
in
numerics
:
self
.
write
(
"{}.append(uatype_{}.pack({}))"
.
format
(
listname
,
uatype
,
name
))
return
elif
uatype
==
"String"
:
...
...
@@ -311,7 +322,8 @@ class CodeGenerator(object):
def
get_default_value
(
self
,
field
):
if
field
.
uatype
in
self
.
model
.
enum_list
:
return
0
enum
=
self
.
model
.
get_enum
(
field
.
uatype
)
return
enum
.
name
+
"(0)"
if
field
.
uatype
in
(
"String"
):
return
"''"
elif
field
.
uatype
in
(
"ByteString"
,
"CharArray"
,
"Char"
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment