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