X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=engine%2Fplugin.py;h=fc68c48a7ea656e0471f31e8b5987bb42f7dc1e6;hb=a222cef0a10a5517349f05cea0e382b35e3a14a6;hp=b69d098fa21ac9e38004cce7e7f225cea7c4960c;hpb=36affe7c738e94c6085f50b5fae8dfb9ae9b81fd;p=unfold.git diff --git a/engine/plugin.py b/engine/plugin.py index b69d098f..fc68c48a 100644 --- a/engine/plugin.py +++ b/engine/plugin.py @@ -14,52 +14,51 @@ from engine.prelude import Prelude # . [ 'SliceList', 'TabbedView' ] : to debug these classes # . True : to debug all plugin -DEBUG= [ 'SliceList' ] +#DEBUG= [ 'Tabs' ] +DEBUG= False class Plugin: - # using a simple incremental scheme to generate uuids for now - uuid=0 + # using a simple incremental scheme to generate domids for now + # we just need this to be unique in a page + domid=0 - # xxx should generate some random id @staticmethod - def newuuid(): - Plugin.uuid += 1 - return Plugin.uuid + def newdomid(): + Plugin.domid += 1 + return "plugin-%d"%Plugin.domid ########## # Constructor #### mandatory # . title: is used visually for displaying the widget - # . name: a simple id suitable for forging css names #### optional - # . hidable: whether it can be turned on and off from the UI - # like e.g. PleKitToggle - # . hidden_by_default: if hidable, what's the initial status - # . visible: if not set the plugin does not show up at all, - # not quite sure what this was for + # . togglable: whether it can be turned on and off (like PleKitToggle) + # . toggled: if togglable, what's the initial status + # . visible: if not set the plugin does not show up at all + # (not quite sure what this was for) #### internal data - # . uuid: created internally + # . domid: created internally, but can be set at creation time if needed + # useful for hand-made css, or for selecting an active plugin in a composite # . rank: this is for plugins sons of a composite plugin #### custom # any other setting can also be set when creating the object, like # p=Plugin(foo='bar') # which will result in 'foo' being accessible to the template engine # - def __init__ (self, title, name, - visible=True, hidable=True, hidden_by_default=False, **settings): + def __init__ (self, title, domid=None, + visible=True, togglable=True, toggled=True, **settings): # what is in this dictionary will get exposed to template and to javascript self._settings=settings self.title=title - self.name=name - self.add_to_settings ( ['title','name'] ) - self.uuid=Plugin.newuuid() + if not domid: domid=Plugin.newdomid() + self.domid=domid self.classname=self._classname() - self.add_to_settings ( [ 'uuid', 'classname' ] ) + self.add_to_settings ( ['title', 'domid', 'classname'] ) self.visible=visible - self.hidable=hidable - self.hidden_by_default=hidden_by_default - self.add_to_settings( ['visible','hidable','hidden_by_default'] ) + self.togglable=togglable + self.toggled=toggled + self.add_to_settings( ['visible','togglable','toggled'] ) # we store as a dictionary the arguments passed to constructor # e.g. SimpleList (list=[1,2,3]) => _settings = { 'list':[1,2,3] } # our own settings are not made part of _settings but could be.. @@ -85,10 +84,6 @@ class Plugin: if setting not in self._settings: return default else: return self._settings[setting] - def is_visible (self): return self.visible - def is_hidable (self): return self.hidable - def is_hidden_by_default (self): return self.hidden_by_default - ########## def need_debug (self): if not DEBUG: return False @@ -98,31 +93,30 @@ class Plugin: # returns the html code for that plugin # in essence, wraps the results of self.render_content () def render (self, request): - uuid = self.uuid - # initialize prelude placeholder - self._init_request (request) - + # initialize prelude placeholder if needed + self._init_prelude (request) # call render_content plugin_content = self.render_content (request) - # expose _settings in json format to js - settings_json = json.dumps (self._settings, separators=(',',':')) - - env= {'plugin_content' : plugin_content, - 'settings_json' : settings_json, - } + # shove this into plugin.html + env = {} + env ['plugin_content']= plugin_content env.update(self._settings) result = render_to_string ('plugin.html',env) - # handle requirements() if defined on this class - try: - self.handle_requirements (request, self.requirements()) - except AttributeError: - # most likely the object does not have that method defined, which is fine - pass - except: - import traceback - traceback.print_exc() - pass + # expose _settings in json format to js, and add plugin_uuid: domid in the mix + # NOTE this plugin_uuid thing might occur in js files, ** do not rename ** + js_env = { 'plugin_uuid' : self.domid } + js_env.update (self._settings) + for k in self.exclude_from_json(): + if k in js_env: del js_env[k] + settings_json = json.dumps (js_env, separators=(',',':')) + env ['settings_json' ] = settings_json + # compute plugin-specific initialization + js_init = render_to_string ( 'plugin-setenv.js', env ) + self.add_js_chunks (request, js_init) + + # interpret the result of requirements () + self.handle_requirements (request) return result @@ -143,13 +137,13 @@ class Plugin: print "%s.render_content: BEG --------------------"%self.classname print "template=%s"%template print "env.keys=%s"%env.keys() - print "env=%s"%env - print result + #print "env=%s"%env + #print result print "%s.render_content: END --------------------"%self.classname return result #################### requirements/prelude management - def _init_request (self, request): + def _init_prelude (self, request): if not hasattr (request, 'plugin_prelude'): # include css/plugins.css request.plugin_prelude=Prelude(css_files='css/plugin.css') @@ -161,26 +155,35 @@ class Plugin: # can be used directly in render_content() def add_js_files (self, request, files): - self._init_request (request) + self._init_prelude (request) request.plugin_prelude.add_js_files (files) def add_css_files (self, request, files): - self._init_request (request) + self._init_prelude (request) request.plugin_prelude.add_css_files (files) def add_js_chunks (self, request, chunks): - self._init_request (request) + self._init_prelude (request) request.plugin_prelude.add_js_chunks (chunks) def add_css_chunks (self, request, chunks): - self._init_request (request) + self._init_prelude (request) request.plugin_prelude.add_css_chunks (chunks) # or from the result of self.requirements() - def handle_requirements (self, request, d): - for (k,v) in d.iteritems(): - if self.need_debug(): - print "%s: handling requirement %s"%(self.classname,v) - method_name='add_'+k - method=Plugin.__dict__[method_name] - method(self,request,v) + def handle_requirements (self, request): + try: + d=self.requirements() + for (k,v) in d.iteritems(): + if self.need_debug(): + print "%s: handling requirement %s"%(self.classname,v) + method_name='add_'+k + method=Plugin.__dict__[method_name] + method(self,request,v) + except AttributeError: + # most likely the object does not have that method defined, which is fine + pass + except: + import traceback + traceback.print_exc() + pass ######################################## abstract interface # your plugin is expected to implement either @@ -210,3 +213,6 @@ class Plugin: # 'css_chunk': [], # likewise for css scripts # } + # for better performance + # you can specify a list of keys that won't be exposed as json attributes + def exclude_from_json (self): return []