def __init__(self, options): pool_cls_path = options.get("CONNECTION_POOL_CLASS", "redis.connection.ConnectionPool") self.pool_cls = import_string(pool_cls_path) self.pool_cls_kwargs = options.get("CONNECTION_POOL_KWARGS", {}) redis_client_cls_path = options.get("REDIS_CLIENT_CLASS", "redis.client.StrictRedis") self.redis_client_cls = import_string(redis_client_cls_path) self.redis_client_cls_kwargs = options.get("REDIS_CLIENT_KWARGS", {}) self.options = options
def get_app_class(self): application_class = self.default_application_class try: application_class = import_string('apps.AnthillApplication') except ImportError as e1: if settings.APPLICATION_CLASS is not None: try: application_class = import_string( settings.APPLICATION_CLASS) except ImportError as e2: logger.warning(e2) logger.warning('Cannot import application class: %s. ' 'Default used.' % settings.APPLICATION_CLASS) return application_class
def get_connection_factory(path=None, options=None): if path is None: path = getattr(settings, "REDIS_CONNECTION_FACTORY", "anthill.framework.core.cache.backends.redis.pool.ConnectionFactory") cls = import_string(path) return cls(options or {})
def service(self): """ Returns an instance of service class ``self.service_class``. """ service_class = import_string(self.service_class) service_instance = service_class(app=self) return service_instance
def create_template_loader(self, template_path): """ Returns a new template loader for the given path. May be overridden by subclasses. By default returns a directory-based loader on the given path, using the ``autoescape`` and ``template_whitespace`` application settings. If a ``template_loader`` application setting is supplied, uses that instead. """ session = getattr(self, 'session', None) if "template_loader" in self.settings: return self.settings["template_loader"] kwargs = {} if "autoescape" in self.settings: # autoescape=None means "no escaping", so we have to be sure # to only pass this kwarg if the user asked for it. kwargs["autoescape"] = self.settings["autoescape"] if "template_whitespace" in self.settings: kwargs["whitespace"] = self.settings["template_whitespace"] template_loader_class = getattr( settings, "TEMPLATE_LOADER_CLASS", "anthill.framework.core.template.Loader") # ``session`` used for caching special template root. return import_string(template_loader_class)(template_path, session=session, **kwargs)
def __init__(self, handlers=None, default_host=None, transforms=None, app=None, **kwargs): kwargs.update(debug=app.debug) kwargs.update(compress_response=app.settings.COMPRESS_RESPONSE) self.setup_static(app, kwargs) static_handler_class = getattr( app.settings, 'STATIC_HANDLER_CLASS', 'anthill.framework.handlers.StaticFileHandler') kwargs.update(static_handler_class=import_string(static_handler_class)) transforms = transforms or list( map(import_string, app.settings.OUTPUT_TRANSFORMS or [])) super().__init__(handlers, default_host, transforms, **kwargs) self.io_loop = IOLoop.current() self.app = app self.config = app.settings self.name = app.label self.db = app.db self.version = app.version self.debug = app.debug self.setup()
def load_handler(path, *args, **kwargs): """ Given a path to a handler, return an instance of that handler. E.g.:: >>> load_handler('anthill.framework.core.files.uploadhandler.TemporaryFileUploadHandler', request) <TemporaryFileUploadHandler object at 0x...> """ return import_string(path)(*args, **kwargs)
def get_hashers(): hashers = [] for hasher_path in settings.PASSWORD_HASHERS: hasher_cls = import_string(hasher_path) hasher = hasher_cls() if not getattr(hasher, 'algorithm'): raise ImproperlyConfigured("hasher doesn't specify an " "algorithm name: %s" % hasher_path) hashers.append(hasher) return hashers
def get_connection(backend=None, fail_silently=False, **kwargs): """Load an email backend and return an instance of it. If backend is None (default), use settings.EMAIL_BACKEND. Both fail_silently and other keyword arguments are used in the constructor of the backend. """ cls = import_string(backend or settings.EMAIL_BACKEND) return cls(fail_silently=fail_silently, **kwargs)
def configure_logging(logging_config, logging_settings): if logging_config: # First find the logging configuration function ... logging_config_func = import_string(logging_config) logging.config.dictConfig(DEFAULT_LOGGING) # ... then invoke it with the logging settings if logging_settings: logging_config_func(logging_settings)
def _parse_callback(self, entry): callback = entry.get('callback') if callback is not None: try: fn = import_string(callback) if fn is not None and not callable(fn): raise ImproperlyConfigured( 'Rate limit callback is not callable') return fn except ImportError: pass
def get_message_moderators(moderator_config): moderators = [] for moderator in moderator_config: try: klass = import_string(moderator['NAME']) except ImportError: msg = "The module in NAME could not be imported: %s." \ "Check your MODERATORS setting." raise ImproperlyConfigured(msg % moderator['NAME']) moderators.append(klass(**moderator.get('OPTIONS', {}))) return moderators
def get_key_func(key_func): """ Function to decide which key function to use. Default to ``default_key_func``. """ if key_func is not None: if callable(key_func): return key_func else: return import_string(key_func) return default_key_func
def get_password_validators(validator_config): validators = [] for validator in validator_config: try: klass = import_string(validator['NAME']) except ImportError: msg = "The module in NAME could not be imported: %s." \ "Check your AUTH_PASSWORD_VALIDATORS setting." raise ImproperlyConfigured(msg % validator['NAME']) validators.append(klass(**validator.get('OPTIONS', {}))) return validators
def _make_backend(self, name, config): # Load the backend class try: backend_class = import_string(self.configs[name]["BACKEND"]) except KeyError: raise InvalidChannelLayerError("No BACKEND specified for %s" % name) except ImportError: raise InvalidChannelLayerError( "Cannot import BACKEND %r specified for %s" % (self.configs[name]["BACKEND"], name) ) # Initialise and pass config return backend_class(**config)
def _create_cache(backend, **kwargs): try: # Try to get the CACHES entry for the given backend name first try: conf = settings.CACHES[backend] except KeyError: try: # Trying to import the given backend, in case it's a dotted path import_string(backend) except ImportError as e: raise InvalidCacheBackendError( "Could not find backend '%s': %s" % (backend, e)) location = kwargs.pop('LOCATION', '') params = kwargs else: params = {**conf, **kwargs} backend = params.pop('BACKEND') location = params.pop('LOCATION', '') backend_cls = import_string(backend) except ImportError as e: raise InvalidCacheBackendError("Could not find backend '%s': %s" % (backend, e)) return backend_cls(location, params)
def __init__(self, server, params, backend): self._backend = backend self._server = server self._params = params self.reverse_key = get_key_func( params.get("REVERSE_KEY_FUNCTION") or "anthill.framework.core.cache.backends.redis.util.default_reverse_key" ) if not self._server: raise ImproperlyConfigured("Missing connections string") if not isinstance(self._server, (list, tuple, set)): self._server = self._server.split(",") self._clients = [None] * len(self._server) self._options = params.get("OPTIONS", {}) self._slave_read_only = self._options.get('SLAVE_READ_ONLY', True) serializer_path = self._options.get( "SERIALIZER", "anthill.framework.core.cache.backends.redis.serializers.pickle.PickleSerializer" ) serializer_cls = import_string(serializer_path) compressor_path = self._options.get( "COMPRESSOR", "anthill.framework.core.cache.backends.redis.compressors.identity.IdentityCompressor" ) compressor_cls = import_string(compressor_path) self._serializer = serializer_cls(options=self._options) self._compressor = compressor_cls(options=self._options) self.connection_factory = pool.get_connection_factory( options=self._options)
def _get_backends(return_tuples=False): backends = [] for backend_data in settings.PAYMENT_SYSTEM_BACKENDS: path = backend_data['NAME'] options = backend_data.get('OPTIONS', {}) try: backend_class = import_string(path) except ImportError: msg = "The module in NAME could not be imported: %s." \ "Check your PAYMENT_SYSTEM_BACKENDS setting." raise ImproperlyConfigured(msg % path) backend = backend_class(**options) backends.append((path, backend) if return_tuples else backend) if not backends: raise ImproperlyConfigured( 'No payment system backends have been defined. ' 'Does PAYMENT_SYSTEM_BACKENDS contain anything?') return backends
def setup(self): # Override `io_loop.handle_callback_exception` method to catch exceptions globally. self.io_loop.handle_callback_exception = self.__io_loop_handle_callback_exception__ self.add_handlers(self.app.host_regex, self.app.routes) logger.debug('Service routes installed.') self.settings.update(cookie_secret=self.config.SECRET_KEY) self.settings.update(xsrf_cookies=self.config.CSRF_COOKIES) self.settings.update(template_path=self.config.TEMPLATE_PATH) self.settings.update(login_url=self.config.LOGIN_URL) default_handler_class = getattr( self.config, 'DEFAULT_HANDLER_CLASS', 'anthill.framework.handlers.Handler404') if default_handler_class is not None: self.settings.update( default_handler_class=import_string(default_handler_class)) self.settings.update( default_handler_args=self.config.DEFAULT_HANDLER_ARGS) # template_loader_class = getattr( # self.app.settings, 'TEMPLATE_LOADER_CLASS', 'anthill.framework.core.template.Loader') # template_loader_kwargs = dict() # if "autoescape" in self.settings: # # autoescape=None means "no escaping", so we have to be sure # # to only pass this kwarg if the user asked for it. # template_loader_kwargs["autoescape"] = self.settings["autoescape"] # if "template_whitespace" in self.settings: # template_loader_kwargs["whitespace"] = self.settings["template_whitespace"] # template_loader = import_string(template_loader_class)( # self.app.settings.TEMPLATE_PATH, **template_loader_kwargs) # self.settings.update(template_loader=template_loader) # logger.debug('Template loader `%s` installed.' % template_loader_class) self._load_ui_modules(self.app.ui_modules) self._load_ui_methods(self.app.ui_modules) logger.debug('Service ui modules loaded.')
def setup_log_streaming(self): log_streaming_config = getattr(self.config, 'LOG_STREAMING', None) if log_streaming_config: from anthill.framework.handlers import WatchLogFileHandler custom_handler_class = log_streaming_config.get('handler', {}).get('class') if not custom_handler_class: handler_class = WatchLogFileHandler else: from anthill.framework.utils.module_loading import import_string handler_class = import_string(custom_handler_class) handler_kwargs = log_streaming_config.get('handler', {}).get( 'kwargs', dict(handler_name='anthill')) log_streaming_url = log_streaming_config.get('path', '/log/') self.add_handlers(self.app.host_regex, [ url(_url_pattern(log_streaming_url), handler_class, kwargs=handler_kwargs, name='log'), ]) logger.debug('Log streaming installed on %s.' % log_streaming_url) else: logger.debug('Log streaming not installed.')
async def build_context_from_context_processors( handler: RequestHandler) -> dict: """Build extra context for current handler on every request.""" ctx = {} for ctx_processor in CONTEXT_PROCESSORS: f = import_string(ctx_processor) # Context processor can be either co routine or plain function result = await f(handler) if inspect.iscoroutinefunction(f) else f( handler) if not isinstance(handler, RequestHandler): raise ImproperlyConfigured( 'Context processor `%s` got `%s` object, ' 'but need `RequestHandler`' % (f.__name__, handler.__class__.__name__)) if not isinstance(result, dict): raise ImproperlyConfigured( 'Context processor `%s` must return dict object, ' 'but `%s` returned' % (f.__name__, type(result))) if not result: logging.warning('Empty result for context processor `%s`' % f.__name__) ctx.update(result) return ctx
def setup_log_streaming(self): streaming_config = getattr(self.config, 'LOG_STREAMING', None) if streaming_config: try: handler_class = streaming_config['handler']['class'] except (KeyError, TypeError): handler_class = 'anthill.framework.handlers.WatchLogFileHandler' handler_class = import_string(handler_class) try: handler_kwargs = streaming_config['handler']['kwargs'] except (KeyError, TypeError): handler_kwargs = {'handler_name': 'anthill'} streaming_url = _url_pattern(streaming_config.get('path', '/log/')) self.add_handlers(self.app.host_regex, [ url(streaming_url, handler_class, kwargs=handler_kwargs, name='log')]) logger.debug('Log streaming installed on %s.' % streaming_url) else: logger.debug('Log streaming not installed.')
def get_storage_class(import_path=None): return import_string(import_path or settings.DEFAULT_FILE_STORAGE)
def __init__(self): self.manager = import_string(UPDATE_MANAGER)()
def get_cookie_signer(salt='anthill.framework.core.signing.get_cookie_signer'): Signer = import_string(settings.SIGNING_BACKEND) key = force_bytes(settings.SECRET_KEY) return Signer(b'anthill.http.cookies' + key, salt=salt)
def load_backend(path): return import_string(path)()
def value_object(self) -> BaseAction: return import_string(self.value)()
def _methods(self): return (import_string(m) for m in self.methods)
def get_parser_cls(self): cls = self.options.get("PARSER_CLASS", None) if cls is None: return DefaultParser return import_string(cls)
def __init__(self, session_key=None): self._session_key = session_key self.accessed = False self.modified = False self.serializer = import_string(settings.SESSION_SERIALIZER)