Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xAAL
Code
Python
Commits
11f27101
Commit
11f27101
authored
5 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
No more type hints error
Ouff ..
parent
5ccfdb4c
No related branches found
No related tags found
1 merge request
!1
First try of type hints
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
libs/lib/tests/test_message.py
+5
-3
5 additions, 3 deletions
libs/lib/tests/test_message.py
libs/lib/xaal/lib/messages.py
+15
-11
15 additions, 11 deletions
libs/lib/xaal/lib/messages.py
with
20 additions
and
14 deletions
libs/lib/tests/test_message.py
+
5
−
3
View file @
11f27101
...
@@ -174,13 +174,15 @@ class TestMessageFactory(unittest.TestCase):
...
@@ -174,13 +174,15 @@ class TestMessageFactory(unittest.TestCase):
mf
=
test_factory
()
mf
=
test_factory
()
# too young
# too young
msg
=
test_message
()
msg
=
test_message
()
msg
.
timestamp
[
0
]
=
msg
.
timestamp
[
0
]
+
60
*
5
target
=
(
msg
.
timestamp
[
0
]
+
60
*
5
,
msg
.
timestamp
[
1
])
msg
.
timestamp
=
target
data
=
mf
.
encode_msg
(
msg
)
data
=
mf
.
encode_msg
(
msg
)
with
self
.
assertRaises
(
MessageParserError
):
with
self
.
assertRaises
(
MessageParserError
):
mf
.
decode_msg
(
data
)
mf
.
decode_msg
(
data
)
# too old
# too old
msg
.
timestamp
=
messages
.
build_timestamp
()
msg
.
timestamp
=
messages
.
build_timestamp
()
msg
.
timestamp
[
0
]
=
msg
.
timestamp
[
0
]
-
60
*
5
target
=
(
msg
.
timestamp
[
0
]
-
60
*
5
,
msg
.
timestamp
[
1
])
msg
.
timestamp
=
target
data
=
mf
.
encode_msg
(
msg
)
data
=
mf
.
encode_msg
(
msg
)
with
self
.
assertRaises
(
MessageParserError
):
with
self
.
assertRaises
(
MessageParserError
):
mf
.
decode_msg
(
data
)
mf
.
decode_msg
(
data
)
...
@@ -214,4 +216,4 @@ class TestMessageFactory(unittest.TestCase):
...
@@ -214,4 +216,4 @@ class TestMessageFactory(unittest.TestCase):
mf
.
decode_msg
(
data
)
mf
.
decode_msg
(
data
)
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
unittest
.
main
()
unittest
.
main
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
libs/lib/xaal/lib/messages.py
+
15
−
11
View file @
11f27101
...
@@ -65,11 +65,11 @@ class Message(object):
...
@@ -65,11 +65,11 @@ class Message(object):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
version
=
config
.
STACK_VERSION
# message API version
self
.
version
=
config
.
STACK_VERSION
# message API version
self
.
__targets
=
[]
# target property
self
.
__targets
=
[]
# target property
self
.
timestamp
=
None
# message timestamp
self
.
timestamp
:
tuple
=
()
# message timestamp
self
.
source
=
None
# message source
self
.
source
:
Optional
[
UUID
]
=
None
# message source
self
.
dev_type
=
None
# message dev_type
self
.
dev_type
:
Optional
[
str
]
=
None
# message dev_type
self
.
msg_type
=
None
# message type
self
.
msg_type
:
Optional
[
MessageType
]
=
None
# message type
self
.
action
=
None
# message action
self
.
action
:
Optional
[
str
]
=
None
# message action
self
.
body
=
{}
# message body
self
.
body
=
{}
# message body
@property
@property
...
@@ -105,7 +105,7 @@ class Message(object):
...
@@ -105,7 +105,7 @@ class Message(object):
tmp
=
tmp
+
"
- %-12s %s
\n
"
%
(
k
,
v
)
tmp
=
tmp
+
"
- %-12s %s
\n
"
%
(
k
,
v
)
# tmp = tmp.strip()
# tmp = tmp.strip()
r
.
append
([
'
body
'
,
tmp
])
r
.
append
([
'
body
'
,
tmp
])
print
(
tabulate
(
r
,
headers
=
[
'
Fied
'
,
'
Value
'
],
tablefmt
=
'
psql
'
))
print
(
tabulate
(
r
,
headers
=
[
'
Fie
l
d
'
,
'
Value
'
],
tablefmt
=
'
psql
'
))
def
__repr__
(
self
)
->
str
:
def
__repr__
(
self
)
->
str
:
return
f
"
<xaal.Message
{
id
(
self
)
:
x
}
{
self
.
source
}
{
self
.
dev_type
}
{
self
.
msg_type
}
{
self
.
action
}
>
"
return
f
"
<xaal.Message
{
id
(
self
)
:
x
}
{
self
.
source
}
{
self
.
dev_type
}
{
self
.
msg_type
}
{
self
.
action
}
>
"
...
@@ -161,7 +161,7 @@ class MessageFactory(object):
...
@@ -161,7 +161,7 @@ class MessageFactory(object):
# key encode / decode message built from passphrase
# key encode / decode message built from passphrase
self
.
cipher_key
=
cipher_key
self
.
cipher_key
=
cipher_key
def
encode_msg
(
self
,
msg
:
Message
):
def
encode_msg
(
self
,
msg
:
Message
)
->
bytes
:
"""
Apply security layer and return encode MSG in CBOR
"""
Apply security layer and return encode MSG in CBOR
:param msg: xAAL msg instance
:param msg: xAAL msg instance
:type msg: Message
:type msg: Message
...
@@ -179,6 +179,10 @@ class MessageFactory(object):
...
@@ -179,6 +179,10 @@ class MessageFactory(object):
# Format payload & ciphering
# Format payload & ciphering
buf
=
[]
buf
=
[]
if
not
msg
.
source
:
raise
MessageError
(
"
No source address in message
"
)
if
not
msg
.
msg_type
:
raise
MessageError
(
"
No msg_type in message
"
)
buf
.
append
(
msg
.
source
.
bytes
)
buf
.
append
(
msg
.
source
.
bytes
)
buf
.
append
(
msg
.
dev_type
)
buf
.
append
(
msg
.
dev_type
)
buf
.
append
(
msg
.
msg_type
.
value
)
buf
.
append
(
msg
.
msg_type
.
value
)
...
@@ -218,7 +222,7 @@ class MessageFactory(object):
...
@@ -218,7 +222,7 @@ class MessageFactory(object):
msg_time
=
data_rx
[
1
]
msg_time
=
data_rx
[
1
]
targets
=
cbor
.
loads
(
data_rx
[
3
])
targets
=
cbor
.
loads
(
data_rx
[
3
])
msg
.
targets
=
[
UUID
(
bytes
=
t
)
for
t
in
targets
]
msg
.
targets
=
[
UUID
(
bytes
=
t
)
for
t
in
targets
]
msg
.
timestamp
=
[
data_rx
[
1
],
data_rx
[
2
]
]
msg
.
timestamp
=
(
data_rx
[
1
],
data_rx
[
2
]
)
except
IndexError
:
except
IndexError
:
raise
MessageParserError
(
"
Bad Message, wrong fields
"
)
raise
MessageParserError
(
"
Bad Message, wrong fields
"
)
...
@@ -336,7 +340,7 @@ def build_nonce(data: tuple) -> bytes:
...
@@ -336,7 +340,7 @@ def build_nonce(data: tuple) -> bytes:
return
nonce
return
nonce
def
build_timestamp
()
->
list
:
def
build_timestamp
()
->
tuple
:
"""
Return array [seconds since epoch, microseconds since last seconds] Time = UTC+0000
"""
"""
Return array [seconds since epoch, microseconds since last seconds] Time = UTC+0000
"""
epoch
=
datetime
.
datetime
.
fromtimestamp
(
0
,
datetime
.
UTC
)
epoch
=
datetime
.
datetime
.
fromtimestamp
(
0
,
datetime
.
UTC
)
timestamp
=
datetime
.
datetime
.
now
(
datetime
.
UTC
)
-
epoch
timestamp
=
datetime
.
datetime
.
now
(
datetime
.
UTC
)
-
epoch
...
@@ -346,6 +350,6 @@ def build_timestamp() -> list:
...
@@ -346,6 +350,6 @@ def build_timestamp() -> list:
# for better performance, I choose to use this trick to fix the change in size for Py3.
# for better performance, I choose to use this trick to fix the change in size for Py3.
# only test once.
# only test once.
if
sys
.
version_info
.
major
==
2
:
if
sys
.
version_info
.
major
==
2
:
_packtimestamp
=
lambda
t1
,
t2
:
[
long
(
t1
),
int
(
t2
)
]
_packtimestamp
=
lambda
t1
,
t2
:
(
long
(
t1
),
int
(
t2
)
)
else
:
else
:
_packtimestamp
=
lambda
t1
,
t2
:
[
int
(
t1
),
int
(
t2
)
]
_packtimestamp
=
lambda
t1
,
t2
:
(
int
(
t1
),
int
(
t2
)
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment