util.py :  » Blog » Frog » FrogComplete-1.8 » snakeserver » 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 » Blog » Frog 
Frog » FrogComplete 1.8 » snakeserver » util.py
#############################################################################
#
#  $Id: util.py,v 1.6 2005/02/18 13:03:12 irmen Exp $
#  Utility code
#
#  This is part of "Snakelets" - Python Web Application Server
#  which is (c) Irmen de Jong - irmen@users.sourceforge.net
#
#############################################################################


#
#   Utility class used to hold HTTP headers (case insensitive dict)
#   We need this to avoid duplicate headers.
#   Thankfully HTTP header fields are case-insensitive :-)
#
class NormalizedHeaderDict(dict):
    def __init__(self, *args):
        if len(args)==1 and type(args[0]) is dict:
            dict.__init__(self)
            d=args[0]
            for key in d:
                self[key.title()]=d[key]   # Titlecased
        else:
            dict.__init__(self, *args)
    def __contains__(self,key):
        return dict.__contains__(self,key.title())
    def __delitem__(self, item):
        return dict.__delitem__(self, item.title())
    def __getitem__(self,item):
        return dict.__getitem__(self, item.title())
    def __setitem__(self,item,value):
        return dict.__setitem__(self, item.title(), value)
    def get(self,item):
        return dict.get(self,item.title())
    def update(self,d):
        for key in d:
            self[key.title()]=d[key]
    def has_key(self,key):
        return dict.has_key(self,key.title())



#
#   just an empty class to store stuff into, it makes the 'context' storage of a snakelet.
#

class ContextContainer:
    pass



# Caching directory lister, outputs (filelist,dirlist) tuple.
# If modification time of the directory is different, re-read directory.
# If it's the same, return cached contents.
# Does not raise errors when the path cannot be found;
# returns empty lists in that case.

# Based upon dircache.py

import os

_listdir_cache = {}

def listdir(path):
    try:
        cached_mtime, files, directories = _listdir_cache[path]
        del _listdir_cache[path]
    except KeyError:
        cached_mtime, files, directories = -1, [], []
    try:
        mtime = os.stat(path).st_mtime
    except os.error:
        return [],[]
    if mtime <> cached_mtime:
        try:
            lizt = os.listdir(path)
        except os.error:
            return [],[]
        files=[]
        directories=[]
        for e in lizt:
            if os.path.isdir(os.path.join(path,e)):
                directories.append(e)
            else:
                files.append(e)
    _listdir_cache[path] = mtime, files, directories
    return files,directories

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