Author: dmeyer
Date: Sun Jan 29 12:18:55 2006
New Revision: 1140

Added:
   trunk/cherrypy/src/templ_cheetah.py
   trunk/cherrypy/src/templ_kid.py
Modified:
   trunk/cherrypy/src/controller.py

Log:
move template wrappers to external files

Modified: trunk/cherrypy/src/controller.py
==============================================================================
--- trunk/cherrypy/src/controller.py    (original)
+++ trunk/cherrypy/src/controller.py    Sun Jan 29 12:18:55 2006
@@ -5,8 +5,8 @@
 # $Id$
 #
 # This module define the expose decorator. The idea is copied from TurboGears.
-# The expose function adds a template (Kid) and it is possible to execute the
-# function from the main thread.
+# The expose function adds a template (Kid or Cheetah) and it is possible to
+# execute the function from the main thread.
 #
 # -----------------------------------------------------------------------------
 # kaa-cherrypy - Web Framework for Kaa based on CherryPy
@@ -42,67 +42,20 @@
 
 engines = []
 
-# Kid template
-
-try:
-    # kid import
-    import kid
-
-    # enable importing kid files as python modules
-    kid.enable_import()
-
-    class KidTemplate(object):
-
-        name = 'kid'
-
-        def detect(self, template):
-            if type(template) == str and template.endswith('.kid'):
-                return True
-            if hasattr(template, 'BaseTemplate') and \
-               template.BaseTemplate == kid.BaseTemplate:
-                return True
-            return False
-
-        def parse(self, template, args):
-            if type(template) == types.ModuleType:
-                return template.Template(**args).serialize(output='xhtml')
-            return kid.Template(file=template, 
**args).serialize(output='xhtml')
-
-    engines.append(KidTemplate())
-
-except ImportError:
-    pass
-
-
-# Cheetah template
-
-try:
-    # import
-    import Cheetah.Template
-
-    class CheetahTemplate(object):
-
-        name = 'cheetah'
-
-        def detect(self, template):
-            if type(template) == str and template.endswith('.tmpl'):
-                return True
-            if hasattr(template, '__CHEETAH_src__'):
-                c = 
os.path.splitext(os.path.basename(template.__CHEETAH_src__))[0]
-                template.__KaaCherrypyTemplate = getattr(template, c)
-                return True
-            return False
-
-        def parse(self, template, args):
-            if type(template) == types.ModuleType:
-                return str(template.__KaaCherrypyTemplate(searchList=[args]))
-            return str(Cheetah.Template.Template(file=template, 
searchList=[args]))
-
-    engines.append(CheetahTemplate())
-
-except ImportError:
-    pass
-
+# load template engines
+for f in os.listdir(os.path.dirname(__file__)):
+    if not f.startswith('templ_') or not f.endswith('.py'):
+        # this is no template engine
+        continue
+    try:
+        # try to import
+        exec('from %s import Template as Engine' % f[:-3])
+        # add to list of engines
+        engines.append(Engine())
+    except ImportError:
+        # engine not supported
+        pass
+    
 
 def _get_engine(template, engine):
     """

Added: trunk/cherrypy/src/templ_cheetah.py
==============================================================================
--- (empty file)
+++ trunk/cherrypy/src/templ_cheetah.py Sun Jan 29 12:18:55 2006
@@ -0,0 +1,64 @@
+# -*- coding: iso-8859-1 -*-
+# -----------------------------------------------------------------------------
+# tmpl_cheetah.py - Cheetah template wrapper
+# -----------------------------------------------------------------------------
+# $Id$
+#
+# This module define a Cheetah wrapper. It is loaded based on the name starting
+# with templ_. To write other template engine wrappers, just dump them
+# into this directory.
+#
+# -----------------------------------------------------------------------------
+# kaa-cherrypy - Web Framework for Kaa based on CherryPy
+# Copyright (C) 2006 Dirk Meyer
+#
+# First Edition: Dirk Meyer <[EMAIL PROTECTED]>
+# Maintainer:    Dirk Meyer <[EMAIL PROTECTED]>
+#
+# This program 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 2 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 MER-
+# CHANTABILITY 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.,
+# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# -----------------------------------------------------------------------------
+
+# python imports
+import os
+import types
+
+# import cheetah
+import Cheetah.Template
+
+class Template(object):
+
+    name = 'cheetah'
+
+    def detect(self, template):
+        """
+        Detect if the given template is a Cheetah template or not.
+        """
+        if type(template) == str and template.endswith('.tmpl'):
+            return True
+        if hasattr(template, '__CHEETAH_src__'):
+            c = os.path.splitext(os.path.basename(template.__CHEETAH_src__))[0]
+            template.__KaaCherrypyTemplate = getattr(template, c)
+            return True
+        return False
+
+
+    def parse(self, template, args):
+        """
+        Parse the template and execute it based on the arguments.
+        """
+        if type(template) == types.ModuleType:
+            return str(template.__KaaCherrypyTemplate(searchList=[args]))
+        return str(Cheetah.Template.Template(file=template, searchList=[args]))

Added: trunk/cherrypy/src/templ_kid.py
==============================================================================
--- (empty file)
+++ trunk/cherrypy/src/templ_kid.py     Sun Jan 29 12:18:55 2006
@@ -0,0 +1,62 @@
+# -*- coding: iso-8859-1 -*-
+# -----------------------------------------------------------------------------
+# tmpl_kid.py - Kid template wrapper
+# -----------------------------------------------------------------------------
+# $Id$
+#
+# This module define a Kid wrapper. It is loaded based on the name starting
+# with templ_. To write other template engine wrappers, just dump them
+# into this directory.
+#
+# -----------------------------------------------------------------------------
+# kaa-cherrypy - Web Framework for Kaa based on CherryPy
+# Copyright (C) 2006 Dirk Meyer
+#
+# First Edition: Dirk Meyer <[EMAIL PROTECTED]>
+# Maintainer:    Dirk Meyer <[EMAIL PROTECTED]>
+#
+# This program 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 2 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 MER-
+# CHANTABILITY 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.,
+# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# -----------------------------------------------------------------------------
+
+# python imports
+import types
+
+# kid import
+import kid
+
+class Template(object):
+
+    name = 'kid'
+
+    def detect(self, template):
+        """
+        Detect if the given template is a Kid template or not.
+        """
+        if type(template) == str and template.endswith('.kid'):
+            return True
+        if hasattr(template, 'BaseTemplate') and \
+           template.BaseTemplate == kid.BaseTemplate:
+            return True
+        return False
+
+
+    def parse(self, template, args):
+        """
+        Parse the template and execute it based on the arguments.
+        """
+        if type(template) == types.ModuleType:
+            return template.Template(**args).serialize(output='xhtml')
+        return kid.Template(file=template, **args).serialize(output='xhtml')


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to