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
ea611693
Commit
ea611693
authored
Feb 25, 2021
by
oroulet
Committed by
oroulet
Mar 10, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix tests
parent
2ef024b3
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
31 additions
and
41 deletions
+31
-41
asyncua/common/structures.py
asyncua/common/structures.py
+9
-2
asyncua/common/structures104.py
asyncua/common/structures104.py
+0
-1
asyncua/ua/uatypes.py
asyncua/ua/uatypes.py
+3
-3
tests/test_custom_structures.py
tests/test_custom_structures.py
+7
-13
tests/test_unit.py
tests/test_unit.py
+5
-6
tests/util_enum_struct.py
tests/util_enum_struct.py
+7
-16
No files found.
asyncua/common/structures.py
View file @
ea611693
...
...
@@ -9,7 +9,8 @@ import logging
# The next two imports are for generated code
from
datetime
import
datetime
from
enum
import
IntEnum
,
EnumMeta
from
dataclasses
import
dataclass
from
dataclasses
import
dataclass
,
field
from
typing
import
List
from
xml.etree
import
ElementTree
as
ET
...
...
@@ -152,7 +153,7 @@ class StructGenerator(object):
field
.
value
=
get_default_value
(
field
.
uatype
,
enums
)
if
array
:
field
.
array
=
True
field
.
value
=
[]
field
.
value
=
"field(default_factory=list)"
array
=
False
struct
.
fields
.
append
(
field
)
self
.
model
.
append
(
struct
)
...
...
@@ -184,6 +185,8 @@ THIS FILE IS AUTOGENERATED, DO NOT EDIT!!!
from datetime import datetime
import uuid
from dataclasses import dataclass, field
from typing import List, Union
from asyncua import ua
"""
)
...
...
@@ -268,6 +271,10 @@ def _generate_python_class(model, env=None):
env
[
'IntEnum'
]
=
IntEnum
if
"dataclass"
not
in
env
:
env
[
'dataclass'
]
=
dataclass
if
"field"
not
in
env
:
env
[
'field'
]
=
field
if
"List"
not
in
env
:
env
[
'List'
]
=
List
# generate classes one by one and add them to dict
for
element
in
model
:
code
=
element
.
get_code
()
...
...
asyncua/common/structures104.py
View file @
ea611693
...
...
@@ -203,7 +203,6 @@ class {struct_name}(ua.FrozenClass):
code += f" '
{
fname
}
': ('
Encoding
', {counter}),
\
n
"
counter += 1
code += " }
\
n
\
n
"
print("CODE", code)
return code
...
...
asyncua/ua/uatypes.py
View file @
ea611693
...
...
@@ -649,10 +649,10 @@ class LocalizedText:
@
staticmethod
def
from_string
(
string
):
m
=
re
.
match
(
r"^LocalizedText\
(
E
ncoding:(.*), Locale:(.*), Text:(.*)
\
)$
", string)
m
=
re
.
match
(
r"^LocalizedText\
(
Loc
ale='(.*)', Text='(.*)'
\
)$
", string)
if m:
text = m.group(
3) if m.group(3
) != str(None) else None
locale = m.group(
2) if m.group(2
) != str(None) else None
text = m.group(
2) if m.group(2
) != str(None) else None
locale = m.group(
1) if m.group(1
) != str(None) else None
return LocalizedText(Text=text, Locale=locale)
return LocalizedText(string)
...
...
tests/test_custom_structures.py
View file @
ea611693
...
...
@@ -148,8 +148,8 @@ async def test_opc_type_dict_get_dict_value(srv):
async
def
test_reference_generator_1
(
srv
):
id1
=
ua
.
NodeId
(
1
,
namespaceidx
=
2
,
nodeidt
ype
=
ua
.
NodeIdType
.
Numeric
)
id2
=
ua
.
NodeId
(
2
,
namespaceidx
=
2
,
nodeidt
ype
=
ua
.
NodeIdType
.
Numeric
)
id1
=
ua
.
NodeId
(
1
,
NamespaceIndex
=
2
,
NodeIdT
ype
=
ua
.
NodeIdType
.
Numeric
)
id2
=
ua
.
NodeId
(
2
,
NamespaceIndex
=
2
,
NodeIdT
ype
=
ua
.
NodeIdType
.
Numeric
)
ref
=
ua
.
NodeId
(
ua
.
ObjectIds
.
HasEncoding
,
0
)
result
=
reference_generator
(
id1
,
id2
,
ref
)
assert
result
.
IsForward
...
...
@@ -160,8 +160,8 @@ async def test_reference_generator_1(srv):
async
def
test_reference_generator_2
(
srv
):
id1
=
ua
.
NodeId
(
1
,
namespaceidx
=
2
,
nodeidt
ype
=
ua
.
NodeIdType
.
Numeric
)
id2
=
ua
.
NodeId
(
2
,
namespaceidx
=
2
,
nodeidt
ype
=
ua
.
NodeIdType
.
Numeric
)
id1
=
ua
.
NodeId
(
1
,
NamespaceIndex
=
2
,
NodeIdT
ype
=
ua
.
NodeIdType
.
Numeric
)
id2
=
ua
.
NodeId
(
2
,
NamespaceIndex
=
2
,
NodeIdT
ype
=
ua
.
NodeIdType
.
Numeric
)
ref
=
ua
.
NodeId
(
ua
.
ObjectIds
.
HasEncoding
,
0
)
result
=
reference_generator
(
id1
,
id2
,
ref
,
False
)
assert
not
result
.
IsForward
...
...
@@ -264,8 +264,6 @@ async def test_data_type_dict_add_field_1(srv):
await
srv
.
dict_builder
.
set_dict_byte_string
()
await
srv
.
srv
.
load_type_definitions
()
struct
=
get_ua_class
(
struct_name
)
assert
struct
.
ua_types
[
0
][
0
]
==
'id'
assert
struct
.
ua_types
[
0
][
1
]
==
'Int32'
struct_instance
=
struct
()
assert
struct_instance
.
id
==
0
...
...
@@ -277,8 +275,6 @@ async def test_data_type_dict_add_field_2(srv):
await
srv
.
dict_builder
.
set_dict_byte_string
()
await
srv
.
srv
.
load_type_definitions
()
struct
=
get_ua_class
(
struct_name
)
assert
struct
.
ua_types
[
0
][
0
]
==
'id'
assert
struct
.
ua_types
[
0
][
1
]
==
'ListOfInt32'
struct_instance
=
struct
()
assert
isinstance
(
struct_instance
.
id
,
list
)
...
...
@@ -298,8 +294,6 @@ async def test_struct_node_add_field(srv):
await
srv
.
dict_builder
.
set_dict_byte_string
()
await
srv
.
srv
.
load_type_definitions
()
struct
=
get_ua_class
(
struct_name
)
assert
struct
.
ua_types
[
0
][
0
]
==
'id'
assert
struct
.
ua_types
[
0
][
1
]
==
'Int32'
struct_instance
=
struct
()
assert
struct_instance
.
id
==
0
...
...
@@ -339,7 +333,7 @@ async def test_functional_basic(srv):
await
srv
.
dict_builder
.
set_dict_byte_string
()
await
srv
.
srv
.
load_type_definitions
()
basic_var
=
await
srv
.
srv
.
nodes
.
objects
.
add_variable
(
ua
.
NodeId
(
namespaceid
x
=
srv
.
idx
),
'BasicStruct'
,
basic_var
=
await
srv
.
srv
.
nodes
.
objects
.
add_variable
(
ua
.
NodeId
(
NamespaceInde
x
=
srv
.
idx
),
'BasicStruct'
,
ua
.
Variant
(
None
,
ua
.
VariantType
.
Null
),
datatype
=
basic_struct
.
data_type
)
...
...
@@ -370,7 +364,7 @@ async def test_functional_advance(srv):
await
srv
.
dict_builder
.
set_dict_byte_string
()
await
srv
.
srv
.
load_type_definitions
()
basic_var
=
await
srv
.
srv
.
nodes
.
objects
.
add_variable
(
ua
.
NodeId
(
namespaceid
x
=
srv
.
idx
),
'BasicStruct'
,
basic_var
=
await
srv
.
srv
.
nodes
.
objects
.
add_variable
(
ua
.
NodeId
(
NamespaceInde
x
=
srv
.
idx
),
'BasicStruct'
,
ua
.
Variant
(
None
,
ua
.
VariantType
.
Null
),
datatype
=
basic_struct
.
data_type
)
...
...
@@ -380,7 +374,7 @@ async def test_functional_advance(srv):
basic_msg
.
Comments
=
'Test string'
await
basic_var
.
write_value
(
basic_msg
)
nested_var
=
await
srv
.
srv
.
nodes
.
objects
.
add_variable
(
ua
.
NodeId
(
namespaceid
x
=
srv
.
idx
),
'NestedStruct'
,
nested_var
=
await
srv
.
srv
.
nodes
.
objects
.
add_variable
(
ua
.
NodeId
(
NamespaceInde
x
=
srv
.
idx
),
'NestedStruct'
,
ua
.
Variant
(
None
,
ua
.
VariantType
.
Null
),
datatype
=
nested_struct
.
data_type
)
...
...
tests/test_unit.py
View file @
ea611693
...
...
@@ -225,14 +225,15 @@ def test_string_to_variant_qname():
def
test_string_to_variant_localized_text
():
string
=
"_This is my nøåæ"
obj
=
ua
.
LocalizedText
(
string
)
string_repr
=
f"LocalizedText(Locale=None, Text='
{
string
}
')"
assert
obj
==
string_to_val
(
string
,
ua
.
VariantType
.
LocalizedText
)
assert
string
==
val_to_string
(
obj
)
assert
string
_repr
==
val_to_string
(
obj
)
def
test_string_to_variant_localized_text_with_locale
():
locale
=
"cs-CZ"
string
=
"Moje jméno"
string_repr
=
f
'LocalizedText(Encoding:3, Locale:
{
locale
}
, Text:
{
string
}
)'
string_repr
=
f
"LocalizedText(Locale='
{
locale
}
', Text='
{
string
}
')"
obj
=
ua
.
LocalizedText
(
string
,
locale
)
assert
obj
==
string_to_val
(
string_repr
,
ua
.
VariantType
.
LocalizedText
)
assert
string_repr
==
val_to_string
(
obj
)
...
...
@@ -241,22 +242,20 @@ def test_string_to_variant_localized_text_with_locale():
def
test_string_to_variant_localized_text_with_none1
():
locale
=
"en-US"
string
=
""
string_repr
=
f"LocalizedText(
Encoding:1, Locale:
{
locale
}
, Text:
{
string
}
)"
string_repr
=
f"LocalizedText(
Locale='
{
locale
}
', Text='
{
string
}
'
)"
obj
=
ua
.
LocalizedText
(
string
,
locale
)
obj2
=
ua
.
LocalizedText
(
string
)
assert
obj
==
string_to_val
(
string_repr
,
ua
.
VariantType
.
LocalizedText
)
assert
obj2
==
string_to_val
(
string
,
ua
.
VariantType
.
LocalizedText
)
assert
""
==
val_to_string
(
obj
)
def
test_string_to_variant_localized_text_with_none2
():
locale
=
None
string
=
"my name is ..."
string_repr
=
f"LocalizedText(
Encoding:2, Locale:
{
locale
}
, Text:
{
string
}
)"
string_repr
=
f"LocalizedText(
Locale='
{
locale
}
', Text='
{
string
}
'
)"
obj
=
ua
.
LocalizedText
(
string
,
locale
)
assert
obj
==
string_to_val
(
string_repr
,
ua
.
VariantType
.
LocalizedText
)
assert
obj
==
string_to_val
(
string
,
ua
.
VariantType
.
LocalizedText
)
assert
string
==
val_to_string
(
obj
)
def
test_string_to_val_xml_element
():
...
...
tests/util_enum_struct.py
View file @
ea611693
import
os
from
dataclasses
import
dataclass
from
asyncua
import
ua
from
asyncua.ua
import
uatypes
from
enum
import
IntEnum
...
...
@@ -18,21 +20,10 @@ import asyncua.ua
setattr
(
asyncua
.
ua
,
'ExampleEnum'
,
ExampleEnum
)
class
ExampleStruct
(
uatypes
.
FrozenClass
):
ua_types
=
[
(
'IntVal1'
,
'Int16'
),
(
'EnumVal'
,
'ExampleEnum'
),
]
def
__init__
(
self
):
self
.
IntVal1
=
0
self
.
EnumVal
=
ExampleEnum
(
0
)
self
.
_freeze
=
True
def
__str__
(
self
):
return
f'ExampleStruct(IntVal1:
{
self
.
IntVal1
}
, EnumVal:
{
self
.
EnumVal
}
)'
__repr__
=
__str__
@
dataclass
class
ExampleStruct
:
IntVal1
:
uatypes
.
Int16
=
0
EnumVal
:
ExampleEnum
=
ExampleEnum
(
0
)
async
def
add_server_custom_enum_struct
(
server
:
Server
):
...
...
@@ -44,4 +35,4 @@ async def add_server_custom_enum_struct(server: Server):
val
.
IntVal1
=
242
val
.
EnumVal
=
ua
.
ExampleEnum
.
EnumVal2
myvar
=
server
.
get_node
(
ua
.
NodeId
(
6009
,
ns
))
await
myvar
.
write_value
(
val
)
\ No newline at end of file
await
myvar
.
write_value
(
val
)
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