template.py :  » Web-Frameworks » Spyce » spyce-2.1 » contrib » modules » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Spyce 
Spyce » spyce 2.1 » contrib » modules » template.py
##################################################
# SPYCE - Python-based HTML Scripting
# Copyright (c) 2002 Rimon Barr.
#
# CVS: $Id: template.py 859 2006-04-30 00:55:03Z ellisj $
##################################################

from spyceModule import spyceModule
import spyceException, spyceCache
import os

__doc__ = """
Template module provides templating functionality: the ability to separate
form from function, or HTML page design from programming code. This module
currently provides support for the Cheetah template engine.

The Cheetah engine is invoked as follows:
  cheetah ( file, [lookup] )

Calling this function will invoke the Cheetah engine to compile (and
cache) the template file> provided.  The engine then "runs" the
template and fills in the appropriate data from the lookup dictionary,
or list of dictionaries. If the lookup is omitted, the convenient
default is to use the local and global variables from the current
context. The template is filled and the resulting string is returned.

In general, that the Python path must simply include the Cheetah
installation directory and Spyce will find it.

Support for other templating engines could be added in a similar fashion.
"""

class template(spyceModule):
  def cheetah(self, filename, lookup=None):
    "Hook into the Cheetah template engine."
    # check whether cheetah installed
    from Cheetah.Compiler import Compiler
    # define template cache
    if not self._api.getModule('pool').has_key('cheetahCache'):
      self._api.getModule('pool')['cheetahCache'] = spyceCache.semanticCache(spyceCache.memoryCache(), cheetahValid, cheetahGenerate)
    cheetahCache = self._api.getModule('pool')['cheetahCache']
    # absolute filename, relative to script filename
    filename = os.path.abspath(os.path.join(
        os.path.dirname(self._api.getFilename()), filename))
    # set lookup variables
    if lookup == None:
      import inspect
      lookup = [inspect.currentframe().f_back.f_locals, inspect.currentframe().f_back.f_globals]
    elif type(lookup)!=type([]):
      lookup = [lookup]
    # compile (or get cached) and run template
    return cheetahCache[filename](searchList=lookup)

##################################################
# Cheetah semantic cache helper functions
#

def cheetahValid(filename, validity):
  try:
    return os.path.getmtime(filename) == validity
  except: return 0

def cheetahGenerate(filename):
  # check permissions
  if not os.path.exists(filename):
    raise spyceException.spyceNotFound(filename)
  if not os.access(filename, os.R_OK):
    raise spyceException.spyceForbidden(filename)
  # read the template
  f = None
  try:
    f = open(filename, 'r')
    buf = f.read()
  finally:
    if f: f.close()
  # compile template, get timestamp
  mtime = os.path.getmtime(filename)
  from Cheetah.Compiler import Compiler
  code = Compiler(source=buf).__str__()
  dict = {}
  exec code in dict
  return mtime, dict['GenTemplate']

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.