def _handlers(module_name): """ Return a list of handlers (subclasses of app.handlers.HandlerBase) defined in the ``handlers`` directory of ``module_name``. Each Python file is expected to contain a single new-style class, which can be named arbitrarily. (But probably shouldn't be.) Return an empty list if no handlers are defined, or the directory can't be opened. All exceptions raised while importing handlers are allowed to propagate, to avoid masking errors. """ handlers_module = try_import("%s.handlers" % module_name) if handlers_module is None: return [] if not hasattr(handlers_module, "__path__"): raise Exception("Module %s must be a directory." % (handlers_module.__name__)) files = find_python_files(handlers_module.__path__[0]) module_names = [ "%s.%s" % (handlers_module.__name__, file) for file in files ] modules = [try_import(mod_name) for mod_name in module_names] return [get_class(mod, BaseHandler) for mod in filter(None, modules)]
def _handlers(module_name): """ Return a list of handlers (subclasses of app.handlers.HandlerBase) defined in the ``handlers`` directory of ``module_name``. Each Python file is expected to contain a single new-style class, which can be named arbitrarily. (But probably shouldn't be.) Return an empty list if no handlers are defined, or the directory can't be opened. All exceptions raised while importing handlers are allowed to propagate, to avoid masking errors. """ handlers_module = try_import( "%s.handlers" % module_name) if handlers_module is None: return [] if not hasattr(handlers_module, "__path__"): return [] files = find_python_files( handlers_module.__path__[0]) module_names = [ "%s.%s" % (handlers_module.__name__, file) for file in files] modules = [ try_import(mod_name) for mod_name in module_names] return [ get_class(mod, BaseHandler) for mod in filter(None, modules)]
def _find_handlers(self, module_name): """ Returns a list of handlers (subclasses of app.handlers.HandlerBase) defined in the "handlers" directory of *module_name*. Each Python file (*.py) is expected to contain a single new-style class, which can be named arbitrarily. (but probably shouldn't be.) Returns an empty list if no handlers are defined, or the directory can't be opened. All exceptions raised while importing handlers are allowed to propagate. """ handlers_module = try_import( "%s.handlers" % module_name) if handlers_module is None: return [] if not hasattr(handlers_module, "__path__"): raise Exception( "Module %s must be a directory." % (handlers_module.__name__)) files = find_python_files( handlers_module.__path__[0]) module_names = [ "%s.%s" % (handlers_module.__name__, file) for file in files] modules = [ try_import(mod_name) for mod_name in module_names] return [ get_class(mod, BaseHandler) for mod in filter(None, modules)]
def test_get_class(self): """get_class() should return the proper class.""" module = import_module('rapidsms.utils.test_modules') class_ = get_class(module, ParentB) self.assertEqual(ParentB, class_)
def find(cls, module_name): module = try_import(module_name) if module is None: return None return get_class(module, cls)