Skip to content

Commit

Permalink
[REF] server_environment: simplify with pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
florentx committed Oct 29, 2024
1 parent 7d35111 commit c4652ea
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 36 deletions.
40 changes: 11 additions & 29 deletions server_environment/server_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import os
from itertools import chain
from pathlib import Path

from lxml import etree

Expand All @@ -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

Check warning on line 25 in server_environment/server_env.py

View check run for this annotation

Codecov / codecov/patch

server_environment/server_env.py#L25

Added line #L25 was not covered by tests
except ImportError:
_logger.info(
"not using server_environment_files for configuration, no directory found"
Expand Down Expand Up @@ -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:
Expand All @@ -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(

Check warning on line 94 in server_environment/server_env.py

View check run for this annotation

Codecov / codecov/patch

server_environment/server_env.py#L94

Added line #L94 was not covered by tests
"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:
Expand Down
6 changes: 2 additions & 4 deletions server_environment/tests/common.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions server_environment/tests/test_server_environment.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c4652ea

Please sign in to comment.