Tidy up keybindings.py for pylint ease
This commit is contained in:
parent
b7661b005b
commit
1a88613904
|
@ -1,9 +1,33 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# Terminator - multiple gnome terminals in one window
|
||||||
|
# Copyright (C) 2006-2008 cmsj@tenshu.net
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, version 2 only.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program; if not, write to the Free Software
|
||||||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
|
||||||
import re, gtk
|
"""Terminator by Chris Jones <cmsj@tenshu.net>
|
||||||
import terminatorlib.config
|
|
||||||
|
Validator and functions for dealing with Terminator's customisable
|
||||||
|
keyboard shortcuts.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
import gtk
|
||||||
from terminatorlib.config import err
|
from terminatorlib.config import err
|
||||||
|
|
||||||
class KeymapError(Exception):
|
class KeymapError(Exception):
|
||||||
|
"""Custom exception for errors in keybinding configurations"""
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
Exception.__init__(self, value)
|
Exception.__init__(self, value)
|
||||||
self.value = value
|
self.value = value
|
||||||
|
@ -12,8 +36,9 @@ class KeymapError(Exception):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Keybinding '%s' invalid: %s" % (self.action, self.value)
|
return "Keybinding '%s' invalid: %s" % (self.action, self.value)
|
||||||
|
|
||||||
Modifier = re.compile('<([^<]+)>')
|
MODIFIER = re.compile('<([^<]+)>')
|
||||||
class TerminatorKeybindings:
|
class TerminatorKeybindings:
|
||||||
|
"""Class to handle loading and lookup of Terminator keybindings"""
|
||||||
|
|
||||||
modifiers = {
|
modifiers = {
|
||||||
'ctrl': gtk.gdk.CONTROL_MASK,
|
'ctrl': gtk.gdk.CONTROL_MASK,
|
||||||
|
@ -23,16 +48,21 @@ class TerminatorKeybindings:
|
||||||
}
|
}
|
||||||
|
|
||||||
empty = {}
|
empty = {}
|
||||||
|
keys = None
|
||||||
|
_masks = None
|
||||||
|
_lookup = None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.keymap = gtk.gdk.keymap_get_default()
|
self.keymap = gtk.gdk.keymap_get_default()
|
||||||
self.configure({})
|
self.configure({})
|
||||||
|
|
||||||
def configure(self, bindings):
|
def configure(self, bindings):
|
||||||
|
"""Accept new bindings and reconfigure with them"""
|
||||||
self.keys = bindings
|
self.keys = bindings
|
||||||
self.reload()
|
self.reload()
|
||||||
|
|
||||||
def reload(self):
|
def reload(self):
|
||||||
|
"""Parse bindings and mangle into an appropriate form"""
|
||||||
self._lookup = {}
|
self._lookup = {}
|
||||||
self._masks = 0
|
self._masks = 0
|
||||||
for action, bindings in self.keys.items():
|
for action, bindings in self.keys.items():
|
||||||
|
@ -47,9 +77,9 @@ class TerminatorKeybindings:
|
||||||
keyval, mask = self._parsebinding(binding)
|
keyval, mask = self._parsebinding(binding)
|
||||||
# Does much the same, but with poorer error handling.
|
# Does much the same, but with poorer error handling.
|
||||||
#keyval, mask = gtk.accelerator_parse(binding)
|
#keyval, mask = gtk.accelerator_parse(binding)
|
||||||
except KeymapError, e:
|
except KeymapError, ex:
|
||||||
e.action = action
|
ex.action = action
|
||||||
raise e
|
raise ex
|
||||||
else:
|
else:
|
||||||
if mask & gtk.gdk.SHIFT_MASK:
|
if mask & gtk.gdk.SHIFT_MASK:
|
||||||
if keyval == gtk.keysyms.Tab:
|
if keyval == gtk.keysyms.Tab:
|
||||||
|
@ -67,12 +97,13 @@ class TerminatorKeybindings:
|
||||||
self._masks |= mask
|
self._masks |= mask
|
||||||
|
|
||||||
def _parsebinding(self, binding):
|
def _parsebinding(self, binding):
|
||||||
|
"""Parse an individual binding using gtk's binding function"""
|
||||||
mask = 0
|
mask = 0
|
||||||
modifiers = re.findall(Modifier, binding)
|
modifiers = re.findall(MODIFIER, binding)
|
||||||
if modifiers:
|
if modifiers:
|
||||||
for modifier in modifiers:
|
for modifier in modifiers:
|
||||||
mask |= self._lookup_modifier(modifier)
|
mask |= self._lookup_modifier(modifier)
|
||||||
key = re.sub(Modifier, '', binding)
|
key = re.sub(MODIFIER, '', binding)
|
||||||
if key == '':
|
if key == '':
|
||||||
raise KeymapError('No key found')
|
raise KeymapError('No key found')
|
||||||
keyval = gtk.gdk.keyval_from_name(key)
|
keyval = gtk.gdk.keyval_from_name(key)
|
||||||
|
@ -81,16 +112,22 @@ class TerminatorKeybindings:
|
||||||
return (keyval, mask)
|
return (keyval, mask)
|
||||||
|
|
||||||
def _lookup_modifier(self, modifier):
|
def _lookup_modifier(self, modifier):
|
||||||
|
"""Map modifier names to gtk values"""
|
||||||
try:
|
try:
|
||||||
return self.modifiers[modifier.lower()]
|
return self.modifiers[modifier.lower()]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise KeymapError("Unhandled modifier '<%s>'" % modifier)
|
raise KeymapError("Unhandled modifier '<%s>'" % modifier)
|
||||||
|
|
||||||
def lookup(self, event):
|
def lookup(self, event):
|
||||||
|
"""Translate a keyboard event into a mapped key"""
|
||||||
try:
|
try:
|
||||||
keyval, egroup, level, consumed = self.keymap.translate_keyboard_state(event.hardware_keycode, event.state & ~gtk.gdk.LOCK_MASK, event.group)
|
keyval, egroup, level, consumed = self.keymap.translate_keyboard_state(
|
||||||
|
event.hardware_keycode,
|
||||||
|
event.state & ~gtk.gdk.LOCK_MASK,
|
||||||
|
event.group)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
err ("keybindings.lookup failed to translate keyboard event: %s"%dir(event))
|
err ("keybindings.lookup failed to translate keyboard event: %s" %
|
||||||
|
dir(event))
|
||||||
return None
|
return None
|
||||||
mask = (event.state & ~consumed) & self._masks
|
mask = (event.state & ~consumed) & self._masks
|
||||||
return self._lookup.get(mask, self.empty).get(keyval, None)
|
return self._lookup.get(mask, self.empty).get(keyval, None)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
from terminatorlib.config import dbg,err,Defaults,TerminatorConfValuestoreRC
|
from terminatorlib.config import dbg,err,Defaults,TerminatorConfValuestoreRC
|
||||||
from terminatorlib.keybindings import TerminatorKeybindings,Modifier
|
from terminatorlib.keybindings import TerminatorKeybindings
|
||||||
from terminatorlib.version import APP_NAME, APP_VERSION
|
from terminatorlib.version import APP_NAME, APP_VERSION
|
||||||
from terminatorlib import translation
|
from terminatorlib import translation
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue