Tori Web Framework

Tori web framework is a simple web framework working on the top of CherryPy 3.1+. It is designed to get started with CherryPy without repeating the commonly used configuration or procedure to run the application.

It is also designed to with deployment in mind where the app starter can be used as standalone WSGI server, WSGI application (wrapper) or Google App Engine application.

class yotsuba.lib.tori.BaseInterface

Base interface provides all basic functionalities for developing a web interface responsible for processing and responding incoming HTTP requests. Only compatible with CherryPy 3.1+.

cache(block_name=None, block_data=None, duration=None)

Cache the data.

If block_name is not given, it will return the clone of the memory blocks.

If block_data is given, the cache data block will be updated.

If block_name is given but the data is not available, it will return null pointer.

If duration is below 0, the cache memory is kept as long as the server is running. If it is more than 0, the cache memory will be purged after the cache duration in seconds. If it is 0, it won’t cache a thing.

clean_up_cache()

Purge all expired cache.

default(*path_info)

Default routing to the static folder when the URL is not handled by Python.

default_render(source, **kwargs)

Render template from source with kwargs as context variables/options.

For rendering options, please see tori.render.

Example: self.default_render(‘shiroyuki.html’, author=’juti noppornpitak’, direct_rendering=False)

isAdmin()

Abstract: Check if the user is an administrator.

isAuth()

Abstract: Check if the user is authenticated.

isDELETE()

Check if this request is a DELETE request.

isGET()

Check if this request is a GET request.

isPOST()

Check if this request is a POST request.

isPUT()

Check if this request is a PUT request.

method()

Return the request method (GET, POST, DELETE, PUT)

purge_cache(*block_names)

Purge cache blocks referenced by block_names

Example: self.purge_cache(‘block_a’, ‘block_b’, ...)

Note that if block names aren’t given, it will purge all cache blocks.

redirect(url, code=None, soft_redirection=True)

Redirect a URL with CherryPy’s HTTPRedirect (exception).

url is a string representing URL.

code is a HTTP status code in interger.

soft_redirection make this method not to raise a redirection exception by CherryPy. This is more customizable to what to be given back. This option is strongly recommended for Google App Engine applications.

render(source, **kwargs)

Wrapper method for tori.BaseInterface.default_render

Example: self.render(‘shiroyuki.html’, author=’juti noppornpitak’, direct_rendering=False)

respond_status(code, message=None, break_now=True)

Set the response status.

code is a HTTP status code in interger.

message is a response reason.

break_now is a boolean. If True, this method will raise CherryPy’s HTTPError exception. Otherwise, return status.

exception yotsuba.lib.tori.CherryPyException

Exception when dealing with errors in CherryPy wrappers which may be caused by CherryPy itself

class yotsuba.lib.tori.MemoryBlock(data, duration)

A memory block used by caching mechanism via BaseInterface-base interfaces

class yotsuba.lib.tori.RESTInterface

Abstract class for a RESTful Interface based on BaseInterface. Only compatible with CherryPy 3.1+.

cache(block_name=None, block_data=None, duration=None)

Cache the data.

If block_name is not given, it will return the clone of the memory blocks.

If block_data is given, the cache data block will be updated.

If block_name is given but the data is not available, it will return null pointer.

If duration is below 0, the cache memory is kept as long as the server is running. If it is more than 0, the cache memory will be purged after the cache duration in seconds. If it is 0, it won’t cache a thing.

clean_up_cache()

Purge all expired cache.

create()

Create a new resource

POST ./

default(*args, **kwargs)

Low-level function handling requests based on the method.

default_render(source, **kwargs)

Render template from source with kwargs as context variables/options.

For rendering options, please see tori.render.

Example: self.default_render(‘shiroyuki.html’, author=’juti noppornpitak’, direct_rendering=False)

delete(*args, **kwargs)

Handle DELETE request

