This repository has been archived by the owner on Oct 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 450
/
config.py
108 lines (88 loc) · 3.36 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import logging
import quokka.core.models as m
from flask.config import Config
from quokka.utils import parse_conf_data
from cached_property import cached_property_ttl, cached_property
logger = logging.getLogger()
class QuokkaConfig(Config):
"""A Config object for Flask that tries to ger vars from
database and then from Config itself"""
@cached_property
def store(self):
return dict(self)
@cached_property_ttl(300)
def all_setings_from_db(self):
"""
As config reads data from database on every app.config.get(key)/[key]
This data is cached as a cached_property
The TTL is fixed in 5 minutes because we can't read it from
config itself.
Find a way to set the config parameter in a file
maybe in a config_setting.ini
It takes 5 minutes for new values to be available
and Make it possible to use REDIS as a cache
"""
try:
return {
item.name: item.value
for item in m.config.Config.objects.get(
group='settings'
).values
}
except Exception as e:
logger.warning('Error reading all settings from db: %s' % e)
return {}
def get_from_db(self, key, default=None):
return self.all_setings_from_db.get(key, default)
def __getitem__(self, key):
return self.get_from_db(key) or dict.__getitem__(self, key)
# def __iter__(self):
# return iter(self.store)
# def __len__(self):
# return len(self.store)
# def __repr__(self):
# return self.store.__repr__()
# def __unicode__(self):
# return unicode(repr(self.store))
# def __call__(self, *args, **kwargs):
# return self.store.get(*args, **kwargs)
# def __contains__(self, item):
# return item in self.store
# def keys(self):
# return self.store.keys()
# def values(self):
# return self.store.values()
def get(self, key, default=None):
return self.get_from_db(key) or self.store.get(key) or default
def from_object(self, obj, silent=False):
try:
super(QuokkaConfig, self).from_object(obj)
except ImportError as e:
if silent:
return False
e.message = 'Unable to load configuration obj (%s)' % e.message
raise
def from_envvar_namespace(self, namespace='QUOKKA', silent=False):
try:
data = {
key.partition('_')[-1]: parse_conf_data(data)
for key, data
in os.environ.items()
if key.startswith(namespace)
}
self.update(data)
except Exception as e:
if silent:
return False
e.message = 'Unable to load config env namespace (%s)' % e.message
raise
def load_quokka_config(self, config=None, mode=None, test=None, **sets):
self.from_object(config or 'quokka.settings')
mode = mode or 'test' if test else os.environ.get(
'QUOKKA_MODE', 'local')
self.from_object('quokka.%s_settings' % mode, silent=True)
path = "QUOKKA_SETTINGS" if not test else "QUOKKATEST_SETTINGS"
self.from_envvar(path, silent=True)
self.from_envvar_namespace(namespace='QUOKKA', silent=True)
self.update(sets)