Mercurial > getan
changeset 148:f0a2beb17f9b
Add config classes to provide keybindings
The new Config class to be able to configure getan. Until now it is only
possible to configure the Keybinding. The config is read from
$HOME/.getan/getanrc and /etc/getanrc of the first file isn't found. If both
files aren't available a default is used.
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Thu, 06 Dec 2012 12:25:58 +0100 |
parents | 8cc1f83d32ee |
children | ac7c6594fc0e |
files | getan/config.py |
diffstat | 1 files changed, 123 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/getan/config.py Thu Nov 15 13:00:13 2012 +0100 +++ b/getan/config.py Thu Dec 06 12:25:58 2012 +0100 @@ -11,6 +11,8 @@ import logging import os +from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError + logger = None def initialize(level=logging.INFO, filename="getan.log"): @@ -40,3 +42,124 @@ if isinstance(lang, str): os.environ["LANG"] = lang + +class Config(object): + + def __init__(self): + self.config = self.load([os.path.expanduser("~/.getan/getanrc"), + "/etc/getanrc"]) + self.keybinding = Keybinding(self) + + def load(self, filenames): + configparser = SafeConfigParser() + configparser.read(filenames) + return configparser + + def get(self, section, key): + return self.config.get(section, key) + + def items(self, section): + return self.config.items(section) + + def get_keybinding(self): + return self.keybinding + + +class Keybinding(object): + + KEYBINDINGS = "keybindings" + + KEY_SWITCH_TIME_MODE = "switch_time_mode" + KEY_SWITCH_PROJECT_ORDER = "switch_project_order" + KEY_SWITCH_LISTS = "switch_lists" + KEY_ENTER = "enter" + KEY_INSERT = "insert" + KEY_DELETE = "delete" + KEY_ESCAPE = "escape" + KEY_ENTRY_DELETE = "entry_delete" + KEY_ENTRY_UP = "entry_up" + KEY_ENTRY_DOWN = "entry_down" + KEY_ENTRY_MOVE = "entry_move" + KEY_ENTRY_EDIT = "entry_edit" + KEY_ADD_TIME = "add_time" + KEY_SUBTRACT_TIME = "subtract_time" + KEY_PAUSE_PROJECT = "pause_project" + + DEFAULT_KEYBINDINGS = { + KEY_SWITCH_TIME_MODE : "f1", + KEY_SWITCH_PROJECT_ORDER : "f2", + KEY_SWITCH_LISTS : "tab", + KEY_ENTER : "enter", + KEY_INSERT : "insert", + KEY_DELETE : "delete", + KEY_ESCAPE : "esc", + KEY_ENTRY_UP : "up", + KEY_ENTRY_DOWN : "down", + KEY_ENTRY_DELETE : "d", + KEY_ENTRY_MOVE : "m", + KEY_ENTRY_EDIT : "e", + KEY_ADD_TIME : "+", + KEY_SUBTRACT_TIME : "-", + KEY_PAUSE_PROJECT : " ", + } + + + def __init__(self, config): + logger.debug("Keybindings are: %r" % config.items(self.KEYBINDINGS)) + self.config = config + + def get_binding(self, key): + value = None + try: + value = self.config.get(self.KEYBINDINGS, key) + except (NoSectionError, NoOptionError): + pass + + if not value: + value = self.DEFAULT_KEYBINDINGS[key] + return value + + def get_switch_time_mode(self): + return self.get_binding(self.KEY_SWITCH_TIME_MODE) + + def get_switch_project_order(self): + return self.get_binding(self.KEY_SWITCH_PROJECT_ORDER) + + def get_switch_lists(self): + return self.get_binding(self.KEY_SWITCH_LISTS) + + def get_enter(self): + return self.get_binding(self.KEY_ENTER) + + def get_insert(self): + return self.get_binding(self.KEY_INSERT) + + def get_delete(self): + return self.get_binding(self.KEY_DELETE) + + def get_escape(self): + return self.get_binding(self.KEY_ESCAPE) + + def get_entry_delete(self): + return self.get_binding(self.KEY_ENTRY_DELETE) + + def get_entry_move(self): + return self.get_binding(self.KEY_ENTRY_MOVE) + + def get_entry_edit(self): + return self.get_binding(self.KEY_ENTRY_EDIT) + + def get_entry_up(self): + return self.get_binding(self.KEY_ENTRY_UP) + + def get_entry_down(self): + return self.get_binding(self.KEY_ENTRY_DOWN) + + def get_add_time(self): + return self.get_binding(self.KEY_ADD_TIME) + + def get_subtract_time(self): + return self.get_binding(self.KEY_SUBTRACT_TIME) + + def get_pause_project(self): + return self.get_binding(self.KEY_PAUSE_PROJECT)