helpers

Various helper functions

Module Contents

Classes

BasicAuth() Http basic authentication helper.
ProxyInfo()
MimeType()
AccessLogger(self,logger,log_format=LOG_FORMAT) Helper object to log access.
reify(self,wrapped) Use as a class method decorator. It operates almost exactly like
TimeoutHandle(self,loop,timeout) Timeout handle
TimerNoop()
TimerContext(self,loop) Low resolution timeout context manager
CeilTimeout()
HeadersMixin()
ChainMapProxy(self,maps)

Functions

noop(*args,**kwargs)
strip_auth_from_url(url)
netrc_from_env()
proxies_from_env()
current_task(loop=None)
isasyncgenfunction(obj)
parse_mimetype(mimetype) Parses a MIME type into its components.
guess_filename(obj,default=None)
content_disposition_header(disptype,quote_fields=True,**params) Sets Content-Disposition header.
is_ip_address(host)
rfc822_formatted_time()
_weakref_handle(info)
weakref_handle(ob,name,timeout,loop,ceil_timeout=True)
call_later(cb,timeout,loop)
set_result(fut,result)
set_exception(fut,exc)
noop(*args, **kwargs)
class BasicAuth

Http basic authentication helper.

__new__(login, password="", encoding="latin1")
decode(auth_header, encoding="latin1")

Create a BasicAuth object from an Authorization HTTP header.

from_url(url)

Create BasicAuth from url.

encode()

Encode credentials.

strip_auth_from_url(url)
netrc_from_env()
class ProxyInfo
proxies_from_env()
current_task(loop=None)
isasyncgenfunction(obj)
class MimeType
parse_mimetype(mimetype)

Parses a MIME type into its components.

mimetype is a MIME type string.

Returns a MimeType object.

Example:

>>> parse_mimetype('text/html; charset=utf-8')
MimeType(type='text', subtype='html', suffix='',
         parameters={'charset': 'utf-8'})
guess_filename(obj, default=None)
content_disposition_header(disptype, quote_fields=True, **params)

Sets Content-Disposition header.

disptype is a disposition type: inline, attachment, form-data. Should be valid extension token (see RFC 2183)

params is a dict with disposition params.

class AccessLogger(logger, log_format=LOG_FORMAT)

Helper object to log access.

Usage:
log = logging.getLogger(“spam”) log_format = “%a %{User-Agent}i” access_logger = AccessLogger(log, log_format) access_logger.log(request, response, time)
Format:

%% The percent sign %a Remote IP-address (IP-address of proxy if using reverse proxy) %t Time when the request was started to process %P The process ID of the child that serviced the request %r First line of request %s Response status code %b Size of response in bytes, including HTTP headers %T Time taken to serve the request, in seconds %Tf Time taken to serve the request, in seconds with floating fraction

in .06f format

%D Time taken to serve the request, in microseconds %{FOO}i request.headers[‘FOO’] %{FOO}o response.headers[‘FOO’] %{FOO}e os.environ[‘FOO’]

__init__(logger, log_format=LOG_FORMAT)

Initialise the logger.

logger is a logger object to be used for logging. log_format is an string with apache compatible log format description.

compile_format(log_format)

Translate log_format into form usable by modulo formatting

All known atoms will be replaced with %s Also methods for formatting of those atoms will be added to _methods in appropriate order

For example we have log_format = “%a %t” This format will be translated to “%s %s” Also contents of _methods will be [self._format_a, self._format_t] These method will be called and results will be passed to translated string format.

Each _format_* method receive ‘args’ which is list of arguments given to self.log

Exceptions are _format_e, _format_i and _format_o methods which also receive key name (by functools.partial)

_format_i(request, response, time)
_format_o(request, response, time)
_format_a(response, time)
_format_t(response, time)
_format_P(response, time)
_format_r(response, time)
_format_s(response, time)
_format_b(response, time)
_format_T(response, time)
_format_Tf(response, time)
_format_D(response, time)
_format_line(request, response, time)
log(request, response, time)
class reify(wrapped)

Use as a class method decorator. It operates almost exactly like the Python @property decorator, but it puts the result of the method it decorates into the instance dict after the first call, effectively replacing the function it decorates with an instance variable. It is, in Python parlance, a data descriptor.

__init__(wrapped)
__get__(inst, owner)
__set__(inst, value)
is_ip_address(host)
rfc822_formatted_time()
_weakref_handle(info)
weakref_handle(ob, name, timeout, loop, ceil_timeout=True)
call_later(cb, timeout, loop)
class TimeoutHandle(loop, timeout)

Timeout handle

__init__(loop, timeout)
register(callback, *args, **kwargs)
close()
start()
timer()
__call__()
class TimerNoop
__enter__()
__exit__(exc_type, exc_val, exc_tb)
class TimerContext(loop)

Low resolution timeout context manager

__init__(loop)
__enter__()
__exit__(exc_type, exc_val, exc_tb)
timeout()
class CeilTimeout
__enter__()
class HeadersMixin
_parse_content_type(raw)
content_type()

The value of content part for Content-Type HTTP header.

charset()

The value of charset part for Content-Type HTTP header.

content_length()

The value of Content-Length HTTP header.

set_result(fut, result)
set_exception(fut, exc)
class ChainMapProxy(maps)
__init__(maps)
__init_subclass__()
__getitem__(key)
get(key, default=None)
__len__()
__iter__()
__contains__(key)
__bool__()
__repr__()