~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

« back to all changes in this revision

Viewing changes to wxPython/wx/tools/Editra/src/ebmlib/calllock.py

  • Committer: Brian Sidebotham
  • Date: 2013-08-03 14:30:08 UTC
  • Revision ID: brian.sidebotham@gmail.com-20130803143008-c7806tkych1tp6fc
Initial import into Bazaar

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
###############################################################################
 
2
# Name: calllock.py                                                           #
 
3
# Purpose: Manager to lock the context of a function call.                    #
 
4
# Author: Cody Precord <cprecord@editra.org>                                  #
 
5
# Copyright: (c) 2010 Cody Precord <staff@editra.org>                         #
 
6
# Licence: wxWindows Licence                                                  #
 
7
###############################################################################
 
8
 
 
9
"""
 
10
Editra Business Model Library: CallLock
 
11
 
 
12
Provides a Lock class for managing a lock during the duration of a function
 
13
call.
 
14
 
 
15
Example:
 
16
 
 
17
lock = CallLock(DoSomething)
 
18
lock.Lock() # Executes DoSomething
 
19
 
 
20
 
 
21
"""
 
22
 
 
23
__author__ = "Cody Precord <cprecord@editra.org>"
 
24
__svnid__ = "$Id: calllock.py 65794 2010-10-13 14:10:09Z CJP $"
 
25
__revision__ = "$Revision: 65794 $"
 
26
 
 
27
__all__ = [ 'CallLock', 'StaticCallLock', 'LockCall']
 
28
 
 
29
#-----------------------------------------------------------------------------#
 
30
 
 
31
class CallLock(object):
 
32
    """Class to lock a context around a function call"""
 
33
    def __init__(self, callable=None, args=[], kwargs={}):
 
34
        super(CallLock, self).__init__()
 
35
 
 
36
        # Attributes
 
37
        self._locked = False
 
38
        self.funct = callable
 
39
        self.args = args
 
40
        self.kwargs = kwargs
 
41
 
 
42
    def Discard(self):
 
43
        """Clear callable"""
 
44
        assert not self.IsLocked(), "Failed to obtain lock!"
 
45
        self.funct = None
 
46
        self.args = []
 
47
        self.kwargs = {}
 
48
 
 
49
    def IsLocked(self):
 
50
        return self._locked
 
51
 
 
52
    def Lock(self):
 
53
        assert not self.IsLocked(), "Failed to obtain lock!"
 
54
        assert callable(self.funct), "No Callable to Lock!"
 
55
        self._locked = True
 
56
        rval = self.funct(*self.args, **self.kwargs)
 
57
        self._locked = False
 
58
        return rval
 
59
 
 
60
    def SetManagedCall(self, callable, args=[], kwargs={}):
 
61
        """Set the call that will be managed by this lock"""
 
62
        assert not self.IsLocked(), "Failed to obtain lock!"
 
63
        self.funct = callable
 
64
        self.args = args
 
65
        self.kwargs = kwargs
 
66
 
 
67
#-----------------------------------------------------------------------------#
 
68
 
 
69
class StaticCallLock(CallLock):
 
70
    """Provides a static lock around a function call"""
 
71
    _staticlock = False
 
72
 
 
73
    def IsLocked(self):
 
74
        return StaticCallLock._staticlock
 
75
 
 
76
    def Lock(self):
 
77
        """Lock the static class member"""
 
78
        StaticCallLock._staticlock = True
 
79
        super(StaticCallLock, self).Lock()
 
80
        StaticCallLock._staticlock = False
 
81
 
 
82
#-----------------------------------------------------------------------------#
 
83
 
 
84
def LockCall(lock, callable, args=[], kwargs={}):
 
85
    """Convenience function for locking an function call with
 
86
    the provided CallLock object.
 
87
 
 
88
    """
 
89
    if not isinstance(lock, CallLock):
 
90
        raise TypeError("lock is not of type CallLock")
 
91
 
 
92
    lock.SetManagedCall(callable, args, kwargs)
 
93
    rval = lock.Lock()
 
94
    lock.Discard()
 
95
    return rval