add some test coverate, split the defaults into sections and move the borg element to a new base object, converting it to use the new borg __init__ syntax.

This commit is contained in:
Chris Jones 2009-12-22 00:24:53 +00:00
parent 90c8b7e8c5
commit 4a96bf99d4
1 changed files with 186 additions and 130 deletions

View File

@ -17,154 +17,210 @@
"""Terminator by Chris Jones <cmsj@tenshu.net> """Terminator by Chris Jones <cmsj@tenshu.net>
Classes relating to configuratoin""" Classes relating to configuration
>>> config = Config()
>>> config['focus']
'click'
>>> config['focus'] = 'sloppy'
>>>
"""
import platform import platform
from borg import Borg from borg import Borg
DEFAULTS = { DEFAULTS = {
'gt_dir' : '/apps/gnome-terminal', 'global': {
'profile_dir' : '/apps/gnome-terminal/profiles', 'focus' : 'click',
'titlebars' : True, 'enable_real_transparency' : True,
'zoomedtitlebar' : True, 'handle_size' : -1,
'allow_bold' : True, 'geometry_hinting' : True,
'audible_bell' : False, 'fullscreen' : False,
'visible_bell' : True, 'borderless' : False,
'urgent_bell' : False, 'maximise' : False,
'background_color' : '#000000', 'hidden' : False,
'background_darkness' : 0.5, 'tab_position' : 'top',
'background_type' : 'solid', 'close_button_on_tab' : True,
'background_image' : '', 'hide_tabbar' : False,
'backspace_binding' : 'ascii-del', 'scroll_tabbar' : False,
'delete_binding' : 'delete-sequence', 'try_posix_regexp' : platform.system() != 'Linux',
'cursor_blink' : True, },
'cursor_shape' : 'block', 'keybindings': {
'cursor_color' : '', 'zoom_in' : '<Ctrl>plus',
'emulation' : 'xterm', 'zoom_out' : '<Ctrl>minus',
'geometry_hinting' : True, 'zoom_normal' : '<Ctrl>0',
'font' : 'Mono 10', 'new_root_tab' : '<Ctrl><Shift><Alt>T',
'foreground_color' : '#AAAAAA', 'new_tab' : '<Ctrl><Shift>T',
'scrollbar_position' : "right", 'go_next' : ('<Ctrl><Shift>N','<Ctrl>Tab'),
'scroll_background' : True, 'go_prev' : ('<Ctrl><Shift>P','<Ctrl><Shift>Tab'),
'scroll_on_keystroke' : True, 'go_up' : '<Alt>Up',
'scroll_on_output' : True, 'go_down' : '<Alt>Down',
'scrollback_lines' : 500, 'go_left' : '<Alt>Left',
'focus' : 'click', 'go_right' : '<Alt>Right',
'exit_action' : 'close', 'split_horiz' : '<Ctrl><Shift>O',
'palette' : '#000000000000:#CDCD00000000:#0000CDCD0000:\ 'split_vert' : '<Ctrl><Shift>E',
#CDCDCDCD0000:#30BF30BFA38E:#A53C212FA53C:\ 'close_term' : '<Ctrl><Shift>W',
#0000CDCDCDCD:#FAFAEBEBD7D7:#404040404040:\ 'copy' : '<Ctrl><Shift>C',
#FFFF00000000:#0000FFFF0000:#FFFFFFFF0000:\ 'paste' : '<Ctrl><Shift>V',
#00000000FFFF:#FFFF0000FFFF:#0000FFFFFFFF:\ 'toggle_scrollbar' : '<Ctrl><Shift>S',
#FFFFFFFFFFFF', 'search' : '<Ctrl><Shift>F',
'word_chars' : '-A-Za-z0-9,./?%&#:_', 'close_window' : '<Ctrl><Shift>Q',
'mouse_autohide' : True, 'resize_up' : '<Ctrl><Shift>Up',
'update_records' : True, 'resize_down' : '<Ctrl><Shift>Down',
'login_shell' : False, 'resize_left' : '<Ctrl><Shift>Left',
'use_custom_command' : False, 'resize_right' : '<Ctrl><Shift>Right',
'custom_command' : '', 'move_tab_right' : '<Ctrl><Shift>Page_Down',
'use_system_font' : True, 'move_tab_left' : '<Ctrl><Shift>Page_Up',
'use_theme_colors' : False, 'toggle_zoom' : '<Ctrl><Shift>X',
'http_proxy' : '', 'scaled_zoom' : '<Ctrl><Shift>Z',
'ignore_hosts' : ['localhost','127.0.0.0/8','*.local'], 'next_tab' : '<Ctrl>Page_Down',
'encoding' : 'UTF-8', 'prev_tab' : '<Ctrl>Page_Up',
'active_encodings' : ['UTF-8', 'ISO-8859-1'], 'switch_to_tab_1' : None,
'fullscreen' : False, 'switch_to_tab_2' : None,
'borderless' : False, 'switch_to_tab_3' : None,
'maximise' : False, 'switch_to_tab_4' : None,
'hidden' : False, 'switch_to_tab_5' : None,
'handle_size' : -1, 'switch_to_tab_6' : None,
'focus_on_close' : 'auto', 'switch_to_tab_7' : None,
'f11_modifier' : False, 'switch_to_tab_8' : None,
'force_no_bell' : False, 'switch_to_tab_9' : None,
'cycle_term_tab' : True, 'switch_to_tab_10' : None,
'copy_on_selection' : False, 'full_screen' : 'F11',
'close_button_on_tab' : True, 'reset' : '<Ctrl><Shift>R',
'tab_position' : 'top', 'reset_clear' : '<Ctrl><Shift>G',
'enable_real_transparency' : True, 'hide_window' : '<Ctrl><Shift><Alt>a',
'title_tx_txt_color' : '#FFFFFF', 'group_all' : '<Super>g',
'title_tx_bg_color' : '#C80003', 'ungroup_all' : '<Super><Shift>g',
'title_rx_txt_color' : '#FFFFFF', 'group_tab' : '<Super>t',
'title_rx_bg_color' : '#0076C9', 'ungroup_tab' : '<Super><Shift>T',
'title_ia_txt_color' : '#000000', 'new_window' : '<Ctrl><Shift>I',
'title_ia_bg_color' : '#C0BEBF', },
'try_posix_regexp' : platform.system() != 'Linux', 'profiles': {
'hide_tabbar' : False, 'default': {
'scroll_tabbar' : False, 'titlebars' : True,
'alternate_screen_scroll': True, 'zoomedtitlebar' : True,
'split_to_group' : False, 'allow_bold' : True,
'autoclean_groups' : True, 'audible_bell' : False,
'keybindings' : { 'visible_bell' : True,
'zoom_in' : '<Ctrl>plus', 'urgent_bell' : False,
'zoom_out' : '<Ctrl>minus', 'background_color' : '#000000',
'zoom_normal' : '<Ctrl>0', 'background_darkness' : 0.5,
'new_root_tab' : '<Ctrl><Shift><Alt>T', 'background_type' : 'solid',
'new_tab' : '<Ctrl><Shift>T', 'background_image' : '',
'go_next' : ('<Ctrl><Shift>N','<Ctrl>Tab'), 'backspace_binding' : 'ascii-del',
'go_prev' : ('<Ctrl><Shift>P','<Ctrl><Shift>Tab'), 'delete_binding' : 'delete-sequence',
'go_up' : '<Alt>Up', 'cursor_blink' : True,
'go_down' : '<Alt>Down', 'cursor_shape' : 'block',
'go_left' : '<Alt>Left', 'cursor_color' : '',
'go_right' : '<Alt>Right', 'emulation' : 'xterm',
'split_horiz' : '<Ctrl><Shift>O', 'font' : 'Mono 10',
'split_vert' : '<Ctrl><Shift>E', 'foreground_color' : '#AAAAAA',
'close_term' : '<Ctrl><Shift>W', 'scrollbar_position' : "right",
'copy' : '<Ctrl><Shift>C', 'scroll_background' : True,
'paste' : '<Ctrl><Shift>V', 'scroll_on_keystroke' : True,
'toggle_scrollbar' : '<Ctrl><Shift>S', 'scroll_on_output' : True,
'search' : '<Ctrl><Shift>F', 'scrollback_lines' : 500,
'close_window' : '<Ctrl><Shift>Q', 'exit_action' : 'close',
'resize_up' : '<Ctrl><Shift>Up', 'palette' :'#000000000000:#CDCD00000000:#0000CDCD0000:\
'resize_down' : '<Ctrl><Shift>Down', #CDCDCDCD0000:#30BF30BFA38E:#A53C212FA53C:\
'resize_left' : '<Ctrl><Shift>Left', #0000CDCDCDCD:#FAFAEBEBD7D7:#404040404040:\
'resize_right' : '<Ctrl><Shift>Right', #FFFF00000000:#0000FFFF0000:#FFFFFFFF0000:\
'move_tab_right' : '<Ctrl><Shift>Page_Down', #00000000FFFF:#FFFF0000FFFF:#0000FFFFFFFF:\
'move_tab_left' : '<Ctrl><Shift>Page_Up', #FFFFFFFFFFFF',
'toggle_zoom' : '<Ctrl><Shift>X', 'word_chars' : '-A-Za-z0-9,./?%&#:_',
'scaled_zoom' : '<Ctrl><Shift>Z', 'mouse_autohide' : True,
'next_tab' : '<Ctrl>Page_Down', 'update_records' : True,
'prev_tab' : '<Ctrl>Page_Up', 'login_shell' : False,
'switch_to_tab_1' : None, 'use_custom_command' : False,
'switch_to_tab_2' : None, 'custom_command' : '',
'switch_to_tab_3' : None, 'use_system_font' : True,
'switch_to_tab_4' : None, 'use_theme_colors' : False,
'switch_to_tab_5' : None, 'encoding' : 'UTF-8',
'switch_to_tab_6' : None, 'active_encodings' : ['UTF-8', 'ISO-8859-1'],
'switch_to_tab_7' : None, 'focus_on_close' : 'auto',
'switch_to_tab_8' : None, 'force_no_bell' : False,
'switch_to_tab_9' : None, 'cycle_term_tab' : True,
'switch_to_tab_10' : None, 'copy_on_selection' : False,
'full_screen' : 'F11', 'title_tx_txt_color' : '#FFFFFF',
'reset' : '<Ctrl><Shift>R', 'title_tx_bg_color' : '#C80003',
'reset_clear' : '<Ctrl><Shift>G', 'title_rx_txt_color' : '#FFFFFF',
'hide_window' : '<Ctrl><Shift><Alt>a', 'title_rx_bg_color' : '#0076C9',
'group_all' : '<Super>g', 'title_ia_txt_color' : '#000000',
'ungroup_all' : '<Super><Shift>g', 'title_ia_bg_color' : '#C0BEBF',
'group_tab' : '<Super>t', 'alternate_screen_scroll': True,
'ungroup_tab' : '<Super><Shift>T', 'split_to_group' : False,
'new_window' : '<Ctrl><Shift>I', 'autoclean_groups' : True,
} 'http_proxy' : '',
'ignore_hosts' : ['localhost','127.0.0.0/8','*.local'],
},
},
'layout': {
},
'plugins': {
},
} }
class Config(Borg, dict): class Config(object):
"""Class to provide access to our user configuration""" """Class to provide a slightly richer config API above ConfigBase"""
base = None
profile = None
def __init__(self, profile='default'):
self.base = ConfigBase()
self.profile = profile
defaults = None def __getitem__(self, key):
"""Look up a configuration item"""
return(self.base.get_item(key, self.profile))
class ConfigBase(Borg, dict):
"""Class to provide access to our user configuration"""
global_config = None
profiles = None
keybindings = None
plugins = None
def __init__(self): def __init__(self):
"""Class initialiser""" """Class initialiser"""
Borg.__init__(self) Borg.__init__(self, self.__class__.__name__)
dict.__init__(self) dict.__init__(self)
self.prepare_attributes() self.prepare_attributes()
self.load_config()
def prepare_attributes(self): def prepare_attributes(self):
"""Set up our borg environment""" """Set up our borg environment"""
if self.defaults is None: if self.global_config is None:
self.defaults = DEFAULTS self.global_config = DEFAULTS['global']
if self.profiles is None:
self.profiles = {}
self.profiles['default'] = DEFAULTS['profiles']['default']
if self.keybindings is None:
self.keybindings = DEFAULTS['keybindings']
if self.plugins is None:
self.plugins = {}
def __getitem__(self, key): def load_config(self):
"""Load configuration data from our various sources"""
# FIXME: Load our config from wherever and merge it into the defaults
pass
def get_item(self, key, profile='default', plugin=None):
"""Look up a configuration item""" """Look up a configuration item"""
return(self.defaults[key]) if self.global_config.has_key(key):
return(self.global_config[key])
elif self.profiles['default'].has_key(key):
return(self.profiles[profile][key])
elif key == 'keybindings':
return(self.keybindings)
elif plugin is not None and self.plugins[plugin].has_key(key):
return(self.plugins[plugin][key])
else:
raise KeyError('ConfigBase::get_item: unknown key %s' % key)
if __name__ == '__main__':
import doctest
(failed, attempted) = doctest.testmod()
print "%d/%d tests failed" % (failed, attempted)