UserDir.py :  » RSS » XPN » xpn-1.2.6 » xpn_src » 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 » RSS » XPN 
XPN » xpn 1.2.6 » xpn_src » UserDir.py
import sys
import os


def getHomeDir():
    ''' Try to find user's home directory, otherwise return current directory.'''
    try:
        path1=os.path.expanduser("~")
    except:
        path1=""
    try:
        path2=os.environ["HOME"]
    except:
        path2=""
    try:
        path3=os.path.join(os.environ["HOMEDRIVE"],os.environ["HOMEPATH"])
    except:
        path3=""
    try:
        path4=os.environ["USERPROFILE"]
    except:
        path4=""

    if not os.path.exists(path1):
        if not os.path.exists(path2):
            if not os.path.exists(path3):
                if not os.path.exists(path4):
                    return os.getcwd()
                else: return path4
            else: return path3
        else: return path2
    else: return path1

def get_wdir():
    return wdir

wdir=""

class UserDir:
    def __init__(self,cwd=False,userHome=False,customPath=""):
        '''Init user dir.

        Arguments:
        cwd       :if True XPN will use it's own directory
        userHome  :if True XPN will create a .xpn directory inside user's home directory.
        customPath:if True XPN will create a .xpn directry inside user's defined path.
        '''
        global wdir
        if cwd:
            self.dir=""
        elif userHome:
            self.dir=os.path.join(getHomeDir(),".xpn")
        else:
            if os.path.exists(customPath):
                self.dir=os.path.join(customPath,".xpn")
            else:
                self.dir=""
        wdir=self.dir

    def Create(self):
        if not os.path.isdir(self.dir) and self.dir:
            if not os.path.exists(self.dir):
                try:
                    os.mkdir(self.dir)
                except:
                    print "Error: Can't create \"%s\"." % self.dir
                    return 1
            else:
                print "Error: Please remove \"%s\" file." % self.dir
                return 2
            #then check write access
        try:
            f=file(os.path.join(self.dir,"write-test"),"w")
        except IOError:
            print "Error: Can't write in \"%s\"." % self.dir
            return 3
        else:
            f.write("test")
            f.close()
            os.remove(os.path.join(self.dir,"write-test"))
        return 0
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.