1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import os.path
18
19 import libxyz
20
21 from libxyz.core.plugins import Namespace
22 from libxyz.core.utils import ustring
23 from libxyz.core.utils import is_func
24 from libxyz.core import dsl
25
26 from libxyz.ui import Shortcut
27
28 from libxyz.exceptions import PluginError
29 from libxyz.exceptions import KeyManagerError
30 from libxyz.exceptions import DSLError
31
33 """
34 Key bindings management class
35 """
36
37 CONTEXT_DEFAULT = u"DEFAULT"
38 CONTEXT_SELF = u"@"
39
41 self.xyz = xyz
42 self.confpathes = confpathes
43 self.keys = libxyz.ui.Keys()
44
45 self._loaded_methods = {}
46 self._bind_data = {}
47 self._prefixes = []
48
49 self._path_sel = libxyz.PathSelector()
50
51
52
53 - def process(self, pressed, context=None):
54 """
55 Process pressed keys
56
57 @return: Tuple (method, arguments)
58 """
59
60 context = context or self.CONTEXT_DEFAULT
61 _p = Shortcut(raw=pressed)
62
63
64 if _p in self._prefixes:
65 _aux = self.xyz.input.get()
66 _p = Shortcut(raw=pressed + _aux)
67
68 _method = None
69
70
71 try:
72 _method = self._bind_data[context][_p]
73 except KeyError:
74
75 pass
76
77 return _method
78
79
80
82
83 try:
84 dsl.exec_file(self.confpathes[0])
85 except DSLError, e:
86 raise KeyManagerError(_(u"Error parsing config %s: %s" %
87 (self.confpathes[0], ustring(str(e)))))
88
89
90 if os.path.exists(self.confpathes[1]):
91 try:
92 dsl.exec_file(self.confpathes[1])
93 except DSLError, e:
94 raise KeyManagerError(_(u"Error parsing config %s: %s" %
95 (self.confpathes[1], ustring(str(e)))))
96
97
98
110
111
112
113
114 - def load(self, method):
115 """
116 Load method
117 """
118
119 _p = Namespace(method)
120
121
122 if _p.full in self._loaded_methods:
123 return
124
125
126 if _p.method == _p.ALL:
127 self._loaded_methods[_p.full] = _p.ALL
128 else:
129 self._loaded_methods[_p.full] = self.xyz.pm.from_load(_p.pfull,
130 _p.method)
131
132
133
134 - def bind(self, method, shortcut, context=None):
135 """
136 Bind a shortcut to a method.
137 A method can be either a string, in that case it should denote the
138 plugin method or it can be a function.
139
140 @return: True on success, False otherwise, also raises exception
141 if method was not loaded
142 """
143
144 if isinstance(method, basestring):
145 _p = Namespace(method)
146 _mobj = None
147
148 if context == self.CONTEXT_SELF:
149 context = _p.pfull
150
151
152 if _p.full not in self._loaded_methods:
153 if "%s:%s" % (_p.pfull, _p.ALL) not in self._loaded_methods:
154 raise KeyManagerError(_(u"Method %s not loaded" % _p))
155
156
157 try:
158 _mobj = self.xyz.pm.from_load(_p.pfull, _p.method)
159 except PluginError, e:
160 raise KeyManagerError(_(u"Load error: %s" % e))
161 else:
162 _mobj = self._loaded_methods[_p.full]
163
164 if _mobj is None:
165
166 self.xyz.pm.wait_for(_p, self._bind_wait_cb, _p.method,
167 shortcut, context)
168
169 elif is_func(method):
170 _mobj = method
171 _mobj.ns = _(u"<Internal function object>")
172 else:
173 raise KeyManagerError(_(u"Invalid method type: %s. "\
174 u"Must be string or function") %
175 type(method))
176
177 self._bind(_mobj, shortcut, context)
178
179
180
182 if method not in plugin_obj.public:
183 xyzlog.error(_(u"Unable to bind method %s. "\
184 u"Plugin %s doesn't export it." %
185 (method, plugin_obj.ns.pfull)))
186 return
187
188 self._bind(plugin_obj.public[method], shortcut, context, force=False)
189
190
191
192 - def _bind(self, mobj, shortcut, context=None, force=True):
204
205
206
208 """
209 Return keybindings data
210 """
211
212 return self._bind_data
213