From dd3d05111249057fa5efb3d1de7c2055e57523ae Mon Sep 17 00:00:00 2001 From: David Vierra Date: Sun, 2 Dec 2012 03:23:13 -1000 Subject: [PATCH] Cleanup: Finding directories in mclevelbase.py --- mclevelbase.py | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/mclevelbase.py b/mclevelbase.py index 53df03a..d14fedf 100644 --- a/mclevelbase.py +++ b/mclevelbase.py @@ -6,6 +6,7 @@ Created on Jul 22, 2011 from contextlib import contextmanager from logging import getLogger +import sys import os log = getLogger(__name__) @@ -41,47 +42,41 @@ def exhaust(_iter): return i -# we need to decode file paths from environment variables or else we get an error -# if they are formatted or joined to a unicode string -import sys -if sys.platform == "win32": +def win32_appdata(): # try to use win32 api to get the AppData folder since python doesn't populate os.environ with unicode strings. try: import win32com.client objShell = win32com.client.Dispatch("WScript.Shell") - appDataDir = objShell.SpecialFolders("AppData") + return objShell.SpecialFolders("AppData") except Exception, e: print "Error while getting AppData folder using WScript.Shell.SpecialFolders: {0!r}".format(e) try: from win32com.shell import shell, shellcon - appDataDir = shell.SHGetPathFromIDListEx( + return shell.SHGetPathFromIDListEx( shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_APPDATA) ) except Exception, e: print "Error while getting AppData folder using SHGetSpecialFolderLocation: {0!r}".format(e) - appDataDir = os.environ['APPDATA'].decode(sys.getfilesystemencoding()) + return os.environ['APPDATA'].decode(sys.getfilesystemencoding()) +if sys.platform == "win32": + appDataDir = win32_appdata() minecraftDir = os.path.join(appDataDir, u".minecraft") + appSupportDir = os.path.join(appDataDir, u"pymclevel") elif sys.platform == "darwin": appDataDir = os.path.expanduser(u"~/Library/Application Support") - minecraftDir = os.path.join(appDataDir, u"minecraft") - minecraftDir.decode(sys.getfilesystemencoding()) + appSupportDir = os.path.expanduser(u"~/Library/Application Support/pymclevel/") + else: appDataDir = os.path.expanduser(u"~") minecraftDir = os.path.expanduser(u"~/.minecraft") - + appSupportDir = os.path.expanduser(u"~/.pymclevel") saveFileDir = os.path.join(minecraftDir, u"saves") -if sys.platform == "win32": - appSupportDir = os.path.join(appDataDir, u"pymclevel") -elif sys.platform == "darwin": - appSupportDir = os.path.expanduser(u"~/Library/Application Support/pymclevel/") -else: - appSupportDir = os.path.expanduser(u"~/.pymclevel")