1
###############################################################################
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
###############################################################################
10
Editra Business Model Library: CallLock
12
Provides a Lock class for managing a lock during the duration of a function
17
lock = CallLock(DoSomething)
18
lock.Lock() # Executes DoSomething
23
__author__ = "Cody Precord <cprecord@editra.org>"
24
__svnid__ = "$Id: calllock.py 65794 2010-10-13 14:10:09Z CJP $"
25
__revision__ = "$Revision: 65794 $"
27
__all__ = [ 'CallLock', 'StaticCallLock', 'LockCall']
29
#-----------------------------------------------------------------------------#
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__()
44
assert not self.IsLocked(), "Failed to obtain lock!"
53
assert not self.IsLocked(), "Failed to obtain lock!"
54
assert callable(self.funct), "No Callable to Lock!"
56
rval = self.funct(*self.args, **self.kwargs)
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!"
67
#-----------------------------------------------------------------------------#
69
class StaticCallLock(CallLock):
70
"""Provides a static lock around a function call"""
74
return StaticCallLock._staticlock
77
"""Lock the static class member"""
78
StaticCallLock._staticlock = True
79
super(StaticCallLock, self).Lock()
80
StaticCallLock._staticlock = False
82
#-----------------------------------------------------------------------------#
84
def LockCall(lock, callable, args=[], kwargs={}):
85
"""Convenience function for locking an function call with
86
the provided CallLock object.
89
if not isinstance(lock, CallLock):
90
raise TypeError("lock is not of type CallLock")
92
lock.SetManagedCall(callable, args, kwargs)