From c4652eae29edcbd757435b7d3532b7da9a15c400 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 | 5 +-- 3 files changed, 15 insertions(+), 36 deletions(-) diff --git a/server_environment/server_env.py b/server_environment/server_env.py index 0e5dfc77..440cfb69 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..9938d0e8 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 @@ -42,13 +41,13 @@ def test_default_non_dev_env(self): self._test_default(hidden_pwd=True) @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()