From 09350b8647df7d475a42dc52419cabc11802e4d1 Mon Sep 17 00:00:00 2001 From: Florent Xicluna Date: Tue, 29 Oct 2024 14:13:12 +0100 Subject: [PATCH] [REF] server_environment: simplify with pathlib --- server_environment/server_env.py | 40 +++++-------------- server_environment/tests/common.py | 6 +-- .../tests/test_server_environment.py | 11 +++-- 3 files changed, 21 insertions(+), 36 deletions(-) diff --git a/server_environment/server_env.py b/server_environment/server_env.py index 8d200da4..faface63 100644 --- a/server_environment/server_env.py +++ b/server_environment/server_env.py @@ -6,6 +6,7 @@ import logging import os from itertools import chain +from pathlib import Path from lxml import etree @@ -21,7 +22,7 @@ try: from odoo.addons import server_environment_files - _dir = os.path.dirname(server_environment_files.__file__) + _dir = Path(server_environment_files.__file__).parent except ImportError: _logger.info( "not using server_environment_files for configuration, no directory found" @@ -64,17 +65,6 @@ def _load_running_env(): _load_running_env() -ck_path = None -if _dir: - ck_path = os.path.join(_dir, system_base_config["running_env"]) - - if not os.path.exists(ck_path): - raise Exception( - "Provided server environment does not exist, " - f"please add a folder {ck_path}" - ) - - def setboolean(obj, attr, _bool=None): """Replace the attribute with a boolean.""" if _bool is None: @@ -97,24 +87,16 @@ def _escape(s): ) -def _listconf(env_path): - """List configuration files in a folder.""" - files = [ - os.path.join(env_path, name) - for name in sorted(os.listdir(env_path)) - if name.endswith(".conf") - ] - return files - - def _load_config_from_server_env_files(config_p): - default = os.path.join(_dir, "default") - running_env = os.path.join(_dir, system_base_config["running_env"]) - if os.path.isdir(default): - conf_files = _listconf(default) + _listconf(running_env) - else: - conf_files = _listconf(running_env) - + default = _dir / "default" + running_env = _dir / system_base_config["running_env"] + if not running_env.is_dir(): + raise Exception( + "Provided server environment does not exist, " + f"please add a folder {running_env}" + ) + conf_files = sorted(default.glob("*.conf")) if default.is_dir() else [] + conf_files += sorted(running_env.glob("*.conf")) try: config_p.read(conf_files) except Exception as e: diff --git a/server_environment/tests/common.py b/server_environment/tests/common.py index 798895ef..62c9e84b 100644 --- a/server_environment/tests/common.py +++ b/server_environment/tests/common.py @@ -1,8 +1,8 @@ # Copyright 2018 Camptocamp (https://www.camptocamp.com). # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) -import os from contextlib import contextmanager +from pathlib import Path from unittest.mock import patch from odoo.tests import common @@ -15,9 +15,7 @@ class ServerEnvironmentCase(common.TransactionCase): @contextmanager def set_config_dir(self, path): original_dir = server_env._dir - if path and not os.path.isabs(path): - path = os.path.join(os.path.dirname(__file__), path) - server_env._dir = path + server_env._dir = path and (Path(__file__).parent / path) try: yield finally: diff --git a/server_environment/tests/test_server_environment.py b/server_environment/tests/test_server_environment.py index cfa6878c..7f319374 100644 --- a/server_environment/tests/test_server_environment.py +++ b/server_environment/tests/test_server_environment.py @@ -1,7 +1,6 @@ # Copyright 2018 Camptocamp (https://www.camptocamp.com). # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) -import os from unittest.mock import patch from odoo.tools.config import config as odoo_config @@ -40,15 +39,21 @@ def test_default_dev(self): def test_default_non_dev_env(self): server_env._load_running_env() self._test_default(hidden_pwd=True) + with self.set_config_dir("non-existent/server_environment_files"): + # Invalid configuration dir + self.assertRaises(Exception, server_env._load_config) + with self.set_config_dir(None): + # No "server_environment_files" add-on installed + server_env._load_config() @patch.dict(odoo_config.options, {"running_env": None}) - @patch.dict(os.environ, {"RUNNING_ENV": "dev"}) + @patch.dict("os.environ", {"RUNNING_ENV": "dev"}) def test_default_dev_from_environ(self): server_env._load_running_env() self._test_default() @patch.dict(odoo_config.options, {"running_env": None}) - @patch.dict(os.environ, {"ODOO_STAGE": "dev"}) + @patch.dict("os.environ", {"ODOO_STAGE": "dev"}) def test_odoosh_dev_from_environ(self): server_env._load_running_env() self._test_default()