tea.utils Module

tea.utils.get_object(path='', obj=None)[source]

Returns an object from a dot path.

Path can either be a full path, in which case the get_object function will try to import the modules in the path and follow it to the final object. Or it can be a path relative to the object passed in as the second argument.

Parameters:
  • path (str) – Full or relative dot path to the desired object
  • obj (object) – Starting object. Dot path is calculated relatively to this object.
Returns:

Object at the end of the path, or list of non hidden objects

if we use the star query.

Return type:

object

Example for full paths:

>>> get_object('os.path.join')
<function join at 0x1002d9ed8>
>>> get_object('tea.process')
<module 'tea.process' from 'tea/process/__init__.pyc'>

Example for relative paths when an object is passed in:

>>> import os
>>> get_object('path.join', os)
<function join at 0x1002d9ed8>

Example for a star query. (Star query can be used only as the last element of the path:

>>> get_object('tea.dsa.*')
[]
>>> get_object('tea.dsa.singleton.*')
[<class 'tea.dsa.singleton.Singleton'>,
 <class 'tea.dsa.singleton.SingletonMetaclass'>
 <module 'six' from '...'>]
>>> get_object('tea.dsa.*')
[<module 'tea.dsa.singleton' from '...'>]    # Since we imported it
class tea.utils.Loader[source]

Module loader class loads recursively a module and all it’s submodules.

Loaded modules will be stored in the modules attribute of the loader as a dictionary of {module_path: module} key, value pairs.

Errors accounted during the loading process will not stop the loading process. They will be stored in the errors attribute of the loader as a dictionary of {module_path: exception} key, value pairs.

Usage:

loader = Loader()
loader.load('foo')
loader.load('baz.bar', 'boo')

import baz
loader.load(baz)
load(*modules)[source]

Load one or modre modules.

Parameters:modules – Either a string full path to a module or an actual module object.
tea.utils.load_subclasses(klass, modules=None)[source]

Load recursively all submodules of the modules and return all the subclasses of the provided class

Parameters:
  • klass (str or list of str) – Class whose subclasses we want to load.
  • modules – List of additional modules or module names that should be recursively imported in order to find all the subclasses of the desired class. Default: None
FIXME: This function is kept only for backward compatibility reasons, it
should not be used. Deprecation warning should be raised and it should be replaces by the Loader class.
tea.utils.get_exception()[source]

Returns full formatted traceback as a string.

tea.utils.cmp(x, y)[source]

Compare function from python2

tea.utils.compress.unzip(archive, destination, filenames=None)[source]

Unzip the a complete zip archive into destination directory, or unzip a specific file(s) from the archive.

Usage:
>>> output = os.path.join(os.getcwd(), 'output')
>>> # Archive can be an instance of a ZipFile class
>>> archive = zipfile.ZipFile('test.zip', 'r')
>>> # Or just a filename
>>> archive = 'test.zip'
>>> # Extracts all files
>>> unzip(archive, output)
>>> # Extract only one file
>>> unzip(archive, output, 'my_file.txt')
>>> # Extract a list of files
>>> unzip(archive, output, ['my_file1.txt', 'my_file2.txt'])
>>> unzip_file('test.zip', 'my_file.txt', output)
Parameters:
  • archive (zipfile.ZipFile or str) – Zipfile object to extract from or path to the zip archive.
  • destination (str) – Path to the output directory
  • filenames (str, list or None) – Path(s) to the filename(s) inside the zip archive that you want to extract.
tea.utils.compress.mkzip(archive, items, mode='w', save_full_paths=False)[source]

Recursively zip a directory

Parameters:
  • archive (zipfile.ZipFile or str) – ZipFile object add to or path to the output zip archive.
  • items (str or list) – Single item or list of items (files and directories) to be added to zipfile
  • mode (str) – w for create new and write a for append to
  • save_full_paths (bool) – preserve full paths
tea.utils.compress.seven_zip(archive, items, self_extracting=False)[source]

Create a 7z archive

tea.utils.compress.seven_unzip(archive, output)[source]

Extract a 7z archive

exception tea.utils.crypto.CryptError[source]
tea.utils.crypto.encrypt(data, digest=True)[source]

Performs encryption of provided data.

tea.utils.crypto.decrypt(data, digest=True)[source]

Decrypts provided data.

class tea.utils.daemon.Daemon(pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null')[source]

A generic daemon class.

Usage: subclass the Daemon class and override the run() method

daemonize()[source]

Do the UNIX double-fork magic

See Stevens’ “Advanced Programming in the UNIX Environment” for details (ISBN 0201563177) http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16

start(*args)[source]

Start the daemon

stop()[source]

Stop the daemon

restart(*args)[source]

Restart the daemon

run(*args)[source]

You should override this method when you subclass Daemon.

It will be called after the process has been daemonized by start() or restart().

tea.utils.encoding.smart_text(s, encoding='utf-8', strings_only=False, errors='strict')[source]

Returns a unicode object representing ‘s’. Treats bytes using the ‘encoding’ codec.

If strings_only is True, don’t convert (some) non-string-like objects.

tea.utils.encoding.smart_bytes(s, encoding='utf-8', strings_only=False, errors='strict')[source]

Returns a bytes version of ‘s’, encoded as specified in ‘encoding’.

If strings_only is True, don’t convert (some) non-string-like objects.