DELETE ./*

destroy(key)

Delete the target resource

POST ./{KEY}/delete DEL ./{KEY}

edit(key)

Prompt to update a new resource

GET ./{key}/edit

get(*args, **kwargs)

Handle GET request

GET ./*

index()

List resources

GET ./

isAdmin()

Abstract: Check if the user is an administrator.

isAuth()

Abstract: Check if the user is authenticated.

isDELETE()

Check if this request is a DELETE request.

isGET()

Check if this request is a GET request.

isPOST()

Check if this request is a POST request.

isPUT()

Check if this request is a PUT request.

method()

Return the request method (GET, POST, DELETE, PUT)

new()

Prompt to create a new resource

GET ./new

post(*args, **kwargs)

Handle POST request

POST ./

purge_cache(*block_names)

Purge cache blocks referenced by block_names

Example: self.purge_cache(‘block_a’, ‘block_b’, ...)

Note that if block names aren’t given, it will purge all cache blocks.

put(*args, **kwargs)

Handle PUT request

PUT ./*

redirect(url, code=None, soft_redirection=True)

Redirect a URL with CherryPy’s HTTPRedirect (exception).

url is a string representing URL.

code is a HTTP status code in interger.

soft_redirection make this method not to raise a redirection exception by CherryPy. This is more customizable to what to be given back. This option is strongly recommended for Google App Engine applications.

render(source, **kwargs)

Wrapper method for tori.BaseInterface.default_render

Example: self.render(‘shiroyuki.html’, author=’juti noppornpitak’, direct_rendering=False)

respond_status(code, message=None, break_now=True)

Set the response status.

code is a HTTP status code in interger.

message is a response reason.

break_now is a boolean. If True, this method will raise CherryPy’s HTTPError exception. Otherwise, return status.

show(key)

Show the target resource

GET ./{KEY}

update(key)

Update the target resource

POST ./{key}/update PUT ./{key}

class yotsuba.lib.tori.ServerInterface

ServerInterface is like a switch to control how you deploy your application.

It supports three modes described in tori.ServiceMode.

static application(*largs, **kwargs)

Run the application in WSGI application/wrapper mode with a web server or on Google App Engine.

To run the application on Google App Engine, it is not recommended to use this function directly. Please use ServerInterface.auto instead.

static auto(*largs, **kwargs)

Run in auto mode. This is vary, depending on the configuration.

static standalone(*largs, **kwargs)

Run the application in WSGI server mode (standalone).

class yotsuba.lib.tori.ServiceMode

ServiceMode is just a group of keywords to tell ServerInterface which mode to run.

This class provides only static attributes. You may instantiate but nothing works.

exception yotsuba.lib.tori.WebFrameworkException

Exception specifically used by Tori during initilization

yotsuba.lib.tori.minify_content(original_code, full_compression=False, force_op=False, file_type=None)

Minify content (text data). Work best with HTML and CSS. This function is automatically temporarily disabled if HTML tag pre or code is detected unless it is forced to do.

original_code is required as a string.

full_compression (optional, boolean, default: False). It tells the function whether it should forcefully minify everything in details. This is not recommended for mixed content.

force_op (optional, boolean, default: False) forces the function to minify content unless it is false.

file_type (optional, string, default: None) indicates the type of target content for the better minification.

Returns a string of minified original_code.

yotsuba.lib.tori.render(source, **kwargs)

This function is to render templates. It is currently support Mako template only. It is to support other libraries in the future.

This function can have more than one parameter. There are three optional parameters only used by this function which are described below. The rest are context variables for rendering.

The required parameter source is a string indicating the location of the template. If the optional parameter direct_rendering is false, it will attempt to render the source directly as if it is a template data.

The optional parameter direct_rendering is a boolean acting as a switch to enable or disable the behaviour described above. This option may be set globally in the configuration file. It is set to ‘False’ by default.

The optional parameter text_minification is a boolean acting as a switch to enable or disable auto HTML minification. This option may be set globally in the configuration file. Currently, it doesn’t fully support JavaScript minification. It is set to ‘False’ by default.

yotsuba.lib.tori.setup(configuration_filename=None, use_tori_custom_error_page=False, support_unicode=True, enable_compression=False, auto_config=False, additional_config=None)

Set up the environment

configuration_filename is the name of the configuration file in XML. By default, it is null and instruct the system to use the default settings.

use_tori_custome_error_page is a flag to tell whether the application process should use the nicer custom error page. Please note that enabling this feature will slightly decrease performance. This is a known issue for CherryPy 3.1+.

support_unicode is a flag to tell whether the application process should support unicode. Please note that enabling this feature will slightly decrease performance.

enable_compression is a flag to tell whether the application process should compress the output. Please note that enabling this feature will decrease performance and possibly mess up JavaScript. This feature is disabled by default when the X/HTML document contains pre elements. Use with caution.

auto_config is a flag to tell whether the application process should create a configuration file (named by configuration_filename) automatically if not existed. When the function detect that the configuration file doesn’t exist, with this flag enabled, it will create the configuration file with the default configuration. Then, it will terminated the process. This gives the developers an opportunity to config the settings before proceeding.

additional_config is a configuration dictionary for CherryPy 3.1+. It is for adding some additional configuration directly to CherryPy which is the underlying framework. Please note that this will override any configuration from the configuration file.

yotsuba.lib.tori.start_session()

Start session (CherryPy)

See also

Module yotsuba.lib.kotoba XML Parser with CSS selectors

Previous topic

Simplified HTTP Module

Next topic

Yotsuba 101: Introduction and Tutorials

This Page