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.""" """Core/router of the Projet Pensées Profondes."""
from .http import app, HttpRequestHandler from .http import app, HttpRequestHandler
from .config import Config
...@@ -19,8 +19,12 @@ class Module(namedtuple('_Module', 'name url coefficient')): ...@@ -19,8 +19,12 @@ class Module(namedtuple('_Module', 'name url coefficient')):
class Config: class Config:
__slots__ = ('debug', 'modules') __slots__ = ('debug',)
def __init__(self, data=None): 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 self.debug = True
if not data: if not data:
try: try:
...@@ -28,16 +32,23 @@ class Config: ...@@ -28,16 +32,23 @@ class Config:
data = json.load(fd) data = json.load(fd)
except ValueError as exc: except ValueError as exc:
raise InvalidConfig(*exc.args) raise InvalidConfig(*exc.args)
self.modules = self._parse_modules(data.get('modules', {})) self.parse_config(data)
self.debug = data.get('debug', False)
@staticmethod @classmethod
def get_config_path(): def get_config_path(cls):
path = os.environ.get('PPP_CORE_CONFIG', '') path = os.environ.get(cls.config_path_variable, '')
if not path: if not path:
raise InvalidConfig('Could not find config file, please set ' raise InvalidConfig('Could not find config file, please set '
'environment variable $PPP_CORE_CONFIG.') 'environment variable $%s.' %
cls.config_path_variable)
return path 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): def _parse_modules(self, data):
modules = [] modules = []
...@@ -49,3 +60,4 @@ class Config: ...@@ -49,3 +60,4 @@ class Config:
config['name']) config['name'])
modules.append(Module(**config)) modules.append(Module(**config))
return modules return modules
...@@ -8,7 +8,7 @@ import itertools ...@@ -8,7 +8,7 @@ import itertools
import functools import functools
from ppp_datamodel import AbstractNode from ppp_datamodel import AbstractNode
from ppp_datamodel.communication import Request, Response from ppp_datamodel.communication import Request, Response
from .config import Config from .config import CoreConfig
from .exceptions import ClientError, BadGateway from .exceptions import ClientError, BadGateway
s = lambda x:x if isinstance(x, str) else x.decode() s = lambda x:x if isinstance(x, str) else x.decode()
...@@ -21,7 +21,7 @@ class Router: ...@@ -21,7 +21,7 @@ class Router:
self.tree = request.tree self.tree = request.tree
self.measures = request.measures self.measures = request.measures
self.trace = request.trace self.trace = request.trace
self.config = Config() self.config = CoreConfig()
def answer(self): def answer(self):
# First make all requests so modules can prepare their answer # 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