def find(cls, module_name): """ Helper function to import backend classes. :param module_name: Dotted Python path to backend class name :returns: Imported class object """ return import_class(module_name, cls)
def get_router(): """Return router defined by RAPIDSMS_ROUTER setting.""" router = getattr(settings, "RAPIDSMS_ROUTER", "rapidsms.router.blocking.BlockingRouter") if isinstance(router, basestring): try: router = import_class(router)() except ImportError as e: raise ImproperlyConfigured(e) return router
def get_router(): """Return router defined by RAPIDSMS_ROUTER setting.""" router = getattr(settings, 'RAPIDSMS_ROUTER', 'rapidsms.router.blocking.BlockingRouter') if isinstance(router, basestring): try: router = import_class(router)() except ImportError as e: raise ImproperlyConfigured(e) return router
def get_handlers(): """ Return a list of the handler classes to use in the current project. This is the classes whose names are listed in the RAPIDSMS_HANDLERS setting, but if that's not set, then we fall back to the deprecated behavior of returning all installed handlers, possibly modified by the INSTALLED_HANDLERS and/or EXCLUDED_HANDLERS settings. """ if hasattr(settings, 'RAPIDSMS_HANDLERS'): return [import_class(name) for name in settings.RAPIDSMS_HANDLERS] warn("Please set RAPIDSMS_HANDLERS to the handlers that should " "be installed. The old behavior of installing all defined " "handlers, possibly modified by INSTALLED_HANDLERS and/or " "EXCLUDED_HANDLERS, is deprecated and will be removed", DeprecationWarning) handlers = _find_handlers(_apps()) # if we're explicitly selecting handlers, filter out all those which # are not matched by one (or more) prefixes in INSTALLED_HANDLERS. if hasattr(settings, 'INSTALLED_HANDLERS') and \ settings.INSTALLED_HANDLERS is not None: copy = [handler for handler in handlers] handlers = [] while len(copy) > 0: for prefix in settings.INSTALLED_HANDLERS: if copy[-1].__module__.startswith(prefix): handlers.append(copy[-1]) break copy.pop() # likewise, in reverse, for EXCLUDED_HANDLERS. if hasattr(settings, 'EXCLUDED_HANDLERS') and \ settings.EXCLUDED_HANDLERS is not None: for prefix in settings.EXCLUDED_HANDLERS: handlers = [ handler for handler in handlers if not handler.__module__.startswith(prefix)] return handlers
def test_valid_base_class(self): """Class should match base_class if supplied.""" class_ = import_class("rapidsms.utils.test_modules.ChildOfA", ParentA) self.assertTrue(issubclass(class_, ParentA)) self.assertEqual(class_, ChildOfA)
def test_valid_class(self): """Valid paths should return the proper class.""" class_ = import_class("rapidsms.utils.test_modules.ParentA") self.assertEqual(class_, ParentA)