diff --git a/moulinette/__init__.py b/moulinette/__init__.py index 0ec41a3c..7c39a673 100755 --- a/moulinette/__init__.py +++ b/moulinette/__init__.py @@ -3,8 +3,8 @@ from moulinette.core import ( MoulinetteError, Moulinette18n, + env, ) -from moulinette.globals import init_moulinette_env __title__ = "moulinette" __author__ = ["Yunohost Contributors"] @@ -78,7 +78,7 @@ def init(logging_config=None, **kwargs): configure_logging(logging_config) # Add library directory to python path - sys.path.insert(0, init_moulinette_env()["LIB_DIR"]) + sys.path.insert(0, env["LIB_DIR"]) # Easy access to interfaces diff --git a/moulinette/actionsmap.py b/moulinette/actionsmap.py index be33cb17..107a0c2d 100644 --- a/moulinette/actionsmap.py +++ b/moulinette/actionsmap.py @@ -11,11 +11,11 @@ from collections import OrderedDict from importlib import import_module from moulinette import m18n, Moulinette -from moulinette.globals import init_moulinette_env from moulinette.core import ( MoulinetteError, MoulinetteLock, MoulinetteValidationError, + env, ) from moulinette.interfaces import BaseActionsMapParser, TO_RETURN_PROP from moulinette.utils.log import start_action_logging @@ -406,9 +406,8 @@ class ActionsMap(object): "Invalid parser class '%s'" % top_parser.__class__.__name__ ) - moulinette_env = init_moulinette_env() - DATA_DIR = moulinette_env["DATA_DIR"] - CACHE_DIR = moulinette_env["CACHE_DIR"] + DATA_DIR = env["DATA_DIR"] + CACHE_DIR = env["CACHE_DIR"] actionsmaps = OrderedDict() @@ -617,12 +616,11 @@ class ActionsMap(object): """ namespaces = [] - moulinette_env = init_moulinette_env() - DATA_DIR = moulinette_env["DATA_DIR"] + DATA_DIR = env["DATA_DIR"] # This var is ['*'] by default but could be set for example to # ['yunohost', 'yml_*'] - NAMESPACE_PATTERNS = moulinette_env["NAMESPACES"] + NAMESPACE_PATTERNS = env["NAMESPACES"] # Look for all files that match the given patterns in the actionsmap dir for namespace_pattern in NAMESPACE_PATTERNS: diff --git a/moulinette/core.py b/moulinette/core.py index f97a83e7..4950af35 100644 --- a/moulinette/core.py +++ b/moulinette/core.py @@ -6,10 +6,23 @@ import json import logging import moulinette -from moulinette.globals import init_moulinette_env logger = logging.getLogger("moulinette.core") +env = { + "DATA_DIR": "/usr/share/moulinette", + "LIB_DIR": "/usr/lib/moulinette", + "LOCALES_DIR": "/usr/share/moulinette/locale", + "CACHE_DIR": "/var/cache/moulinette", + "NAMESPACES": "*", # By default we'll load every namespace we find +} + +for key in env.keys(): + value_from_environ = os.environ.get(f"MOULINETTE_{key}") + if value_from_environ: + env[key] = value_from_environ + +env["NAMESPACES"] = env["NAMESPACES"].split() def during_unittests_run(): return "TESTS_RUN" in os.environ @@ -195,8 +208,7 @@ class Moulinette18n(object): self.default_locale = default_locale self.locale = default_locale - moulinette_env = init_moulinette_env() - self.locales_dir = moulinette_env["LOCALES_DIR"] + self.locales_dir = env["LOCALES_DIR"] # Init global translator self._global = Translator(self.locales_dir, default_locale) @@ -217,7 +229,7 @@ class Moulinette18n(object): """ if namespace not in self._namespaces: # Create new Translator object - lib_dir = init_moulinette_env()["LIB_DIR"] + lib_dir = env["LIB_DIR"] translator = Translator( "%s/%s/locales" % (lib_dir, namespace), self.default_locale ) diff --git a/moulinette/globals.py b/moulinette/globals.py index 8a169cea..093a16b1 100644 --- a/moulinette/globals.py +++ b/moulinette/globals.py @@ -1,17 +1,3 @@ """Moulinette global configuration core.""" -from os import environ - -def init_moulinette_env(): - return { - "DATA_DIR": environ.get("MOULINETTE_DATA_DIR", "/usr/share/moulinette"), - "LIB_DIR": environ.get("MOULINETTE_LIB_DIR", "/usr/lib/moulinette"), - "LOCALES_DIR": environ.get( - "MOULINETTE_LOCALES_DIR", "/usr/share/moulinette/locale" - ), - "CACHE_DIR": environ.get("MOULINETTE_CACHE_DIR", "/var/cache/moulinette"), - "NAMESPACES": environ.get( - "MOULINETTE_NAMESPACES", "*" - ).split(), # By default we'll load every namespace we find - } diff --git a/setup.py b/setup.py index a7f7c1b6..f0c3c023 100755 --- a/setup.py +++ b/setup.py @@ -3,10 +3,10 @@ import os import sys from setuptools import setup, find_packages -from moulinette import init_moulinette_env +from moulinette import env -LOCALES_DIR = init_moulinette_env()["LOCALES_DIR"] +LOCALES_DIR = env["LOCALES_DIR"] # Extend installation locale_files = [] diff --git a/test/conftest.py b/test/conftest.py index b868f362..d40e1116 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -93,9 +93,9 @@ def moulinette(tmp_path_factory): tmp_cache = str(tmp_path_factory.mktemp("cache")) tmp_data = str(tmp_path_factory.mktemp("data")) tmp_lib = str(tmp_path_factory.mktemp("lib")) - os.environ["MOULINETTE_CACHE_DIR"] = tmp_cache - os.environ["MOULINETTE_DATA_DIR"] = tmp_data - os.environ["MOULINETTE_LIB_DIR"] = tmp_lib + moulinette.env["CACHE_DIR"] = tmp_cache + moulinette.env["DATA_DIR"] = tmp_data + moulinette.env["LIB_DIR"] = tmp_lib shutil.copytree("./test/actionsmap", "%s/actionsmap" % tmp_data) shutil.copytree("./test/src", "%s/%s" % (tmp_lib, namespace)) shutil.copytree("./test/locales", "%s/%s/locales" % (tmp_lib, namespace))