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