Commit 44bd0943 authored by Simon Chabot's avatar Simon Chabot
Browse files

feat(config): read required variables from environment

Overload the `load_defaults` method of logilab.common.configuration to
load REQUIRED variables when the value is specified in the environment.

closes #85
parent e179a3f2c2d7
Pipeline #25926 passed with stages
in 6 minutes and 32 seconds
......@@ -184,7 +184,7 @@ from logilab.common.decorators import cached
from logilab.common.logging_ext import set_log_methods, init_log
from logilab.common.configuration import (Configuration, Method,
ConfigurationMixIn, merge_options,
_validate as lgc_validate)
_validate as lgc_validate, REQUIRED)
from cubicweb import (CW_SOFTWARE_ROOT, CW_MIGRATION_MAP,
ConfigurationError, Binary, _)
......@@ -395,8 +395,27 @@ this option is set to yes",
}),
)
def load_defaults(self) -> None:
""" overload the parent `load_defaults` to load REQUIRED variables with
environment values
"""
for opt, optdict in self.options:
action = optdict.get("action")
if action != "callback":
# callback action have no default
default = self.option_default(opt, optdict)
if default is REQUIRED:
# the next two lines are different from the parent's method.
# In the parent's, we always continue.
# In this case, we continue only if the value can not be
# read from the environment
default = option_value_from_env(opt)
if default is None:
continue
self.set_option(opt, default, action, optdict)
def __getitem__(self, key):
"""Get configuration option, by first looking at environmnent."""
"""Get configuration option, by first looking at environment."""
file_value = super(CubicWebNoAppConfiguration, self).__getitem__(key)
value = option_value_from_env(key, file_value)
if value is not None:
......
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
"""required variables's __init__
"""
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
"""
"""
distname = "cubicweb-required-variables"
version = "1.0.0"
#!/usr/bin/env python3
# coding: utf-8
from logilab.common.configuration import REQUIRED
options = (
(
"a-required-string-option",
{
"type": "string",
"default": REQUIRED,
"help": "",
"group": "test",
"level": 1,
},
),
(
"a-required-int-option",
{
"type": "int",
"default": REQUIRED,
"help": "",
"group": "test",
},
),
(
"a-required-yn-option",
{
"type": "yn",
"default": REQUIRED,
"help": "",
"group": "test",
},
),
)
......@@ -126,6 +126,7 @@ class CubicWebConfigurationTC(BaseTestCase):
'cubicweb_forge',
'cubicweb_localperms',
'cubicweb_mycube',
'cubicweb_required_variables',
'cubicweb_tag',
]
self.assertEqual(self.config.available_cubes(), expected_cubes)
......@@ -213,6 +214,35 @@ class CubicWebConfigurationTC(BaseTestCase):
finally:
del os.environ['CW_ALLOW_EMAIL_LOGIN']
def test_config_value_from_environment_required(self):
# check that without any variable environment, required options are not
# loaded.
config = ApptestConfiguration('data', __file__)
config._cubes = ('required_variables', )
config.load_site_cubicweb()
self.assertNotIn('a-required-string-option', config)
self.assertNotIn('a-required-int-option', config)
self.assertNotIn('a-required-yn-option', config)
# insert the options, and reload the configuration
os.environ['CW_A_REQUIRED_STRING_OPTION'] = '42'
os.environ['CW_A_REQUIRED_INT_OPTION'] = '42'
os.environ['CW_A_REQUIRED_YN_OPTION'] = 'y'
try:
config = ApptestConfiguration('data', __file__)
config._cubes = ('required_variables', )
config.load_site_cubicweb()
self.assertEqual(config['a-required-string-option'], '42')
self.assertEqual(config['a-required-int-option'], 42)
self.assertEqual(config['a-required-yn-option'], True)
finally:
del os.environ['CW_A_REQUIRED_STRING_OPTION']
del os.environ['CW_A_REQUIRED_INT_OPTION']
del os.environ['CW_A_REQUIRED_YN_OPTION']
class ModnamesTC(unittest.TestCase):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment