Skip to content
Snippets Groups Projects
Commit 015736cd authored by Valentin Lorentz's avatar Valentin Lorentz
Browse files

Export the Config class so modules can use it instead of reimplementing it.

parent 1e6b3fa8
No related branches found
No related tags found
No related merge requests found
"""Core/router of the Projet Pensées Profondes."""
from .http import app, HttpRequestHandler
from .config import Config
......@@ -19,8 +19,12 @@ class Module(namedtuple('_Module', 'name url coefficient')):
class Config:
__slots__ = ('debug', 'modules')
__slots__ = ('debug',)
def __init__(self, data=None):
if not hasattr(self, 'config_path_variable') or \
not hasattr(self, 'parse_config'):
raise NotImplementedError('Config class does not implement all '
'required attributes.')
self.debug = True
if not data:
try:
......@@ -28,16 +32,23 @@ class Config:
data = json.load(fd)
except ValueError as exc:
raise InvalidConfig(*exc.args)
self.modules = self._parse_modules(data.get('modules', {}))
self.debug = data.get('debug', False)
self.parse_config(data)
@staticmethod
def get_config_path():
path = os.environ.get('PPP_CORE_CONFIG', '')
@classmethod
def get_config_path(cls):
path = os.environ.get(cls.config_path_variable, '')
if not path:
raise InvalidConfig('Could not find config file, please set '
'environment variable $PPP_CORE_CONFIG.')
'environment variable $%s.' %
cls.config_path_variable)
return path
class CoreConfig(Config):
__slots__ = ('modules')
config_path_variable = 'PPP_CORE_CONFIG'
def parse_config(self, data):
self.modules = self._parse_modules(data.get('modules', {}))
self.debug = data.get('debug', False)
def _parse_modules(self, data):
modules = []
......@@ -49,3 +60,4 @@ class Config:
config['name'])
modules.append(Module(**config))
return modules
......@@ -8,7 +8,7 @@ import itertools
import functools
from ppp_datamodel import AbstractNode
from ppp_datamodel.communication import Request, Response
from .config import Config
from .config import CoreConfig
from .exceptions import ClientError, BadGateway
s = lambda x:x if isinstance(x, str) else x.decode()
......@@ -21,7 +21,7 @@ class Router:
self.tree = request.tree
self.measures = request.measures
self.trace = request.trace
self.config = Config()
self.config = CoreConfig()
def answer(self):
# First make all requests so modules can prepare their answer
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment