diff --git a/qutebrowser/utils/appdirs.py b/qutebrowser/utils/appdirs.py index 4f4a292ec..6c558769d 100644 --- a/qutebrowser/utils/appdirs.py +++ b/qutebrowser/utils/appdirs.py @@ -13,13 +13,19 @@ See for details and usage. # - Mac OS X: http://developer.apple.com/documentation/MacOSX/Conceptual/BPFileSystem/index.html # - XDG spec for Un*x: http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html -__version_info__ = (1, 3, 0) +__version_info__ = (1, 4, 0) __version__ = '.'.join(map(str, __version_info__)) import sys import os +PY3 = sys.version_info[0] == 3 + +if PY3: + unicode = str + + def user_data_dir(appname=None, appauthor=None, version=None, roaming=False): r"""Return full path to the user-specific data dir for this application. @@ -115,12 +121,12 @@ def site_data_dir(appname=None, appauthor=None, version=None, multipath=False): # XDG default for $XDG_DATA_DIRS # only first, if multipath is False path = os.getenv('XDG_DATA_DIRS', - os.pathsep.join(['/usr/local/share', '/usr/share'])) - pathlist = [ os.path.expanduser(x.rstrip(os.sep)) for x in path.split(os.pathsep) ] + os.pathsep.join(['/usr/local/share', '/usr/share'])) + pathlist = [os.path.expanduser(x.rstrip(os.sep)) for x in path.split(os.pathsep)] if appname: if version: appname = os.path.join(appname, version) - pathlist = [ os.sep.join([x, appname]) for x in pathlist ] + pathlist = [os.sep.join([x, appname]) for x in pathlist] if multipath: path = os.pathsep.join(pathlist) @@ -161,7 +167,7 @@ def user_config_dir(appname=None, appauthor=None, version=None, roaming=False): For Unix, we follow the XDG spec and support $XDG_CONFIG_HOME. That means, by deafult "~/.config/". """ - if sys.platform in [ "win32", "darwin" ]: + if sys.platform in ["win32", "darwin"]: path = user_data_dir(appname, appauthor, None, roaming) else: path = os.getenv('XDG_CONFIG_HOME', os.path.expanduser("~/.config")) @@ -201,7 +207,7 @@ def site_config_dir(appname=None, appauthor=None, version=None, multipath=False) WARNING: Do not use this on Windows. See the Vista-Fail note above for why. """ - if sys.platform in [ "win32", "darwin" ]: + if sys.platform in ["win32", "darwin"]: path = site_data_dir(appname, appauthor) if appname and version: path = os.path.join(path, version) @@ -209,11 +215,11 @@ def site_config_dir(appname=None, appauthor=None, version=None, multipath=False) # XDG default for $XDG_CONFIG_DIRS # only first, if multipath is False path = os.getenv('XDG_CONFIG_DIRS', '/etc/xdg') - pathlist = [ os.path.expanduser(x.rstrip(os.sep)) for x in path.split(os.pathsep) ] + pathlist = [os.path.expanduser(x.rstrip(os.sep)) for x in path.split(os.pathsep)] if appname: if version: appname = os.path.join(appname, version) - pathlist = [ os.sep.join([x, appname]) for x in pathlist ] + pathlist = [os.sep.join([x, appname]) for x in pathlist] if multipath: path = os.pathsep.join(pathlist) @@ -221,6 +227,7 @@ def site_config_dir(appname=None, appauthor=None, version=None, multipath=False) path = pathlist[0] return path + def user_cache_dir(appname=None, appauthor=None, version=None, opinion=True): r"""Return full path to the user-specific cache dir for this application. @@ -273,6 +280,7 @@ def user_cache_dir(appname=None, appauthor=None, version=None, opinion=True): path = os.path.join(path, version) return path + def user_log_dir(appname=None, appauthor=None, version=None, opinion=True): r"""Return full path to the user-specific log dir for this application. @@ -309,11 +317,13 @@ def user_log_dir(appname=None, appauthor=None, version=None, opinion=True): os.path.expanduser('~/Library/Logs'), appname) elif sys.platform == "win32": - path = user_data_dir(appname, appauthor, version); version=False + path = user_data_dir(appname, appauthor, version) + version = False if opinion: path = os.path.join(path, "Logs") else: - path = user_cache_dir(appname, appauthor, version); version=False + path = user_cache_dir(appname, appauthor, version) + version = False if opinion: path = os.path.join(path, "log") if appname and version: @@ -323,39 +333,43 @@ def user_log_dir(appname=None, appauthor=None, version=None, opinion=True): class AppDirs(object): """Convenience wrapper for getting application dirs.""" - def __init__(self, appname, appauthor=None, version=None, - roaming=False, multipath=False): + def __init__(self, appname, appauthor=None, version=None, roaming=False, + multipath=False): self.appname = appname self.appauthor = appauthor self.version = version self.roaming = roaming self.multipath = multipath + @property def user_data_dir(self): return user_data_dir(self.appname, self.appauthor, - version=self.version, roaming=self.roaming) + version=self.version, roaming=self.roaming) + @property def site_data_dir(self): return site_data_dir(self.appname, self.appauthor, - version=self.version, multipath=self.multipath) + version=self.version, multipath=self.multipath) + @property def user_config_dir(self): return user_config_dir(self.appname, self.appauthor, - version=self.version, roaming=self.roaming) + version=self.version, roaming=self.roaming) + @property def site_config_dir(self): return site_data_dir(self.appname, self.appauthor, - version=self.version, multipath=self.multipath) + version=self.version, multipath=self.multipath) + @property def user_cache_dir(self): return user_cache_dir(self.appname, self.appauthor, - version=self.version) + version=self.version) + @property def user_log_dir(self): return user_log_dir(self.appname, self.appauthor, - version=self.version) - - + version=self.version) #---- internal support stuff @@ -365,7 +379,10 @@ def _get_win_folder_from_registry(csidl_name): registry for this guarantees us the correct answer for all CSIDL_* names. """ - import winreg + if PY3: + import winreg as _winreg + else: + import _winreg shell_folder_name = { "CSIDL_APPDATA": "AppData", @@ -373,11 +390,14 @@ def _get_win_folder_from_registry(csidl_name): "CSIDL_LOCAL_APPDATA": "Local AppData", }[csidl_name] - key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, - r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders") - dir, type = winreg.QueryValueEx(key, shell_folder_name) + key = _winreg.OpenKey( + _winreg.HKEY_CURRENT_USER, + r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" + ) + dir, type = _winreg.QueryValueEx(key, shell_folder_name) return dir + def _get_win_folder_with_pywin32(csidl_name): from win32com.shell import shellcon, shell dir = shell.SHGetFolderPath(0, getattr(shellcon, csidl_name), 0, 0) @@ -385,7 +405,7 @@ def _get_win_folder_with_pywin32(csidl_name): # not return unicode strings when there is unicode data in the # path. try: - dir = str(dir) + dir = unicode(dir) # Downgrade to short path name if have highbit chars. See # . @@ -404,6 +424,7 @@ def _get_win_folder_with_pywin32(csidl_name): pass return dir + def _get_win_folder_with_ctypes(csidl_name): import ctypes @@ -442,7 +463,6 @@ if sys.platform == "win32": _get_win_folder = _get_win_folder_from_registry - #---- self test code if __name__ == "__main__": @@ -456,15 +476,14 @@ if __name__ == "__main__": print("-- app dirs (with optional 'version')") dirs = AppDirs(appname, appauthor, version="1.0") for prop in props: - print(("%s: %s" % (prop, getattr(dirs, prop)))) + print("%s: %s" % (prop, getattr(dirs, prop))) print("\n-- app dirs (without optional 'version')") dirs = AppDirs(appname, appauthor) for prop in props: - print(("%s: %s" % (prop, getattr(dirs, prop)))) + print("%s: %s" % (prop, getattr(dirs, prop))) print("\n-- app dirs (without optional 'appauthor')") dirs = AppDirs(appname) for prop in props: - print(("%s: %s" % (prop, getattr(dirs, prop)))) - + print("%s: %s" % (prop, getattr(dirs, prop)))