deliverance.util – miscellaneous functions

Modules

converters

Converters for boolean and HTML

deliverance.util.converters.asbool(obj)

Converts a string to a boolean

This converts the values “true”, “t”, “yes”, “y”, “on”, and “1” into true, and “false”, “f”, “no”, “n”, “off” and “0” into false. Non-string values are left alone. Other strings are errors.

deliverance.util.converters.html_quote(s)

HTML quotes the string

filetourl

Converts filenames to file: URLs and back again

deliverance.util.filetourl.filename_to_url(filename)

Convert a path to a file: URL. The path will be made absolute.

deliverance.util.filetourl.url_to_filename(url)

Convert a file: URL to a path.

importstring

‘imports’ a string – converts a string to a Python object, importing any necessary modules and evaluating the expression. Everything before the : in an import expression is the module path; everything after is an expression to be evaluated in the namespace of that module.

Alternately, if no : is present, then import the modules and get the attributes as necessary. Arbitrary expressions are not allowed in that case.

deliverance.util.importstring.eval_import(s)

Import a module, or import an object from a module.

A module name like foo.bar:baz() can be used, where foo.bar is the module, and baz() is an expression evaluated in the context of that module. Note this is not safe on arbitrary strings because of the eval.

deliverance.util.importstring.simple_import(s)

Import a module, or import an object from a module.

A name like foo.bar.baz can be a module foo.bar.baz or a module foo.bar with an object baz in it, or a module foo with an object bar with an attribute baz.

deliverance.util.importstring.import_module(s)

Import a module.

deliverance.util.importstring.try_import_module(module_name)

Imports a module, but catches import errors. Only catches errors when that module doesn’t exist; if that module itself has an import error it will still get raised. Returns None if the module doesn’t exist.

nesteddict

Implements NestedDict

class deliverance.util.nesteddict.NestedDict(*dicts)

A dictionary that dispatches to one of its sub-dictionaries, returning whatever the value is for the first dictionary with the key.

uritemplate

A simple implementation of URI templates. Note: this is incomplete!

This only implements simple {var} substitution, not any of the other operations in the URI template (unfinished) spec.

deliverance.util.uritemplate.uri_template_substitute(uri_template, vars)

Does URI template substitution

This only substitutes simple {var}, none of the fancier substitution techniques.

urlnormalize

Normalize URLs

deliverance.util.urlnormalize.url_normalize(url)

Normalizes the quoting of URLs, quoting any characters that should be quoted (but not double-quoting already quoted characters)