Structure plugin communications

parent cc1d3454
......@@ -12,4 +12,5 @@ conf/*.conf
ssl/*.key
logs/*
run/*
penguidom/plugins/*/dropin.cache
*.old.py
###########################################################################
# Copyright (c) 2018- Franco (nextime) Lanza <franco@unixmedia.it>
#
# Penguidom System Controller Daemon "penguidomd"
#
# This file is part of penguidomd.
#
# penguidomd 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from zope.interface import Interface, Attribute
from zope.interface import implements
from twisted.python import reflect
import logging
from dmlib import constants as C
log = logging.getLogger( 'Plugins' )
class IModules(Interface):
""" Base module interface """
def initialize(self, callback, logger):
""" initialize a plugin module passing a callback """
def context2section(ctx):
if int(ctx) in C.SECTIONS.keys():
section=C.SECTIONS[int(ctx)]
else:
section="none"
return section
......@@ -39,6 +39,7 @@ import struct
from dmlib import dmdomain
from singleton import Singleton
import pluggable
import ikap
......@@ -98,11 +99,15 @@ class penguidomService(service.Service):
log.debug("isStarted() called")
self.isConfigured()
def isConfigured(self):
self.initializePlugins()
self.initialized=True
log.debug("isConfigured() called")
def initializePlugins(self):
self.plugins=pluggable.Loader(ConvenienceCaller(lambda c: self._callback('plugin', c)))
def getIkapUDP(self):
caller = ConvenienceCaller(lambda c: self._callback('ikap', c))
self.udp = ikap.DomIkaUDP(caller)
......
import logging
import os, sys
import imodules, plugins
from nexlibs.utils.genutils import ConvenienceCaller
log = logging.getLogger('Plugins')
try:
from twisted.plugin import IPlugin, getPlugins
except ImportError:
pass
else:
list(getPlugins(imodules.IModules, plugins)) # To refresh cache
PLUGINDIR=os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), 'penguidom', 'plugins')
def getPlugin(name, core=False):
for p in getPlugins(imodules.IModules, plugins):
qual = "%s.%s" % (p.__module__, p.__class__.__name__)
log.info("Loading plugin "+qual)
if p.__module__.split('.')[-1]==name:
p.core = core
return p
return None
class PluginLogger(object):
def __init__(self, plugname):
self.name = plugname
def _format(self, what):
return str(self.name)+": "+what
def info(self, what):
log.info(self._format(what))
def error(self, what):
log.error(self._format(what))
def warning(self, what):
log.warning(self._format(what))
def debug(self, what):
log.debug(self._format(what))
class Loader(object):
plugreg = {}
def __init__(self, core):
log.info("Initializing Plugins...")
self.core = core
for name in os.listdir(PLUGINDIR):
plugpath = "/".join([PLUGINDIR, name])
if os.path.isdir(plugpath):
p = getPlugin(name, core)
self.plugreg[name] = p
p.initialize(ConvenienceCaller(lambda c: self._plugincback(name, c)), PluginLogger(name))
def _plugincback(self, who, cmd, *args, **kwargs):
"""
The callback that try to find an appropriate method exported
to a higher level object by the ConvenienceCaller metaclass.
This isn't intended to be called by the user directly, instead pass it
to the instance of the higher level object initialization or by
setting it using the abstraction of the ConvenienceCaller metaclass
"""
try:
try:
f=getattr(self, who+'_on_'+cmd)
except:
try:
f=getattr(self, 'on_'+cmd)
except:
f=self.core.cmd
if f and callable(f):
return f
except:
raise AttributeError(" ".join([cmd, 'doesn\'t exists']))
###########################################################################
# Copyright (c) 2018- Franco (nextime) Lanza <franco@unixmedia.it>
#
# Penguidom System Controller Daemon "penguidomd"
#
# This file is part of penguidomd.
#
# penguidomd 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# This is a twisted plugin directory
try:
from twisted.plugin import pluginPackagePaths
__path__.extend(pluginPackagePaths(__name__))
except ImportError:
# Twisted 2.5 doesn't include pluginPackagePaths
import sys, os
__path__.extend([os.path.abspath(os.path.join(x, 'plugins'))
for x in sys.path])
__all__ = []
from paradox import Paradox
plugin=Paradox()
This diff is collapsed.
......@@ -97,6 +97,8 @@ if __name__ == "__main__":
logdict={"corelog":
{"file":"penguidom.log","name":[("Core","general")]},
"pluginslog":
{"file":"plugins.log","name":[("Plugins","plugins")]},
"protocollog":
{"file":"ikap.log","name":
[("IKAProtocol","ikap"),("IKAPServer","ikap"),("DMDomain","ikap")]},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment