utils Package

debug Module

Some useful class for request logging.

class thriftpool.utils.debug.RequestLogger(logger, colored)[source]

Bases: object

decorate(signal, sender, fn)[source]
setup()[source]
thriftpool.utils.debug.qualname(obj)[source]

encoding Module

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

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

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

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

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

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

local Module

thriftpool.utils.local

This module implements context-local objects.

copyright:
  1. 2011 by the Werkzeug Team, see AUTHORS for more details.
license:

BSD, see LICENSE for more details.

class thriftpool.utils.local.Local[source]

Bases: object

class thriftpool.utils.local.LocalProxy(local, name=None)[source]

Bases: object

Acts as a proxy for a werkzeug local. Forwards all operations to a proxied object. The only operations not supported for forwarding are right handed operands and any kind of assignment.

Example usage:

from thriftpool.utils.local import Local
l = Local()

# these are proxies
request = l('request')
user = l('user')


from thriftpool.utils.local import LocalStack
_response_local = LocalStack()

# this is a proxy
response = _response_local()

Whenever something is bound to l.user / l.request the proxy objects will forward all operations. If no object is bound a RuntimeError will be raised.

To create proxies to Local or LocalStack objects, call the object as shown above. If you want to have a proxy to an object looked up by a function, you can (as of Werkzeug 0.6.1) pass a function to the LocalProxy constructor:

session = LocalProxy(lambda: get_current_request().session)
class thriftpool.utils.local.LocalStack[source]

Bases: object

This class works similar to a Local but keeps a stack of objects instead. This is best explained with an example:

>>> ls = LocalStack()
>>> ls.push(42)
>>> ls.top
42
>>> ls.push(23)
>>> ls.top
23
>>> ls.pop()
23
>>> ls.top
42

They can be force released by using a LocalManager or with the release_local() function but the correct way is to pop the item from the stack after using. When the stack is empty it will no longer be bound to the current context (and as such released).

By calling the stack without arguments it returns a proxy that resolves to the topmost item on the stack.

pop()[source]

Removes the topmost item from the stack, will return the old value or None if the stack was already empty.

push(obj)[source]

Pushes a new item to the stack

top[source]

The topmost item on the stack. If the stack is empty, None is returned.

thriftpool.utils.local.release_local(local)[source]

Releases the contents of the local for the current context. This makes it possible to use locals without a manager.

Example:

>>> loc = Local()
>>> loc.foo = 42
>>> release_local(loc)
>>> hasattr(loc, 'foo')
False

With this function one can release Local objects as well as StackLocal objects. However it is not possible to release data held by proxies that way, one always has to retain a reference to the underlying local object in order to be able to release it.

logs Module

Some useful tools for logging.

This file was copied and adapted from celery.

copyright:
  1. 2009 - 2012 by Ask Solem.
license:

BSD, see LICENSE for more details.

class thriftpool.utils.logs.ColorFormatter(fmt=None, datefmt=None, use_color=True)[source]

Bases: logging.Formatter

COLORS = {u'blue': <bound method colored.blue of u''>, u'black': <bound method colored.black of u''>, u'yellow': <bound method colored.yellow of u''>, u'cyan': <bound method colored.cyan of u''>, u'green': <bound method colored.green of u''>, u'magenta': <bound method colored.magenta of u''>, u'white': <bound method colored.white of u''>, u'red': <bound method colored.red of u''>}

Loglevel -> Color mapping.

colors = {'DEBUG': <bound method colored.blue of u''>, 'CRITICAL': <bound method colored.magenta of u''>, 'WARNING': <bound method colored.yellow of u''>, 'ERROR': <bound method colored.red of u''>}
format(record)[source]
formatException(ei)[source]
class thriftpool.utils.logs.LoggingProxy(logger, loglevel=None)[source]

Bases: object

Forward file object to logging.Logger instance.

Parameters:
  • logger – The logging.Logger instance to forward to.
  • loglevel – Loglevel to use when writing messages.
close()[source]

When the object is closed, no write requests are forwarded to the logging object anymore.

closed = False
fileno()[source]
flush()[source]

This object is not buffered so any flush() requests are ignored.

isatty()[source]

Always returns False. Just here for file support.

loglevel = 40
mode = 'w'
name = None
write(data)[source]

Write message to logging object.

writelines(sequence)[source]

writelines(sequence_of_strings) -> None.

Write the strings to the file.

The sequence can be any iterable object producing strings. This is equivalent to calling write() for each string.

thriftpool.utils.logs.mlevel(level)[source]

mixin Module

class thriftpool.utils.mixin.LogsMixin[source]

Bases: object

Simple helper for logging.

class thriftpool.utils.mixin.SubclassMixin[source]

Bases: object

subclass_with_self(Class, name=None, attribute='app', reverse=None, **kw)[source]

Subclass an app-compatible class by setting its app attribute to be this app instance.

App-compatible means that the class has a class attribute that provides the default app it should use, e.g. class Foo: app = None.

