2005-01-20 00:22:35 +01:00
|
|
|
###
|
|
|
|
# Copyright (c) 2002-2005, Jeremiah Fincher
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
import os
|
2020-01-26 11:22:59 +01:00
|
|
|
import re
|
2005-01-20 00:22:35 +01:00
|
|
|
import sys
|
|
|
|
import os.path
|
|
|
|
import linecache
|
2020-01-26 11:22:59 +01:00
|
|
|
import importlib.util
|
|
|
|
|
2020-05-08 18:50:14 +02:00
|
|
|
try:
|
|
|
|
import pkg_resources
|
|
|
|
except ImportError:
|
|
|
|
pkg_resources = None
|
|
|
|
|
2020-01-26 11:22:59 +01:00
|
|
|
if not hasattr(importlib.util, 'module_from_spec'):
|
|
|
|
# Python < 3.5
|
|
|
|
import imp
|
2005-01-20 00:22:35 +01:00
|
|
|
|
2012-09-18 04:12:11 +02:00
|
|
|
from . import callbacks, conf, log, registry
|
2005-01-20 00:22:35 +01:00
|
|
|
|
2020-05-08 18:50:14 +02:00
|
|
|
ENTRYPOINT_GROUPS = [
|
|
|
|
'limnoria.plugins',
|
|
|
|
]
|
|
|
|
|
2009-03-12 19:50:46 +01:00
|
|
|
installDir = os.path.dirname(sys.modules[__name__].__file__)
|
|
|
|
_pluginsDir = os.path.join(installDir, 'plugins')
|
|
|
|
|
2005-01-20 00:22:35 +01:00
|
|
|
class Deprecated(ImportError):
|
|
|
|
pass
|
|
|
|
|
2020-05-08 18:50:14 +02:00
|
|
|
def loadPluginFromEntrypoint(name):
|
|
|
|
if pkg_resources:
|
|
|
|
for entrypoint_group in ENTRYPOINT_GROUPS:
|
|
|
|
for entrypoint in pkg_resources.iter_entry_points(entrypoint_group):
|
|
|
|
if entrypoint.name.lower() == name.lower():
|
|
|
|
return entrypoint.load()
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2005-01-20 00:22:35 +01:00
|
|
|
def loadPluginModule(name, ignoreDeprecation=False):
|
|
|
|
"""Loads (and returns) the module for the plugin with the given name."""
|
|
|
|
files = []
|
2009-03-12 19:50:46 +01:00
|
|
|
pluginDirs = conf.supybot.directories.plugins()[:]
|
|
|
|
pluginDirs.append(_pluginsDir)
|
2020-05-08 18:50:14 +02:00
|
|
|
module = None
|
2005-01-20 00:22:35 +01:00
|
|
|
for dir in pluginDirs:
|
|
|
|
try:
|
|
|
|
files.extend(os.listdir(dir))
|
|
|
|
except EnvironmentError: # OSError, IOError superclass.
|
2005-02-01 14:43:57 +01:00
|
|
|
log.warning('Invalid plugin directory: %s; removing.', dir)
|
2005-01-20 00:22:35 +01:00
|
|
|
conf.supybot.directories.plugins().remove(dir)
|
2010-08-11 06:43:05 +02:00
|
|
|
if name not in files:
|
Start accelerating the 2to3 step (remove fix_apply, fix_buffer, fix_callable, fix_exec, fix_execfile, fix_exitfunc, fix_filter, fix_funcattrs, fix_future, fix_getcwdu, and fix_has_key).
2014-01-20 14:49:47 +01:00
|
|
|
search = lambda x: re.search(r'(?i)^%s$' % (name,), x)
|
|
|
|
matched_names = list(filter(search, files))
|
2017-01-22 21:22:39 +01:00
|
|
|
if len(matched_names) >= 1:
|
2010-08-11 06:43:05 +02:00
|
|
|
name = matched_names[0]
|
|
|
|
else:
|
2020-05-08 18:50:14 +02:00
|
|
|
module = loadPluginFromEntrypoint(name)
|
|
|
|
if module is None:
|
2020-03-21 02:20:59 +01:00
|
|
|
raise ImportError(name)
|
2020-05-08 18:50:14 +02:00
|
|
|
|
|
|
|
if module is None:
|
|
|
|
# Found by listing files; must now import it
|
|
|
|
try:
|
|
|
|
if hasattr(importlib.util, 'module_from_spec'):
|
|
|
|
# Python >= 3.5
|
|
|
|
spec = importlib.machinery.PathFinder.find_spec(name, pluginDirs)
|
|
|
|
if spec is None or spec.loader is None:
|
|
|
|
# spec is None if 'name' can't be found; and
|
|
|
|
# spec.loader might be None in some rare occasions as well
|
|
|
|
# (eg. for namespace packages)
|
|
|
|
raise ImportError(name)
|
|
|
|
module = importlib.util.module_from_spec(spec)
|
|
|
|
sys.modules[module.__name__] = module
|
|
|
|
spec.loader.exec_module(module)
|
|
|
|
else:
|
|
|
|
# Python < 3.5
|
|
|
|
moduleInfo = imp.find_module(name, pluginDirs)
|
|
|
|
module = imp.load_module(name, *moduleInfo)
|
|
|
|
except:
|
|
|
|
sys.modules.pop(name, None)
|
|
|
|
keys = list(sys.modules.keys())
|
|
|
|
for key in keys:
|
|
|
|
if key.startswith(name + '.'):
|
|
|
|
sys.modules.pop(key)
|
|
|
|
raise
|
|
|
|
|
2005-01-20 00:22:35 +01:00
|
|
|
if 'deprecated' in module.__dict__ and module.deprecated:
|
|
|
|
if ignoreDeprecation:
|
|
|
|
log.warning('Deprecated plugin loaded: %s', name)
|
|
|
|
else:
|
2014-01-20 15:43:55 +01:00
|
|
|
raise Deprecated(format('Attempted to load deprecated plugin %s',
|
|
|
|
name))
|
2005-01-20 00:22:35 +01:00
|
|
|
if module.__name__ in sys.modules:
|
|
|
|
sys.modules[module.__name__] = module
|
|
|
|
linecache.checkcache()
|
|
|
|
return module
|
|
|
|
|
2014-07-18 20:43:24 +02:00
|
|
|
def renameCommand(cb, name, newName):
|
|
|
|
assert not hasattr(cb, newName), 'Cannot rename over existing attributes.'
|
|
|
|
assert newName == callbacks.canonicalName(newName), \
|
|
|
|
'newName must already be normalized.'
|
|
|
|
if name != newName:
|
|
|
|
method = getattr(cb.__class__, name)
|
|
|
|
setattr(cb.__class__, newName, method)
|
|
|
|
delattr(cb.__class__, name)
|
|
|
|
|
|
|
|
def registerRename(plugin, command=None, newName=None):
|
|
|
|
g = conf.registerGlobalValue(conf.supybot.commands.renames, plugin,
|
|
|
|
registry.SpaceSeparatedSetOfStrings([], """Determines what commands
|
|
|
|
in this plugin are to be renamed."""))
|
|
|
|
if command is not None:
|
|
|
|
g().add(command)
|
|
|
|
v = conf.registerGlobalValue(g, command, registry.String('', ''))
|
|
|
|
if newName is not None:
|
|
|
|
v.setValue(newName) # In case it was already registered.
|
|
|
|
return v
|
|
|
|
else:
|
|
|
|
return g
|
|
|
|
|
2005-01-20 00:22:35 +01:00
|
|
|
def loadPluginClass(irc, module, register=None):
|
|
|
|
"""Loads the plugin Class from the given module into the given Irc."""
|
|
|
|
try:
|
2005-01-29 20:16:29 +01:00
|
|
|
cb = module.Class(irc)
|
2014-01-20 15:49:15 +01:00
|
|
|
except TypeError as e:
|
2005-02-23 01:04:07 +01:00
|
|
|
s = str(e)
|
|
|
|
if '2 given' in s and '__init__' in s:
|
2014-01-20 15:43:55 +01:00
|
|
|
raise callbacks.Error('In our switch from CVS to Darcs (after 0.80.1), we ' \
|
2005-02-08 04:35:26 +01:00
|
|
|
'changed the __init__ for callbacks.Privmsg* to also ' \
|
2005-02-23 01:04:07 +01:00
|
|
|
'accept an irc argument. This plugin (%s) is overriding ' \
|
2005-02-08 04:35:26 +01:00
|
|
|
'its __init__ method and needs to update its prototype ' \
|
|
|
|
'to be \'def __init__(self, irc):\' as well as passing ' \
|
|
|
|
'that irc object on to any calls to the plugin\'s ' \
|
2011-03-05 09:44:40 +01:00
|
|
|
'parent\'s __init__. Another possible cause: the code in ' \
|
2011-03-05 09:51:05 +01:00
|
|
|
'your __init__ raised a TypeError when calling a function ' \
|
2011-03-05 09:44:40 +01:00
|
|
|
'or creating an object, which doesn\'t take 2 arguments.' %\
|
2014-01-20 15:43:55 +01:00
|
|
|
module.__name__)
|
2005-02-08 04:35:26 +01:00
|
|
|
else:
|
|
|
|
raise
|
2014-01-20 15:49:15 +01:00
|
|
|
except AttributeError as e:
|
2005-01-20 00:22:35 +01:00
|
|
|
if 'Class' in str(e):
|
2014-01-20 15:43:55 +01:00
|
|
|
raise callbacks.Error('This plugin module doesn\'t have a "Class" ' \
|
2005-01-20 00:22:35 +01:00
|
|
|
'attribute to specify which plugin should be ' \
|
|
|
|
'instantiated. If you didn\'t write this ' \
|
|
|
|
'plugin, but received it with Supybot, file ' \
|
2014-01-20 15:43:55 +01:00
|
|
|
'a bug with us about this error.')
|
2005-01-20 00:22:35 +01:00
|
|
|
else:
|
|
|
|
raise
|
2005-02-18 07:14:17 +01:00
|
|
|
cb.classModule = module
|
2005-01-20 00:22:35 +01:00
|
|
|
plugin = cb.name()
|
|
|
|
public = True
|
|
|
|
if hasattr(cb, 'public'):
|
|
|
|
public = cb.public
|
|
|
|
conf.registerPlugin(plugin, register, public)
|
2005-03-22 20:03:02 +01:00
|
|
|
assert not irc.getCallback(plugin), \
|
|
|
|
'There is already a %r plugin registered.' % plugin
|
2005-01-20 00:22:35 +01:00
|
|
|
try:
|
2014-07-18 20:43:24 +02:00
|
|
|
v = registerRename(plugin)
|
|
|
|
renames = conf.supybot.commands.renames.get(plugin)()
|
2005-01-20 00:22:35 +01:00
|
|
|
if renames:
|
|
|
|
for command in renames:
|
|
|
|
v = registerRename(plugin, command)
|
|
|
|
newName = v()
|
|
|
|
assert newName
|
|
|
|
renameCommand(cb, command, newName)
|
|
|
|
else:
|
|
|
|
conf.supybot.commands.renames.unregister(plugin)
|
2014-01-20 15:49:15 +01:00
|
|
|
except registry.NonExistentRegistryEntry as e:
|
2005-01-20 00:22:35 +01:00
|
|
|
pass # The plugin isn't there.
|
|
|
|
irc.addCallback(cb)
|
|
|
|
return cb
|
2005-01-31 14:06:43 +01:00
|
|
|
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|