Tidy custom_commands plugin up and port it to using our shiny new plugin config API

This commit is contained in:
Chris Jones 2010-01-05 22:29:02 +00:00
parent 7328d9aa4f
commit 8c117ff6e9
1 changed files with 28 additions and 36 deletions

62
terminatorlib/plugins/custom_commands.py Normal file → Executable file
View File

@ -1,50 +1,44 @@
# Terminator by Chris Jones <cmsj@tenshu.net? #!/usr/bin/python
# Terminator by Chris Jones <cmsj@tenshu.net>
# GPL v2 only # GPL v2 only
"""terminal_menu.py - Default plugins for the terminal menu""" """custom_commands.py - Terminator Plugin to add custom command menu entries"""
import sys import sys
import os import os
# only ran when testing plugin
# Fix imports when testing this file directly
if __name__ == '__main__': if __name__ == '__main__':
sys.path.append( os.path.join(os.path.dirname(__file__), "../..")) sys.path.append( os.path.join(os.path.dirname(__file__), "../.."))
import gtk import gtk
import terminatorlib.plugin as plugin import terminatorlib.plugin as plugin
from terminatorlib.config import Config
from terminatorlib.translation import _ from terminatorlib.translation import _
from terminatorlib.util import get_config_dir from terminatorlib.util import get_config_dir
import ConfigParser
(CC_COL_ENABLED, CC_COL_NAME, CC_COL_COMMAND) = range(0,3) (CC_COL_ENABLED, CC_COL_NAME, CC_COL_COMMAND) = range(0,3)
# Every plugin you want Terminator to load *must* be listed in 'available' # Every plugin you want Terminator to load *must* be listed in 'available'
# This is commented out because we don't want any menu item plugins by default
#available = ['MyFirstMenuItem']
available = ['CustomCommandsMenu'] available = ['CustomCommandsMenu']
class MenuItem(plugin.Plugin): class CustomCommandsMenu(plugin.MenuItem):
"""Base class for menu items""" """Add custom commands to the terminal menu"""
capabilities = ['terminal_menu']
def callback(self, menuitems, menu, terminal):
"""Callback to transform the enclosed URL"""
raise NotImplementedError
class CustomCommandsMenu(MenuItem):
"""Simple proof of concept"""
capabilities = ['terminal_menu'] capabilities = ['terminal_menu']
cmd_list = [] cmd_list = []
conf_file = os.path.join(get_config_dir(),"custom_commands") conf_file = os.path.join(get_config_dir(),"custom_commands")
def __init__( self): def __init__( self):
config = ConfigParser.ConfigParser() config = Config()
config.read(self.conf_file) sections = config.plugin_get_config(self.__class__.__name__)
sections = config.sections() if not isinstance(sections, dict):
for s in sections: return
if not (config.has_option(s, "name") and config.has_option(s, "command")): for part in sections:
s = sections[part]
if not (s.has_key("name") and s.has_key("command")):
print "CustomCommandsMenu: Ignoring section %s" % s print "CustomCommandsMenu: Ignoring section %s" % s
continue continue
name = config.get(s, "name") name = s["name"]
command = config.get(s, "command") command = s["command"]
enabled = config.has_option(s, "enabled") and config.getboolean(s, "enabled") or False enabled = s["enabled"] and s["enabled"] or False
self.cmd_list.append( self.cmd_list.append(
{'enabled' : enabled, {'enabled' : enabled,
'name' : name, 'name' : name,
@ -83,7 +77,7 @@ class CustomCommandsMenu(MenuItem):
submenu.append(menuitem) submenu.append(menuitem)
def _save_config(self): def _save_config(self):
config = ConfigParser.ConfigParser() config = Config()
i = 0 i = 0
length = len(self.cmd_list) length = len(self.cmd_list)
while i < length: while i < length:
@ -91,17 +85,15 @@ class CustomCommandsMenu(MenuItem):
name = self.cmd_list[i]['name'] name = self.cmd_list[i]['name']
command = self.cmd_list[i]['command'] command = self.cmd_list[i]['command']
config.add_section(name) item = {}
config.set(name,'enabled', enabled) item['enabled'] = enabled
config.set(name,'name', name) item['name'] = name
config.set(name,'command', command) item['command'] = command
config.plugin_set(self.__class__.__name__, name, item)
config.save()
i = i + 1 i = i + 1
with open(self.conf_file, 'wb') as configfile:
config.write(configfile)
configfile.close()
def _execute(self, widget, data): def _execute(self, widget, data):
command = data['command'] command = data['command']
if command[len(command)-1] != '\n': if command[len(command)-1] != '\n':