Fixing commands so they don't hide import errors

This commit is contained in:
Yoann Roman
2011-04-20 10:46:11 -04:00
parent 4793e1aab9
commit 1581c005e2

View File

@@ -32,36 +32,31 @@ class Command(paste_command.Command):
ex.args[0] = self.parser.error(ex.args[0]) ex.args[0] = self.parser.error(ex.args[0])
raise raise
def can_import(self, name):
try:
__import__(name)
return True
except ImportError:
return False
def get_package_names(self, config): def get_package_names(self, config):
if not hasattr(config.app, 'modules'): if not hasattr(config.app, 'modules'):
return [] return []
return [module.__name__ for module in config.app.modules if hasattr(module, '__name__')] return [module.__name__ for module in config.app.modules if hasattr(module, '__name__')]
def import_module(self, package, name):
parent = __import__(package, fromlist=[name])
return getattr(parent, name, None)
def load_configuration(self, name): def load_configuration(self, name):
set_config(name) set_config(name)
return _runtime_conf return _runtime_conf
def load_app(self, config): def load_app(self, config):
for package_name in self.get_package_names(config): for package_name in self.get_package_names(config):
module_name = '%s.app' % package_name module = self.import_module(package_name, 'app')
if self.can_import(module_name): if hasattr(module, 'setup_app'):
module = sys.modules[module_name] return module.setup_app(config)
if hasattr(module, 'setup_app'): raise paste_command.BadCommand('No app.setup_app found in any app modules')
return module.setup_app(config)
raise paste_command.BadCommand('No app.setup_app found in any of the configured app.modules')
def load_model(self, config): def load_model(self, config):
for package_name in self.get_package_names(config): for package_name in self.get_package_names(config):
module_name = '%s.model' % package_name module = self.import_module(package_name, 'model')
if self.can_import(module_name): if module:
return sys.modules[module_name] return module
return None return None
def logging_file_config(self, config_file): def logging_file_config(self, config_file):