PSPServletFactory.py :  » Web-Frameworks » Webware » Webware-1.0.2 » PSP » 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 » Webware 
Webware » Webware 1.0.2 » PSP » PSPServletFactory.py

"""This module handles requests from the application for PSP pages.

  (c) Copyright by Jay Love, 2000 (mailto:jsliv@jslove.org)

  Permission to use, copy, modify, and distribute this software and its
  documentation for any purpose and without fee or royalty is hereby granted,
  provided that the above copyright notice appear in all copies and that
  both that copyright notice and this permission notice appear in
  supporting documentation or portions thereof, including modifications,
  that you make.

  THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO
  THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
  INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  WITH THE USE OR PERFORMANCE OF THIS SOFTWARE !

"""

import os, sys
from glob import glob
from WebKit.ServletFactory import ServletFactory
from PSP import Context,PSPCompiler


class PSPServletFactory(ServletFactory):
  """Servlet Factory for PSP files.

  Very sloppy. Need to come back and do a serious cleanup.

  """

  def __init__(self, application):
    ServletFactory.__init__(self, application)
    self._cacheDir = os.path.join(application._cacheDir, 'PSP')
    sys.path.append(self._cacheDir)
    self._cacheClassFiles = self._cacheClasses
    t = ['_'] * 256
    from string import digits,letters
    for c in digits + letters:
      t[ord(c)] = c
    self._classNameTrans = ''.join(t)
    setting = application.setting
    self._extensions = setting('ExtensionsForPSP', ['.psp'])
    self._fileEncoding = setting('PSPFileEncoding', None)
    if setting('ClearPSPCacheOnStart', 0):
      self.clearFileCache()

  def uniqueness(self):
    return 'file'

  def extensions(self):
    return self._extensions

  def fileEncoding(self):
    """Return the file encoding used in PSP files."""
    return self._fileEncoding

  def flushCache(self):
    """Clean out the cache of classes in memory and on disk."""
    ServletFactory.flushCache(self)
    self.clearFileCache()

  def clearFileCache(self):
    """Clear class files stored on disk."""
    files = glob(os.path.join(self._cacheDir, '*.*'))
    map(os.remove, files)

  def computeClassName(self, pagename):
    """Generates a (hopefully) unique class/file name for each PSP file.

    Argument: pagename: the path to the PSP source file
    Returns: a unique name for the class generated fom this PSP source file

    """
    # Compute class name by taking the path and substituting
    # underscores for all non-alphanumeric characters:
    return os.path.splitdrive(pagename)[1].translate(self._classNameTrans)

  def loadClassFromFile(self, transaction, filename, classname):
    """Create an actual class instance.

    The module containing the class is imported as though it were a
    module within the context's package (and appropriate subpackages).

    """
    module = self.importAsPackage(transaction, filename)
    assert module.__dict__.has_key(classname), \
      'Cannot find expected class named %s in %s.' \
        % (repr(classname), repr(filename))
    theClass = getattr(module, classname)
    return theClass

  def loadClass(self, transaction, path):
    classname = self.computeClassName(path)
    classfile = os.path.join(self._cacheDir, classname + ".py")
    mtime = os.path.getmtime(path)
    if not os.path.exists(classfile) \
        or os.path.getmtime(classfile) != mtime:
      context = Context.PSPCLContext(path, transaction)
      context.setClassName(classname)
      context.setPythonFileName(classfile)
      context.setPythonFileEncoding(self._fileEncoding)
      clc = PSPCompiler.Compiler(context)
      clc.compile()
      # Set the modification time of the compiled file
      # to be the same as the source file;
      # that's how we'll know if it needs to be recompiled:
      os.utime(classfile, (os.path.getatime(classfile), mtime))
    theClass = self.loadClassFromFile(transaction, classfile, classname)
    return theClass
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.