Parameters:
  • Class – The app-compatible class to subclass.
  • name – Custom name for the target class.
  • attribute – Name of the attribute holding the app, default is ‘app’.
thriftpool.utils.mixin.rgetattr(obj, path)[source]

Get nested attribute from object.

Parameters:
  • obj – object
  • path – path to attribute

platforms Module

class thriftpool.utils.platforms.Daemon(fake=False, excluded_fds=None)[source]

Bases: object

Fork and close needed resources.

close(*args)[source]
open()[source]
preserved_fds[source]
redirect_to_null(fd)[source]
umask = 0
workdir = '/'
exception thriftpool.utils.platforms.LockFailed[source]

Bases: exceptions.Exception

class thriftpool.utils.platforms.PIDLock(path)[source]

Bases: object

Create PID lock and work with it as with resource.

FLAGS = 193
MODE = 420
acquire()[source]

Try to write PID lock.

exists()[source]

Returns True if the PID lock exists.

maybe_remove()[source]

Remove existed PID lock if we can.

static process_exists(pid)[source]

Check that process exists.

read()[source]

Reads and returns the writed PID lock.

release(*args)[source]

Try to remove PID lock.

remove()[source]

Remove PID lock if exists.

write(pid)[source]

Write given PID to lock.

thriftpool.utils.platforms.create_pidlock(pidfile)[source]

Create PID lock, exit if fail.

thriftpool.utils.platforms.daemonize(fake=False, excluded_fds=None)[source]

Try to start daemon.

thriftpool.utils.platforms.fileno(f)[source]
thriftpool.utils.platforms.get_fdmax(default=None)[source]

Returns the maximum number of open file descriptors on this system.

Parameters:default – Value returned if there’s no file descriptor limit.
thriftpool.utils.platforms.ignore_EBADF(*args, **kwds)[source]
thriftpool.utils.platforms.set_process_title(name)[source]

Change process title.

serializers Module

class thriftpool.utils.serializers.StreamSerializer[source]

Bases: object

Helper to pass python objects over streams.

static decode(message)[source]
decode_from_stream(fd, timeout=5)[source]

Read object from given stream and return it.

static encode(obj)[source]
encode_with_length(obj)[source]

Encode object and prepend length to message.

length_format = '!i'

signals Module

structures Module

Some base structures.

This file was copied and adapted from celery.

copyright:
  1. 2009 - 2012 by Ask Solem.
license:

BSD, see LICENSE for more details.

class thriftpool.utils.structures.DependencyGraph(it=None)[source]

Bases: object

A directed acyclic graph of objects and their dependencies.

Supports a robust topological sort to detect the order in which they must be handled.

Takes an optional iterator of (obj, dependencies) tuples to build the graph from.

Warning

Does not support cycle detection.

add_arc(obj)[source]

Add an object to the graph.

add_edge(A, B)[source]

Add an edge from object A to object B (A depends on B).

edges()[source]

Returns generator that yields for all edges in the graph.

items()
iteritems()
repr_node(obj, level=1)[source]
to_dot(fh, ws=' ')[source]

Convert the graph to DOT format.

Parameters:fh – A file, or a file-like object to write the graph to.
topsort()[source]

Sort the graph topologically.

Returns:a list of objects in the order in which they must be handled.
update(it)[source]

Update the graph with data from a list of (obj, dependencies) tuples.

valency_of(obj)[source]

Returns the velency (degree) of a vertex in the graph.

class thriftpool.utils.structures.AttributeDict[source]

Bases: dict, thriftpool.utils.structures.AttributeDictMixin

Dict subclass with attribute access.

class thriftpool.utils.structures.AggregatedView(d)[source]

Bases: thriftpool.utils.structures.AttributeDictMixin

Combine multiple dictionary to one.

add_default(d)[source]
get(key, default=None)[source]
items()[source]
keys()[source]
setdefault(key, default)[source]
update(*args, **kwargs)[source]
values()[source]

term Module

celery.utils.term

Terminals and colors.

class thriftpool.utils.term.colored(*s, **kwargs)[source]

Bases: object

Terminal colored text.

Example::
>>> c = colored(enabled=True)
>>> print(str(c.red('the quick '), c.blue('brown ', c.bold('fox ')),
...       c.magenta(c.underline('jumps over')),
...       c.yellow(' the lazy '),
...       c.green('dog ')))
black(*s)[source]
blue(*s)[source]
bold(*s)[source]
bright(*s)[source]
cyan(*s)[source]
embed()[source]
green(*s)[source]
iblue(*s)[source]
icyan(*s)[source]
igreen(*s)[source]
imagenta(*s)[source]
ired(*s)[source]
iwhite(*s)[source]
iyellow(*s)[source]
magenta(*s)[source]
no_color()[source]
node(s, op)[source]
red(*s)[source]
reset(*s)[source]
reverse(*s)[source]
underline(*s)[source]
white(*s)[source]
yellow(*s)[source]
thriftpool.utils.term.fg(s)
thriftpool.utils.term.isatty(stream)[source]

text Module

thriftpool.utils.text.indent(t, indent=0)[source]

Indent text.