Package libxyz :: Package core :: Module actionmanager |
[hide private]
[frames] | no frames]

# Source Code for Module libxyz.core.actionmanager

     1  #-*- coding: utf8 -*
     2  #
     3  # Max E. Kuznecov ~syhpoon <mek@mek.uz.ua> 2008-2009
     4  #
     5  # This file is part of XYZCommander.
     6  # XYZCommander is free software: you can redistribute it and/or modify
     7  # it under the terms of the GNU Lesser Public License as published by
     8  # the Free Software Foundation, either version 3 of the License, or
     9  # (at your option) any later version.
    10  # XYZCommander is distributed in the hope that it will be useful,
    11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  # GNU Lesser Public License for more details.
    14  # You should have received a copy of the GNU Lesser Public License
    15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>.
    16
    17  from libxyz.core import FSRule
    18  from libxyz.core.utils import ustring
    19  from libxyz.exceptions import XYZRuntimeError
    20
    21 -class ActionManager(object):
    22      """
    23      Action rules handler
    24      """
    25
    26 -    def __init__(self, xyz):
    27          self.xyz = xyz
    28          self._actions = []
    29
    30      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    31
    32 -    def register(self, rule, fn):
    33          """
    34          Register function to be run upon matching rule
    35          @param rule: String FS rule
    36          @param fn: Action function. Function receives matched VFS object as
    37                     its only argument.
    38          """
    39
    40          try:
    41              _rule = FSRule(rule)
    42          except Exception, e:
    43              raise XYZRuntimeError(
    44                  _(u"Unable to register action: invalid rule: %s") %
    45                  ustring(str(e)))
    46
    47          self._actions.insert(0, (_rule, fn))
    48
    49      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    50
    51 -    def match(self, vfsobj):
    52          """
    53          Loop through registered actions and return action assosiated
    54          with the first matched rule. If no rule matched return None
    55          """
    56
    57          for r, f in self._actions:
    58              if r.match(vfsobj):
    59                  return f
    60
    61          return None
    62