2008-03-08 00:04:41 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# TerminatorConfig - layered config classes
|
|
|
|
# Copyright (C) 2006-2008 cmsj@tenshu.net
|
|
|
|
#
|
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'
|
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-25 21:22:04 +00:00
|
|
|
import sys
|
2009-12-26 01:19:42 +00:00
|
|
|
from copy import copy
|
2009-12-25 21:22:04 +00:00
|
|
|
from configobj import ConfigObj
|
2009-08-18 11:46:41 +00:00
|
|
|
from borg import Borg
|
2009-12-26 19:43:01 +00:00
|
|
|
from factory import Factory
|
2009-12-26 01:19:42 +00:00
|
|
|
from util import dbg, get_config_dir, dict_diff
|
2008-08-15 03:11:24 +00:00
|
|
|
|
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,
|
|
|
|
'fullscreen' : False,
|
|
|
|
'borderless' : False,
|
|
|
|
'maximise' : False,
|
|
|
|
'hidden' : False,
|
|
|
|
'tab_position' : 'top',
|
|
|
|
'close_button_on_tab' : True,
|
|
|
|
'hide_tabbar' : False,
|
|
|
|
'scroll_tabbar' : False,
|
|
|
|
'try_posix_regexp' : platform.system() != 'Linux',
|
|
|
|
},
|
|
|
|
'keybindings': {
|
|
|
|
'zoom_in' : '<Ctrl>plus',
|
|
|
|
'zoom_out' : '<Ctrl>minus',
|
|
|
|
'zoom_normal' : '<Ctrl>0',
|
|
|
|
'new_root_tab' : '<Ctrl><Shift><Alt>T',
|
|
|
|
'new_tab' : '<Ctrl><Shift>T',
|
|
|
|
'go_next' : ('<Ctrl><Shift>N','<Ctrl>Tab'),
|
|
|
|
'go_prev' : ('<Ctrl><Shift>P','<Ctrl><Shift>Tab'),
|
|
|
|
'go_up' : '<Alt>Up',
|
|
|
|
'go_down' : '<Alt>Down',
|
|
|
|
'go_left' : '<Alt>Left',
|
|
|
|
'go_right' : '<Alt>Right',
|
|
|
|
'split_horiz' : '<Ctrl><Shift>O',
|
|
|
|
'split_vert' : '<Ctrl><Shift>E',
|
|
|
|
'close_term' : '<Ctrl><Shift>W',
|
|
|
|
'copy' : '<Ctrl><Shift>C',
|
|
|
|
'paste' : '<Ctrl><Shift>V',
|
|
|
|
'toggle_scrollbar' : '<Ctrl><Shift>S',
|
|
|
|
'search' : '<Ctrl><Shift>F',
|
|
|
|
'close_window' : '<Ctrl><Shift>Q',
|
|
|
|
'resize_up' : '<Ctrl><Shift>Up',
|
|
|
|
'resize_down' : '<Ctrl><Shift>Down',
|
|
|
|
'resize_left' : '<Ctrl><Shift>Left',
|
|
|
|
'resize_right' : '<Ctrl><Shift>Right',
|
|
|
|
'move_tab_right' : '<Ctrl><Shift>Page_Down',
|
|
|
|
'move_tab_left' : '<Ctrl><Shift>Page_Up',
|
|
|
|
'toggle_zoom' : '<Ctrl><Shift>X',
|
|
|
|
'scaled_zoom' : '<Ctrl><Shift>Z',
|
|
|
|
'next_tab' : '<Ctrl>Page_Down',
|
|
|
|
'prev_tab' : '<Ctrl>Page_Up',
|
|
|
|
'switch_to_tab_1' : None,
|
|
|
|
'switch_to_tab_2' : None,
|
|
|
|
'switch_to_tab_3' : None,
|
|
|
|
'switch_to_tab_4' : None,
|
|
|
|
'switch_to_tab_5' : None,
|
|
|
|
'switch_to_tab_6' : None,
|
|
|
|
'switch_to_tab_7' : None,
|
|
|
|
'switch_to_tab_8' : None,
|
|
|
|
'switch_to_tab_9' : None,
|
|
|
|
'switch_to_tab_10' : None,
|
|
|
|
'full_screen' : 'F11',
|
|
|
|
'reset' : '<Ctrl><Shift>R',
|
|
|
|
'reset_clear' : '<Ctrl><Shift>G',
|
|
|
|
'hide_window' : '<Ctrl><Shift><Alt>a',
|
|
|
|
'group_all' : '<Super>g',
|
|
|
|
'ungroup_all' : '<Super><Shift>g',
|
|
|
|
'group_tab' : '<Super>t',
|
|
|
|
'ungroup_tab' : '<Super><Shift>T',
|
|
|
|
'new_window' : '<Ctrl><Shift>I',
|
|
|
|
},
|
|
|
|
'profiles': {
|
|
|
|
'default': {
|
|
|
|
'titlebars' : True,
|
|
|
|
'zoomedtitlebar' : True,
|
|
|
|
'allow_bold' : True,
|
|
|
|
'audible_bell' : False,
|
|
|
|
'visible_bell' : True,
|
|
|
|
'urgent_bell' : False,
|
|
|
|
'background_color' : '#000000',
|
|
|
|
'background_darkness' : 0.5,
|
|
|
|
'background_type' : 'solid',
|
|
|
|
'background_image' : '',
|
|
|
|
'backspace_binding' : 'ascii-del',
|
|
|
|
'delete_binding' : 'delete-sequence',
|
|
|
|
'cursor_blink' : True,
|
|
|
|
'cursor_shape' : 'block',
|
|
|
|
'cursor_color' : '',
|
|
|
|
'emulation' : 'xterm',
|
|
|
|
'font' : 'Mono 10',
|
|
|
|
'foreground_color' : '#AAAAAA',
|
|
|
|
'scrollbar_position' : "right",
|
|
|
|
'scroll_background' : True,
|
|
|
|
'scroll_on_keystroke' : True,
|
|
|
|
'scroll_on_output' : True,
|
|
|
|
'scrollback_lines' : 500,
|
|
|
|
'exit_action' : 'close',
|
|
|
|
'palette' :'#000000000000:#CDCD00000000:#0000CDCD0000:\
|
|
|
|
#CDCDCDCD0000:#30BF30BFA38E:#A53C212FA53C:\
|
|
|
|
#0000CDCDCDCD:#FAFAEBEBD7D7:#404040404040:\
|
|
|
|
#FFFF00000000:#0000FFFF0000:#FFFFFFFF0000:\
|
|
|
|
#00000000FFFF:#FFFF0000FFFF:#0000FFFFFFFF:\
|
|
|
|
#FFFFFFFFFFFF',
|
|
|
|
'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': {
|
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
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-12-26 19:52:58 +00:00
|
|
|
def del_profile(self, profile):
|
|
|
|
"""Delete a profile"""
|
|
|
|
if self.base.profiles.has_key(profile):
|
|
|
|
del(self.base.profiles[profile])
|
|
|
|
|
|
|
|
def list_profiles(self):
|
|
|
|
"""List all configured profiles"""
|
|
|
|
return(self.base.profiles.keys())
|
|
|
|
|
2009-12-25 21:22:04 +00:00
|
|
|
def save(self):
|
|
|
|
"""Cause ConfigBase to save our config to file"""
|
|
|
|
return(self.base.save())
|
|
|
|
|
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
|
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:
|
|
|
|
self.layouts = copy(DEFAULTS['layouts'])
|
2009-11-20 22:46:50 +00:00
|
|
|
|
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:
|
|
|
|
dbg('ConfigBase::load: Unable to open %s (%s)' % (filename, ex))
|
|
|
|
return
|
|
|
|
|
2009-12-26 13:39:14 +00:00
|
|
|
parser = ConfigObj(configfile)
|
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):
|
|
|
|
section[profile] = copy(DEFAULTS['profiles']['default'])
|
|
|
|
section[profile].update(parser[section_name][profile])
|
|
|
|
elif section_name == ['layouts', 'plugins']:
|
|
|
|
for part in parser[section_name]:
|
|
|
|
dbg('ConfigBase::load: Processing %s: %s' % (section_name,
|
|
|
|
part))
|
|
|
|
section[part] = parser[section_name][part]
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
section.update(parser[section_name])
|
|
|
|
except KeyError, ex:
|
|
|
|
dbg('ConfigBase::load: skipping loading missing section %s' %
|
|
|
|
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)
|
|
|
|
parser['profiles'][profile] = dict_diff(DEFAULTS['profiles']['default'],
|
|
|
|
self.profiles[profile])
|
|
|
|
|
|
|
|
parser['layouts'] = {}
|
|
|
|
for layout in self.layouts:
|
|
|
|
dbg('ConfigBase::save: Processing layout: %s' % layout)
|
|
|
|
parser['layouts'][layout] = self.layouts[layout]
|
|
|
|
|
|
|
|
parser['plugins'] = {}
|
|
|
|
for plugin in self.plugins:
|
|
|
|
dbg('ConfigBase::save: Processing plugin: %s' % plugin)
|
|
|
|
parser['plugins'][plugin] = self.plugins[plugin]
|
|
|
|
|
2009-12-26 13:39:14 +00:00
|
|
|
parser.write(open(os.path.join(get_config_dir(), 'epic-config'), 'w'))
|
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-26 19:43:01 +00:00
|
|
|
dbg('ConfigBase::get_item: %s:%s' % (profile, key))
|
2009-12-22 00:24:53 +00:00
|
|
|
if self.global_config.has_key(key):
|
2009-12-22 00:32:21 +00:00
|
|
|
dbg('ConfigBase::get_item: found in globals: %s' %
|
|
|
|
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):
|
2009-12-26 19:43:01 +00:00
|
|
|
dbg('ConfigBase::get_item: found in profile %s (%s)' % (
|
|
|
|
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):
|
2009-12-26 19:43:01 +00:00
|
|
|
dbg('ConfigBase::get_item: found in plugin %s (%s)' % (
|
|
|
|
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
|
|
|
|
elif plugin is not None and self.plugins[plugin].has_key(key):
|
|
|
|
self.plugins[plugin][key] = value
|
|
|
|
else:
|
|
|
|
raise KeyError('ConfigBase::set_item: unknown key %s' % key)
|
|
|
|
|
|
|
|
return(True)
|
|
|
|
|
2009-12-22 00:24:53 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import doctest
|
|
|
|
(failed, attempted) = doctest.testmod()
|
|
|
|
print "%d/%d tests failed" % (failed, attempted)
|