CodeSources.py :  » Web-Server » SkunkWEB » skunkweb-3.4.4 » pylibs » AE » 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 Server » SkunkWEB 
SkunkWEB » skunkweb 3.4.4 » pylibs » AE » CodeSources.py
#  
#  Copyright (C) 2001 Andrew T. Csillag <drew_csillag@geocities.com>
#  
#      You may distribute under the terms of either the GNU General
#      Public License or the SkunkWeb License, as specified in the
#      README file.
#   
# Time-stamp: <01/04/10 14:19:30 smulloni>
########################################################################

"""
This contains functionality dealing with sources of templates we evaluate, so 
that we can generate a more intelligent traceback information when something
blows up
"""

import linecache

# Let's cache them here
sources = {}



def getSource ( filename ):
    """get source for a filename that was a template - called by TBLineCache"""
    if not sources.has_key(filename):
        return 
    text = sources[filename]
    return text.split ( '\n' )

def putSource ( filename, source ):
    """put source code in an area reachable by getSources"""
    sources[filename] = source

def clearSources (*args):
    "Gets called after each request is done"
    sources.clear()

def updatecache(filename, updcache=linecache.updatecache):
    # XXX Important - do not move to the top of the file, it will
    # get unloaded on restart then!!!
    try:
        import sys
    except: #basically, we're in reload and our namespace is kinda trashed
        return updcache(filename)
        
    #import sys
    import os
    from stat import ST_MTIME,ST_SIZE
    
    if linecache.cache.has_key(filename):
        del linecache.cache[filename]
    if not filename or filename[0] + filename[-1] == '<>':
        return []
    fullname = filename
    try:
        stat = os.stat(fullname)
    except os.error, msg:
        # Is it one of ours?
        ret = getSource ( filename )
        if ret:
            return ret
        
        # Try looking through the module search path
        basename = os.path.split(filename)[1]
    
        for dirname in sys.path:
            fullname = os.path.join(dirname, basename)
            try:
                stat = os.stat(fullname)
                break
            except os.error:
                pass
            # No luck
            ## print '*** Cannot stat', filename, ':', msg
            return []
    try:
        fp = open(fullname, 'r')
        lines = fp.readlines()
        fp.close()
    except IOError, msg:
        ## print '*** Cannot open', fullname, ':', msg
        return []
    size, mtime = stat[ST_SIZE], stat[ST_MTIME]
    linecache.cache[filename] = size, mtime, lines, fullname
    return lines

def installIntoTraceback():
    linecache._updatecache = linecache.updatecache
    linecache.updatecache = updatecache

class _killGuard:
    def __init__(self):
        self.lc=linecache
    def __del__(self):
        self.lc.updatecache = self.lc._updatecache
        
installIntoTraceback()        
_kg = _killGuard()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.