Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[18.0][REF] server_environment: simplify #214

Open
wants to merge 2 commits into
base: 18.0
Choose a base branch
from
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
62 changes: 17 additions & 45 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 All @@ -45,7 +46,7 @@

def _load_running_env():
if not system_base_config.get("running_env"):
env_running_env = os.environ.get("RUNNING_ENV", os.environ.get("ODOO_STAGE"))
env_running_env = os.getenv("RUNNING_ENV") or os.getenv("ODOO_STAGE")
if env_running_env:
system_base_config["running_env"] = env_running_env
else:
Expand All @@ -64,17 +65,6 @@
_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 _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:
Expand Down Expand Up @@ -154,22 +136,12 @@
serv_config = _load_config()


class _Defaults(dict):
__slots__ = ()

def __setitem__(self, key, value):
def func(*a):
return str(value)

return dict.__setitem__(self, key, func)


class ServerConfiguration(models.TransientModel):
"""Display server configuration."""

_name = "server.config"
_description = "Display server configuration"
_conf_defaults = _Defaults()
_conf_defaults = {}

config = Serialized()

Expand Down Expand Up @@ -201,9 +173,9 @@
def _add_columns(cls):
"""Add columns to model dynamically"""
cols = chain(
list(cls._get_base_cols().items()),
list(cls._get_env_cols().items()),
list(cls._get_system_cols().items()),
cls._get_base_cols().items(),
cls._get_env_cols().items(),
cls._get_system_cols().items(),
)
for col, value in cols:
col_name = col.replace(".", "_")
Expand Down Expand Up @@ -328,5 +300,5 @@
if not self.show_passwords and self._is_secret(key=key):
res[key] = "**********"
else:
res[key] = self._conf_defaults[key]()
res[key] = str(self._conf_defaults[key])
return res
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
11 changes: 8 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 @@ -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()
Expand Down
Loading