Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions server_environment/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,22 @@ Note: empty environment keys always take precedence over default fields
Read the documentation of the class
[models/server_env_mixin.py](models/server_env_mixin.py) and [models/server_env_tech_name_mixin.py]
(models/server_env_tech_name_mixin.py)

## ConfigParser interpolation

By default, ConfigParser interpolation is enabled to preserve compatibility

It can be disabled:

::

[options]
server_environment_config_interpolation = False

Or with the environment variable:

::

export SERVER_ENV_CONFIG_INTERPOLATION=False

When enabled, standard Python ``configparser`` interpolation rules apply.
2 changes: 2 additions & 0 deletions server_environment/readme/newsfragments/265.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
make configparser interpolation parameter controllable via Odoo configuration
in 'server_environnment'
19 changes: 17 additions & 2 deletions server_environment/server_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,23 @@ def _load_config_from_env(config_p):


def _load_config():
"""Load the configuration and return a ConfigParser instance."""
config_p = configparser.ConfigParser()
"""
Load the configuration and return a ConfigParser instance.
Uses the server_environment config param 'config_interpolation'
To know whether the config must be parsed with interpolation
of '%' characters. By default interpolation is enabled.
"""
interpolation = system_base_config.get(
"server_environment_config_interpolation",
os.environ.get("SERVER_ENV_CONFIG_INTERPOLATION", True),
)
if isinstance(interpolation, str):
interpolation = _boolean_states.get(interpolation.lower(), True)

if interpolation:
config_p = configparser.ConfigParser()
else:
config_p = configparser.ConfigParser(interpolation=None)
# options are case-sensitive
config_p.optionxform = str

Expand Down
1 change: 1 addition & 0 deletions server_environment/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import test_server_environment
from . import test_environment_variable
from . import test_config_interpolation
70 changes: 70 additions & 0 deletions server_environment/tests/test_config_interpolation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2018 Camptocamp (https://www.camptocamp.com).
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)

import logging
import os
from unittest.mock import patch

from odoo.tools.config import config as odoo_config

from .. import server_env
from . import common

_logger = logging.getLogger(__name__)


class TestConfigInterpolation(common.ServerEnvironmentCase):
def test_interpolation_matrix(self):
cases = [
("default_default", None, None, True),
("default_env_true", None, "True", True),
("default_env_false", None, "False", False),
("config_true_default", "True", None, True),
("config_false_default", "False", None, False),
("config_true_env_true", "True", "True", True),
("config_true_env_false", "True", "False", True),
("config_false_env_true", "False", "True", False),
("config_false_env_false", "False", "False", False),
]

for name, config_value, env_value, expected in cases:
with self.subTest(name=name):
_logger.info(name)
self._assert_interpolation(
config_value,
env_value,
expected,
)

def _assert_interpolation(
self,
config_value,
env_value,
expected,
):
config_patch = {"running_env": "testing"}

if config_value is not None:
config_patch["server_environment_config_interpolation"] = config_value

env_patch = {}

if env_value is not None:
env_patch["SERVER_ENV_CONFIG_INTERPOLATION"] = env_value

with (
patch.dict(odoo_config.options, config_patch),
patch.dict(os.environ, env_patch),
self.set_config_dir("testfiles"),
):
parser = server_env._load_config()

val = parser.get(
"external_service.ftp",
"user",
)

if expected:
self.assertEqual(val, "testing")
else:
self.assertEqual(val, "%(base_user)s")
3 changes: 2 additions & 1 deletion server_environment/tests/testfiles/testing/base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
odoo_test_option = Set in config file for testing env

[external_service.ftp]
user = testing
base_user = testing
user = %(base_user)s
Loading