add mandatory title and name args to plugin creation, remove title()
[unfold.git] / engine / plugin.py
index c7df2d3..2645810 100644 (file)
@@ -17,27 +17,61 @@ DEBUG= [ 'SliceList' ]
 
 class Plugin:
 
-    uid=0
-
-    def __init__ (self, visible=True, hidable=True, hidden_by_default=False, **settings):
-        # xxx should generate some random id
-        self.uuid=Plugin.uid
-        Plugin.uid += 1
+    # using a simple incremental scheme to generate uuids for now
+    uuid=0
+
+    # xxx should generate some random id
+    @staticmethod
+    def newuuid():
+        Plugin.uuid += 1
+        return Plugin.uuid
+
+    ########## 
+    # 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
+    #### internal data
+    # . uuid: created internally
+    # . 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):
+        # 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()
         self.visible=visible
         self.hidable=hidable
         self.hidden_by_default=hidden_by_default
+        self.add_to_settings( ['uuid','visible','hidable','hidden_by_default'] )
         # 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..
-        self._settings=settings
         if self.need_debug():
             print "Plugin.__init__ Created plugin with settings %s"%self._settings.keys()
 
     # subclasses might handle some fields in their own way, 
     # in which case this call is needed to capture that setting
     # see e.g. SimpleList or SliceList for an example of that
-    def add_to_settings (self, setting_name):
-        self._settings[setting_name]=getattr(self,setting_name)
+    def add_to_settings (self, setting_name_s):
+        if not isinstance (setting_name_s, list):
+            self._settings[setting_name_s]=getattr(self,setting_name_s)
+        else:
+            for setting_name in setting_name_s:
+                self._settings[setting_name]=getattr(self,setting_name)
 
     def classname (self): 
         try:    return self.__class__.__name__
@@ -95,14 +129,14 @@ class Plugin:
         return result
         
     # you may redefine this completely, but if you don't we'll just use methods 
-    # . template() to find out which template to use, and 
-    # . render_env() to compute a dictionary to pass along to the templating system
+    # . template_file() to find out which template to use, and 
+    # . template_env() to compute a dictionary to pass along to the templating system
     def render_content (self, request):
         """Should return an HTML fragment"""
-        template = self.template()
-        env=self.render_env(request)
+        template = self.template_file()
+        env=self.template_env(request)
         if not isinstance (env,dict):
-            raise Exception, "%s.render_env returns wrong type"%self.classname()
+            raise Exception, "%s.template_env returns wrong type"%self.classname()
         # expose this class's settings to the template
         # xxx we might need to check that this does not overwrite env..
         env.update(self._settings)
@@ -150,20 +184,18 @@ class Plugin:
             method(self,request,v)
 
     ######################################## abstract interface
-    def title (self): return "you should redefine title()"
-
     # your plugin is expected to implement either 
     # (*) def render_content(self, request) -> html fragment
     # -- or --
-    # (*) def template(self) -> filename
+    # (*) def template_file(self) -> filename
     #   relative to STATIC 
-    # (*) def render_env (self, request) -> dict
+    # (*) def template_env (self, request) -> dict
     #   this is the variable->value association used to render the template
     # in which case the html template will be used
 
-    # if you see this string somewhere your template() code is not kicking in
-    def template (self):                return "undefined_template"
-    def render_env (self, request):     return {}
+    # if you see this string somewhere your template_file() code is not kicking in
+    def template_file (self):                return "undefined_template"
+    def template_env (self, request):     return {}
 
 #    # tell the framework about requirements (for the document <header>)
 #    # the notion of 'Media' in django provides for medium-dependant