Cleanup: Finding directories in mclevelbase.py

This commit is contained in:
David Vierra 2012-12-02 03:23:13 -10:00
parent 250f479132
commit dd3d051112

View File

@ -6,6 +6,7 @@ Created on Jul 22, 2011
from contextlib import contextmanager from contextlib import contextmanager
from logging import getLogger from logging import getLogger
import sys
import os import os
log = getLogger(__name__) log = getLogger(__name__)
@ -41,47 +42,41 @@ def exhaust(_iter):
return i 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 to use win32 api to get the AppData folder since python doesn't populate os.environ with unicode strings.
try: try:
import win32com.client import win32com.client
objShell = win32com.client.Dispatch("WScript.Shell") objShell = win32com.client.Dispatch("WScript.Shell")
appDataDir = objShell.SpecialFolders("AppData") return objShell.SpecialFolders("AppData")
except Exception, e: except Exception, e:
print "Error while getting AppData folder using WScript.Shell.SpecialFolders: {0!r}".format(e) print "Error while getting AppData folder using WScript.Shell.SpecialFolders: {0!r}".format(e)
try: try:
from win32com.shell import shell, shellcon from win32com.shell import shell, shellcon
appDataDir = shell.SHGetPathFromIDListEx( return shell.SHGetPathFromIDListEx(
shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_APPDATA) shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_APPDATA)
) )
except Exception, e: except Exception, e:
print "Error while getting AppData folder using SHGetSpecialFolderLocation: {0!r}".format(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") minecraftDir = os.path.join(appDataDir, u".minecraft")
appSupportDir = os.path.join(appDataDir, u"pymclevel")
elif sys.platform == "darwin": elif sys.platform == "darwin":
appDataDir = os.path.expanduser(u"~/Library/Application Support") appDataDir = os.path.expanduser(u"~/Library/Application Support")
minecraftDir = os.path.join(appDataDir, u"minecraft") minecraftDir = os.path.join(appDataDir, u"minecraft")
minecraftDir.decode(sys.getfilesystemencoding()) appSupportDir = os.path.expanduser(u"~/Library/Application Support/pymclevel/")
else: else:
appDataDir = os.path.expanduser(u"~") appDataDir = os.path.expanduser(u"~")
minecraftDir = os.path.expanduser(u"~/.minecraft") minecraftDir = os.path.expanduser(u"~/.minecraft")
appSupportDir = os.path.expanduser(u"~/.pymclevel")
saveFileDir = os.path.join(minecraftDir, u"saves") 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")