Support for context variables and constants
This commit is contained in:
parent
19cfafb2c7
commit
959296b15a
2 changed files with 44 additions and 0 deletions
|
@ -1,7 +1,9 @@
|
||||||
|
import datetime
|
||||||
import errno
|
import errno
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
|
import time
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
""" Config singleton instance """
|
""" Config singleton instance """
|
||||||
|
@ -31,6 +33,11 @@ class Config(object):
|
||||||
os.path.join(os.sep, 'etc', 'platypush', 'config.yaml'),
|
os.path.join(os.sep, 'etc', 'platypush', 'config.yaml'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
_default_constants = {
|
||||||
|
'today': datetime.date.today().isoformat,
|
||||||
|
'now': datetime.datetime.now().isoformat,
|
||||||
|
}
|
||||||
|
|
||||||
_workdir_location = os.path.join(os.environ['HOME'], '.local', 'share', 'platypush')
|
_workdir_location = os.path.join(os.environ['HOME'], '.local', 'share', 'platypush')
|
||||||
|
|
||||||
def __init__(self, cfgfile=None):
|
def __init__(self, cfgfile=None):
|
||||||
|
@ -68,6 +75,9 @@ class Config(object):
|
||||||
self.plugins = {}
|
self.plugins = {}
|
||||||
self.event_hooks = {}
|
self.event_hooks = {}
|
||||||
self.procedures = {}
|
self.procedures = {}
|
||||||
|
self.constants = {}
|
||||||
|
|
||||||
|
self._init_constants()
|
||||||
self._init_components()
|
self._init_components()
|
||||||
|
|
||||||
|
|
||||||
|
@ -115,6 +125,14 @@ class Config(object):
|
||||||
else:
|
else:
|
||||||
self.plugins[key] = self._config[key]
|
self.plugins[key] = self._config[key]
|
||||||
|
|
||||||
|
def _init_constants(self):
|
||||||
|
if 'constants' in self._config:
|
||||||
|
self.constants = self._config['constants']
|
||||||
|
|
||||||
|
for (key,value) in self._default_constants.items():
|
||||||
|
self.constants[key] = value
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_backends():
|
def get_backends():
|
||||||
global _default_config_instance
|
global _default_config_instance
|
||||||
|
@ -139,6 +157,25 @@ class Config(object):
|
||||||
if _default_config_instance is None: _default_config_instance = Config()
|
if _default_config_instance is None: _default_config_instance = Config()
|
||||||
return _default_config_instance.procedures
|
return _default_config_instance.procedures
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_constants():
|
||||||
|
global _default_config_instance
|
||||||
|
if _default_config_instance is None: _default_config_instance = Config()
|
||||||
|
constants = {}
|
||||||
|
|
||||||
|
for name in _default_config_instance.constants.keys():
|
||||||
|
constants[name] = Config.get_constant(name)
|
||||||
|
return constants
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_constant(name):
|
||||||
|
global _default_config_instance
|
||||||
|
if _default_config_instance is None: _default_config_instance = Config()
|
||||||
|
|
||||||
|
if name not in _default_config_instance.constants: return None
|
||||||
|
value = _default_config_instance.constants[name]
|
||||||
|
return value() if callable(value) else value
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_default_pusher_backend():
|
def get_default_pusher_backend():
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -69,8 +69,15 @@ class Request(Message):
|
||||||
|
|
||||||
|
|
||||||
def _expand_context(self, event_args=None, **context):
|
def _expand_context(self, event_args=None, **context):
|
||||||
|
from platypush.config import Config
|
||||||
|
|
||||||
if event_args is None: event_args = copy.deepcopy(self.args)
|
if event_args is None: event_args = copy.deepcopy(self.args)
|
||||||
|
|
||||||
|
constants = Config.get_constants()
|
||||||
|
context['constants'] = {}
|
||||||
|
for (name,value) in constants.items():
|
||||||
|
context['constants'][name] = value
|
||||||
|
|
||||||
keys = []
|
keys = []
|
||||||
if isinstance(event_args, dict):
|
if isinstance(event_args, dict):
|
||||||
keys = event_args.keys()
|
keys = event_args.keys()
|
||||||
|
|
Loading…
Reference in a new issue