Commit 1192cc3da85826fd876748607a330c6bc96dbd78

Authored by Etienne Pallier
1 parent d02a5e4b
Exists in dev

Nouveau système de configuration de "base" (ROOT_DIR, ...)

Inspiré de https://whalesalad.com/blog/doing-python-configuration-right
et https://12factor.net/config
config/__init__.py
... ... @@ -0,0 +1,21 @@
  1 +# Inspired from https://whalesalad.com/blog/doing-python-configuration-right
  2 +
  3 +import os
  4 +import importlib
  5 +
  6 +# Determine the environment we want to load, default is development.py
  7 +PYROS_ENV = os.environ.get("PYROS_ENV", "config_dev")
  8 +
  9 +module = importlib.import_module(
  10 + #f".environments.{ENV}", package="service.config"
  11 + f".{PYROS_ENV}", package="config"
  12 +)
  13 +
  14 +# update globals of this module (i.e. settings) with imported.
  15 +globals().update(vars(module))
  16 +
  17 +
  18 +def is_dev_env(): return PYROS_ENV == "config_dev"
  19 +def is_test_env(): return PYROS_ENV == "config_test"
  20 +def is_prod_env(): return PYROS_ENV == "config_prod"
  21 +
... ...
config/config_base.py 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +# Inspired from https://whalesalad.com/blog/doing-python-configuration-right
  2 +
  3 +import os
  4 +
  5 +APP_NAME = "PYROS"
  6 +
  7 +# Conveniences to always have a quick reference to the
  8 +# top-level application directory.
  9 +ROOT_DIR = os.path.join(
  10 + os.path.dirname(os.path.realpath(__file__)),
  11 + os.pardir, # '..'
  12 +)
  13 +ROOT_DIR = os.path.realpath(ROOT_DIR)
  14 +
  15 +# root directories
  16 +DOC_DIR = os.path.join(ROOT_DIR, "doc")
  17 +INSTALL_DIR = os.path.join(ROOT_DIR, "install")
  18 +LOG_DIR = os.path.join(ROOT_DIR, "config", "logs")
  19 +SOURCE_DIR = os.path.join(ROOT_DIR, "src")
  20 +
  21 +# source sub-directories
  22 +CELME_DIR = os.path.join(SOURCE_DIR, "core", "celme")
  23 +PYROS_DJANGO_DIR = os.path.join(SOURCE_DIR, "core", "pyros_django")
  24 +DEVICES_DIR = os.path.join(SOURCE_DIR, "device_controller")
  25 +
  26 +
  27 +SYSTEM_REBOOT_COMMAND = "sudo systemctl restart foo.service"
0 28 \ No newline at end of file
... ...
config/config_dev.py 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +import os
  2 +
  3 +from .config_base import *
  4 +
  5 +# Now, override some configuration settings, or add new ones
  6 +
  7 +# Ex:
  8 +# Override DOC_DIR
  9 +DOC_DIR = os.path.join(DOC_DIR, "dev-doc")
  10 +
... ...
config/config_prod.py 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +import os
  2 +
  3 +from .config_base import *
  4 +
  5 +# Now, override some configuration settings, or add new ones
  6 +
  7 +# Ex:
  8 +# Override DOC_DIR
  9 +DOC_DIR = os.path.join(DOC_DIR, "prod-doc")
  10 +
... ...
config/config_test.py 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +import os
  2 +
  3 +from .config_base import *
  4 +
  5 +# Now, override some configuration settings, or add new ones
  6 +
  7 +# Ex:
  8 +# Override DOC_DIR
  9 +DOC_DIR = os.path.join(DOC_DIR, "test-doc")
  10 +
... ...
src/core/pyros_django/agent/Agent.py
... ... @@ -110,6 +110,11 @@ from django import db
110 110 #from django.conf import settings as djangosettings
111 111  
112 112 # --- SPECIFIC IMPORT ---
  113 +# Many ways to import configuration settings:
  114 +import config
  115 +#from config import PYROS_ENV, ROOT_DIR, DOC_DIR
  116 +#from config import *
  117 +
113 118 from common.models import AgentSurvey, Command, AgentLogs
114 119 from config.configpyros import ConfigPyros
115 120 #from dashboard.views import get_sunelev
... ... @@ -402,6 +407,18 @@ class Agent:
402 407 #def __init__(self, name:str="Agent", config_filename:str=None, RUN_IN_THREAD=True):
403 408 #def __init__(self, config_filename:str=None, RUN_IN_THREAD=True, DEBUG_MODE=False):
404 409 def __init__(self, config_filename:str=None, RUN_IN_THREAD=True):
  410 +
  411 + '''
  412 + print('PYROS_ENV', PYROS_ENV)
  413 + print('ROOT_DIR', ROOT_DIR)
  414 + print('DOC_DIR', DOC_DIR)
  415 + '''
  416 + print('PYROS_ENV', config.PYROS_ENV)
  417 + print('ROOT_DIR', config.ROOT_DIR)
  418 + print('DOC_DIR', config.DOC_DIR)
  419 + if config.is_dev_env(): print("DEV ENV")
  420 + if config.is_prod_env(): print("PROD ENV")
  421 +
405 422 #self.name = name
406 423 self.name = self.__class__.__name__
407 424 printd("*** ENVIRONMENT VARIABLE PYROS_DEBUG is:", os.environ.get('PYROS_DEBUG'), '***')
... ...