Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pycalibration
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Contributor 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
calibration
pycalibration
Commits
93043172
Commit
93043172
authored
4 years ago
by
Cyril Danilevski
Browse files
Options
Downloads
Patches
Plain Diff
Add webservice.parse_config test
parent
baff7e0e
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
tests/test_webservice.py
+28
-1
28 additions, 1 deletion
tests/test_webservice.py
webservice/webservice.py
+17
-7
17 additions, 7 deletions
webservice/webservice.py
with
45 additions
and
8 deletions
tests/test_webservice.py
+
28
−
1
View file @
93043172
...
...
@@ -4,7 +4,7 @@ from pathlib import Path
import
pytest
sys
.
path
.
insert
(
0
,
Path
(
__file__
).
parent
/
'
webservice
'
)
from
webservice.webservice
import
check_files
,
merge
# noqa
from
webservice.webservice
import
check_files
,
merge
,
parse_config
# noqa
def
test_check_files
():
...
...
@@ -36,3 +36,30 @@ def test_merge():
'
number
'
:
1
}},
'
completely
'
:
'
different
'
}
assert
ret
==
expected
def
test_parse_config
():
cmd
=
[
'
whatever
'
]
config
=
{
'
somebool
'
:
True
,
'
notsomebool
'
:
False
,
'
alist
'
:
[
1
,
2
,
3
],
'
some_empty_key
'
:
'""'
,
'
other_empty_key
'
:
"''"
,
'
brian
'
:
'
scone
'
}
expected
=
[
'
whatever
'
,
'
--somebool
'
,
'
--alist
'
,
'
1
'
,
'
2
'
,
'
3
'
,
'
--some_empty_key
'
,
''
,
'
--other_empty_key
'
,
''
,
'
--brian
'
,
'
scone
'
]
config
=
parse_config
(
cmd
,
config
)
assert
config
==
expected
assert
'
--notsomebool
'
not
in
config
with
pytest
.
raises
(
ValueError
):
config
=
{
'
some key
'
:
'
value
'
}
config
=
parse_config
(
cmd
,
config
)
with
pytest
.
raises
(
ValueError
):
config
=
{
'
somekey
'
:
'
a value
'
}
config
=
parse_config
(
cmd
,
config
)
This diff is collapsed.
Click to expand it.
webservice/webservice.py
+
17
−
7
View file @
93043172
...
...
@@ -13,7 +13,7 @@ import urllib.parse
from
asyncio
import
get_event_loop
,
shield
from
datetime
import
datetime
from
pathlib
import
Path
from
typing
import
List
from
typing
import
Any
,
Dict
,
List
import
yaml
import
zmq
...
...
@@ -128,9 +128,8 @@ async def upload_config(socket, config, yaml, instrument, cycle, proposal):
socket
.
send
(
Success
.
UPLOADED_CONFIG
.
format
(
cycle
,
proposal
).
encode
())
def
merge
(
source
,
destination
):
"""
Deep merge two dictionaries
def
merge
(
source
:
Dict
,
destination
:
Dict
)
->
Dict
:
"""
Deep merge two dictionaries.
:param source: source dictionary to merge into destination
:param destination: destination dictionary which is being merged in
...
...
@@ -284,16 +283,27 @@ async def query_rid(conn, socket, rid):
socket
.
send
(
msg
.
encode
())
def
parse_config
(
cmd
,
config
):
def
parse_config
(
cmd
:
List
[
str
],
config
:
Dict
[
str
,
Any
])
->
List
[
str
]:
"""
Convert a dictionary to a list of arguments.
Values that are not strings will be cast.
Lists will be converted to several strings following their `--key`
flag.
Booleans will be converted to a `--key` flag, where `key` is the
dictionary key.
"""
for
key
,
value
in
config
.
items
():
if
'
'
in
key
or
(
isinstance
(
value
,
str
)
and
'
'
in
value
):
raise
ValueError
(
'
Spaces are not allowed
'
,
key
,
value
)
if
isinstance
(
value
,
list
):
cmd
+=
[
"
--{}
"
.
format
(
key
)]
cmd
.
append
(
f
"
--
{
key
}
"
)
cmd
+=
[
str
(
v
)
for
v
in
value
]
elif
isinstance
(
value
,
bool
):
if
value
:
cmd
+=
[
"
--{}
"
.
format
(
key
)]
else
:
if
value
==
'""'
or
value
==
"''"
:
if
value
in
[
'""'
,
"''"
]
:
value
=
""
cmd
+=
[
"
--{}
"
.
format
(
key
),
str
(
value
)]
...
...
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