def constant_or_function(value): try: value = int(value) except ValueError: return module_attribute(value) else: return Constant(value)
def middleware(self, app): """Add websocket middleware """ url = app.config["WS_URL"] if url: handler = module_attribute(app.config["WS_HANDLER"]) return [SocketIO(url, handler(app))]
def create_store(url, **kw): '''Create a new :class:`Store` for a valid ``url``. :param url: a valid ``url`` takes the following forms: :ref:`Pulsar datastore <store_pulsar>`:: pulsar://user:[email protected]:6410 :ref:`Redis <store_redis>`:: redis://user:[email protected]:6500/11?namespace=testdb :param kw: additional key-valued parameters to pass to the :class:`.Store` initialisation method. It can contains parameters such as ``database``, ``user`` and ``password`` to override the ``url`` values. Additional parameters are processed by the :meth:`.Store._init` method. :return: a :class:`Store`. ''' if isinstance(url, Store): return url scheme, address, params = parse_store_url(url) dotted_path = data_stores.get(scheme) if not dotted_path: raise NoSuchStore('%s store not available' % scheme) store_class = module_attribute(dotted_path, safe=True) if not store_class: raise ImproperlyConfigured('"%s" store not available' % dotted_path) if not store_class.registered: store_class.registered = True store_class.register() params.update(kw) return store_class(scheme, address, **params)
def validate_plugin_list(val): if val and not isinstance(val, (list, tuple)): raise TypeError("Not a list: %s" % val) values = [] for v in val: values.append(module_attribute(v, safe=False)()) return values
def middleware(self, app): """Add websocket middleware """ url = app.config['WS_URL'] if url: handler = module_attribute(app.config['WS_HANDLER']) return [SocketIO(url, handler(app))]
def handler(self): '''Build the wsgi handler from *hnd*. This function is called at start-up only. :parameter hnd: This is the WSGI handle which can be A :class:`WsgiHandler`, a WSGI callable or a list WSGI callables. :parameter resp_middleware: Optional list of response middleware functions.''' hnd = self.callable if not isinstance(hnd, WsgiHandler): if not isinstance(hnd, (list, tuple)): hnd = [hnd] hnd = WsgiHandler(hnd) response_middleware = self.cfg.response_middleware or [] for m in response_middleware: if '.' not in m: mm = getattr(middleware,m,None) if not mm: raise ValueError('Response middleware "{0}" not available'\ .format(m)) else: mm = module_attribute(m) if isclass(mm): mm = mm() hnd.response_middleware.append(mm) return hnd
def middleware(self, app): middleware = [self] for backend in self.backends: middleware.extend(backend.middleware(app) or ()) dotted_path = app.config['PAGINATION'] pagination = module_attribute(dotted_path) if not pagination: raise ImproperlyConfigured('Could not load paginator "%s"', dotted_path) app.pagination = pagination() url = app.config['API_URL'] # If the api url is not absolute, add the api middleware if url is not None: if not is_absolute_uri(url): # Add the preflight and token events events = ('on_preflight', 'on_token') app.add_events(events) for backend in self.backends: app.bind_events(backend, events) api = RestRoot(url) middleware.append(api) for extension in app.extensions.values(): api_sections = getattr(extension, 'api_sections', None) if api_sections: for router in api_sections(app): api.add_child(router) app.api = ApiClient(app) return middleware
def create_store(url, loop=None, **kw): '''Create a new client :class:`Store` for a valid ``url``. A valid ``url`` taks the following forms:: pulsar://user:[email protected]:6410 redis://user:[email protected]:6500/11?namespace=testdb. postgresql://user:[email protected]:6500/testdb couchdb://user:[email protected]:6500/testdb :param loop: optional event loop, if not provided it is obtained via the ``get_event_loop`` method. If not loop is installed a bright new event loop is created via the :func:`.new_event_loop`. In the latter case the event loop is employed only for synchronous type requests via the :meth:`~.EventLoop.run_until_complete` method. :param kw: additional key-valued parameters to pass to the :class:`Store` initialisation method. :return: a :class:`Store`. ''' if isinstance(url, Store): return url scheme, address, params = parse_store_url(url) dotted_path = data_stores.get(scheme) if not dotted_path: raise ImproperlyConfigured('%s store not available' % scheme) loop = loop or get_event_loop() if not loop: loop = new_event_loop(logger=logging.getLogger(dotted_path)) store_class = module_attribute(dotted_path) params.update(kw) return store_class(scheme, address, loop, **params)
def middleware(self, app): # API urls not available - no middleware to add if not app.apis: return middleware = [] # Add routers and models routes = OrderedDict() for extension in app.extensions.values(): api_sections = getattr(extension, "api_sections", None) if api_sections: for router in api_sections(app) or (): routes[router.route.path] = router # Allow router override for router in routes.values(): if isinstance(router, RestRouter): # Register model router.model = app.models.register(router.model) if router.model: router.model.api_route = router.route # Add router to API root-router app.apis.add_child(router) # Create the rest-api handler app.api = app.providers["Api"](app) # # Create paginator dotted_path = app.config["PAGINATION"] pagination = module_attribute(dotted_path) if not pagination: raise ImproperlyConfigured('Could not load paginator "%s"', dotted_path) app.pagination = pagination() has_api = False for api in app.apis: # router not required when api is remote if api.netloc: continue has_api = True # # Add API root-router to middleware middleware.append(api.router) url = str(api.router) if url != "/": # when the api is served by a path, make sure 404 is raised # when no suitable routes are found middleware.append(Rest404(remove_double_slash("%s/<path:path>" % url))) # # Add the preflight and token events if has_api: app.add_events(("on_preflight", "on_token")) return middleware
def __setstate__(self, state): mod, data = state test = module_attribute(mod) inject_async_assert(test) if data is not None: test = test.__new__(test) test.__dict__.update(data) self.test = test
def create(cls, app): protocol = module_attribute(app.config['PUBSUB_PROTOCOL'])() addr = app.config['PUBSUB_STORE'] if not addr or not app.green_pool: return store = create_store(addr) channels = store.channels(protocol=protocol, namespace=app.config['PUBSUB_PREFIX']) return cls(app, channels)
async def run(self, name, config, options): function = config.get('function') if not function: raise self.error('function path not specified') result = module_attribute(function) if hasattr(result, '__call__'): result = await as_coroutine(result(self)) if result: self.context[name] = result
def middleware(self, app): self._response_middleware = [] middleware = [] for dotted_path in app.config['AUTHENTICATION_BACKENDS']: backend = module_attribute(dotted_path) backend = backend(app) app.permissions.auth_backends.append(backend) middleware.extend(backend.middleware(app)) self._response_middleware.extend(backend.response_middleware(app)) return middleware
def _on_config(self, config): self.auth_backend = MultiAuthBackend() for dotted_path in config['AUTHENTICATION_BACKENDS']: backend = module_attribute(dotted_path) if not backend: self.logger.error('Could not load backend "%s"', dotted_path) continue backend = backend() self.auth_backend.append(backend) self.bind_events(backend)
def register_broker(name, factory=None): if factory is None: dotted_path = brokers.get(name) if not dotted_path: raise ImproperlyConfigured("No such message broker: %s" % name) factory = module_attribute(dotted_path, safe=True) if not factory: raise ImproperlyConfigured('"%s" store not available' % dotted_path) else: brokers[name] = factory return factory
def create_cache(app, url): if isinstance(url, Cache): return url scheme, _, _ = parse_store_url(url) dotted_path = data_caches.get(scheme) if not dotted_path: raise ImproperlyConfigured('%s cache not available' % scheme) store_class = module_attribute(dotted_path) if not store_class: raise ImproperlyConfigured('"%s" store not available' % dotted_path) return store_class(app, scheme, url)
def create(cls, app): protocol = module_attribute(app.config['PUBSUB_PROTOCOL'])() addr = app.config['PUBSUB_STORE'] if not addr or not app.green_pool: return store = create_store(addr) channels = store.channels( protocol=protocol, namespace=app.config['PUBSUB_PREFIX'] ) return cls(app, channels)
def register_broker(name, factory=None): if factory is None: dotted_path = brokers.get(name) if not dotted_path: raise ImproperlyConfigured('No such message broker: %s' % name) factory = module_attribute(dotted_path, safe=True) if not factory: raise ImproperlyConfigured( '"%s" store not available' % dotted_path) else: brokers[name] = factory return factory
def on_config(self, app): self.backends = [] module = import_module(app.meta.module_name) for dotted_path in app.config['AUTHENTICATION_BACKENDS']: backend = module_attribute(dotted_path)() backend.setup(app.config, module, app.params) self.backends.append(backend) app.bind_events(backend, exclude=('on_config', )) for backend in self.backends: if hasattr(backend, 'on_config'): backend.on_config(app) app.auth_backend = self
def on_config(self, app): self.backends = [] module = import_module(app.meta.module_name) for dotted_path in app.config['AUTHENTICATION_BACKENDS']: backend = module_attribute(dotted_path)() backend.setup(app.config, module, app.params) self.backends.append(backend) app.bind_events(backend, exclude=('on_config',)) for backend in self.backends: if hasattr(backend, 'on_config'): backend.on_config(app) app.auth_backend = self
def setup(self, environ=None): '''Set up the :class:`.WsgiHandler` the first time this middleware is accessed. ''' from django.conf import settings from django.core.wsgi import get_wsgi_application # try: dotted = settings.WSGI_APPLICATION except AttributeError: # pragma nocover dotted = None if dotted: return module_attribute(dotted)() else: app = middleware_in_executor(get_wsgi_application()) return WsgiHandler((wait_for_body_middleware, app))
def create_store(url, loop=None, **kw): '''Create a new :class:`Store` for a valid ``url``. :param url: a valid ``url`` takes the following forms: :ref:`Pulsar datastore <store_pulsar>`:: pulsar://user:[email protected]:6410 :ref:`Redis <store_redis>`:: redis://user:[email protected]:6500/11?namespace=testdb :ref:`CouchDb <store_couchdb>`:: couchdb://user:[email protected]:6500/testdb https+couchdb://user:[email protected]:6500/testdb :param loop: optional event loop, obtained by :func:`~asyncio.get_event_loop` if not provided. To create a synchronous client pass a new event loop created via the :func:`~asyncio.new_event_loop`. In the latter case the event loop is employed only for synchronous type requests via the :meth:`~asyncio.BaseEventLoop.run_until_complete` method. :param kw: additional key-valued parameters to pass to the :class:`.Store` initialisation method. It can contains parameters such as ``database``, ``user`` and ``password`` to override the ``url`` values. Additional parameters are processed by the :meth:`.Store._init` method. :return: a :class:`Store`. ''' if isinstance(url, Store): return url scheme, address, params = parse_store_url(url) dotted_path = data_stores.get(scheme) if not dotted_path: raise ImproperlyConfigured('%s store not available' % scheme) loop = loop or get_event_loop() if not loop: loop = new_event_loop(logger=logging.getLogger(dotted_path)) store_class = module_attribute(dotted_path) if not store_class.registered: store_class.registered = True store_class.register() params.update(kw) return store_class(scheme, address, loop, **params)
def setup(self, environ=None): '''Set up the :class:`.WsgiHandler` the first time this middleware is accessed. ''' from django.conf import settings from django.core.wsgi import get_wsgi_application # try: dotted = settings.WSGI_APPLICATION except AttributeError: # pragma nocover dotted = None if dotted: app = module_attribute(dotted) else: app = get_wsgi_application() app = middleware_in_executor(app) return WsgiHandler((wait_for_body_middleware, app), async=True)
def routes(self): """Build the API routes This method generates routes only when it is a server side API """ # # Create paginator dotted_path = self.config['PAGINATION'] pagination = module_attribute(dotted_path) if not pagination: raise ImproperlyConfigured('Could not load paginator "%s"', dotted_path) self.app.pagination = pagination() api_routers = OrderedDict() # Allow router override for extension in self.app.extensions.values(): api_sections = getattr(extension, 'api_sections', None) if api_sections: for router in api_sections(self.app) or (): api_routers[router.route.path] = router for router in api_routers.values(): if isinstance(router, RestRouter): # Register model router.model = self.app.models.register(router.model) if router.model: router.model.api_route = router.route # Add router to an API self.add_child(router) for api in self: # router not required when api is remote if api.netloc: continue # # Add API root-router to middleware router = api.router() yield router url = str(router) if url != '/': # when the api is served by a path, make sure 404 is raised # when no suitable routes are found yield Rest404(remove_double_slash('%s/<path:path>' % url))
def search_engine(url, loop=None, **kw): if isinstance(url, SearchEngine): return url loop = loop or get_event_loop() if not loop: raise ImproperlyConfigured('no event loop') if isinstance(url, dict): extra = url.copy() url = extra.pop('url', None) extra.update(kw) kw = extra scheme, address, params = parse_store_url(url) dotted_path = search_engines.get(scheme) if not dotted_path: raise ImproperlyConfigured('%s search engine not available' % scheme) engine_class = module_attribute(dotted_path) params.update(kw) return engine_class(scheme, address, loop, **params)
def __init__(self, cfg, *, logger=None, **kw): # create the store for channels store = create_store(cfg.data_store, loop=cfg.params.pop('loop', None)) self.cfg = cfg self._loop = store._loop self.logger = logger self._closing_waiter = None if not cfg.message_broker: broker = store else: broker = create_store(cfg.message_broker, loop=self._loop) self.manager = (self.cfg.callable or Manager)(self) self.broker = register_broker(broker.name)(self, broker) self.channels = store.channels(protocol=self.broker, status_channel=ConsumerMessage.type, logger=self.logger) self.http = self.manager.http() self.green_pool = self.manager.green_pool() self.consumers = [] for consumer_path in self.cfg.consumers: consumer = module_attribute(consumer_path)(self) self.consumers.append(consumer) setattr(self, consumer.name, consumer)
def setup(self, environ=None): '''Set up the :class:`.WsgiHandler` the first time this middleware is accessed. ''' from django.conf import settings from django.core.wsgi import get_wsgi_application # try: dotted = settings.WSGI_APPLICATION except AttributeError: # pragma nocover dotted = None if dotted: app = module_attribute(dotted) else: app = get_wsgi_application() green_workers = self.cfg.greenlet if self.cfg else 0 if greenio and green_workers: if pg: pg.make_asynchronous() app = greenio.RunInPool(app, green_workers) self.green_safe_connections() else: app = middleware_in_executor(app) return WsgiHandler((wait_for_body_middleware, app))
def on_config(self, app): self.backends = [] url = app.config['API_URL'] if url is not None and not is_absolute_uri(url): app.config['API_URL'] = str(RestRoot(url)) module = import_module(app.meta.module_name) for dotted_path in app.config['AUTHENTICATION_BACKENDS']: backend = module_attribute(dotted_path) if not backend: self.logger.error('Could not load backend "%s"', dotted_path) continue backend = backend() backend.setup(app.config, module, app.params) self.backends.append(backend) app.bind_events(backend, exclude=('on_config',)) for backend in self.backends: if hasattr(backend, 'on_config'): backend.on_config(app) app.auth_backend = self
def validate_username(request, username): module_attribute(request.config['CHECK_USERNAME'])(request, username)
def validate_username(session, username): module_attribute(session.config['CHECK_USERNAME'])(session, username)
def get(self): return module_attribute(self.value)
def middleware(self, app): # API urls not available - no middleware to add if not app.apis: return middleware = [] # Add routers and models routes = OrderedDict() for extension in app.extensions.values(): api_sections = getattr(extension, 'api_sections', None) if api_sections: for router in api_sections(app) or (): routes[router.route.path] = router # Allow router override for router in routes.values(): if isinstance(router, RestRouter): # Register model router.model = app.models.register(router.model) if router.model: router.model.api_route = router.route # Add router to API root-router app.apis.add_child(router) # Create the rest-api handler app.api = app.providers['Api'](app) # # Create paginator dotted_path = app.config['PAGINATION'] pagination = module_attribute(dotted_path) if not pagination: raise ImproperlyConfigured('Could not load paginator "%s"', dotted_path) app.pagination = pagination() has_api = False for api in app.apis: # router not required when api is remote if api.netloc: continue has_api = True # # Add API root-router to middleware middleware.append(api.router) url = str(api.router) if url != '/': # when the api is served by a path, make sure 404 is raised # when no suitable routes are found middleware.append( Rest404(remove_double_slash('%s/<path:path>' % url))) # # Add the preflight and token events if has_api: app.add_events(('on_preflight', 'on_token')) return middleware
def email_backend(self): '''Email backend for this application ''' dotted_path = self.config['EMAIL_BACKEND'] return module_attribute(dotted_path)(self)