Common utilities (bottle_utils.common)

The bottle_utils.common module contains functions and constants that are used in other modules.

This module also contains a few constants and variables that improve code that targets both Python 2.x and Python 3.x. Note, though, that this mostly works in the context of Bottle Utils and hasn’t been tested in too many different versions of Python. If you want a more comprehensive solution, you should look at six.

This module also contains names unicode and basestring, which work as expected in both Python 2.x and Python 3.x.

Module contents

bottle_utils.common.PY3 = False

Whether Python version is 3.x

bottle_utils.common.PY2 = True

Whether Python version is 2.x

bottle_utils.common.to_unicode(v, encoding=u'utf8')[source]

Convert a value to Unicode string (or just string in Py3). This function can be used to ensure string is a unicode string. This may be useful when input can be of different types (but meant to be used when input can be either bytestring or Unicode string), and desired output is always Unicode string.

The encoding argument is used to specify the encoding for bytestrings.

bottle_utils.common.to_bytes(v, encoding=u'utf8')[source]

Convert a value to bytestring (or just string in Py2). This function is useful when desired output is always a bytestring, and input can be any type (although it is intended to be used with strings and bytestrings).

The encoding argument is used to specify the encoding of the resulting bytestring.

bottle_utils.common.attr_escape(attr)[source]

Escape attr string containing HTML attribute value. This function escapes certain characters that are undesirable in HTML attribute values. Functions that construct attribute values using user-supplied data should escape the values using this function.

bottle_utils.common.html_escape(html)[source]

Escape html strning containing HTML. This function escapes characters that are not desirable in HTML markup, when the source string should represent text content only. User-supplied data that should appar in markup should be escaped using this function.

bottle_utils.common.full_url(path=u'/')[source]

Convert a specified path to full URL based on request data. This function uses the current request context information about the request URL to construct a full URL using specified path. In particular it uses bottle.request.urlparts to obtain information about scheme, hostname, and port (if any).

Because it uses the request context, it cannot be called outside a request.

bottle_utils.common.urlquote(s)[source]

Quote (URL-encode) a string with Unicode support. This is a simple wrapper for urllib.quote (or urllib.parse.quote) that converts the input to UTF-8-encoded bytestring before quoting.