Debut gestion plugins

This commit is contained in:
Roven Gabriel 2013-03-30 22:39:44 +01:00
parent 8348fdcf5a
commit 92e0bafcc6
9 changed files with 139 additions and 0 deletions

View File

@ -0,0 +1,9 @@
from brie.plugins.wifi.controller import *
from brie.plugins.shell.controller import *
mappings = {
"brie.controllers.show.member" : [
("wifi", Wifi.show),
("shell", Shell.show)
]
}

86
Brie/brie/lib/plugins.py Normal file
View File

@ -0,0 +1,86 @@
# -*- coding: utf-8 -*-
import tg
from brie.config import plugins_config
from decorator import decorator
from tg.decorators import Decoration
from brie.lib.aurore_helper import *
from brie.model.ldap import Plugins
""" Classe permettant d'utiliser les resultats dictionnaires d'un plugin
comme une classe (à la manière des LdapResult).
e.g.
au lieu de shell_show["uid"] on utilise comme : shell_show.uid
"""
class PluginVars:
def __init__(self, vars_dict):
self.__dict__ = vars_dict
#end def
#end class
""" Decorateur plugin, execute toutes les fonctions du scope donné """
def plugins(scope):
def plugin(f, *args, **kw):
results_dict = f(*args, **kw)
user = args[0].user
residence_var = "residence"
plugins_templates = list()
# un plugin est defini pour le scope et residence est defini
if scope in plugins_config.mappings and residence_var in results_dict:
residence_dn = Residences.get_dn_by_name(user, results_dict[residence_var])
scope_mappings = plugins_config.mappings[scope]
for plugin_name, function in scope_mappings:
plugin_activated = Plugins.get_by_name(user, residence_dn, plugin_name)
if plugin_activated is None:
continue
#end if
template_name = None
# obtenir le nom du template à partir du decorator "expose"
deco = Decoration.get_decoration(function)
try:
template_name = deco.engines["text/html"][1]
except:
pass
if template_name is not None:
# transformer le nom de template en chemin fichier
template_path = (
tg.config['pylons.app_globals']
.dotted_filename_finder
.get_dotted_filename(template_name, template_extension='.html')
)
# ajouter dans les plugin templates
plugins_templates.append(template_path)
#end if
# executer la fonction du plugin
mapping_results = function(results_dict)
# constuire le nom de regroupement des variable de ce plugin
method_name = function.__name__
plugin_section = str.lower(plugin_name + "_" + method_name)
# ajout du groupe au dictionnaire de la methode du controlleur
results_dict[plugin_section] = PluginVars(mapping_results)
#end for
#end if
# ajout des templates dans un champs spécial du dictionnaire pour le rendu
results_dict["_plugins_templates"] = plugins_templates
return results_dict
#end def
return decorator(plugin)
#end def

View File

View File

View File

@ -0,0 +1,11 @@
class Shell:
@staticmethod
def show(models):
return {
"name" : "shell"
}
#end def
#end class

View File

View File

@ -0,0 +1,14 @@
from tg.decorators import expose
class Wifi:
@staticmethod
@expose("brie.plugins.wifi.show")
def show(models):
return {
"name" : "wifi"
}
#end def
#end class

View File

@ -0,0 +1,7 @@
<html
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude"
py:strip="" >
hello wifi
</html>

View File

@ -0,0 +1,12 @@
<html
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude"
py:strip="" >
toto
<py:if test="defined('_plugins_templates')">
<xi:include
py:for="plugin_template in _plugins_templates"
href="${plugin_template}" />
</py:if>
</html>