Adopt appdirs/usersettings for py3 and qutebrowser

This commit is contained in:
Florian Bruhin 2014-01-20 11:33:47 +01:00
parent 97c2e7aae9
commit 930f84906e
2 changed files with 14 additions and 21 deletions

View File

@ -20,13 +20,6 @@ __version__ = '.'.join(map(str, __version_info__))
import sys import sys
import os import os
PY3 = sys.version_info[0] == 3
if PY3:
unicode = str
def user_data_dir(appname=None, appauthor=None, version=None, roaming=False): 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. r"""Return full path to the user-specific data dir for this application.
@ -372,7 +365,7 @@ def _get_win_folder_from_registry(csidl_name):
registry for this guarantees us the correct answer for all CSIDL_* registry for this guarantees us the correct answer for all CSIDL_*
names. names.
""" """
import _winreg import winreg
shell_folder_name = { shell_folder_name = {
"CSIDL_APPDATA": "AppData", "CSIDL_APPDATA": "AppData",
@ -380,9 +373,9 @@ def _get_win_folder_from_registry(csidl_name):
"CSIDL_LOCAL_APPDATA": "Local AppData", "CSIDL_LOCAL_APPDATA": "Local AppData",
}[csidl_name] }[csidl_name]
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders") r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
dir, type = _winreg.QueryValueEx(key, shell_folder_name) dir, type = winreg.QueryValueEx(key, shell_folder_name)
return dir return dir
def _get_win_folder_with_pywin32(csidl_name): def _get_win_folder_with_pywin32(csidl_name):
@ -392,7 +385,7 @@ def _get_win_folder_with_pywin32(csidl_name):
# not return unicode strings when there is unicode data in the # not return unicode strings when there is unicode data in the
# path. # path.
try: try:
dir = unicode(dir) dir = str(dir)
# Downgrade to short path name if have highbit chars. See # Downgrade to short path name if have highbit chars. See
# <http://bugs.activestate.com/show_bug.cgi?id=85099>. # <http://bugs.activestate.com/show_bug.cgi?id=85099>.
@ -463,15 +456,15 @@ if __name__ == "__main__":
print("-- app dirs (with optional 'version')") print("-- app dirs (with optional 'version')")
dirs = AppDirs(appname, appauthor, version="1.0") dirs = AppDirs(appname, appauthor, version="1.0")
for prop in props: 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')") print("\n-- app dirs (without optional 'version')")
dirs = AppDirs(appname, appauthor) dirs = AppDirs(appname, appauthor)
for prop in props: 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')") print("\n-- app dirs (without optional 'appauthor')")
dirs = AppDirs(appname) dirs = AppDirs(appname)
for prop in props: for prop in props:
print("%s: %s" % (prop, getattr(dirs, prop))) print(("%s: %s" % (prop, getattr(dirs, prop))))

View File

@ -3,10 +3,10 @@
Provide interface for persistent portable editable user settings Provide interface for persistent portable editable user settings
""" """
import os import os
import ConfigParser import configparser
import ast import ast
import appdirs import qutebrowser.utils.appdirs as appdirs
class Settings(dict): class Settings(dict):
@ -38,13 +38,13 @@ class Settings(dict):
def load_settings(self): def load_settings(self):
""" Set default values and parse stored settings file """ """ Set default values and parse stored settings file """
# Set the defaults # Set the defaults
for key, value in self._settings_defaults.items(): for key, value in list(self._settings_defaults.items()):
if key not in self._settings_types: if key not in self._settings_types:
self._settings_types[key] = str self._settings_types[key] = str
super(Settings, self).__setitem__(key, value) super(Settings, self).__setitem__(key, value)
# Load the stored values # Load the stored values
parser = ConfigParser.RawConfigParser() parser = configparser.RawConfigParser()
try: try:
with open(self.settings_file, 'r') as settings_fp: with open(self.settings_file, 'r') as settings_fp:
parser.readfp(settings_fp) parser.readfp(settings_fp)
@ -75,10 +75,10 @@ class Settings(dict):
def save_settings(self): def save_settings(self):
""" Write the settings data to disk """ """ Write the settings data to disk """
if not os.path.exists(self.settings_directory): if not os.path.exists(self.settings_directory):
os.makedirs(self.settings_directory, 0755) os.makedirs(self.settings_directory, 0o755)
parser = ConfigParser.RawConfigParser() parser = configparser.RawConfigParser()
parser.add_section('settings') parser.add_section('settings')
for key, value in self.items(): for key, value in list(self.items()):
parser.set('settings', key, value) parser.set('settings', key, value)
with open(self.settings_file, 'wb') as settings_fp: with open(self.settings_file, 'wb') as settings_fp:
parser.write(settings_fp) parser.write(settings_fp)