Package libxyz :: Package core :: Module hookmanager

[hide private]
[frames] | no frames]

# Source Code for Module libxyz.core.hookmanager

     1  #-*- coding: utf8 -*
     2  #
     3  # Max E. Kuznecov ~syhpoon <mek@mek.uz.ua> 2008
     4  #
     5
     6  # This file is part of XYZCommander.
     7  # XYZCommander is free software: you can redistribute it and/or modify
     8  # it under the terms of the GNU Lesser Public License as published by
     9  # the Free Software Foundation, either version 3 of the License, or
    10  # (at your option) any later version.
    11  # XYZCommander is distributed in the hope that it will be useful,
    12  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    14  # GNU Lesser Public License for more details.
    15  # You should have received a copy of the GNU Lesser Public License
    16  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>.
    17
    18  import traceback
    19
    20  from libxyz.core.utils import ustring
    21
    22 -class HookManager(object):
    23      """
    24      Hooks dispatcher
    25      """
    26
    27 -    def __init__(self):
    28          self._data = {}
    29
    30      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    31
    32 -    def register(self, event, proc):
    33          """
    34          Register proc to be run upon event occured
    35          """
    36
    37          if event not in self._data:
    38              self._data[event] = []
    39
    40          if not callable(proc):
    41              xyzlog.error(_(u"HookManager: Callable argument expected"))
    42              return False
    43
    44          self._data[event].append(proc)
    45
    46          return True
    47
    48      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    49
    50 -    def clear(self, event):
    51          """
    52          Clear all data assosiated with an event
    53          """
    54
    55          self._data[event] = []
    56
    57      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    58
    59 -    def dispatch(self, event, *args):
    60          """
    61          Sequentially run procedures registered with provided event
    62          """
    63
    64          # No callbacks registered
    65          if event not in self._data or not self._data[event]:
    66              return False
    67
    68          for proc in self._data[event]:
    69              try:
    70                  proc(*args)
    71              except Exception, e:
    72                  xyzlog.error(
    73                      _(u"Error running callback procedure for event %s") %
    74                      ustring(str(e)))
    75
    76                  xyzlog.debug(ustring(traceback.format_exc()))
    77
    78                  return False
    79
    80          return True
    81