1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
26 """
27 Action rules handler
28 """
29
31 self.xyz = xyz
32 self._confs = confs
33 self._actions = []
34
35
36
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
57
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
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
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