def handle_route(self, url, environ): '''Method used to identify the given url method handler. It enrich the environ dictionary with a new entry that holds a controller instance and a function to be executed from that controller.''' route_configs = self._find_url_regex(url) route_config = None http_verb = environ.get("REQUEST_METHOD").upper() for route_config in route_configs: if http_verb not in route_config["http_verbs"]: route_config = None continue break if not route_config: raise FantasticoHttpVerbNotSupported(http_verb) http_verb_config = route_config["http_verbs"][http_verb] last_dot = http_verb_config.rfind(".") if last_dot == -1: raise FantasticoNoRoutesError("Route %s has an invalid controller mapped." % url) controller_cls = http_verb_config[:last_dot] controller_meth = http_verb_config[last_dot + 1:] environ["route_%s_handler" % url] = {"controller": instantiator.instantiate_class(controller_cls, [self._settings_facade]), "method": controller_meth, "url_params": route_config.get("url_params")}
def get_loaders(self): '''Method used to retrieve all available loaders. If loaders are not currently instantiated they are by these method. This method also supports multi threaded workers mode of wsgi with really small memory footprint. It uses an internal lock so that it makes sure available loaders are instantiated only once per wsgi worker.''' conf_loaders = self._settings_facade.get("routes_loaders") or [] if len(conf_loaders) == 0: raise FantasticoNoRoutesError("No loaders configured.") if self._loader_lock is None and len(self._loaders) == 0: self._loader_lock = threading.Lock() self._loaders = [] if self._loader_lock: self._loader_lock.acquire() if len(self._loaders) == 0: for loader_cls in conf_loaders: loader = instantiator.instantiate_class(loader_cls, [self._settings_facade]) self._loaders.append(loader) if self._loader_lock is not None: self._loader_lock.release() self._loader_lock = None return self._loaders
def get_loaders(self): '''Method used to retrieve all available loaders. If loaders are not currently instantiated they are by these method. This method also supports multi threaded workers mode of wsgi with really small memory footprint. It uses an internal lock so that it makes sure available loaders are instantiated only once per wsgi worker.''' conf_loaders = self._settings_facade.get("routes_loaders") or [] if len(conf_loaders) == 0: raise FantasticoNoRoutesError("No loaders configured.") if self._loader_lock is None and len(self._loaders) == 0: self._loader_lock = threading.Lock() self._loaders = [] if self._loader_lock: self._loader_lock.acquire() if len(self._loaders) == 0: for loader_cls in conf_loaders: loader = instantiator.instantiate_class( loader_cls, [self._settings_facade]) self._loaders.append(loader) if self._loader_lock is not None: self._loader_lock.release() self._loader_lock = None return self._loaders
def get_config(self): '''Method used to return the active configuration which is used by this facade. :rtype: :py:class:`fantastico.settings.BasicSettings` :returns: Active configuration currently used.''' active_config = self._environ.get(self.__ENV_ACTIVE_CONFIG) or "fantastico.settings.BasicSettings" self._cached_config = self._cached_config or instantiator.instantiate_class(active_config) return self._cached_config
def _wrap_middlewares(self): """Method used to register all configured middlewares in the correct order.""" installed_middlewares = self._settings_facade.get("installed_middleware") curr_app_cls, curr_app_inst = FantasticoApp, FantasticoApp.OldCallableApp(self.__call__) for middleware_cls in reversed(installed_middlewares): middleware = instantiator.instantiate_class(middleware_cls, [curr_app_inst]) curr_app_cls.__call__ = lambda inst, *args: middleware(*args) curr_app_cls, curr_app_inst = middleware.__class__, FantasticoApp.OldCallableApp(middleware.__call__)
def get_config(self): '''Method used to return the active configuration which is used by this facade. :rtype: :py:class:`fantastico.settings.BasicSettings` :returns: Active configuration currently used.''' active_config = self._environ.get( self.__ENV_ACTIVE_CONFIG) or "fantastico.settings.BasicSettings" self._cached_config = self._cached_config or instantiator.instantiate_class( active_config) return self._cached_config
def _wrap_middlewares(self): '''Method used to register all configured middlewares in the correct order.''' installed_middlewares = self._settings_facade.get( "installed_middleware") curr_app_cls, curr_app_inst = FantasticoApp, FantasticoApp.OldCallableApp( self.__call__) for middleware_cls in reversed(installed_middlewares): middleware = instantiator.instantiate_class( middleware_cls, [curr_app_inst]) curr_app_cls.__call__ = lambda inst, *args: middleware(*args) curr_app_cls, curr_app_inst = middleware.__class__, FantasticoApp.OldCallableApp( middleware.__call__)
def handle_route(self, url, environ): '''Method used to identify the given url method handler. It enrich the environ dictionary with a new entry that holds a controller instance and a function to be executed from that controller.''' route_configs = self._find_url_regex(url) route_config = None http_verb = environ.get("REQUEST_METHOD").upper() for route_config in route_configs: if http_verb not in route_config["http_verbs"]: route_config = None continue break if not route_config: raise FantasticoHttpVerbNotSupported(http_verb) http_verb_config = route_config["http_verbs"][http_verb] last_dot = http_verb_config.rfind(".") if last_dot == -1: raise FantasticoNoRoutesError( "Route %s has an invalid controller mapped." % url) controller_cls = http_verb_config[:last_dot] controller_meth = http_verb_config[last_dot + 1:] environ["route_%s_handler" % url] = { "controller": instantiator.instantiate_class(controller_cls, [self._settings_facade]), "method": controller_meth, "url_params": route_config.get("url_params") }