Changeset 3147

Show
Ignore:
Timestamp:
05/31/2007 09:16:37 AM (2 years ago)
Author:
paul
Message:

Refactored getUserDaboDirectory and getUserHomeDirectory to use the Windows API to get the information, if available. Thanks to Anders J. Munch from wxPython-users for posting the relevant code. Previously, we were relying on environmental variables that may or may not exist on a given user system. \n\nRenamed getUserDaboDirectory to getUserAppDataDirectory.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/dPref.py

    r3145 r3147  
    6565                datetime.datetime: "datetime", self._noneType: "none"} 
    6666        if crs is None: 
    67             prefdir = utils.getUserDaboDirectory(appName) 
     67            prefdir = utils.getUserAppDataDirectory(appName) 
    6868            self._cxn = dabo.db.dConnection(connectInfo={"dbType": "SQLite", 
    6969                    "database": os.path.join(prefdir, "DaboPreferences.db")}) 
  • trunk/dabo/lib/utils.py

    r3145 r3147  
    1313import sys 
    1414 
     15try: 
     16    from win32com.shell import shell, shellcon 
     17except ImportError: 
     18    shell, shellcon = None, None 
     19 
    1520 
    1621def reverseText(tx): 
     
    5459    If the home directory cannot be determined, return None. 
    5560    """ 
     61    hd = None 
     62 
     63    # If we are on Windows and win32com is available, get the user home 
     64    # directory using the Windows API: 
     65    if shell and shellcon: 
     66        return shell.SHGetFolderPath(0, shellcon.CSIDL_PROFILE, 0, 0) 
     67 
    5668    # os.path.expanduser should work on all posix systems (*nix, Mac, and some 
    5769    # Windows NT setups): 
    58     hd = None 
    5970    try: 
    6071        hd = os.path.expanduser("~") 
     
    7788 
    7889 
    79 def getUserDaboDirectory(appName="Dabo"): 
     90def getUserAppDataDirectory(appName="Dabo"): 
    8091    """Return the directory where Dabo can save user preference and setting information. 
    8192 
     
    93104    """ 
    94105    dd = None 
    95     if sys.platform in ("win32", ): 
     106 
     107    if sys.platform not in ("win32",): 
     108        # On Unix, change appname to lower, don't allow spaces, and prepend a ".": 
     109        appName = ".%s" % appNameappName.lower().replace(" ", "_") 
     110 
     111    # First, on Windows, try the Windows API function: 
     112    if shell and shellcon: 
     113        dd = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0) 
     114        print "!", dd 
     115    if dd is None and sys.platform == "win32": 
     116        # We are on Windows, but win32com wasn't installed. Look for the APPDATA 
     117        # environmental variable: 
    96118        dd = os.environ.get("APPDATA") 
    97         if dd is not None: 
    98             dd = os.path.join(dd, appName) 
     119 
    99120 
    100121    if dd is None: 
    101         # On Unix, change appname to lower and don't allow spaces: 
    102         appName = appName.lower().replace(" ", "_") 
     122        # We are either not on Windows, or we couldn't locate the directory for  
     123        # whatever reason. Try going off the home directory: 
    103124        dd = getUserHomeDirectory() 
    104125 
    105         if dd is not None: 
    106             dd = os.path.join(dd, ".%s" % appName.lower()) 
    107  
    108     if not os.path.exists(dd): 
    109         # try to create the dabo directory: 
    110         try: 
    111             os.makedirs(dd) 
    112         except: 
    113             print "Couldn't create the user setting directory (%s)." % dd 
    114             dd = None 
    115  
     126 
     127    if dd is not None: 
     128        dd = os.path.join(dd, appName) 
     129        if not os.path.exists(dd): 
     130            # try to create the dabo directory: 
     131            try: 
     132                os.makedirs(dd) 
     133            except: 
     134                sys.stderr.write("Couldn't create the user setting directory (%s)." % dd) 
     135                dd = None 
    116136    return dd 
    117137