Rewrite config using dotted dict object and add loading from files
This commit is contained in:
parent
77c4dee5f9
commit
db0e788197
11
.config
Normal file
11
.config
Normal file
@ -0,0 +1,11 @@
|
||||
dblayer: riak
|
||||
redis:
|
||||
host: localhost
|
||||
port: '6379'
|
||||
riak:
|
||||
host: localhost
|
||||
port: '8087'
|
||||
protocol: pbc
|
||||
sqlite:
|
||||
backend: memory
|
||||
location: ':memory:'
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -46,3 +46,4 @@ solar/.coverage
|
||||
|
||||
# pytest cache
|
||||
solar/.cache
|
||||
.config.override
|
||||
|
@ -19,3 +19,4 @@ celery
|
||||
mock
|
||||
multipledispatch==0.4.8
|
||||
pydot
|
||||
bunch
|
||||
|
@ -1,83 +1,56 @@
|
||||
|
||||
import os
|
||||
import yaml
|
||||
from bunch import Bunch
|
||||
|
||||
class DictWrp(object):
|
||||
CWD = os.getcwd()
|
||||
|
||||
def __init__(self, store):
|
||||
self.store = store
|
||||
|
||||
def __getitem__(self, item):
|
||||
return self.store[item]
|
||||
|
||||
__getattr__ = __getitem__
|
||||
C = Bunch()
|
||||
C.redis = Bunch(port='6379', host='10.0.0.2')
|
||||
C.riak = Bunch(port='8087', host='10.0.0.2', protocol='pbc')
|
||||
C.sqlite = Bunch(backend='memory', location=':memory:')
|
||||
C.dblayer = 'riak'
|
||||
|
||||
|
||||
class Conf(object):
|
||||
|
||||
def __init__(self):
|
||||
self.store = {}
|
||||
self.types = {}
|
||||
|
||||
def add(self, name, _type=None, default=None):
|
||||
if default:
|
||||
if hasattr(default, '__call__'):
|
||||
val = default()
|
||||
def _lookup_vals(setter, config, prefix=None):
|
||||
for key, val in config.iteritems():
|
||||
if prefix is None:
|
||||
sub = [key]
|
||||
else:
|
||||
val = default
|
||||
_type = type(val)
|
||||
self.types[name] = _type
|
||||
if '.' in name:
|
||||
parent, child = name.split('.')
|
||||
if parent not in self.store:
|
||||
self.store[parent] = {}
|
||||
self.types[parent] = dict
|
||||
self.store[parent][child] = val
|
||||
else:
|
||||
self.store[name] = val
|
||||
|
||||
def __getitem__(self, item):
|
||||
val = self.store[item]
|
||||
if isinstance(val, dict):
|
||||
return DictWrp(val)
|
||||
return val
|
||||
|
||||
def __setitem__(self, item, val):
|
||||
stack = item.split('.')
|
||||
while stack[:-1]:
|
||||
nxt = stack.pop(0)
|
||||
store = self.store[nxt]
|
||||
store[stack[-1]] = val
|
||||
|
||||
def init_env(self):
|
||||
for var, _type in self.types.iteritems():
|
||||
if '.' in var:
|
||||
variable = '_'.join(var.split('.'))
|
||||
sub = prefix + [key]
|
||||
if isinstance(val, Bunch):
|
||||
_lookup_vals(setter, val, sub)
|
||||
else:
|
||||
variable = var
|
||||
env_var = variable.upper()
|
||||
val = os.getenv(env_var)
|
||||
if not val: continue
|
||||
setter(config, sub)
|
||||
|
||||
if _type == list:
|
||||
val_lst = val.split('|')
|
||||
self.store[var].extend(val_lst)
|
||||
elif _type == dict:
|
||||
pass
|
||||
else:
|
||||
self.store[var] = val
|
||||
def from_configs():
|
||||
paths = [
|
||||
os.path.join(CWD, '.config'),
|
||||
os.path.join(CWD, '.config.override')
|
||||
]
|
||||
data = {}
|
||||
for path in paths:
|
||||
with open(path) as f:
|
||||
loaded = yaml.load(f)
|
||||
if loaded:
|
||||
data.update(loaded)
|
||||
|
||||
def _setter(config, path):
|
||||
vals = data
|
||||
for key in path:
|
||||
vals = vals[key]
|
||||
config[path[-1]] = vals
|
||||
_lookup_vals(_setter, C)
|
||||
|
||||
def from_env():
|
||||
def _setter(config, path):
|
||||
env_key = '_'.join(path).upper()
|
||||
if env_key in os.environ:
|
||||
config[path[-1]] = os.environ[env_key]
|
||||
_lookup_vals(_setter, C)
|
||||
|
||||
__getattr__ = __getitem__
|
||||
|
||||
|
||||
C = Conf()
|
||||
C.add('redis.port', default='6379')
|
||||
C.add('redis.host', default='10.0.0.2')
|
||||
C.add('riak.host', default='10.0.0.2')
|
||||
C.add('riak.port', default='8087')
|
||||
C.add('riak.protocol', default='pbc')
|
||||
C.init_env()
|
||||
from_configs()
|
||||
from_env()
|
||||
|
||||
if __name__ == '__main__':
|
||||
print C.store
|
||||
print C
|
||||
|
@ -2,9 +2,29 @@ from solar.dblayer.model import ModelMeta
|
||||
from solar.dblayer.riak_client import RiakClient
|
||||
from solar.config import C
|
||||
|
||||
client = RiakClient(
|
||||
protocol=C.riak.protocol, host=C.riak.host, pb_port=C.riak.port)
|
||||
# client = RiakClient(protocol='http', host='10.0.0.2', http_port=8098)
|
||||
if C.dblayer == 'sqlite':
|
||||
from solar.dblayer.sql_client import SqlClient
|
||||
if C.sqlite.backend == 'memory':
|
||||
client = SqlClient(C.sqlite.location, threadlocals=False, autocommit=False)
|
||||
elif C.sqlite.backend == 'file':
|
||||
client = SqlClient(C.sqlite.location, threadlocals=True,
|
||||
autocommit=False, pragmas=(('journal_mode', 'WAL'),
|
||||
('synchronous', 'NORMAL')))
|
||||
else:
|
||||
raise Exception('Unknown sqlite backend %s', C.sqlite.backend)
|
||||
|
||||
elif C.dblayer == 'riak':
|
||||
from solar.dblayer.riak_client import RiakClient
|
||||
if C.riak.protocol == 'pbc':
|
||||
client = RiakClient(
|
||||
protocol=C.riak.protocol, host=C.riak.host, pb_port=C.riak.port)
|
||||
elif C.riak.protocol == 'http':
|
||||
client = RiakClient(
|
||||
protocol=C.riak.protocol, host=C.riak.host, http_port=C.riak.port)
|
||||
else:
|
||||
raise Exception('Unknown riak protocol %s', C.riak.protocol)
|
||||
else:
|
||||
raise Exception('Unknown dblayer backend %s', C.dblayer)
|
||||
|
||||
ModelMeta.setup(client)
|
||||
|
||||
|
@ -19,8 +19,6 @@ import time
|
||||
def patched_get_bucket_name(cls):
|
||||
return cls.__name__ + str(time.time())
|
||||
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup(request):
|
||||
|
||||
@ -49,16 +47,3 @@ def pytest_runtest_call(item):
|
||||
|
||||
|
||||
Model.get_bucket_name = classmethod(patched_get_bucket_name)
|
||||
|
||||
# from solar.dblayer.sql_client import SqlClient
|
||||
# client = SqlClient(':memory:', threadlocals=False, autocommit=False)
|
||||
# client = SqlClient('/tmp/blah.db', threadlocals=True,
|
||||
# autocommit=False, pragmas=(('journal_mode', 'WAL'),
|
||||
# ('synchronous', 'NORMAL')))
|
||||
|
||||
from solar.dblayer.riak_client import RiakClient
|
||||
client = RiakClient(protocol='pbc', host='10.0.0.2', pb_port=8087)
|
||||
# client = RiakClient(protocol='http', host='10.0.0.3', http_port=18098)
|
||||
|
||||
|
||||
ModelMeta.setup(client)
|
||||
|
Loading…
Reference in New Issue
Block a user