2008-03-08 00:04:41 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# TerminatorConfig - layered config classes
|
2010-01-04 23:56:28 +00:00
|
|
|
# Copyright (C) 2006-2010 cmsj@tenshu.net
|
2008-03-08 00:04:41 +00:00
|
|
|
#
|
2009-05-07 01:35:23 +00:00
|
|
|
# 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.
|
2008-03-08 00:04:41 +00:00
|
|
|
#
|
2009-05-07 01:35:23 +00:00
|
|
|
# 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.
|
2008-03-08 00:04:41 +00:00
|
|
|
#
|
2009-05-07 01:35:23 +00:00
|
|
|
# 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
|
2008-03-08 00:04:41 +00:00
|
|
|
|
2009-08-18 11:46:41 +00:00
|
|
|
"""Terminator by Chris Jones <cmsj@tenshu.net>
|
2008-03-08 00:04:41 +00:00
|
|
|
|
2009-12-22 00:24:53 +00:00
|
|
|
Classes relating to configuration
|
|
|
|
|
2009-12-26 01:19:42 +00:00
|
|
|
>>> DEFAULTS['global_config']['focus']
|
|
|
|
'click'
|
2009-12-22 00:24:53 +00:00
|
|
|
>>> config = Config()
|
|
|
|
>>> config['focus'] = 'sloppy'
|
2009-12-23 00:10:57 +00:00
|
|
|
>>> config['focus']
|
|
|
|
'sloppy'
|
2009-12-26 01:19:42 +00:00
|
|
|
>>> DEFAULTS['global_config']['focus']
|
|
|
|
'click'
|
2009-12-23 10:09:53 +00:00
|
|
|
>>> config2 = Config()
|
|
|
|
>>> config2['focus']
|
|
|
|
'sloppy'
|
|
|
|
>>> config2['focus'] = 'click'
|
|
|
|
>>> config2['focus']
|
|
|
|
'click'
|
|
|
|
>>> config['focus']
|
|
|
|
'click'
|
2010-01-08 01:02:49 +00:00
|
|
|
>>> config['geometry_hinting'].__class__.__name__
|
2009-12-28 22:16:47 +00:00
|
|
|
'bool'
|
2010-01-05 21:44:12 +00:00
|
|
|
>>> plugintest = {}
|
|
|
|
>>> plugintest['foo'] = 'bar'
|
|
|
|
>>> config.plugin_set_config('testplugin', plugintest)
|
|
|
|
>>> config.plugin_get_config('testplugin')
|
|
|
|
{'foo': 'bar'}
|
|
|
|
>>> config.plugin_get('testplugin', 'foo')
|
|
|
|
'bar'
|
2010-01-14 23:29:47 +00:00
|
|
|
>>> config.get_profile()
|
|
|
|
'default'
|
|
|
|
>>> config.set_profile('my_first_new_testing_profile')
|
|
|
|
>>> config.get_profile()
|
|
|
|
'my_first_new_testing_profile'
|
|
|
|
>>> config.del_profile('my_first_new_testing_profile')
|
|
|
|
>>> config.get_profile()
|
|
|
|
'default'
|
|
|
|
>>> config.list_profiles().__class__.__name__
|
|
|
|
'list'
|
|
|
|
>>> config.options_set({})
|
|
|
|
>>> config.options_get()
|
|
|
|
{}
|
|
|
|
>>>
|
2009-12-22 00:24:53 +00:00
|
|
|
|
|
|
|
"""
|
2008-03-08 00:04:41 +00:00
|
|
|
|
2009-05-07 01:35:23 +00:00
|
|
|
import platform
|
2009-12-25 21:25:31 +00:00
|
|
|
import os
|
2009-12-26 01:19:42 +00:00
|
|
|
from copy import copy
|
2010-01-10 17:36:55 +00:00
|
|
|
from configobj.configobj import ConfigObj, flatten_errors
|
2010-01-05 00:03:16 +00:00
|
|
|
from configobj.validate import Validator
|
2009-08-18 11:46:41 +00:00
|
|
|
from borg import Borg
|
2009-12-28 22:06:23 +00:00
|
|
|
from util import dbg, err, DEBUG, get_config_dir, dict_diff
|
2008-08-15 03:11:24 +00:00
|
|
|
|
2010-01-21 21:39:05 +00:00
|
|
|
try:
|
|
|
|
import gconf
|
|
|
|
except ImportError:
|
|
|
|
dbg('Unable to import gconf, GNOME defaults unavailable')
|
|
|
|
|
2009-05-07 01:35:23 +00:00
|
|
|
DEFAULTS = {
|
2009-12-26 01:19:42 +00:00
|
|
|
'global_config': {
|
2009-12-22 00:24:53 +00:00
|
|
|
'focus' : 'click',
|
|
|
|
'enable_real_transparency' : True,
|
|
|
|
'handle_size' : -1,
|
|
|
|
'geometry_hinting' : True,
|
2010-01-08 01:02:49 +00:00
|
|
|
'window_state' : 'normal',
|
2009-12-22 00:24:53 +00:00
|
|
|
'borderless' : False,
|
|
|
|
'tab_position' : 'top',
|
|
|
|
'close_button_on_tab' : True,
|
|
|
|
'hide_tabbar' : False,
|
|
|
|
'scroll_tabbar' : False,
|
|
|
|
'try_posix_regexp' : platform.system() != 'Linux',
|
2010-02-08 20:22:08 +00:00
|
|
|
'title_transmit_fg_color' : '#ffffffffffff',
|
|
|
|
'title_transmit_bg_color' : '#c8c800000303',
|
|
|
|
'title_receive_fg_color' : '#ffffffffffff',
|
|
|
|
'title_receive_bg_color' : '#00007676c9c9',
|
|
|
|
'title_inactive_fg_color' : '#000000000000',
|
|
|
|
'title_inactive_bg_color' : '#c0c0bebebfbf',
|
2010-01-06 00:27:58 +00:00
|
|
|
'disabled_plugins' : ['TestPlugin', 'CustomCommandsMenu'],
|
2009-12-22 00:24:53 +00:00
|
|
|
},
|
|
|
|
'keybindings': {
|
2010-01-10 00:20:24 +00:00
|
|
|
'zoom_in' : '<Control>plus',
|
|
|
|
'zoom_out' : '<Control>minus',
|
|
|
|
'zoom_normal' : '<Control>0',
|
|
|
|
'new_tab' : '<Shift><Control>t',
|
2010-01-18 20:39:46 +00:00
|
|
|
'cycle_next' : '<Control>Tab',
|
|
|
|
'cycle_prev' : '<Shift><Control>Tab',
|
|
|
|
'go_next' : '<Shift><Control>n',
|
|
|
|
'go_prev' : '<Shift><Control>p',
|
2009-12-22 00:24:53 +00:00
|
|
|
'go_up' : '<Alt>Up',
|
|
|
|
'go_down' : '<Alt>Down',
|
|
|
|
'go_left' : '<Alt>Left',
|
|
|
|
'go_right' : '<Alt>Right',
|
2010-01-10 00:20:24 +00:00
|
|
|
'split_horiz' : '<Shift><Control>o',
|
|
|
|
'split_vert' : '<Shift><Control>e',
|
|
|
|
'close_term' : '<Shift><Control>w',
|
|
|
|
'copy' : '<Shift><Control>c',
|
|
|
|
'paste' : '<Shift><Control>v',
|
|
|
|
'toggle_scrollbar' : '<Shift><Control>s',
|
|
|
|
'search' : '<Shift><Control>f',
|
|
|
|
'close_window' : '<Shift><Control>q',
|
|
|
|
'resize_up' : '<Shift><Control>Up',
|
|
|
|
'resize_down' : '<Shift><Control>Down',
|
|
|
|
'resize_left' : '<Shift><Control>Left',
|
|
|
|
'resize_right' : '<Shift><Control>Right',
|
|
|
|
'move_tab_right' : '<Shift><Control>Page_Down',
|
|
|
|
'move_tab_left' : '<Shift><Control>Page_Up',
|
|
|
|
'toggle_zoom' : '<Shift><Control>x',
|
|
|
|
'scaled_zoom' : '<Shift><Control>z',
|
|
|
|
'next_tab' : '<Control>Page_Down',
|
|
|
|
'prev_tab' : '<Control>Page_Up',
|
|
|
|
'switch_to_tab_1' : '',
|
|
|
|
'switch_to_tab_2' : '',
|
|
|
|
'switch_to_tab_3' : '',
|
|
|
|
'switch_to_tab_4' : '',
|
|
|
|
'switch_to_tab_5' : '',
|
|
|
|
'switch_to_tab_6' : '',
|
|
|
|
'switch_to_tab_7' : '',
|
|
|
|
'switch_to_tab_8' : '',
|
|
|
|
'switch_to_tab_9' : '',
|
|
|
|
'switch_to_tab_10' : '',
|
2009-12-22 00:24:53 +00:00
|
|
|
'full_screen' : 'F11',
|
2010-01-10 00:20:24 +00:00
|
|
|
'reset' : '<Shift><Control>r',
|
|
|
|
'reset_clear' : '<Shift><Control>g',
|
|
|
|
'hide_window' : '<Shift><Control><Alt>a',
|
2009-12-22 00:24:53 +00:00
|
|
|
'group_all' : '<Super>g',
|
2010-01-10 00:20:24 +00:00
|
|
|
'ungroup_all' : '<Shift><Super>g',
|
2009-12-22 00:24:53 +00:00
|
|
|
'group_tab' : '<Super>t',
|
2010-01-10 00:20:24 +00:00
|
|
|
'ungroup_tab' : '<Shift><Super>t',
|
|
|
|
'new_window' : '<Shift><Control>i',
|
2010-01-29 23:49:16 +00:00
|
|
|
'new_terminator' : '<Super>i',
|
2009-12-22 00:24:53 +00:00
|
|
|
},
|
|
|
|
'profiles': {
|
|
|
|
'default': {
|
|
|
|
'allow_bold' : True,
|
|
|
|
'audible_bell' : False,
|
2010-02-11 13:05:12 +00:00
|
|
|
'visible_bell' : False,
|
2009-12-22 00:24:53 +00:00
|
|
|
'urgent_bell' : False,
|
2010-02-11 13:05:12 +00:00
|
|
|
'icon_bell' : True,
|
2010-01-09 17:42:59 +00:00
|
|
|
'background_color' : '#000000000000',
|
2009-12-22 00:24:53 +00:00
|
|
|
'background_darkness' : 0.5,
|
|
|
|
'background_type' : 'solid',
|
2010-01-09 17:42:59 +00:00
|
|
|
'background_image' : None,
|
2009-12-22 00:24:53 +00:00
|
|
|
'backspace_binding' : 'ascii-del',
|
2010-01-09 17:42:59 +00:00
|
|
|
'delete_binding' : 'escape-sequence',
|
2010-01-08 23:51:45 +00:00
|
|
|
'color_scheme' : 'grey_on_black',
|
2009-12-22 00:24:53 +00:00
|
|
|
'cursor_blink' : True,
|
|
|
|
'cursor_shape' : 'block',
|
2010-02-08 22:10:59 +00:00
|
|
|
'cursor_color' : '#aaaaaaaaaaaa',
|
2009-12-22 00:24:53 +00:00
|
|
|
'emulation' : 'xterm',
|
|
|
|
'font' : 'Mono 10',
|
2010-01-09 17:42:59 +00:00
|
|
|
'foreground_color' : '#aaaaaaaaaaaa',
|
2010-02-09 00:23:21 +00:00
|
|
|
'show_titlebar' : True,
|
2009-12-22 00:24:53 +00:00
|
|
|
'scrollbar_position' : "right",
|
|
|
|
'scroll_background' : True,
|
|
|
|
'scroll_on_keystroke' : True,
|
|
|
|
'scroll_on_output' : True,
|
|
|
|
'scrollback_lines' : 500,
|
|
|
|
'exit_action' : 'close',
|
2010-01-21 21:20:01 +00:00
|
|
|
'palette' :'#000000000000:#cdcd00000000:#0000cdcd0000:\
|
|
|
|
#cdcdcdcd0000:#30bf30bfa38e:#a53c212fa53c:\
|
|
|
|
#0000cdcdcdcd:#fafaebebd7d7:#404040404040:\
|
|
|
|
#ffff00000000:#0000ffff0000:#ffffffff0000:\
|
|
|
|
#00000000ffff:#ffff0000ffff:#0000ffffffff:\
|
|
|
|
#ffffffffffff',
|
2009-12-22 00:24:53 +00:00
|
|
|
'word_chars' : '-A-Za-z0-9,./?%&#:_',
|
|
|
|
'mouse_autohide' : True,
|
|
|
|
'update_records' : True,
|
|
|
|
'login_shell' : False,
|
|
|
|
'use_custom_command' : False,
|
|
|
|
'custom_command' : '',
|
|
|
|
'use_system_font' : True,
|
|
|
|
'use_theme_colors' : False,
|
|
|
|
'encoding' : 'UTF-8',
|
|
|
|
'active_encodings' : ['UTF-8', 'ISO-8859-1'],
|
|
|
|
'focus_on_close' : 'auto',
|
|
|
|
'force_no_bell' : False,
|
|
|
|
'cycle_term_tab' : True,
|
|
|
|
'copy_on_selection' : False,
|
|
|
|
'title_tx_txt_color' : '#FFFFFF',
|
|
|
|
'title_tx_bg_color' : '#C80003',
|
|
|
|
'title_rx_txt_color' : '#FFFFFF',
|
|
|
|
'title_rx_bg_color' : '#0076C9',
|
|
|
|
'title_ia_txt_color' : '#000000',
|
|
|
|
'title_ia_bg_color' : '#C0BEBF',
|
|
|
|
'alternate_screen_scroll': True,
|
|
|
|
'split_to_group' : False,
|
|
|
|
'autoclean_groups' : True,
|
|
|
|
'http_proxy' : '',
|
|
|
|
'ignore_hosts' : ['localhost','127.0.0.0/8','*.local'],
|
|
|
|
},
|
|
|
|
},
|
2009-12-26 19:52:58 +00:00
|
|
|
'layouts': {
|
2010-02-02 00:39:41 +00:00
|
|
|
'default': {
|
|
|
|
'window0': {
|
|
|
|
'type': 'Window',
|
|
|
|
'parent': ''
|
|
|
|
},
|
|
|
|
'child1': {
|
|
|
|
'type': 'Terminal',
|
|
|
|
'parent': 'window0'
|
|
|
|
}
|
|
|
|
}
|
2010-02-01 12:11:44 +00:00
|
|
|
},
|
2009-12-22 00:24:53 +00:00
|
|
|
'plugins': {
|
|
|
|
},
|
2008-08-15 12:40:10 +00:00
|
|
|
}
|
|
|
|
|
2009-12-22 00:24:53 +00:00
|
|
|
class Config(object):
|
|
|
|
"""Class to provide a slightly richer config API above ConfigBase"""
|
|
|
|
base = None
|
|
|
|
profile = None
|
2010-01-21 21:39:05 +00:00
|
|
|
gconf = None
|
2010-02-09 22:33:47 +00:00
|
|
|
system_font = None
|
|
|
|
system_focus = None
|
2009-12-22 00:24:53 +00:00
|
|
|
|
|
|
|
def __init__(self, profile='default'):
|
|
|
|
self.base = ConfigBase()
|
|
|
|
self.profile = profile
|
2008-08-15 12:40:10 +00:00
|
|
|
|
2009-12-22 00:24:53 +00:00
|
|
|
def __getitem__(self, key):
|
|
|
|
"""Look up a configuration item"""
|
|
|
|
return(self.base.get_item(key, self.profile))
|
|
|
|
|
2009-12-23 00:10:57 +00:00
|
|
|
def __setitem__(self, key, value):
|
|
|
|
"""Set a particular configuration item"""
|
|
|
|
return(self.base.set_item(key, value, self.profile))
|
|
|
|
|
2010-01-13 23:02:17 +00:00
|
|
|
def get_profile(self):
|
|
|
|
"""Get our profile"""
|
|
|
|
return(self.profile)
|
|
|
|
|
2009-12-23 00:10:57 +00:00
|
|
|
def set_profile(self, profile):
|
|
|
|
"""Set our profile (which usually means change it)"""
|
2009-12-26 20:09:16 +00:00
|
|
|
dbg('Config::set_profile: Changing profile to %s' % profile)
|
2009-12-23 00:10:57 +00:00
|
|
|
self.profile = profile
|
2009-12-26 19:43:01 +00:00
|
|
|
if not self.base.profiles.has_key(profile):
|
2009-12-26 20:09:16 +00:00
|
|
|
dbg('Config::set_profile: %s does not exist, creating' % profile)
|
2009-12-26 19:43:01 +00:00
|
|
|
self.base.profiles[profile] = copy(DEFAULTS['profiles']['default'])
|
2009-12-23 00:10:57 +00:00
|
|
|
|
2010-01-09 15:51:56 +00:00
|
|
|
def add_profile(self, profile):
|
|
|
|
"""Add a new profile"""
|
|
|
|
return(self.base.add_profile(profile))
|
|
|
|
|
2009-12-26 19:52:58 +00:00
|
|
|
def del_profile(self, profile):
|
|
|
|
"""Delete a profile"""
|
2010-01-14 23:29:47 +00:00
|
|
|
if profile == self.profile:
|
|
|
|
err('Config::del_profile: Deleting in-use profile %s.' % profile)
|
|
|
|
self.set_profile('default')
|
2009-12-26 19:52:58 +00:00
|
|
|
if self.base.profiles.has_key(profile):
|
|
|
|
del(self.base.profiles[profile])
|
|
|
|
|
2010-01-08 01:02:49 +00:00
|
|
|
def rename_profile(self, profile, newname):
|
|
|
|
"""Rename a profile"""
|
|
|
|
if self.base.profiles.has_key(profile):
|
|
|
|
self.base.profiles[newname] = self.base.profiles[profile]
|
|
|
|
del(self.base.profiles[profile])
|
2010-01-10 00:53:27 +00:00
|
|
|
if profile == self.profile:
|
|
|
|
self.profile = newname
|
2010-01-08 01:02:49 +00:00
|
|
|
|
2009-12-26 19:52:58 +00:00
|
|
|
def list_profiles(self):
|
|
|
|
"""List all configured profiles"""
|
|
|
|
return(self.base.profiles.keys())
|
|
|
|
|
2010-02-01 12:11:44 +00:00
|
|
|
def add_layout(self, name, layout):
|
2010-01-30 00:27:57 +00:00
|
|
|
"""Add a new layout"""
|
2010-02-01 12:11:44 +00:00
|
|
|
return(self.base.add_layout(name, layout))
|
2010-01-30 00:27:57 +00:00
|
|
|
|
|
|
|
def del_layout(self, layout):
|
|
|
|
"""Delete a layout"""
|
|
|
|
if self.base.layouts.has_key(layout):
|
|
|
|
del(self.base.layouts[layout])
|
|
|
|
|
|
|
|
def rename_layout(self, layout, newname):
|
|
|
|
"""Rename a layout"""
|
|
|
|
if self.base.layouts.has_key(layout):
|
|
|
|
self.base.layouts[newname] = self.base.layouts[layout]
|
|
|
|
del(self.base.layouts[layout])
|
|
|
|
|
|
|
|
def list_layouts(self):
|
|
|
|
"""List all configured layouts"""
|
|
|
|
return(self.base.layouts.keys())
|
|
|
|
|
2010-01-21 21:39:05 +00:00
|
|
|
def get_system_font(self):
|
|
|
|
"""Look up the system font"""
|
2010-02-09 22:33:47 +00:00
|
|
|
if self.system_font is not None:
|
|
|
|
return(self.system_font)
|
|
|
|
elif 'gconf' not in globals():
|
2010-01-21 21:39:05 +00:00
|
|
|
return
|
2010-02-09 22:33:47 +00:00
|
|
|
else:
|
|
|
|
if self.gconf is None:
|
|
|
|
self.gconf = gconf.client_get_default()
|
2010-01-21 21:39:05 +00:00
|
|
|
|
2010-02-09 22:33:47 +00:00
|
|
|
value = self.gconf.get('/desktop/gnome/interface/monospace_font_name')
|
|
|
|
self.system_font = value.get_string()
|
|
|
|
self.gconf.notify_add('/desktop/gnome/interface/monospace_font_name',
|
|
|
|
self.on_gconf_notify)
|
|
|
|
return(self.system_font)
|
2010-01-21 21:39:05 +00:00
|
|
|
|
|
|
|
def get_system_focus(self):
|
|
|
|
"""Look up the system focus setting"""
|
2010-02-09 22:33:47 +00:00
|
|
|
if self.system_focus is not None:
|
|
|
|
return(self.system_focus)
|
|
|
|
elif 'gconf' not in globals():
|
2010-01-21 21:39:05 +00:00
|
|
|
return
|
2010-02-09 22:33:47 +00:00
|
|
|
else:
|
|
|
|
if self.gconf is None:
|
|
|
|
self.gconf = gconf.client_get_default()
|
2010-01-21 21:39:05 +00:00
|
|
|
|
2010-02-09 22:33:47 +00:00
|
|
|
value = self.gconf.get('/apps/metacity/general/focus_mode')
|
|
|
|
self.system_focus = value.get_string()
|
|
|
|
self.gconf.notify_add('/apps/metacity/general/focus_mode',
|
|
|
|
self.on_gconf_notify)
|
|
|
|
return(self.system_focus)
|
2010-01-21 21:39:05 +00:00
|
|
|
|
2010-02-09 22:33:47 +00:00
|
|
|
def on_gconf_notify(self, client, cnxn_id, entry, what):
|
|
|
|
"""Handle a gconf watch changing"""
|
|
|
|
dbg('GConf notification received. Invalidating caches')
|
|
|
|
self.system_focus = None
|
|
|
|
self.system_font = None
|
2010-01-21 21:39:05 +00:00
|
|
|
|
2009-12-25 21:22:04 +00:00
|
|
|
def save(self):
|
|
|
|
"""Cause ConfigBase to save our config to file"""
|
|
|
|
return(self.base.save())
|
|
|
|
|
2010-01-11 20:56:30 +00:00
|
|
|
def options_set(self, options):
|
|
|
|
"""Set the command line options"""
|
|
|
|
self.base.command_line_options = options
|
|
|
|
|
|
|
|
def options_get(self):
|
|
|
|
"""Get the command line options"""
|
|
|
|
return(self.base.command_line_options)
|
|
|
|
|
2010-01-05 21:44:12 +00:00
|
|
|
def plugin_get(self, pluginname, key):
|
|
|
|
"""Get a plugin config value"""
|
|
|
|
return(self.base.get_item(key, plugin=pluginname))
|
|
|
|
|
|
|
|
def plugin_set(self, pluginname, key, value):
|
|
|
|
"""Set a plugin config value"""
|
|
|
|
return(self.base.set_item(key, value, plugin=pluginname))
|
|
|
|
|
|
|
|
def plugin_get_config(self, plugin):
|
|
|
|
"""Return a whole config tree for a given plugin"""
|
|
|
|
return(self.base.get_plugin(plugin))
|
|
|
|
|
|
|
|
def plugin_set_config(self, plugin, tree):
|
|
|
|
"""Set a whole config tree for a given plugin"""
|
|
|
|
return(self.base.set_plugin(plugin, tree))
|
|
|
|
|
2010-02-01 12:11:44 +00:00
|
|
|
def layout_get_config(self, layout):
|
|
|
|
"""Return a layout"""
|
|
|
|
return(self.base.get_layout(layout))
|
|
|
|
|
|
|
|
def layout_set_config(self, layout, tree):
|
|
|
|
"""Set a layout"""
|
|
|
|
return(self.base.set_layout(layout, tree))
|
|
|
|
|
2009-12-23 00:10:57 +00:00
|
|
|
class ConfigBase(Borg):
|
2009-12-22 00:24:53 +00:00
|
|
|
"""Class to provide access to our user configuration"""
|
2009-12-26 19:43:01 +00:00
|
|
|
loaded = None
|
|
|
|
sections = None
|
2009-12-22 00:24:53 +00:00
|
|
|
global_config = None
|
|
|
|
profiles = None
|
|
|
|
keybindings = None
|
|
|
|
plugins = None
|
2009-12-26 19:43:01 +00:00
|
|
|
layouts = None
|
2010-01-11 20:56:30 +00:00
|
|
|
command_line_options = None
|
2009-11-20 22:46:50 +00:00
|
|
|
|
2009-08-18 11:46:41 +00:00
|
|
|
def __init__(self):
|
|
|
|
"""Class initialiser"""
|
2008-04-07 20:49:10 +00:00
|
|
|
|
2009-12-22 00:24:53 +00:00
|
|
|
Borg.__init__(self, self.__class__.__name__)
|
2008-03-08 00:04:41 +00:00
|
|
|
|
2009-11-20 22:52:26 +00:00
|
|
|
self.prepare_attributes()
|
2009-12-26 13:39:14 +00:00
|
|
|
self.load()
|
2009-11-20 22:52:26 +00:00
|
|
|
|
2009-11-20 22:46:50 +00:00
|
|
|
def prepare_attributes(self):
|
|
|
|
"""Set up our borg environment"""
|
2009-12-26 19:43:01 +00:00
|
|
|
if self.loaded is None:
|
|
|
|
self.loaded = False
|
|
|
|
if self.sections is None:
|
|
|
|
self.sections = ['global_config', 'keybindings', 'profiles',
|
|
|
|
'layouts', 'plugins']
|
2009-12-22 00:24:53 +00:00
|
|
|
if self.global_config is None:
|
2009-12-26 01:19:42 +00:00
|
|
|
self.global_config = copy(DEFAULTS['global_config'])
|
2009-12-22 00:24:53 +00:00
|
|
|
if self.profiles is None:
|
|
|
|
self.profiles = {}
|
2009-12-26 01:19:42 +00:00
|
|
|
self.profiles['default'] = copy(DEFAULTS['profiles']['default'])
|
2009-12-22 00:24:53 +00:00
|
|
|
if self.keybindings is None:
|
2009-12-26 01:19:42 +00:00
|
|
|
self.keybindings = copy(DEFAULTS['keybindings'])
|
2009-12-22 00:24:53 +00:00
|
|
|
if self.plugins is None:
|
|
|
|
self.plugins = {}
|
2009-12-26 19:43:01 +00:00
|
|
|
if self.layouts is None:
|
2010-02-01 12:11:44 +00:00
|
|
|
self.layouts = {}
|
|
|
|
for layout in DEFAULTS['layouts']:
|
2010-02-02 00:39:41 +00:00
|
|
|
self.layouts[layout] = copy(DEFAULTS['layouts'][layout])
|
2009-11-20 22:46:50 +00:00
|
|
|
|
2009-12-28 22:06:23 +00:00
|
|
|
def defaults_to_configspec(self):
|
|
|
|
"""Convert our tree of default values into a ConfigObj validation
|
|
|
|
specification"""
|
|
|
|
configspecdata = {}
|
|
|
|
|
2010-01-22 18:54:36 +00:00
|
|
|
keymap = {
|
|
|
|
'int': 'integer',
|
|
|
|
'str': 'string',
|
|
|
|
'bool': 'boolean',
|
|
|
|
}
|
|
|
|
|
2009-12-28 22:06:23 +00:00
|
|
|
section = {}
|
|
|
|
for key in DEFAULTS['global_config']:
|
|
|
|
keytype = DEFAULTS['global_config'][key].__class__.__name__
|
|
|
|
value = DEFAULTS['global_config'][key]
|
2010-01-22 18:54:36 +00:00
|
|
|
if keytype in keymap:
|
|
|
|
keytype = keymap[keytype]
|
2009-12-28 22:06:23 +00:00
|
|
|
elif keytype == 'list':
|
|
|
|
value = 'list(%s)' % ','.join(value)
|
|
|
|
|
|
|
|
keytype = '%s(default=%s)' % (keytype, value)
|
|
|
|
|
|
|
|
section[key] = keytype
|
|
|
|
configspecdata['global_config'] = section
|
|
|
|
|
|
|
|
section = {}
|
|
|
|
for key in DEFAULTS['keybindings']:
|
|
|
|
value = DEFAULTS['keybindings'][key]
|
2010-01-10 17:36:55 +00:00
|
|
|
if value is None or value == '':
|
2009-12-28 22:06:23 +00:00
|
|
|
continue
|
|
|
|
section[key] = 'string(default=%s)' % value
|
|
|
|
configspecdata['keybindings'] = section
|
|
|
|
|
|
|
|
section = {}
|
|
|
|
for key in DEFAULTS['profiles']['default']:
|
|
|
|
keytype = DEFAULTS['profiles']['default'][key].__class__.__name__
|
|
|
|
value = DEFAULTS['profiles']['default'][key]
|
2010-01-22 18:54:36 +00:00
|
|
|
if keytype in keymap:
|
|
|
|
keytype = keymap[keytype]
|
2009-12-28 22:06:23 +00:00
|
|
|
elif keytype == 'list':
|
|
|
|
value = 'list(%s)' % ','.join(value)
|
2010-01-22 18:54:36 +00:00
|
|
|
if keytype == 'string':
|
|
|
|
value = '"%s"' % value
|
2009-12-28 22:06:23 +00:00
|
|
|
|
|
|
|
keytype = '%s(default=%s)' % (keytype, value)
|
|
|
|
|
|
|
|
section[key] = keytype
|
|
|
|
configspecdata['profiles'] = {}
|
|
|
|
configspecdata['profiles']['__many__'] = section
|
|
|
|
|
2010-02-02 00:39:41 +00:00
|
|
|
section = {}
|
|
|
|
section['type'] = 'string'
|
|
|
|
section['parent'] = 'string'
|
|
|
|
section['profile'] = 'string(default=default)'
|
|
|
|
section['command'] = 'string(default="")'
|
2010-02-27 13:55:38 +00:00
|
|
|
section['position'] = 'string(default="")'
|
2010-02-02 00:39:41 +00:00
|
|
|
configspecdata['layouts'] = {}
|
|
|
|
configspecdata['layouts']['__many__'] = {}
|
|
|
|
configspecdata['layouts']['__many__']['__many__'] = section
|
|
|
|
|
2010-02-06 15:02:16 +00:00
|
|
|
configspecdata['plugins'] = {}
|
|
|
|
|
2009-12-28 22:06:23 +00:00
|
|
|
configspec = ConfigObj(configspecdata)
|
|
|
|
if DEBUG == True:
|
2009-12-29 01:37:32 +00:00
|
|
|
configspec.write(open('/tmp/terminator_configspec_debug.txt', 'w'))
|
2009-12-28 22:06:23 +00:00
|
|
|
return(configspec)
|
|
|
|
|
2009-12-26 13:39:14 +00:00
|
|
|
def load(self):
|
2009-12-22 00:24:53 +00:00
|
|
|
"""Load configuration data from our various sources"""
|
2009-12-26 19:43:01 +00:00
|
|
|
if self.loaded is True:
|
|
|
|
dbg('ConfigBase::load: config already loaded')
|
|
|
|
return
|
|
|
|
|
|
|
|
filename = os.path.join(get_config_dir(), 'epic-config')
|
|
|
|
try:
|
|
|
|
configfile = open(filename, 'r')
|
|
|
|
except Exception, ex:
|
2010-02-08 21:11:38 +00:00
|
|
|
err('ConfigBase::load: Unable to open %s (%s)' % (filename, ex))
|
2009-12-26 19:43:01 +00:00
|
|
|
return
|
|
|
|
|
2010-02-08 21:11:38 +00:00
|
|
|
try:
|
|
|
|
configspec = self.defaults_to_configspec()
|
|
|
|
parser = ConfigObj(configfile, configspec=configspec)
|
|
|
|
validator = Validator()
|
|
|
|
result = parser.validate(validator, preserve_errors=True)
|
|
|
|
except Exception, ex:
|
|
|
|
err('Unable to load configuration: %s' % ex)
|
|
|
|
return
|
2009-12-28 22:06:23 +00:00
|
|
|
|
|
|
|
if result != True:
|
|
|
|
err('ConfigBase::load: config format is not valid')
|
2010-01-22 18:54:36 +00:00
|
|
|
for (section_list, key, _other) in flatten_errors(parser, result):
|
2010-01-10 17:36:55 +00:00
|
|
|
if key is not None:
|
2010-02-02 00:39:41 +00:00
|
|
|
err('[%s]: %s is invalid' % (','.join(section_list), key))
|
2010-01-10 17:36:55 +00:00
|
|
|
else:
|
2010-02-02 00:39:41 +00:00
|
|
|
err('[%s] missing' % ','.join(section_list))
|
|
|
|
else:
|
|
|
|
dbg('config validated successfully')
|
2009-12-28 22:06:23 +00:00
|
|
|
|
2009-12-26 19:43:01 +00:00
|
|
|
for section_name in self.sections:
|
|
|
|
dbg('ConfigBase::load: Processing section: %s' % section_name)
|
2009-12-26 13:39:14 +00:00
|
|
|
section = getattr(self, section_name)
|
2009-12-26 19:43:01 +00:00
|
|
|
if section_name == 'profiles':
|
|
|
|
for profile in parser[section_name]:
|
|
|
|
dbg('ConfigBase::load: Processing profile: %s' % profile)
|
|
|
|
if not section.has_key(section_name):
|
2010-02-02 00:39:41 +00:00
|
|
|
# FIXME: Should this be outside the loop?
|
2009-12-26 19:43:01 +00:00
|
|
|
section[profile] = copy(DEFAULTS['profiles']['default'])
|
|
|
|
section[profile].update(parser[section_name][profile])
|
2010-02-01 12:11:44 +00:00
|
|
|
elif section_name == 'plugins':
|
2010-02-07 12:41:08 +00:00
|
|
|
if not parser.has_key(section_name):
|
|
|
|
continue
|
2009-12-26 19:43:01 +00:00
|
|
|
for part in parser[section_name]:
|
|
|
|
dbg('ConfigBase::load: Processing %s: %s' % (section_name,
|
|
|
|
part))
|
|
|
|
section[part] = parser[section_name][part]
|
2010-02-01 12:11:44 +00:00
|
|
|
elif section_name == 'layouts':
|
2010-02-02 00:39:41 +00:00
|
|
|
for layout in parser[section_name]:
|
2010-02-01 12:11:44 +00:00
|
|
|
dbg('ConfigBase::load: Processing %s: %s' % (section_name,
|
2010-02-02 00:39:41 +00:00
|
|
|
layout))
|
|
|
|
section[layout] = parser[section_name][layout]
|
2009-12-26 19:43:01 +00:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
section.update(parser[section_name])
|
|
|
|
except KeyError, ex:
|
2010-01-22 18:54:36 +00:00
|
|
|
dbg('ConfigBase::load: skipping missing section %s' %
|
2009-12-26 19:43:01 +00:00
|
|
|
section_name)
|
|
|
|
|
|
|
|
self.loaded = True
|
2009-12-26 13:39:14 +00:00
|
|
|
|
|
|
|
def save(self):
|
|
|
|
"""Save the config to a file"""
|
2009-12-26 19:43:01 +00:00
|
|
|
dbg('ConfigBase::save: saving config')
|
2009-12-26 13:39:14 +00:00
|
|
|
parser = ConfigObj()
|
|
|
|
parser.indent_type = ' '
|
2009-12-26 19:43:01 +00:00
|
|
|
|
|
|
|
for section_name in ['global_config', 'keybindings']:
|
|
|
|
dbg('ConfigBase::save: Processing section: %s' % section_name)
|
2009-12-26 13:39:14 +00:00
|
|
|
section = getattr(self, section_name)
|
|
|
|
parser[section_name] = dict_diff(DEFAULTS[section_name], section)
|
|
|
|
|
2009-12-26 19:43:01 +00:00
|
|
|
parser['profiles'] = {}
|
|
|
|
for profile in self.profiles:
|
|
|
|
dbg('ConfigBase::save: Processing profile: %s' % profile)
|
2010-01-22 18:54:36 +00:00
|
|
|
parser['profiles'][profile] = dict_diff(
|
|
|
|
DEFAULTS['profiles']['default'], self.profiles[profile])
|
2009-12-26 19:43:01 +00:00
|
|
|
|
|
|
|
parser['layouts'] = {}
|
|
|
|
for layout in self.layouts:
|
|
|
|
dbg('ConfigBase::save: Processing layout: %s' % layout)
|
2010-02-02 00:39:41 +00:00
|
|
|
parser['layouts'][layout] = self.layouts[layout]
|
2009-12-26 19:43:01 +00:00
|
|
|
|
|
|
|
parser['plugins'] = {}
|
|
|
|
for plugin in self.plugins:
|
|
|
|
dbg('ConfigBase::save: Processing plugin: %s' % plugin)
|
|
|
|
parser['plugins'][plugin] = self.plugins[plugin]
|
|
|
|
|
2010-01-14 22:58:41 +00:00
|
|
|
config_dir = get_config_dir()
|
2010-01-15 23:32:16 +00:00
|
|
|
if not os.path.isdir(config_dir):
|
2010-01-14 22:58:41 +00:00
|
|
|
os.makedirs(config_dir)
|
|
|
|
try:
|
|
|
|
parser.write(open(os.path.join(config_dir, 'epic-config'), 'w'))
|
|
|
|
except Exception, ex:
|
|
|
|
err('ConfigBase::save: Unable to save config: %s' % ex)
|
2009-12-22 00:24:53 +00:00
|
|
|
|
|
|
|
def get_item(self, key, profile='default', plugin=None):
|
2009-08-18 11:46:41 +00:00
|
|
|
"""Look up a configuration item"""
|
2009-12-22 00:24:53 +00:00
|
|
|
if self.global_config.has_key(key):
|
2010-01-10 23:47:15 +00:00
|
|
|
dbg('ConfigBase::get_item: %s found in globals: %s' %
|
|
|
|
(key, self.global_config[key]))
|
2009-12-22 00:24:53 +00:00
|
|
|
return(self.global_config[key])
|
2009-12-23 00:10:57 +00:00
|
|
|
elif self.profiles[profile].has_key(key):
|
2010-01-10 23:47:15 +00:00
|
|
|
dbg('ConfigBase::get_item: %s found in profile %s: %s' % (
|
|
|
|
key, profile, self.profiles[profile][key]))
|
2009-12-22 00:24:53 +00:00
|
|
|
return(self.profiles[profile][key])
|
|
|
|
elif key == 'keybindings':
|
|
|
|
return(self.keybindings)
|
|
|
|
elif plugin is not None and self.plugins[plugin].has_key(key):
|
2010-01-10 23:47:15 +00:00
|
|
|
dbg('ConfigBase::get_item: %s found in plugin %s: %s' % (
|
|
|
|
key, plugin, self.plugins[plugin][key]))
|
2009-12-22 00:24:53 +00:00
|
|
|
return(self.plugins[plugin][key])
|
|
|
|
else:
|
|
|
|
raise KeyError('ConfigBase::get_item: unknown key %s' % key)
|
2008-03-08 00:04:41 +00:00
|
|
|
|
2009-12-23 00:10:57 +00:00
|
|
|
def set_item(self, key, value, profile='default', plugin=None):
|
|
|
|
"""Set a configuration item"""
|
|
|
|
dbg('ConfigBase::set_item: Setting %s=%s (profile=%s, plugin=%s)' %
|
|
|
|
(key, value, profile, plugin))
|
|
|
|
|
|
|
|
if self.global_config.has_key(key):
|
|
|
|
self.global_config[key] = value
|
|
|
|
elif self.profiles[profile].has_key(key):
|
2009-12-26 19:43:01 +00:00
|
|
|
self.profiles[profile][key] = value
|
2009-12-23 00:10:57 +00:00
|
|
|
elif key == 'keybindings':
|
|
|
|
self.keybindings = value
|
2010-01-05 21:44:12 +00:00
|
|
|
elif plugin is not None:
|
2010-01-05 22:15:56 +00:00
|
|
|
if not self.plugins.has_key(plugin):
|
|
|
|
self.plugins[plugin] = {}
|
2009-12-23 00:10:57 +00:00
|
|
|
self.plugins[plugin][key] = value
|
|
|
|
else:
|
|
|
|
raise KeyError('ConfigBase::set_item: unknown key %s' % key)
|
|
|
|
|
|
|
|
return(True)
|
|
|
|
|
2010-01-05 21:44:12 +00:00
|
|
|
def get_plugin(self, plugin):
|
|
|
|
"""Return a whole tree for a plugin"""
|
|
|
|
if self.plugins.has_key(plugin):
|
|
|
|
return(self.plugins[plugin])
|
|
|
|
|
|
|
|
def set_plugin(self, plugin, tree):
|
|
|
|
"""Set a whole tree for a plugin"""
|
|
|
|
self.plugins[plugin] = tree
|
|
|
|
|
2010-01-09 15:51:56 +00:00
|
|
|
def add_profile(self, profile):
|
|
|
|
"""Add a new profile"""
|
|
|
|
if profile in self.profiles:
|
|
|
|
return(False)
|
|
|
|
self.profiles[profile] = copy(DEFAULTS['profiles']['default'])
|
|
|
|
return(True)
|
|
|
|
|
2010-02-01 12:11:44 +00:00
|
|
|
def add_layout(self, name, layout):
|
2010-01-30 00:27:57 +00:00
|
|
|
"""Add a new layout"""
|
2010-02-01 12:11:44 +00:00
|
|
|
if name in self.layouts:
|
2010-01-30 00:27:57 +00:00
|
|
|
return(False)
|
2010-02-01 12:11:44 +00:00
|
|
|
self.layouts[name] = layout
|
2010-01-30 00:27:57 +00:00
|
|
|
return(True)
|
|
|
|
|
2010-02-01 12:11:44 +00:00
|
|
|
def get_layout(self, layout):
|
|
|
|
"""Return a layout"""
|
|
|
|
if self.layouts.has_key(layout):
|
|
|
|
return(self.layouts[layout])
|
|
|
|
else:
|
|
|
|
err('layout does not exist: %s' % layout)
|
|
|
|
|
|
|
|
def set_layout(self, layout, tree):
|
|
|
|
"""Set a layout"""
|
|
|
|
self.layouts[layout] = tree
|
|
|
|
|