WSGI Components

Middleware and other components that can be used with other WSGI components.

pail.wsgi.DEFAULT_RESOLUTIONS = '1382, 992, 768, 480'

Default Resolution set used, when no other was given in config.

class pail.wsgi.ImageAdaptingMiddleware(app, global_conf, resolutions='1382, 992, 768, 480')

Bases: object

A WSGI middleware to shrink images on-the-fly.

It works as a filter-wrapper that examines images delivered by other WSGI components and might reduce the image sizes on-the-fly.

a string containing a comma separated list of integers. These number give the supported resolutions. The list does not have to be sorted.
acceptable_types = ['image/jpeg', 'image/png', 'image/gif']

The content types considered as handable. Only HTTP responses providing one of these types are handled by the middleware.

create_resized_image(response, resolution)

Create a resized version of current content image.


Get the client screen resolution from request.


Determine a desired resolution from client screen resolution and avaiilable resolutions.


Is the device that sent request a mobile?


Should we adapt the response from the wrapped app?

should_ignore(request, resolution)

Should the given request be ignored?

pail.wsgi.filter_app(app, global_conf, **kw)

A factory that returns ImageAdaptingMiddleware instances.

pail.wsgi.reg_b = <_sre.SRE_Pattern object at 0x3ec3020>

Regular expressions from http://detectmobilebrowsers.com/ (public domain)

Helper Functions

Minor helper functions.


Get length of file denoted by a file descriptor.

As we cannot get a stat of files of which we only have a file descriptor (and no path or similar), we have to do some tricks to get the file-length anyway.

fd should be the descriptor of a file already open for reading.

Returns the length of file as an integer.

pail.helpers.get_resolution(client_width, pixel_density, resolution_list, is_mobile=True)

Get the desired resolution based on the input parameters.

The screen width of some client device.
By default 1. The amount of physical pixels representing a single CSS pixel. Normally 1 but on retina displays it can be more.
A list of supported screen widths (integers) from the web applications side.
A boolean. Indicates, whether the client device seems to be a mobile device. If so and no client_width is given, then the lowest possible resolution (based on resolution_list) is returned.
pail.helpers.resize(image, resolution=None, client_resolution=None, enlarge=False)

Resize image to have width resolution.

image can be a path or some open file descriptor.

resolution gives the desired maximum width in pixels.

Returns None or a tuple


with <FORMAT> being an image type as determined by PIL and <RESULT_FILE_FD> being an opened temporary file.

If the image is already narrower than resolution, None is returned.

The same applies if the image cannot be read or the resizing operation fails.


Try to turn a string into a list of integers.

Turns strings like ‘1, 2, 3’ into regular lists like [1, 2, 3]. The number have to be comma-separated. Also lists with only one entry are handled correctly.

Project Versions

Table Of Contents

Previous topic

Developers’ Instructions

This Page