fix sub-namespace pollution problem
This commit is contained in:
		@@ -47,7 +47,7 @@ class ServeCommand(_ServeCommand, Command):
 | 
				
			|||||||
        # for file-watching to work, we need a filename, not a module
 | 
					        # for file-watching to work, we need a filename, not a module
 | 
				
			||||||
        if self.requires_config_file and self.args:
 | 
					        if self.requires_config_file and self.args:
 | 
				
			||||||
            self.config = self.load_configuration(self.args[0])
 | 
					            self.config = self.load_configuration(self.args[0])
 | 
				
			||||||
            self.args[0] = self.config._filename
 | 
					            self.args[0] = self.config.__file__
 | 
				
			||||||
            if self.options.reload is None:
 | 
					            if self.options.reload is None:
 | 
				
			||||||
                self.options.reload = getattr(self.config.app, 'reload', False)
 | 
					                self.options.reload = getattr(self.config.app, 'reload', False)
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,7 +11,8 @@ class ConfigDict(dict):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class Config(object):
 | 
					class Config(object):
 | 
				
			||||||
    def __init__(self, conf_dict={}, filename=''):
 | 
					    def __init__(self, conf_dict={}, filename=''):
 | 
				
			||||||
        self._filename = filename
 | 
					        self.__values__ = {}
 | 
				
			||||||
 | 
					        self.__file__ = filename
 | 
				
			||||||
        self.update(conf_dict)
 | 
					        self.update(conf_dict)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def update(self, conf_dict):
 | 
					    def update(self, conf_dict):
 | 
				
			||||||
@@ -24,7 +25,7 @@ class Config(object):
 | 
				
			|||||||
            if not IDENTIFIER.match(k):
 | 
					            if not IDENTIFIER.match(k):
 | 
				
			||||||
                raise ValueError('\'%s\' is not a valid indentifier' % k)
 | 
					                raise ValueError('\'%s\' is not a valid indentifier' % k)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            cur_val = self.__dict__.get(k)
 | 
					            cur_val = self.__values__.get(k)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if isinstance(cur_val, Config):
 | 
					            if isinstance(cur_val, Config):
 | 
				
			||||||
                cur_val.update(conf_dict[k])
 | 
					                cur_val.update(conf_dict[k])
 | 
				
			||||||
@@ -34,30 +35,36 @@ class Config(object):
 | 
				
			|||||||
    def update_with_module(self, module):
 | 
					    def update_with_module(self, module):
 | 
				
			||||||
        self.update(conf_from_module(module))
 | 
					        self.update(conf_from_module(module))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __getattr__(self, name):
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            return self.__values__[name]
 | 
				
			||||||
 | 
					        except KeyError:
 | 
				
			||||||
 | 
					            raise AttributeError, "'pecan.conf' object has no attribute '%s'" % name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __getitem__(self, key):
 | 
					    def __getitem__(self, key):
 | 
				
			||||||
        return self.__dict__[key]
 | 
					        return self.__values__[key]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __setitem__(self, key, value):
 | 
					    def __setitem__(self, key, value):
 | 
				
			||||||
        if isinstance(value, dict) and not isinstance(value, ConfigDict):
 | 
					        if isinstance(value, dict) and not isinstance(value, ConfigDict):
 | 
				
			||||||
            if value.get('__force_dict__'):
 | 
					            if value.get('__force_dict__'):
 | 
				
			||||||
                del value['__force_dict__']
 | 
					                del value['__force_dict__']
 | 
				
			||||||
                self.__dict__[key] = ConfigDict(value)
 | 
					                self.__values__[key] = ConfigDict(value)
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                self.__dict__[key] = Config(value, filename=self._filename)
 | 
					                self.__values__[key] = Config(value, filename=self.__file__)
 | 
				
			||||||
        elif isinstance(value, basestring) and '%(confdir)s' in value:
 | 
					        elif isinstance(value, basestring) and '%(confdir)s' in value:
 | 
				
			||||||
            confdir = os.path.dirname(self._filename) or os.getcwd()
 | 
					            confdir = os.path.dirname(self.__file__) or os.getcwd()
 | 
				
			||||||
            self.__dict__[key] = value.replace('%(confdir)s', confdir)
 | 
					            self.__values__[key] = value.replace('%(confdir)s', confdir)
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            self.__dict__[key] = value
 | 
					            self.__values__[key] = value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __iter__(self):
 | 
					    def __iter__(self):
 | 
				
			||||||
        return self.__dict__.iteritems()
 | 
					        return self.__values__.iteritems()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __dir__(self):
 | 
					    def __dir__(self):
 | 
				
			||||||
        return self.__dict__.keys()
 | 
					        return self.__values__.keys()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __repr__(self):
 | 
					    def __repr__(self):
 | 
				
			||||||
        return 'Config(%s)' % str(self.__dict__)
 | 
					        return 'Config(%s)' % str(self.__values__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def conf_from_module(module):
 | 
					def conf_from_module(module):
 | 
				
			||||||
    if isinstance(module, str):
 | 
					    if isinstance(module, str):
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -79,7 +79,7 @@ class TestConf(TestCase):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def test_config_repr(self):
 | 
					    def test_config_repr(self):
 | 
				
			||||||
        conf = configuration.Config({'a':1})
 | 
					        conf = configuration.Config({'a':1})
 | 
				
			||||||
        self.assertEqual(repr(conf),"Config({'a': 1, '_filename': ''})")
 | 
					        self.assertEqual(repr(conf),"Config({'a': 1})")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_config_from_dict(self):
 | 
					    def test_config_from_dict(self):
 | 
				
			||||||
        conf = configuration.conf_from_dict({})
 | 
					        conf = configuration.conf_from_dict({})
 | 
				
			||||||
@@ -94,7 +94,7 @@ class TestConf(TestCase):
 | 
				
			|||||||
    def test_config_illegal_ids(self):
 | 
					    def test_config_illegal_ids(self):
 | 
				
			||||||
        conf = configuration.Config({})
 | 
					        conf = configuration.Config({})
 | 
				
			||||||
        conf.update_with_module('bad.module_and_underscore')
 | 
					        conf.update_with_module('bad.module_and_underscore')
 | 
				
			||||||
        self.assertEqual(['_filename'], dir(conf))
 | 
					        self.assertEqual([], dir(conf))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_config_bad_module(self):
 | 
					    def test_config_bad_module(self):
 | 
				
			||||||
        conf = configuration.Config({})
 | 
					        conf = configuration.Config({})
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user