
cherokee at cherokee-project
Feb 9, 2010, 8:02 AM
Post #1 of 1
(114 views)
Permalink
|
|
[4222] CTK/trunk/CTK: Adds plug-ins support.
|
|
Revision: 4222 http://svn.cherokee-project.com/changeset/4222 Author: alo Date: 2010-02-09 17:02:02 +0100 (Tue, 09 Feb 2010) Log Message: ----------- Adds plug-ins support. Modified Paths: -------------- CTK/trunk/CTK/Combobox.py CTK/trunk/CTK/PropsTable.py CTK/trunk/CTK/__init__.py Added Paths: ----------- CTK/trunk/CTK/Plugin.py Modified: CTK/trunk/CTK/Combobox.py =================================================================== --- CTK/trunk/CTK/Combobox.py 2010-02-09 15:13:23 UTC (rev 4221) +++ CTK/trunk/CTK/Combobox.py 2010-02-09 16:02:02 UTC (rev 4222) @@ -31,6 +31,10 @@ self._props = props self._options = options + if not 'id' in props: + self._props['id'] = 'Combobox_%s' %(self.uniq_id) + self.id = self._props['id'] + def Render (self): selected = self._props.get('selected') Added: CTK/trunk/CTK/Plugin.py =================================================================== --- CTK/trunk/CTK/Plugin.py (rev 0) +++ CTK/trunk/CTK/Plugin.py 2010-02-09 16:02:02 UTC (rev 4222) @@ -0,0 +1,146 @@ +# CTK: Cherokee Toolkit +# +# Authors: +# Alvaro Lopez Ortega <alvaro [at] alobbs> +# +# Copyright (C) 2009 Alvaro Lopez Ortega +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of version 2 of the GNU General Public +# License as published by the Free Software Foundation. +# +# 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, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. +# + +__author__ = 'Alvaro Lopez Ortega <alvaro [at] alobbs>' + +import os +import sys +import traceback + +from Widget import Widget +from Container import Container +from Combobox import ComboCfg +from Server import cfg, publish, post + +SELECTOR_CHANGED_JS = """ +$('#%(id)s').bind ('change', this, function() { + info = {'%(key)s': $('#%(id)s')[0].value }; + $.ajax ({url: '%(url)s', + type: 'POST', + async: true, + data: info, + success: function(data) { + $('#%(plugin_id)s').html(data); + }, + error: function (xhr, ajaxOptions, thrownError) { + alert ("Error: " + xhr.status +"\\n"+ xhr.statusText); + } + }); +}); +""" + + +class Plugin (Container): + def __init__ (self, **kwargs): + Container.__init__ (self, **kwargs) + self.id = "Plugin_%s" %(self.uniq_id) + + +class PluginInstanceProxy: + def __call__ (self, key, modules): + # Update the configuration + new_val = post.get_val (key, None) + cfg[key] = new_val + + # Intance the content + plugin = intance_plugin (new_val) + if not plugin: + return '' + + # Render it + render = plugin.Render() + render.html = '<div id="%s">%s</div>' %(plugin.id, render.html) + return render.html + render.js + + +class PluginSelector (Container): + def __init__ (self, key, modules): + Container.__init__ (self) + + # Properties + self._key = key + self._mods = modules + self._url = '/plugin_content_%d' %(self.uniq_id) + name = cfg.get_val (self._key) + + # Widgets + self.selector_widget = ComboCfg (key, modules) + self.plugin = intance_plugin (name) + + # Register hidden URL for the plugin content + publish (self._url, PluginInstanceProxy, key=key, modules=modules, method='POST') + + def Render (self): + # Load the plugin + render = self.plugin.Render() + + # Warp the content + render.html = '<div id="%s">%s</div>' %(self.plugin.id, render.html) + + # Add the initialization Javascript + render.js += SELECTOR_CHANGED_JS %({ + 'id': self.selector_widget.id, + 'url': self._url, + 'plugin_id': self.plugin.id, + 'key': self._key}) + + return render + + +# Helper functions +# + +def load_module (name): + # Sanity check + if not name: + return + + # Shortcut: Is it already loaded? + if sys.modules.has_key(name): + return sys.modules[name] + + # Figure the path to admin's python source + stack = traceback.extract_stack() + first_py = stack[0][0] + mod_path = os.path.abspath(os.path.join (first_py, '../plugins')) + + # Load the plug-in + sys.path.append (mod_path) + try: + plugin = __import__(name) + sys.modules[plugin.__name__] = plugin + finally: + del sys.path[-1] + + # Got it + return plugin + + +def intance_plugin (name): + # Load the Python module + module = load_module (name) + if not module: + return None + + # Intance an object + class_name = 'Plugin_%s' %(name) + return module.__dict__[class_name]() Modified: CTK/trunk/CTK/PropsTable.py =================================================================== --- CTK/trunk/CTK/PropsTable.py 2010-02-09 15:13:23 UTC (rev 4221) +++ CTK/trunk/CTK/PropsTable.py 2010-02-09 16:02:02 UTC (rev 4222) @@ -95,10 +95,10 @@ def AddConstant (self, key, val): self.constants[key] = val - def Add (self, title, widget, comment): + def Add (self, title, widget, comment, use_submitter=True): # No constants, just the widget if not self.constants: - self.entries.append ((title, widget, comment)) + self.entries.append ((title, widget, comment, use_submitter)) return # Wrap it @@ -112,10 +112,13 @@ render = RenderResponse() for e in self.entries: - title, widget, comment = e + title, widget, comment, use_submitter = e - submit = Submitter (self._url) - submit += widget + if use_submitter: + submit = Submitter (self._url) + submit += widget + else: + submit = widget widget_r = submit.Render() widget_html = widget_r.html Modified: CTK/trunk/CTK/__init__.py =================================================================== --- CTK/trunk/CTK/__init__.py 2010-02-09 15:13:23 UTC (rev 4221) +++ CTK/trunk/CTK/__init__.py 2010-02-09 16:02:02 UTC (rev 4222) @@ -21,11 +21,12 @@ # # Generic -from Widget import Widget +from Widget import Widget, RenderResponse from Container import Container from Submitter import Submitter, SubmitterButton from Page import Page from Config import Config +from Plugin import Plugin # Widgets from Table import Table, TableFixed @@ -43,3 +44,4 @@ from HTTP import HTTP_Response, HTTP_Redir, HTTP_Error, HTTP_XSendfile from HiddenField import HiddenField from Uploader import Uploader +from Plugin import PluginSelector
|