예제 #1
0
파일: shortcut.py 프로젝트: me-vlad/xyzcmd
    def __init__(self, sc=None, raw=None):
        if (sc is raw is sc is None) or ((sc, raw) == (None, None)):
            raise XYZValueError(
                _(u"Only one of the arguments must be provided"))

        self._keys = Keys()

        if sc:
            self.sc = sc
            self.raw = self._parse_sc(sc)
        elif raw:
            self.raw = raw
            self.sc = self._parse_raw(raw)

        self.composite = len(self.sc[0]) > 1
예제 #2
0
파일: shortcut.py 프로젝트: me-vlad/xyzcmd
class Shortcut(object):
    """
    Shortcut abstraction
    """

    def __init__(self, sc=None, raw=None):
        if (sc is raw is sc is None) or ((sc, raw) == (None, None)):
            raise XYZValueError(
                _(u"Only one of the arguments must be provided"))

        self._keys = Keys()

        if sc:
            self.sc = sc
            self.raw = self._parse_sc(sc)
        elif raw:
            self.raw = raw
            self.sc = self._parse_raw(raw)

        self.composite = len(self.sc[0]) > 1

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def __hash__(self):
        return self._hash(self)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    def __eq__(self, other):
        return self._hash(self) == self._hash(other)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def __str__(self):
        return "<Shortcut: %s>" % str(self.sc)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def __unicode__(self):
        return ustring(self.__str__())

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    __repr__ = __str__

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def _hash(self, obj):
        return hash(tuple(obj.raw))
    
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def _parse_sc(self, sc):
        """
        Parse shortcut into raw form
        @param sc: List of shortcuts as read from config file
        """

        _shortcut = []

        for s in sc:
            _tmp = []
            
            for _key in s.split("-"):
                try:
                    _tmp.append(getattr(self._keys, _key))
                except AttributeError:
                    _tmp.append(_key)

            _shortcut.append(u" ".join([ustring(x) for x in _tmp]))

        return _shortcut

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def _parse_raw(self, raw):
        """
        Make shortcut from raw keys received
        """

        _raw = []

        for el in raw:
            el = ustring(el)
            _data = el
            
            if el in (u"page up", u"page down"):
                _data = self._keys.get_key(el)
            elif len(el) > 1: # Shortcut
                _data = u"-".join([self._keys.get_key(x) or x
                                   for x in el.split(u" ")])
                
            _raw.append(_data)

        return _raw