2009-12-17 01:07:01 +00:00
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
2009-12-17 12:54:42 +00:00
|
|
|
"""plugin.py - Base plugin system
|
2009-12-19 15:07:22 +00:00
|
|
|
Inspired by Armin Ronacher's post at
|
|
|
|
http://lucumr.pocoo.org/2006/7/3/python-plugin-system
|
|
|
|
Used with permission (the code in that post is to be
|
|
|
|
considered BSD licenced, per the authors wishes)
|
2009-12-17 12:54:42 +00:00
|
|
|
|
|
|
|
>>> registry = PluginRegistry()
|
2020-05-01 14:36:16 +00:00
|
|
|
>>> isinstance(registry.instances, dict)
|
|
|
|
True
|
|
|
|
>>> registry.enable('TestPlugin')
|
|
|
|
>>> registry.load_plugins()
|
2009-12-17 13:51:55 +00:00
|
|
|
>>> plugins = registry.get_plugins_by_capability('test')
|
|
|
|
>>> len(plugins)
|
|
|
|
1
|
|
|
|
>>> plugins[0] #doctest: +ELLIPSIS
|
|
|
|
<testplugin.TestPlugin object at 0x...>
|
2009-12-17 12:54:42 +00:00
|
|
|
>>> registry.get_plugins_by_capability('this_should_not_ever_exist')
|
|
|
|
[]
|
2009-12-17 13:51:55 +00:00
|
|
|
>>> plugins[0].do_test()
|
|
|
|
'TestPluginWin'
|
2009-12-17 12:54:42 +00:00
|
|
|
|
|
|
|
"""
|
2009-12-17 01:07:01 +00:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
2018-04-24 18:22:10 +00:00
|
|
|
from . import borg
|
|
|
|
from .config import Config
|
|
|
|
from .util import dbg, err, get_config_dir
|
|
|
|
from .terminator import Terminator
|
2009-12-17 01:07:01 +00:00
|
|
|
|
|
|
|
class Plugin(object):
|
|
|
|
"""Definition of our base plugin class"""
|
|
|
|
capabilities = None
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Class initialiser."""
|
|
|
|
pass
|
|
|
|
|
2010-06-22 23:48:06 +00:00
|
|
|
def unload(self):
|
|
|
|
"""Prepare to be unloaded"""
|
|
|
|
pass
|
|
|
|
|
2009-12-17 01:07:01 +00:00
|
|
|
class PluginRegistry(borg.Borg):
|
|
|
|
"""Definition of a class to store plugin instances"""
|
2010-06-20 21:41:55 +00:00
|
|
|
available_plugins = None
|
2009-12-17 01:07:01 +00:00
|
|
|
instances = None
|
|
|
|
path = None
|
2009-12-17 23:16:42 +00:00
|
|
|
done = None
|
2009-12-17 01:07:01 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Class initialiser"""
|
2009-12-22 00:25:25 +00:00
|
|
|
borg.Borg.__init__(self, self.__class__.__name__)
|
2009-12-17 01:07:01 +00:00
|
|
|
self.prepare_attributes()
|
|
|
|
|
|
|
|
def prepare_attributes(self):
|
|
|
|
"""Prepare our attributes"""
|
|
|
|
if not self.instances:
|
|
|
|
self.instances = {}
|
|
|
|
if not self.path:
|
2009-12-24 21:35:07 +00:00
|
|
|
self.path = []
|
2010-01-22 19:08:12 +00:00
|
|
|
(head, _tail) = os.path.split(borg.__file__)
|
2009-12-24 21:35:07 +00:00
|
|
|
self.path.append(os.path.join(head, 'plugins'))
|
|
|
|
self.path.append(os.path.join(get_config_dir(), 'plugins'))
|
2022-01-28 20:51:54 +00:00
|
|
|
dbg('Plugin path: %s' % self.path)
|
2009-12-17 23:16:42 +00:00
|
|
|
if not self.done:
|
|
|
|
self.done = False
|
2010-06-20 21:41:55 +00:00
|
|
|
if not self.available_plugins:
|
|
|
|
self.available_plugins = {}
|
2009-12-17 01:07:01 +00:00
|
|
|
|
2023-08-09 11:01:41 +00:00
|
|
|
def load_plugins(self, force=False):
|
2009-12-17 01:07:01 +00:00
|
|
|
"""Load all plugins present in the plugins/ directory in our module"""
|
2023-08-09 11:01:41 +00:00
|
|
|
if self.done and (not force):
|
2022-01-28 20:51:54 +00:00
|
|
|
dbg('Already loaded')
|
2009-12-17 23:16:42 +00:00
|
|
|
return
|
|
|
|
|
2023-08-09 11:01:41 +00:00
|
|
|
dbg('loading plugins, force:(%s)' % force)
|
|
|
|
|
2010-01-06 00:27:58 +00:00
|
|
|
config = Config()
|
|
|
|
|
2009-12-24 21:35:07 +00:00
|
|
|
for plugindir in self.path:
|
|
|
|
sys.path.insert(0, plugindir)
|
|
|
|
try:
|
|
|
|
files = os.listdir(plugindir)
|
|
|
|
except OSError:
|
|
|
|
sys.path.remove(plugindir)
|
|
|
|
continue
|
|
|
|
for plugin in files:
|
2015-11-28 17:58:47 +00:00
|
|
|
if plugin == '__init__.py':
|
|
|
|
continue
|
2009-12-24 21:35:07 +00:00
|
|
|
pluginpath = os.path.join(plugindir, plugin)
|
|
|
|
if os.path.isfile(pluginpath) and plugin[-3:] == '.py':
|
2022-01-28 20:51:54 +00:00
|
|
|
dbg('Importing plugin %s' % plugin)
|
2009-12-24 21:35:07 +00:00
|
|
|
try:
|
|
|
|
module = __import__(plugin[:-3], None, None, [''])
|
2010-06-10 15:56:17 +00:00
|
|
|
for item in getattr(module, 'AVAILABLE'):
|
2023-08-09 11:01:41 +00:00
|
|
|
func = getattr(module, item)
|
2018-04-24 18:22:10 +00:00
|
|
|
if item not in list(self.available_plugins.keys()):
|
2010-06-20 21:41:55 +00:00
|
|
|
self.available_plugins[item] = func
|
|
|
|
|
2020-05-01 14:36:16 +00:00
|
|
|
if item not in config['enabled_plugins']:
|
2010-06-18 12:07:02 +00:00
|
|
|
dbg('plugin %s not enabled, skipping' % item)
|
2010-01-06 00:27:58 +00:00
|
|
|
continue
|
2009-12-24 21:35:07 +00:00
|
|
|
if item not in self.instances:
|
2010-06-20 21:41:55 +00:00
|
|
|
self.instances[item] = func()
|
2023-08-09 11:01:41 +00:00
|
|
|
elif force:
|
|
|
|
#instead of multiple copies of loaded
|
|
|
|
#plugin objects, unload where plugins
|
|
|
|
#can clean up and then re-init so there
|
|
|
|
#is one plugin object
|
|
|
|
self.instances[item].unload()
|
|
|
|
self.instances.pop(item, None)
|
|
|
|
self.instances[item] = func()
|
2018-04-24 18:22:10 +00:00
|
|
|
except Exception as ex:
|
2009-12-24 21:35:07 +00:00
|
|
|
err('PluginRegistry::load_plugins: Importing plugin %s \
|
2010-01-22 19:08:12 +00:00
|
|
|
failed: %s' % (plugin, ex))
|
2009-12-17 01:07:01 +00:00
|
|
|
|
2009-12-17 23:16:42 +00:00
|
|
|
self.done = True
|
|
|
|
|
2009-12-17 01:07:01 +00:00
|
|
|
def get_plugins_by_capability(self, capability):
|
|
|
|
"""Return a list of plugins with a particular capability"""
|
|
|
|
result = []
|
2022-01-28 20:51:54 +00:00
|
|
|
dbg('searching %d plugins \
|
2009-12-17 13:51:55 +00:00
|
|
|
for %s' % (len(self.instances), capability))
|
|
|
|
for plugin in self.instances:
|
|
|
|
if capability in self.instances[plugin].capabilities:
|
2009-12-17 01:07:01 +00:00
|
|
|
result.append(self.instances[plugin])
|
|
|
|
return result
|
|
|
|
|
2009-12-17 13:51:55 +00:00
|
|
|
def get_all_plugins(self):
|
|
|
|
"""Return all plugins"""
|
|
|
|
return(self.instances)
|
|
|
|
|
2010-06-20 21:41:55 +00:00
|
|
|
def get_available_plugins(self):
|
|
|
|
"""Return a list of all available plugins whether they are enabled or
|
|
|
|
disabled"""
|
2018-04-24 18:22:10 +00:00
|
|
|
return(list(self.available_plugins.keys()))
|
2010-06-20 21:41:55 +00:00
|
|
|
|
|
|
|
def is_enabled(self, plugin):
|
|
|
|
"""Return a boolean value indicating whether a plugin is enabled or
|
|
|
|
not"""
|
2018-04-24 18:22:10 +00:00
|
|
|
return(plugin in self.instances)
|
2010-06-20 21:41:55 +00:00
|
|
|
|
|
|
|
def enable(self, plugin):
|
|
|
|
"""Enable a plugin"""
|
2010-06-22 23:48:06 +00:00
|
|
|
if plugin in self.instances:
|
|
|
|
err("Cannot enable plugin %s, already enabled" % plugin)
|
2010-06-20 21:41:55 +00:00
|
|
|
dbg("Enabling %s" % plugin)
|
2010-06-22 23:48:06 +00:00
|
|
|
self.instances[plugin] = self.available_plugins[plugin]()
|
2010-06-20 21:41:55 +00:00
|
|
|
|
|
|
|
def disable(self, plugin):
|
|
|
|
"""Disable a plugin"""
|
2010-06-22 23:48:06 +00:00
|
|
|
dbg("Disabling %s" % plugin)
|
|
|
|
self.instances[plugin].unload()
|
|
|
|
del(self.instances[plugin])
|
2010-06-20 21:41:55 +00:00
|
|
|
|
2010-01-05 22:22:13 +00:00
|
|
|
# This is where we should define a base class for each type of plugin we
|
|
|
|
# support
|
|
|
|
|
|
|
|
# URLHandler - This adds a regex match to the Terminal widget and provides a
|
|
|
|
# callback to turn that into a URL.
|
|
|
|
class URLHandler(Plugin):
|
|
|
|
"""Base class for URL handlers"""
|
|
|
|
capabilities = ['url_handler']
|
|
|
|
handler_name = None
|
|
|
|
match = None
|
2012-01-14 20:09:25 +00:00
|
|
|
nameopen = None
|
|
|
|
namecopy = None
|
2010-06-22 23:48:06 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Class initialiser"""
|
|
|
|
Plugin.__init__(self)
|
|
|
|
terminator = Terminator()
|
|
|
|
for terminal in terminator.terminals:
|
|
|
|
terminal.match_add(self.handler_name, self.match)
|
2010-01-05 22:22:13 +00:00
|
|
|
|
|
|
|
def callback(self, url):
|
|
|
|
"""Callback to transform the enclosed URL"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2010-06-22 23:48:06 +00:00
|
|
|
def unload(self):
|
|
|
|
"""Handle being removed"""
|
2020-09-26 02:26:27 +00:00
|
|
|
if not self.handler_name:
|
2010-06-22 23:48:06 +00:00
|
|
|
err('unload called without self.handler_name being set')
|
|
|
|
return
|
|
|
|
terminator = Terminator()
|
|
|
|
for terminal in terminator.terminals:
|
|
|
|
terminal.match_remove(self.handler_name)
|
|
|
|
|
2010-01-05 22:22:13 +00:00
|
|
|
# MenuItem - This is able to execute code during the construction of the
|
|
|
|
# context menu of a Terminal.
|
|
|
|
class MenuItem(Plugin):
|
|
|
|
"""Base class for menu items"""
|
|
|
|
capabilities = ['terminal_menu']
|
|
|
|
|
|
|
|
def callback(self, menuitems, menu, terminal):
|
|
|
|
"""Callback to transform the enclosed URL"""
|
|
|
|
raise NotImplementedError
|
2022-11-22 09:48:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
-Basic plugin util for key-press handling, has all mapping to be used
|
|
|
|
in layout keybindings
|
|
|
|
|
|
|
|
Vishweshwar Saran Singh Deo vssdeo@gmail.com
|
|
|
|
"""
|
|
|
|
|
|
|
|
from gi.repository import Gtk, Gdk
|
|
|
|
from terminatorlib.keybindings import Keybindings, KeymapError
|
|
|
|
|
|
|
|
PLUGIN_UTIL_DESC = 0
|
|
|
|
PLUGIN_UTIL_ACT = 1
|
|
|
|
PLUGIN_UTIL_KEYS = 2
|
|
|
|
|
|
|
|
class KeyBindUtil:
|
|
|
|
|
|
|
|
keybindings = Keybindings()
|
|
|
|
|
|
|
|
map_key_to_act = {}
|
|
|
|
map_act_to_keys = {}
|
|
|
|
map_act_to_desc = {}
|
|
|
|
|
|
|
|
def __init__(self, config=None):
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
#Example
|
|
|
|
# bind
|
|
|
|
# first param is desc, second is action str
|
|
|
|
# self.keyb.bindkey([PluginUrlFindNext , PluginUrlActFindNext, "<Alt>j"])
|
|
|
|
#
|
|
|
|
# get action name
|
|
|
|
# act = self.keyb.keyaction(event)
|
|
|
|
|
|
|
|
# if act == "url_find_next":
|
|
|
|
|
|
|
|
|
|
|
|
#check map key_val_mask -> action
|
|
|
|
def _check_keybind_change(self, key):
|
|
|
|
act = key[PLUGIN_UTIL_ACT]
|
|
|
|
for key_val_mask in self.map_key_to_act:
|
|
|
|
old_act = self.map_key_to_act[key_val_mask]
|
|
|
|
if act == old_act:
|
|
|
|
return key_val_mask
|
|
|
|
return None
|
|
|
|
|
|
|
|
#check in config before binding
|
|
|
|
def bindkey_check_config(self, key):
|
|
|
|
if not self.config:
|
|
|
|
raise Warning("bindkey_check_config called without config init")
|
|
|
|
|
|
|
|
actstr = key[PLUGIN_UTIL_ACT]
|
|
|
|
kbsect = self.config.base.get_item('keybindings')
|
|
|
|
keystr = kbsect[actstr] if actstr in kbsect else ""
|
|
|
|
dbg("bindkey_check_config:action (%s) key str:(%s)" % (actstr, keystr))
|
|
|
|
if len(keystr):
|
|
|
|
key[PLUGIN_UTIL_KEYS] = keystr
|
|
|
|
dbg("found new Action->KeyVal in config: (%s, %s)"
|
|
|
|
% (actstr, keystr));
|
|
|
|
self.bindkey(key)
|
|
|
|
|
|
|
|
def bindkey(self, key):
|
|
|
|
(keyval, mask) = self.keybindings._parsebinding(key[PLUGIN_UTIL_KEYS])
|
|
|
|
keyval = Gdk.keyval_to_lower(keyval)
|
|
|
|
mask = Gdk.ModifierType(mask)
|
|
|
|
|
|
|
|
ret = (keyval, mask)
|
|
|
|
dbg("bindkey: (%s) (%s)" % (key[PLUGIN_UTIL_KEYS], str(ret)))
|
|
|
|
|
|
|
|
#remove if any old key_val_mask
|
|
|
|
old_key_val_mask = self._check_keybind_change(key)
|
|
|
|
if old_key_val_mask:
|
|
|
|
dbg("found old key binding, removing: (%s)" % str(old_key_val_mask))
|
|
|
|
del self.map_key_to_act[old_key_val_mask]
|
|
|
|
|
|
|
|
#map key-val-mask to action, used to ease key-press management
|
|
|
|
self.map_key_to_act[ret] = key[PLUGIN_UTIL_ACT]
|
|
|
|
|
|
|
|
|
|
|
|
#map action to key-combo-str, used in preferences->keybinding
|
|
|
|
self.map_act_to_keys[key[PLUGIN_UTIL_ACT]] = key[PLUGIN_UTIL_KEYS]
|
|
|
|
#map action to key-combo description, in used preferences->keybinding
|
|
|
|
self.map_act_to_desc[key[PLUGIN_UTIL_ACT]] = key[PLUGIN_UTIL_DESC]
|
|
|
|
|
|
|
|
def unbindkey(self, key):
|
|
|
|
|
|
|
|
# Suppose user changed the key-combo and its diff from
|
|
|
|
# what the plugin had set by default, we need to get
|
|
|
|
# current key-combo
|
|
|
|
act = key[PLUGIN_UTIL_ACT]
|
|
|
|
act_keys = self.map_act_to_keys[act]
|
|
|
|
|
|
|
|
(keyval, mask) = self.keybindings._parsebinding(act_keys)
|
|
|
|
keyval = Gdk.keyval_to_lower(keyval)
|
|
|
|
mask = Gdk.ModifierType(mask)
|
|
|
|
|
|
|
|
ret = (keyval, mask)
|
|
|
|
dbg("unbindkey: (%s) (%s)" % (key[PLUGIN_UTIL_KEYS], str(ret)))
|
|
|
|
|
|
|
|
# FIXME keys should always be there, can also use .pop(key, None)
|
|
|
|
# lets do it after testing
|
|
|
|
del self.map_key_to_act[ret]
|
|
|
|
del self.map_act_to_keys[key[PLUGIN_UTIL_ACT]]
|
|
|
|
del self.map_act_to_desc[key[PLUGIN_UTIL_ACT]]
|
|
|
|
|
|
|
|
|
|
|
|
def keyaction(self, event):
|
|
|
|
#FIXME MOD2 mask comes in the event, remove
|
|
|
|
event.state &= ~Gdk.ModifierType.MOD2_MASK
|
|
|
|
|
|
|
|
keyval = Gdk.keyval_to_lower(event.keyval)
|
|
|
|
ret = (keyval, event.state)
|
|
|
|
dbg("keyaction: (%s)" % str(ret))
|
|
|
|
return self.map_key_to_act.get(ret, None)
|
|
|
|
|
2022-11-23 10:04:22 +00:00
|
|
|
def get_act_to_keys(self, key):
|
|
|
|
return self.map_act_to_keys.get(key)
|
|
|
|
|
|
|
|
def get_all_act_to_keys(self):
|
2022-11-22 09:48:26 +00:00
|
|
|
return self.map_act_to_keys
|
|
|
|
|
2022-11-23 10:04:22 +00:00
|
|
|
def get_all_act_to_desc(self):
|
2022-11-22 09:48:26 +00:00
|
|
|
return self.map_act_to_desc
|
2022-11-23 10:04:22 +00:00
|
|
|
|
|
|
|
def get_act_to_desc(self, act):
|
|
|
|
return self.map_act_to_desc.get(act)
|
|
|
|
|
|
|
|
#get action to key binding from config
|
|
|
|
def get_act_to_keys_config(self, act):
|
|
|
|
if not self.config:
|
|
|
|
raise Warning("get_keyvalmask_for_act called without config init")
|
|
|
|
|
|
|
|
keybindings = self.config["keybindings"]
|
|
|
|
return keybindings.get(act)
|