Attempt to simplify the moulinette_env mess

This commit is contained in:
Alexandre Aubin 2021-08-27 21:27:32 +02:00
parent a263039958
commit ac94de6afd
6 changed files with 28 additions and 32 deletions

View file

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

View file

@ -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:

View file

@ -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
)

View file

@ -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
}

View file

@ -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 = []

View file

@ -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))