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  import os.path 
18   
19  from libxyz.core import FSRule 
20  from libxyz.core import dsl 
21  from libxyz.core.utils import ustring 
22  from libxyz.exceptions import XYZRuntimeError 
23  from libxyz.exceptions import DSLError 
24   
25 -class ActionManager(object):
26 """ 27 Action rules handler 28 """ 29
30 - def __init__(self, xyz, confs):
31 self.xyz = xyz 32 self._confs = confs 33 self._actions = []
34 35 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 36
37 - def register(self, rule, fn):
38 """ 39 Register function to be run upon matching rule 40 @param rule: String FS rule 41 @param fn: Action function. Function receives matched VFS object as 42 its only argument. 43 """ 44 45 try: 46 _rule = FSRule(rule) 47 except Exception, e: 48 raise XYZRuntimeError( 49 _(u"Unable to register action: invalid rule: %s") % 50 ustring(str(e))) 51 52 self._actions.insert(0, (_rule, fn))
53 54 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 55
56 - def parse_configs(self):
57 # First mandatory system keys file 58 try: 59 dsl.exec_file(self._confs[0]) 60 except DSLError, e: 61 raise XYZRuntimeError(_(u"Error parsing config %s: %s" % 62 (self._confs[0], ustring(str(e))))) 63 64 # Next optional user's keys file 65 if os.path.exists(self._confs[1]): 66 try: 67 dsl.exec_file(self._confs[1]) 68 except DSLError, e: 69 raise XYZRuntimeError(_(u"Error parsing config %s: %s" % 70 (self._confs[1], ustring(str(e)))))
71 72 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 73
74 - def match(self, vfsobj):
75 """ 76 Loop through registered actions and return action assosiated 77 with the first matched rule. If no rule matched return None 78 """ 79 80 for r, f in self._actions: 81 if r.match(vfsobj): 82 return f 83 84 return None
85