Skip to content

Commit

Permalink
implement freezing/thawing hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
nphilipp committed Aug 5, 2008
1 parent 1f20de6 commit 4825874
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
5 changes: 4 additions & 1 deletion python-slip.spec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
%{!?python_version: %define python_version %(%{__python} -c "from distutils.sysconfig import get_python_version; print get_python_version()")}

Name: python-slip
Version: 0.1.10
Version: 0.1.11
Release: 1%{?dist}
Summary: Miscellaneous convenience, extension and workaround code for Python

Expand Down Expand Up @@ -85,6 +85,9 @@ rm -rf %buildroot
%{python_sitelib}/slip.gtk-%{version}-py%{python_version}.egg-info

%changelog
* Tue Aug 05 2008 Nils Philippsen <[email protected]> - 0.1.11
- implement freezing/thawing hooks

* Tue Aug 05 2008 Nils Philippsen <[email protected]> - 0.1.10
- implement disabling/enabling hooks

Expand Down
33 changes: 31 additions & 2 deletions slip/util/hookable.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ def _set_hooks_enabled (self, enabled):

hooks_enabled = property (_get_hooks_enabled, _set_hooks_enabled)

def _get_hooks_frozen (self):
if not hasattr (self, "__hooks_frozen__"):
self.__hooks_frozen__ = False
return self.__hooks_frozen__

def _set_hooks_frozen (self, freeze):
if freeze == self.hooks_frozen:
return

self.__hooks_frozen__ = freeze

if freeze:
self.__hooks_frozen_entries__ = set ()
else:
for hookentry in self.__hooks_frozen_entries__:
hookentry.run ()
del self.__hooks_frozen_entries__

hooks_frozen = property (_get_hooks_frozen, _set_hooks_frozen)

def freeze_hooks (self):
self.hooks_frozen = True

def thaw_hooks (self):
self.hooks_frozen = False

def add_hook (self, hook, *args, **kwargs):
assert callable (hook)
hookentry = _HookEntry (hook, args, kwargs)
Expand All @@ -100,8 +126,11 @@ def remove_hook (self, hook, *args, **kwargs):

def _run_hooks (self):
if self.hooks_enabled:
for hookentry in self.__hooks__:
hookentry.run ()
if not self.hooks_frozen:
for hookentry in self.__hooks__:
hookentry.run ()
else:
self.__hooks_frozen_entries__.update (self.__hooks__)

class HookableSet (set, Hookable):
"""A set object which calls registered hooks on changes."""
Expand Down

0 comments on commit 4825874

Please sign in to comment.