Package libxyz :: Package ui :: Module shortcut

[hide private]
[frames] | no frames]

# Source Code for Module libxyz.ui.shortcut

      1  #-*- coding: utf8 -*
      2  #
      3  # Max E. Kuznecov ~syhpoon <mek@mek.uz.ua> 2008-2009
      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  from libxyz.core.utils import ustring
     18  from libxyz.exceptions import XYZValueError
     19
     20  from libxyz.ui import Keys
     21
     22 -class Shortcut(object):
     23      """
     24      Shortcut abstraction
     25      """
     26
     27 -    def __init__(self, sc=None, raw=None):
     28          if (sc is raw is sc is None) or ((sc, raw) == (None, None)):
     29              raise XYZValueError(
     30                  _(u"Only one of the arguments must be provided"))
     31
     32          self._keys = Keys()
     33
     34          if sc:
     35              self.sc = sc
     36              self.raw = self._parse_sc(sc)
     37          elif raw:
     38              self.raw = raw
     39              self.sc = self._parse_raw(raw)
     40
     41      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     42
     43 -    def __hash__(self):
     44          return self._hash(self)
     45
     46      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     47
     48 -    def __eq__(self, other):
     49          return self._hash(self) == self._hash(other)
     50
     51      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     52
     53 -    def __str__(self):
     54          return "<Shortcut: %s>" % str(self.sc)
     55
     56      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     57
     58      __repr__ = __str__
     59
     60      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     61
     62 -    def _hash(self, obj):
     63          return hash(tuple(obj.raw))
     64
     65      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     66
     67 -    def _parse_sc(self, sc):
     68          """
     69          Parse shortcut into raw form
     70          @param sc: List of shortcuts as read from config file
     71          """
     72
     73          _shortcut = []
     74
     75          for s in sc:
     76              _tmp = []
     77
     78              for _key in s.split("-"):
     79                  try:
     80                      _tmp.append(getattr(self._keys, _key))
     81                  except AttributeError:
     82                      _tmp.append(_key)
     83
     84              _shortcut.append(u" ".join([ustring(x) for x in _tmp]))
     85
     86          return _shortcut
     87
     88      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     89
     90 -    def _parse_raw(self, raw):
     91          """
     92          Make shortcut from raw keys received
     93          """
     94
     95          _raw = []
     96
     97          for el in raw:
     98              el = ustring(el)
     99              _data = el
    100
    101              if el in (u"page up", u"page down"):
    102                  _data = self._keys.get_key(raw)
    103              elif len(el) > 1: # Shortcut
    104                  _data = u"-".join([self._keys.get_key(x) or x
    105                                     for x in el.split(u" ")])
    106
    107              _raw.append(_data)
    108
    109          return _raw
    110