kyoukai.app

The core application.

Functions

run_application(component, *[, ...]) Configure logging and start the given root component in the default asyncio event loop.

Classes

Blueprint(name[, parent, prefix, ...]) A Blueprint class contains a Map of URL rules, which is checked and ran for every
Context([parent, default_timeout]) Contexts give request handlers and callbacks access to resources.
HTTPRequestContext(parent, request) The context subclass passed to all requests within Kyoukai.
Kyoukai(application_name, *[, server_name]) The Kyoukai type is the core of the Kyoukai framework, and the core of your web application based upon the Kyoukai framework.
Request(environ[, populate_request, shallow]) Full featured request object implementing the following mixins:
Response([response, status, headers, ...]) Full featured response object implementing the following mixins:

Exceptions

HTTPException([description, response]) Baseclass for all HTTP exceptions.
InternalServerError([description, response]) 500 Internal Server Error
MethodNotAllowed([valid_methods, description]) 405 Method Not Allowed
NotFound([description, response]) 404 Not Found
RequestRedirect(new_url) Raise if the map requests a redirect.
class kyoukai.app.Kyoukai(application_name, *, server_name=None, **kwargs)[source]

Bases: object

The Kyoukai type is the core of the Kyoukai framework, and the core of your web application based upon the Kyoukai framework. It acts as a central router and request processor that takes in requests from the protocols and returns responses.

The application name is currently unused, but it is good practice to set it correctly anyway in case it is used in future editions of Kyoukai.

You normally create an application instance inside your component file, like so:

from kyoukai.app import Kyoukai

... # setup code

kyk = Kyoukai("my_app")
kyk.register_blueprint(whatever)

... # other setup

class MyContainer(ContainerComponent):
    async def start(self, ctx):
        self.add_component('kyoukai', KyoukaiComponent, ip="127.0.0.1", port=4444,
                           app="app:app")

Of course, you can also embed Kyoukai inside another app, by awaiting Kyoukai.start().

Parameters:
  • application_name (str) – The name of the application that is being created. This is passed to the Blueprint being created as the root blueprint.
  • server_name (Optional[str]) – Keyword-only. The SERVER_NAME to use inside the fake WSGI environment created for url_for, if applicable.
  • host_matching – Should host matching be enabled? This will be implicitly True if host is not None.
  • host – The host used for host matching, to be passed to the root Blueprint. By default, no host is used, so all hosts are matched on the root Blueprint.
  • application_root – Keyword-only. The APPLICATION_ROOT to use inside the fake WSGI environment created for url_for, if applicable.
  • loop – Keyword-only. The asyncio event loop to use for this app. If no loop is specified it, will be automatically fetched using asyncio.get_event_loop().
  • request_class – Keyword-only. The custom request class to instantiate requests with.
  • response_class – Keyword-only. The custom response class to instantiate responses with.
request_class

The class of request to spawn every request. This should be a subclass of werkzeug.wrappers.Request. You can override this by passing request_class as a keyword argument to the app.

alias of Request

response_class

The class of response to wrap automatically. This should be a subclass of werkzeug.wrappers.Response. You can override this by passing response_class as a keyword argument to the app.

alias of Response

root
Return type:Blueprint
Returns:The root Blueprint for the routing tree.
register_blueprint(child)[source]

Registers a child blueprint to this app’s root Blueprint.

This will set up the Blueprint tree, as well as setting up the routing table when finalized.

Parameters:child (Blueprint) – The child Blueprint to add. This must be an instance of Blueprint.
finalize()[source]

Finalizes the app and blueprints.

This will calculate the current werkzeug.routing.Map which is required for routing to work.

log_route(request, code)[source]

Logs a route invocation.

Parameters:
  • request (Request) – The request produced.
  • code (int) – The response code of the route.
coroutine handle_httpexception(self, ctx, exception, environ=None)[source]

Handle a HTTP Exception.

Parameters:
Return type:

Response

Returns:

A werkzeug.wrappers.Response that handles this response.

coroutine process_request(self, request, parent_context)[source]

Processes a Request and returns a Response object.

This is the main processing method of Kyoukai, and is meant to be used by one of the HTTP server backends, and not by client code.

Parameters:
  • request (Request) – The werkzeug.wrappers.Request object to process. A new HTTPRequestContext will be provided to wrap this request inside of to client code.
  • parent_context (Context) – The asphalt.core.Context that is the parent context for this particular app. It will be used as the parent for the HTTPRequestContext.
Return type:

Response

Returns:

A werkzeug.wrappers.Response object that can be written to the client as a response.

coroutine start(self, ip='127.0.0.1', port=4444, *, component=None, base_context=None)[source]

Runs the Kyoukai component asynchronously.

This will bypass Asphalt’s default runner, and allow you to run your app easily inside something else, for example.

Parameters:
  • ip (str) – The IP of the built-in server.
  • port (int) – The port of the built-in server.
  • component – The component to start the app with. This should be an instance of kyoukai.asphalt.KyoukaiComponent.
  • base_context (Optional[Context]) – The base context that the HTTPRequestContext should be started with.
run(ip='127.0.0.1', port=4444, *, component=None)[source]

Runs the Kyoukai server from within your code.

This is not normally invoked - instead Asphalt should invoke the Kyoukai component. However, this is here for convenience.