added filepaths for config files, variable replacement, resolving, and
100% conf test coverage
This commit is contained in:
@@ -6,14 +6,13 @@ from paste.recursive import RecursiveMiddleware
|
||||
from pecan import Pecan, request, response, override_template, redirect, error_for
|
||||
from decorators import expose
|
||||
|
||||
import configuration
|
||||
from configuration import set_config
|
||||
from configuration import _runtime_conf as conf
|
||||
|
||||
__all__ = [
|
||||
'make_app', 'Pecan', 'request', 'response', 'override_template', 'expose', 'conf', 'set_config'
|
||||
'make_app', 'Pecan', 'request', 'response', 'override_template', 'expose', 'conf', 'set_config', 'use_config'
|
||||
]
|
||||
|
||||
conf = configuration.initconf()
|
||||
|
||||
def make_app(root, static_root=None, debug=False, errorcfg={}, **kw):
|
||||
app = Pecan(root, **kw)
|
||||
app = RecursiveMiddleware(app)
|
||||
@@ -21,6 +20,3 @@ def make_app(root, static_root=None, debug=False, errorcfg={}, **kw):
|
||||
if static_root:
|
||||
app = Cascade([StaticURLParser(static_root), app])
|
||||
return app
|
||||
|
||||
def set_config(name):
|
||||
conf.update_with_module(name)
|
||||
|
||||
@@ -1,8 +1,41 @@
|
||||
import re
|
||||
import inspect
|
||||
import os
|
||||
import string
|
||||
|
||||
IDENTIFIER = re.compile('[A-Za-z_]([A-Za-z0-9_])*$')
|
||||
IDENTIFIER = re.compile(r'[a-z_](\w)*$', re.IGNORECASE)
|
||||
STRING_FORMAT = re.compile(r'{pecan\.conf(?P<var>([.][a-z_][\w]*)+)+?}', re.IGNORECASE)
|
||||
|
||||
class ConfigString(object):
|
||||
def __init__(self, format_string):
|
||||
self.raw_string = format_string
|
||||
|
||||
def __call__(self):
|
||||
retval = self.raw_string
|
||||
|
||||
for candidate in STRING_FORMAT.finditer(self.raw_string):
|
||||
var = candidate.groupdict().get('var','')
|
||||
|
||||
try:
|
||||
obj = _runtime_conf
|
||||
for dotted_part in var.split('.'):
|
||||
if dotted_part == '':
|
||||
continue
|
||||
obj = getattr(obj, dotted_part)
|
||||
|
||||
retval = retval.replace(candidate.group(), str(obj))
|
||||
|
||||
except AttributeError, e:
|
||||
raise AttributeError, 'Cannot substitute \'%s\' using the current configuration' % candidate.group()
|
||||
|
||||
return retval
|
||||
|
||||
def __str__(self):
|
||||
return self.raw_string
|
||||
|
||||
@staticmethod
|
||||
def contains_formatting(value):
|
||||
return STRING_FORMAT.match(value)
|
||||
|
||||
class Config(object):
|
||||
def __init__(self, conf_dict={}):
|
||||
@@ -25,7 +58,7 @@ class Config(object):
|
||||
if isinstance(cur_val, Config):
|
||||
cur_val.update(conf_dict[k])
|
||||
else:
|
||||
self.__dict__[k] = conf_dict[k]
|
||||
self[k] = conf_dict[k]
|
||||
|
||||
def update_with_module(self, module):
|
||||
self.update(conf_from_module(module))
|
||||
@@ -34,7 +67,12 @@ class Config(object):
|
||||
return self.__dict__[key]
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self.__dict__[key] = value
|
||||
if isinstance(value, dict):
|
||||
self.__dict__[key] = Config(value)
|
||||
elif isinstance(value, str) and ConfigString.contains_formatting(value):
|
||||
self.__dict__[key] = ConfigString(value)
|
||||
else:
|
||||
self.__dict__[key] = value
|
||||
|
||||
def __iter__(self):
|
||||
return self.__dict__.iteritems()
|
||||
@@ -45,27 +83,57 @@ class Config(object):
|
||||
def __repr__(self):
|
||||
return 'Config(%s)' % str(self.__dict__)
|
||||
|
||||
def initconf():
|
||||
import default_config
|
||||
return conf_from_module(default_config)
|
||||
def __call__(self):
|
||||
for k,v in self:
|
||||
if isinstance(v, Config):
|
||||
v()
|
||||
elif hasattr(v, '__call__'):
|
||||
self.__dict__[k] = v()
|
||||
return self
|
||||
|
||||
|
||||
|
||||
def conf_from_module(module):
|
||||
if isinstance(module, str):
|
||||
module = import_runtime_conf(module)
|
||||
module = import_module(module)
|
||||
|
||||
module_dict = dict(inspect.getmembers(module))
|
||||
|
||||
return conf_from_dict(module_dict)
|
||||
|
||||
def conf_from_file(filepath):
|
||||
abspath = os.path.abspath(os.path.expanduser(filepath))
|
||||
conf_dict = {}
|
||||
|
||||
execfile(abspath, globals(), conf_dict)
|
||||
conf_dict['__file__'] = abspath
|
||||
|
||||
return conf_from_dict(conf_dict)
|
||||
|
||||
def conf_from_dict(conf_dict):
|
||||
conf = Config()
|
||||
|
||||
for k,v in inspect.getmembers(module):
|
||||
# set the configdir
|
||||
conf_dir = os.path.dirname(conf_dict.get('__file__', ''))
|
||||
if conf_dir == '':
|
||||
conf_dir = os.getcwd()
|
||||
|
||||
conf['__confdir__'] = conf_dir + '/'
|
||||
|
||||
for k,v in conf_dict.iteritems():
|
||||
if k.startswith('__'):
|
||||
continue
|
||||
elif inspect.ismodule(v):
|
||||
continue
|
||||
|
||||
if isinstance(v, dict):
|
||||
conf[k] = Config(v)
|
||||
else:
|
||||
conf[k] = v
|
||||
conf()
|
||||
return conf
|
||||
|
||||
def import_runtime_conf(conf):
|
||||
def import_module(conf):
|
||||
if '.' in conf:
|
||||
parts = conf.split('.')
|
||||
name = '.'.join(parts[:-1])
|
||||
@@ -87,3 +155,18 @@ def import_runtime_conf(conf):
|
||||
raise ImportError('No module named %s' % conf)
|
||||
|
||||
return conf_mod
|
||||
|
||||
def initconf():
|
||||
import default_config
|
||||
conf = conf_from_module(default_config)
|
||||
conf()
|
||||
return conf
|
||||
|
||||
def set_config(name):
|
||||
if '/' in name:
|
||||
_runtime_conf.update(conf_from_file(name))
|
||||
else:
|
||||
_runtime_conf.update_with_module(name)
|
||||
|
||||
_runtime_conf = initconf()
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import os
|
||||
from unittest import TestCase
|
||||
from pecan import configuration
|
||||
from pecan import conf as _runtime_conf
|
||||
|
||||
class TestConf(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
import sys
|
||||
import os
|
||||
|
||||
test_config_d = os.path.join(os.path.dirname(__file__), 'test_config')
|
||||
|
||||
@@ -47,4 +48,59 @@ class TestConf(TestCase):
|
||||
self.assertEqual(conf.server.host, '0.0.0.0')
|
||||
self.assertEqual(conf.server.port, '8080')
|
||||
|
||||
def test_update_config_fail_bad_attribute(self):
|
||||
conf = configuration.initconf()
|
||||
self.assertRaises(AttributeError, conf.update_with_module, 'bad.attribute')
|
||||
def test_update_config_with_dict(self):
|
||||
conf = configuration.initconf()
|
||||
d = {'attr':True}
|
||||
conf['attr'] = d
|
||||
self.assertTrue(conf.attr.attr)
|
||||
|
||||
def test_config_dir(self):
|
||||
conf = configuration.initconf()
|
||||
|
||||
self.assertTrue('__confdir__' in dir(conf))
|
||||
|
||||
def test_config_repr(self):
|
||||
conf = configuration.Config({'a':1})
|
||||
self.assertEqual(repr(conf),"Config({'a': 1})")
|
||||
|
||||
def test_config_from_dict(self):
|
||||
conf = configuration.conf_from_dict({})
|
||||
self.assertTrue(os.path.samefile(conf.__confdir__, os.getcwd()))
|
||||
|
||||
def test_config_from_file(self):
|
||||
path = os.path.join(os.path.dirname(__file__), 'test_config', 'config.py')
|
||||
conf = configuration.conf_from_file(path)
|
||||
self.assertTrue(conf.app.debug)
|
||||
|
||||
def test_config_illegal_ids(self):
|
||||
conf = configuration.Config({})
|
||||
conf.update_with_module('bad.module_and_underscore')
|
||||
self.assertEqual(['__confdir__'], dir(conf))
|
||||
|
||||
def test_config_bad_module(self):
|
||||
conf = configuration.Config({})
|
||||
self.assertRaises(ImportError, conf.update_with_module, 'doesnotexist')
|
||||
self.assertRaises(ImportError, conf.update_with_module, 'bad.doesnotexists')
|
||||
self.assertRaises(ImportError, conf.update_with_module, 'bad.bad.doesnotexist')
|
||||
|
||||
def test_config_set_from_file(self):
|
||||
path = os.path.join(os.path.dirname(__file__), 'test_config', 'empty.py')
|
||||
configuration.set_config(path)
|
||||
self.assertTrue(dir(_runtime_conf.app), [])
|
||||
|
||||
def test_config_set_from_module(self):
|
||||
configuration.set_config('config')
|
||||
self.assertEqual(_runtime_conf.server.host, '1.1.1.1')
|
||||
|
||||
def test_config_user
|
||||
|
||||
|
||||
def test_config_string(self):
|
||||
s = '{pecan.conf.app}'
|
||||
self.assertTrue(configuration.ConfigString.contains_formatting(s))
|
||||
cs = configuration.ConfigString(s)
|
||||
self.assertEqual(str(cs), s)
|
||||
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
# Server Specific Configurations
|
||||
server = {
|
||||
'port' : '8081',
|
||||
'host' : '1.1.1.1'
|
||||
'host' : '1.1.1.1',
|
||||
'socket': '{pecan.conf.server.host}:{pecan.conf.server.host}'
|
||||
}
|
||||
|
||||
# Pecan Application Configurations
|
||||
|
||||
Reference in New Issue
Block a user