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
bbe85a62
Commit
bbe85a62
authored
5 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Type hints for tools.py is Ok
At least one piece is Ok ;)
parent
40cf3231
Branches
Branches containing commit
No related tags found
1 merge request
!1
First try of type hints
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
libs/lib/xaal/lib/tools.py
+16
-15
16 additions, 15 deletions
libs/lib/xaal/lib/tools.py
with
16 additions
and
15 deletions
libs/lib/xaal/lib/tools.py
+
16
−
15
View file @
bbe85a62
...
...
@@ -20,6 +20,7 @@
import
os
import
re
from
typing
import
Optional
,
Union
import
pysodium
...
...
@@ -32,7 +33,7 @@ from .bindings import UUID
XAAL_DEVTYPE_PATTERN
=
'
^[a-zA-Z][a-zA-Z0-9_-]*
\\
.[a-zA-Z][a-zA-Z0-9_-]*$
'
def
get_cfg_filename
(
name
,
cfg_dir
=
config
.
conf_dir
):
def
get_cfg_filename
(
name
:
str
,
cfg_dir
:
str
=
config
.
conf_dir
)
->
str
:
if
name
.
startswith
(
'
xaal.
'
):
name
=
name
[
5
:]
filename
=
'
%s.ini
'
%
name
...
...
@@ -40,71 +41,71 @@ def get_cfg_filename(name, cfg_dir=config.conf_dir):
print
(
"
Your configuration directory doesn
'
t exist: [%s]
"
%
cfg_dir
)
return
os
.
path
.
join
(
cfg_dir
,
filename
)
def
load_cfg_file
(
filename
)
:
def
load_cfg_file
(
filename
:
str
)
->
Optional
[
ConfigObj
]
:
"""
load .ini file and return it as dict
"""
if
os
.
path
.
isfile
(
filename
):
return
ConfigObj
(
filename
,
indent_type
=
'
'
,
encoding
=
"
utf8
"
)
return
None
def
load_cfg
(
app_name
)
:
def
load_cfg
(
app_name
:
str
)
->
Optional
[
ConfigObj
]
:
filename
=
get_cfg_filename
(
app_name
)
return
load_cfg_file
(
filename
)
def
load_cfg_or_die
(
app_name
)
:
def
load_cfg_or_die
(
app_name
:
str
)
->
ConfigObj
:
cfg
=
load_cfg
(
app_name
)
if
not
cfg
:
print
(
"
Unable to load config file %s
"
%
get_cfg_filename
(
app_name
))
sys
.
exit
(
-
1
)
return
cfg
def
new_cfg
(
app_name
)
:
def
new_cfg
(
app_name
:
str
)
->
ConfigObj
:
filename
=
get_cfg_filename
(
app_name
)
cfg
=
ConfigObj
(
filename
,
indent_type
=
'
'
)
cfg
[
'
config
'
]
=
{}
cfg
[
'
config
'
][
'
addr
'
]
=
get_random_uuid
().
str
return
cfg
def
get_random_uuid
():
def
get_random_uuid
()
->
UUID
:
return
UUID
.
random
()
def
get_random_base_uuid
(
digit
=
2
):
def
get_random_base_uuid
(
digit
=
2
)
->
UUID
:
return
UUID
.
random_base
(
digit
)
def
get_uuid
(
val
)
:
def
get_uuid
(
val
:
Union
[
UUID
,
str
,
None
])
->
Optional
[
UUID
]
:
if
isinstance
(
val
,
UUID
):
return
val
if
isinstance
(
val
,
str
):
return
str_to_uuid
(
val
)
return
None
def
str_to_uuid
(
val
)
:
def
str_to_uuid
(
val
:
str
)
->
Optional
[
UUID
]
:
"""
return an xAAL address for a given string
"""
try
:
return
UUID
(
val
)
except
ValueError
:
return
None
def
bytes_to_uuid
(
val
)
:
def
bytes_to_uuid
(
val
:
bytes
)
->
Optional
[
UUID
]
:
try
:
return
UUID
(
bytes
=
val
)
except
ValueError
:
return
None
def
is_valid_uuid
(
val
)
:
def
is_valid_uuid
(
val
:
Union
[
UUID
,
str
])
->
bool
:
return
isinstance
(
val
,
UUID
)
def
is_valid_address
(
val
)
:
def
is_valid_address
(
val
:
Union
[
UUID
,
str
])
->
bool
:
return
is_valid_uuid
(
val
)
@functools.lru_cache
(
maxsize
=
128
)
def
is_valid_dev_type
(
val
)
:
def
is_valid_dev_type
(
val
:
str
)
->
bool
:
if
not
isinstance
(
val
,
str
):
return
False
if
re
.
match
(
XAAL_DEVTYPE_PATTERN
,
val
):
return
True
return
False
def
pass2key
(
passphrase
)
:
def
pass2key
(
passphrase
:
str
)
->
bytes
:
"""
Generate key from passphrase using libsodium
crypto_pwhash_scryptsalsa208sha256 func
salt: buffer of zeros
...
...
@@ -123,7 +124,7 @@ def pass2key(passphrase):
return
key
@functools.lru_cache
(
maxsize
=
128
)
def
reduce_addr
(
addr
)
:
def
reduce_addr
(
addr
:
UUID
)
->
str
:
"""
return a string based addred without all digits
"""
tmp
=
addr
.
str
return
tmp
[:
5
]
+
'
..
'
+
tmp
[
-
5
:]
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