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