Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
FS-SC
AsapoWorker
Commits
cb0abc25
Commit
cb0abc25
authored
Dec 10, 2021
by
Tim Schoof
Browse files
Merge branch 'fix_configuration' into 'master'
Fix configuration with values considered as False See merge request
!37
parents
7c5741f0
64df2de1
Pipeline
#14433
passed with stage
in 57 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/AsapoWorker/configuration.py
View file @
cb0abc25
...
...
@@ -155,6 +155,13 @@ def create_cli_parser_for_attribute(attribute, parser, prefix, short_prefix):
def
create_instance_from_configurable
(
type
,
options
,
kwargs
=
None
,
builder
=
None
):
"""
Create an instance of type using the given options.
The kwargs argument can be used to overwrite options.
If no builder is given the constructor of type will be used.
"""
if
kwargs
is
None
:
kwargs
=
{}
if
builder
is
None
:
...
...
@@ -165,11 +172,25 @@ def create_instance_from_configurable(
def
create_instance_from_attribute
(
attribute
,
options
,
kwargs
):
"""
Insert a value for the attribute into kwargs using the values given in
options.
If the attribute has a complex type, the value will be an instance
that is constructed from the given options.
Does nothing if kwargs already has an entry for the attribute name.
"""
if
attribute
.
name
in
kwargs
:
return
config_entry
=
attribute
.
metadata
[
CONFIGURABLE_CONFIG_ENTRY
]
if
not
config_entry
.
flatten
:
options
=
options
.
get
(
attribute
.
name
,
{})
if
options
is
not
attr
.
NOTHING
:
options
=
options
.
get
(
attribute
.
name
,
attr
.
NOTHING
)
# else default options for this attribute will be used
else
:
# flatten only works for attributes that have complex types
assert
config_entry
.
proxy
is
not
None
or
attr
.
has
(
attribute
.
type
)
if
config_entry
.
proxy
is
not
None
:
proxy_instance
=
create_instance_from_configurable
(
config_entry
.
proxy
,
options
)
...
...
@@ -179,8 +200,12 @@ def create_instance_from_attribute(attribute, options, kwargs):
kwargs
[
attribute
.
name
]
=
create_instance_from_configurable
(
attribute
.
type
,
options
,
builder
=
config_entry
.
builder
)
else
:
if
options
:
# attribute has a simple type
assert
not
config_entry
.
flatten
if
options
is
not
attr
.
NOTHING
:
# use given option for this attribute
kwargs
[
attribute
.
name
]
=
options
# else default options for this attribute will be used
def
parsed_args_to_dict
(
parsed
):
...
...
tests/test_configuration.py
View file @
cb0abc25
...
...
@@ -473,3 +473,13 @@ def test_create_instance_overwrite_options_via_kwargs():
assert
instance
.
simple_attribute
is
simple_attribute
assert
instance
.
configurable_attribute
is
configurable_attribute
assert
instance
.
extern_attribute
is
extern_attribute
def
test_create_instance_from_configurable_zero
():
@
Configurable
class
Foo
:
i
=
Config
(
"An integer"
,
type
=
int
,
default
=
1
)
instance
=
create_instance_from_configurable
(
Foo
,
{
"i"
:
0
})
assert
instance
.
i
==
0
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment