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
|
# pytest cache
|
||||||
solar/.cache
|
solar/.cache
|
||||||
|
.config.override
|
||||||
|
@ -19,3 +19,4 @@ celery
|
|||||||
mock
|
mock
|
||||||
multipledispatch==0.4.8
|
multipledispatch==0.4.8
|
||||||
pydot
|
pydot
|
||||||
|
bunch
|
||||||
|
@ -1,83 +1,56 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
|
import yaml
|
||||||
|
from bunch import Bunch
|
||||||
|
|
||||||
class DictWrp(object):
|
CWD = os.getcwd()
|
||||||
|
|
||||||
def __init__(self, store):
|
C = Bunch()
|
||||||
self.store = store
|
C.redis = Bunch(port='6379', host='10.0.0.2')
|
||||||
|
C.riak = Bunch(port='8087', host='10.0.0.2', protocol='pbc')
|
||||||
def __getitem__(self, item):
|
C.sqlite = Bunch(backend='memory', location=':memory:')
|
||||||
return self.store[item]
|
C.dblayer = 'riak'
|
||||||
|
|
||||||
__getattr__ = __getitem__
|
|
||||||
|
|
||||||
|
|
||||||
class Conf(object):
|
def _lookup_vals(setter, config, prefix=None):
|
||||||
|
for key, val in config.iteritems():
|
||||||
def __init__(self):
|
if prefix is None:
|
||||||
self.store = {}
|
sub = [key]
|
||||||
self.types = {}
|
|
||||||
|
|
||||||
def add(self, name, _type=None, default=None):
|
|
||||||
if default:
|
|
||||||
if hasattr(default, '__call__'):
|
|
||||||
val = default()
|
|
||||||
else:
|
else:
|
||||||
val = default
|
sub = prefix + [key]
|
||||||
_type = type(val)
|
if isinstance(val, Bunch):
|
||||||
self.types[name] = _type
|
_lookup_vals(setter, val, sub)
|
||||||
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('.'))
|
|
||||||
else:
|
else:
|
||||||
variable = var
|
setter(config, sub)
|
||||||
env_var = variable.upper()
|
|
||||||
val = os.getenv(env_var)
|
|
||||||
if not val: continue
|
|
||||||
|
|
||||||
if _type == list:
|
def from_configs():
|
||||||
val_lst = val.split('|')
|
paths = [
|
||||||
self.store[var].extend(val_lst)
|
os.path.join(CWD, '.config'),
|
||||||
elif _type == dict:
|
os.path.join(CWD, '.config.override')
|
||||||
pass
|
]
|
||||||
else:
|
data = {}
|
||||||
self.store[var] = val
|
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__
|
from_configs()
|
||||||
|
from_env()
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
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.dblayer.riak_client import RiakClient
|
||||||
from solar.config import C
|
from solar.config import C
|
||||||
|
|
||||||
client = RiakClient(
|
if C.dblayer == 'sqlite':
|
||||||
protocol=C.riak.protocol, host=C.riak.host, pb_port=C.riak.port)
|
from solar.dblayer.sql_client import SqlClient
|
||||||
# client = RiakClient(protocol='http', host='10.0.0.2', http_port=8098)
|
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)
|
ModelMeta.setup(client)
|
||||||
|
|
||||||
|
@ -19,8 +19,6 @@ import time
|
|||||||
def patched_get_bucket_name(cls):
|
def patched_get_bucket_name(cls):
|
||||||
return cls.__name__ + str(time.time())
|
return cls.__name__ + str(time.time())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def setup(request):
|
def setup(request):
|
||||||
|
|
||||||
@ -49,16 +47,3 @@ def pytest_runtest_call(item):
|
|||||||
|
|
||||||
|
|
||||||
Model.get_bucket_name = classmethod(patched_get_bucket_name)
|
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…
x
Reference in New Issue
Block a user