util.py :  » Web-Server » Snakelets » Snakelets-1.50 » 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 » Web Server » Snakelets 
Snakelets » Snakelets 1.50 » snakeserver » util.py
#############################################################################
#
#  $Id: util.py,v 1.11 2008/08/01 21:49:18 irmen Exp $
#  Utility code
#
#  This is part of "Snakelets" - Python Web Application Server
#  which is (c) Irmen de Jong - irmen@users.sourceforge.net
#
#############################################################################

import os


#
#   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(object):
    pass



# Directory lister, outputs (filelist,dirlist) tuple.
# Does not raise errors when the path cannot be found;
# returns empty lists in that case.
def listdir(path):
    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)
    return files,directories

def getCurrentUserAndGroupId():
    try:
        return os.getuid(), os.getgid()
    except Exception,x:
        return None

def getCurrentUserAndGroupName():
    cur=getCurrentUserAndGroupId()
    try:
        import pwd,grp
    except ImportError:
        return None
    else:
        curUserName = curGroupName = None
        if cur:
            curUserName = pwd.getpwuid(cur[0]).pw_name
            curGroupName = grp.getgrgid(cur[1]).gr_name
        return curUserName, curGroupName

    
def printCurrentUserAndGroupInfo():
    cur=getCurrentUserAndGroupId()
    cur_names=getCurrentUserAndGroupName()
    if cur:
        print "Current user/group: %s / %s" % (cur, cur_names)  
    else:
        print "Could not determine current user/group."

def changeUserAndGroup(userName, groupName):
    if groupName:
        try:
            import grp
        except ImportError:
            print "Your system doesn't support unix user groups."
        else:
            grpid = grp.getgrnam(groupName).gr_gid
            os.setgid(grpid)
    if userName:
        try:
            import pwd
        except ImportError:
            print "Your system doesn't support unix user ids."
        else:
            userid = pwd.getpwnam(userName).pw_uid
            os.setuid(userid)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.