~alsuren/shiny/trunk

« back to all changes in this revision

Viewing changes to shiny/shiny.py

  • Committer: David Laban
  • Date: 2009-07-31 13:18:34 UTC
  • Revision ID: alsuren@gmail.com-20090731131834-6kfpmxbm99pbe1le
Some more debugging crap

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 
7
7
"""
8
8
 
9
 
 
10
 
 
11
 
 
 
9
import types
 
10
import sys
 
11
import pdb
 
12
 
 
13
#TODO: split helper functions into a helper-functions module.
 
14
DEBUG=True
 
15
def dprint(*args):
 
16
    """ THIS IS NOT A DECORATOR,
 
17
    like py3k's print function, but controllable like logging. """
 
18
    if DEBUG:
 
19
        for arg in args:
 
20
            print arg,
 
21
        print
 
22
        sys.stdout.flush()
 
23
 
 
24
 
 
25
 
 
26
#TODO: import some memoising decorators
12
27
def only_once(func):
13
28
    """ 
14
29
    >>> import shiny
21
36
    >>>
22
37
    """
23
38
    func.done = False
24
 
    def run(*args, **kwargs):
 
39
    def call(*args, **kwargs):
25
40
        if not func.done:
26
41
            func.done = True
27
42
            func.retval = func(*args, **kwargs)
28
43
        return func.retval
29
44
            
30
 
    return run
31
 
 
32
 
 
 
45
    return call
 
46
 
 
47
#TODO: write guard decorator
 
48
def debug_exceptions(func):
 
49
    """Useful if some calling function is ignoring exceptions you raised 
 
50
    (eg threads, mainloops)"""
 
51
    def call(*args, **kwargs):
 
52
        try:
 
53
            return func(*args, **kwargs)
 
54
        except Exception, e:
 
55
            pdb.post_mortem()
 
56
            
 
57
    return call
 
58
    
 
59
#TODO: write an "only for functions" decorator, to trivialise this.
 
60
def debug_method(func, prefix='', ignore_self=False):
 
61
    if not isinstance(func, (types.FunctionType, types.MethodType)):
 
62
        return func #untouched, as it's not a func
 
63
    else:
 
64
        def call(*args, **kwargs):
 
65
            callstr = (prefix + func.func_name + '(' + ', '.join(
 
66
                        list(map(repr, args[ignore_self:])) + 
 
67
                        [k+'='+repr(v) for k,v in kwargs.items()]
 
68
                    )+')')
 
69
            dprint(' '*debug_method.depth, callstr)
 
70
            debug_method.depth += 1
 
71
            retval = debug_exceptions(func)(*args, **kwargs)
 
72
            debug_method.depth -= 1
 
73
            dprint(' '*debug_method.depth, '->', retval)
 
74
            return retval
 
75
        return call
 
76
debug_method.depth = 0
 
77
 
 
78
def debug_class(cls, prefix='', exclude=['Get']):
 
79
    prefix = prefix + cls.__name__ + '.'
 
80
    for name in dir(cls):
 
81
        if name.strip('_') == name and name not in exclude: #not private or magic
 
82
            attr = getattr(cls, name)
 
83
            attr = debug_method(attr, prefix, True)
 
84
            setattr(cls, name, attr)
 
85
    return cls
 
 
b'\\ No newline at end of file'