提交 63a6af3c 编写于 作者: C Cleber Rosa

Avocado compatibility: handle new settings behavior

Avocado has introduced a new settings module implementation, currently
available as "avocado.core.future.settings, but with the goal of
replacing the one at "avocado.core.settings".

If find that the this new implementation is available, we can be
certain that we're dealing with configurations that are dictionaries,
and never and argparse.Namespace instance.

Also, because it will live in the same location as the current one,
and because the change will happen at a given point before a release,
we need to implement some heuristic to check for new or old
implementation.

When using the new implementation, it means that new responsibilities
are needed, and registering the core options is required.  In this
case, the compatibility helpers get_opt()/set_opt() can now be defined
according to the presence of the settings implementation, because that
will guarantee that the opts will be a dictionary or not.
Signed-off-by: NCleber Rosa <crosa@redhat.com>
上级 11c614e3
import argparse
from avocado.core.settings import settings
def get_opt(opt, name):
"""
Compatibility handler for options in either argparse.Namespace or dict
:param opt: either an argpase.Namespace instance or a dict
:param name: the name of the attribute or key
"""
if isinstance(opt, argparse.Namespace):
return getattr(opt, name, None)
else:
return opt.get(name)
def is_registering_settings_required():
"""Checks the characteristics of the Avocado settings API.
And signals if the explicit registration of options is required, along
with other API details that should be followed.
def set_opt(opt, name, value):
"""
Compatibility handler for options in either argparse.Namespace or dict
The heuristic used here is to check for methods that are only present
in the new API, and should be "safe enough".
:param opt: either an argpase.Namespace instance or a dict
:param name: the name of the attribute or key
:param value: the value to be set
TODO: remove this once support for Avocado releases before 81.0,
including 69.x LTS is dropped.
"""
if isinstance(opt, argparse.Namespace):
setattr(opt, name, value)
else:
return all((hasattr(settings, 'add_argparser_to_option'),
hasattr(settings, 'register_option'),
hasattr(settings, 'as_json')))
if is_registering_settings_required():
def get_opt(opt, name):
"""
Compatibility handler to Avocado with configuration as dict
:param opt: a configuration dict, usually from settings.as_dict()
:param name: the name of the configuration key, AKA namespace
"""
return opt.get(name)
def set_opt(opt, name, value):
"""
Compatibility handler to Avocado with configuration as dict
:param opt: a configuration dict, usually from settings.as_dict()
:param name: the name of the configuration key, AKA namespace
:param value: the value to be set
"""
opt[name] = value
else:
def get_opt(opt, name):
"""
Compatibility handler for options in either argparse.Namespace or dict
:param opt: either an argpase.Namespace instance or a dict
:param name: the name of the attribute or key
"""
if isinstance(opt, argparse.Namespace):
return getattr(opt, name, None)
else:
return opt.get(name)
def set_opt(opt, name, value):
"""
Compatibility handler for options in either argparse.Namespace or dict
:param opt: either an argpase.Namespace instance or a dict
:param name: the name of the attribute or key
:param value: the value to be set
"""
if isinstance(opt, argparse.Namespace):
setattr(opt, name, value)
else:
opt[name] = value
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册