Package plugins :: Package core :: Package keycodes :: Module main
[hide private]
[frames] | no frames]

Source Code for Module plugins.core.keycodes.main

  1  #-*- coding: utf8 -* 
  2  # 
  3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008 
  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 
 18  import cPickle 
 19   
 20  import libxyz.ui as uilib 
 21   
 22  from libxyz.core.plugins import BasePlugin 
 23  from libxyz.core import UserData 
 24  from libxyz.core.utils import ustring 
 25  from libxyz.exceptions import PluginError 
 26  from libxyz.exceptions import XYZRuntimeError 
 27   
28 -class XYZPlugin(BasePlugin):
29 """ 30 Terminal keycodes handling 31 """ 32 33 NAME = u"keycodes" 34 AUTHOR = u"Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name>" 35 VERSION = u"0.1" 36 NAMESPACE = u"core" 37 38 BRIEF_DESCRIPTION = u"Setup terminal keycodes" 39 40 FULL_DESCRIPTION = u"keycodes plugin is used to properly "\ 41 u"configure terminal keycodes.\n"\ 42 u"For each terminal type keycodes are stored "\ 43 u"independently. Terminal type determined by examining "\ 44 u"TERM environment variable." 45 46 HOMEPAGE = u"xyzcmd.syhpoon.name" 47
48 - def __init__(self, xyz):
49 super(XYZPlugin, self).__init__(xyz) 50 51 self.export(self.learn_keys) 52 self.export(self.delete_keys) 53 self.export(self.get_keys) 54 55 self._keysfile = "keycodes" 56 self._keyssubdir = "data" 57 self._terminal = None 58 59 self._ud = UserData() 60 61 self._keys = uilib.Keys() 62 63 self.keys = (("F1", self._keys.F1), 64 ("F2", self._keys.F2), 65 ("F3", self._keys.F3), 66 ("F4", self._keys.F4), 67 ("F5", self._keys.F5), 68 ("F6", self._keys.F6), 69 ("F7", self._keys.F7), 70 ("F8", self._keys.F8), 71 ("F9", self._keys.F9), 72 ("F10", self._keys.F10), 73 ("F11", self._keys.F11), 74 ("F12", self._keys.F12), 75 ("F13", self._keys.F13), 76 ("F14", self._keys.F14), 77 ("F15", self._keys.F15), 78 ("F16", self._keys.F16), 79 ("F17", self._keys.F17), 80 ("F18", self._keys.F18), 81 ("F19", self._keys.F19), 82 ("F20", self._keys.F20), 83 ("BACKSPACE", self._keys.BACKSPACE), 84 ("END", self._keys.END), 85 ("UP", self._keys.UP), 86 ("DOWN", self._keys.DOWN), 87 ("LEFT", self._keys.LEFT), 88 ("RIGHT", self._keys.RIGHT), 89 ("HOME", self._keys.HOME), 90 ("PAGE UP", self._keys.PAGE_UP), 91 ("PAGE DOWN", self._keys.PAGE_DOWN), 92 ("INSERT", self._keys.INSERT), 93 ("TAB", self._keys.TAB), 94 )
95 96 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 97
98 - def prepare(self):
99 self._terminal = os.getenv("TERM") or "DEFAULT"
100 101 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 102
103 - def learn_keys(self):
104 """ 105 Show LearnKeys dialog 106 """ 107 108 _title = _(u"%s - %s" % (self.NAME, self.VERSION)) 109 110 _pressed = self._load_data() 111 112 if self._terminal not in _pressed: 113 _pressed[self._terminal] = {} 114 115 _msg = _(u"Please press key %s\nPress ENTER to skip key\n"\ 116 u"Press ESCAPE to quit dialog") 117 118 for _label, _key in self.keys: 119 _m = _msg % _label 120 _p = uilib.MessageBox(self.xyz, self.xyz.top, _m, _title).show() 121 122 if _p == [] or _p[0] == self._keys.ENTER: 123 continue 124 125 if _p[0] == self._keys.ESCAPE: 126 break 127 128 _cur = _pressed[self._terminal] 129 _tkey = tuple(_p) 130 131 if _p[0] != _key or (_tkey in _cur and tuple(_p[0]) !=_cur[_tkey]): 132 _cur[_tkey] = _key 133 134 _ask_msg = _(u"Save learned keys?") 135 136 if uilib.YesNoBox(self.xyz, self.xyz.top, _ask_msg, _title).show(): 137 # Save data 138 self._save_data(_pressed)
139 140 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 141
142 - def delete_keys(self, all=False):
143 """ 144 Delete learned keycodes data. 145 If all is True, delete all saved data for all terminal types, 146 otherwise delete only current terminal type data. 147 """ 148 149 if all: 150 try: 151 self._ud.delfile(self._keysfile, self._keyssubdir) 152 except XYZRuntimeError, e: 153 pass 154 else: 155 _data = self._load_data() 156 157 if self._terminal in _data: 158 del _data[self._terminal] 159 160 try: 161 self._save_data(_data) 162 except PluginError, e: 163 pass
164 165 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 166
167 - def get_keys(self, all=False):
168 """ 169 Return saved keycodes data as dictionary. 170 If all is True, return all saved data for all terminal types, 171 otherwise return only current terminal type data. 172 """ 173 174 _data = self._load_data() 175 176 if not all: 177 try: 178 _data = _data[self._terminal] 179 except KeyError: 180 _data = {} 181 182 return _data
183 184 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 185
186 - def _save_data(self, data):
187 """ 188 Store learned keycodes 189 """ 190 191 try: 192 _file = self._ud.openfile(self._keysfile, "wb", self._keyssubdir) 193 except XYZRuntimeError, e: 194 raise PluginError(_(u"Unable to open file: %s" % ustring(str(e)))) 195 196 try: 197 cPickle.dump(data, _file) 198 except cPickle.PicklingError: 199 raise PluginError(_(u"Unable to save learned data")) 200 finally: 201 _file.close() 202 203 # Update inputwrapper data to make it available without restarting 204 self.xyz.input.update(data[self._terminal])
205 206 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 207
208 - def _load_data(self):
209 """ 210 Load stored keycodes 211 """ 212 213 _data = {} 214 215 try: 216 _file = self._ud.openfile(self._keysfile, "rb", self._keyssubdir) 217 except XYZRuntimeError, e: 218 # Skip open error 219 pass 220 else: 221 _data = cPickle.load(_file) 222 _file.close() 223 224 return _data
225