utils: Generically useful stuff that doesn’t fit elsewhere

flattened(input, return_types=(<class 'list'>, <class 'tuple'>, <class 'set'>))

Return new flattened version of input

Parameters:input : a sequence instance (list, tuple, or set)
Returns:A sequence of the same type as the input.
html_user_agent(app_dirs)

“Return HTML User-Agent header according to RFC 2068

Parameters:app_dirs : a appdirs.AppDirs instance (session.app_dirs)

Notes

Typical use:

url = "http://www.example.com/example_file"
from urllib.request import URLError, Request
from chimera.core import utils
request = Request(url, unverifiable=True, headers={
    "User-Agent": utils.html_user_agent(session.app_dirs),
})
try:
    utils.retrieve_cached_url(request, filename, session.logger)
except URLError as e:
    from chimera.core.errors import UsereError
    raise UserError(str(e))
retrieve_cached_url(request, filename, logger=None)

Return requested URL in (cached) filename

Parameters:
  • request – a urlib.request.Request
  • filename – where to cache the URL
Returns:

the filename if successful

Raises urllib.request.URLError:
 

if unsuccessful

If the filename already exists, fetch the HTTP headers for the URL and check the last modified date to see if there is a newer version or not. If there isn’t a newer version, return the filename. If there is a newer version, or if the filename does not exist, save the URL in the filename, and set the file’s modified date to the HTTP last modified date, and return the filename.