1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-06-09 03:42:16 +03:00

Avoid os.path.expanduser on Windows.

expanduser() doesn't handle non-ASCII - http://bugs.python.org/issue13207
Fixes #70.
This commit is contained in:
Jonathan Harris 2016-01-02 20:31:33 +00:00
parent 8bf63e52f0
commit f96dc0ef6e
2 changed files with 17 additions and 7 deletions

View File

@ -22,6 +22,7 @@ elif platform=='win32':
CSIDL_PERSONAL = 0x0005 CSIDL_PERSONAL = 0x0005
CSIDL_LOCAL_APPDATA = 0x001C CSIDL_LOCAL_APPDATA = 0x001C
CSIDL_PROFILE = 0x0028
# _winreg that ships with Python 2 doesn't support unicode, so do this instead # _winreg that ships with Python 2 doesn't support unicode, so do this instead
from ctypes.wintypes import * from ctypes.wintypes import *
@ -87,6 +88,8 @@ class Config:
if not isdir(self.app_dir): if not isdir(self.app_dir):
mkdir(self.app_dir) mkdir(self.app_dir)
self.home = expanduser('~')
if not getattr(sys, 'frozen', False): if not getattr(sys, 'frozen', False):
# Don't use Python's settings if interactive # Don't use Python's settings if interactive
self.bundle = 'uk.org.marginal.%s' % appname.lower() self.bundle = 'uk.org.marginal.%s' % appname.lower()
@ -127,6 +130,10 @@ class Config:
if not isdir(self.app_dir): if not isdir(self.app_dir):
mkdir(self.app_dir) mkdir(self.app_dir)
# expanduser in Python 2 on Windows doesn't handle non-ASCII - http://bugs.python.org/issue13207
ctypes.windll.shell32.SHGetSpecialFolderPathW(0, buf, CSIDL_PROFILE, 0)
self.home = buf.value
self.hkey = HKEY() self.hkey = HKEY()
disposition = DWORD() disposition = DWORD()
if RegCreateKeyEx(HKEY_CURRENT_USER, r'Software\Marginal\EDMarketConnector', 0, None, 0, KEY_ALL_ACCESS, None, ctypes.byref(self.hkey), ctypes.byref(disposition)): if RegCreateKeyEx(HKEY_CURRENT_USER, r'Software\Marginal\EDMarketConnector', 0, None, 0, KEY_ALL_ACCESS, None, ctypes.byref(self.hkey), ctypes.byref(disposition)):
@ -152,6 +159,7 @@ class Config:
RegCloseKey(sparklekey) RegCloseKey(sparklekey)
if not self.get('outdir') or not isdir(self.get('outdir')): if not self.get('outdir') or not isdir(self.get('outdir')):
buf = ctypes.create_unicode_buffer(MAX_PATH)
ctypes.windll.shell32.SHGetSpecialFolderPathW(0, buf, CSIDL_PERSONAL, 0) ctypes.windll.shell32.SHGetSpecialFolderPathW(0, buf, CSIDL_PERSONAL, 0)
self.set('outdir', buf.value) self.set('outdir', buf.value)
@ -197,6 +205,8 @@ class Config:
if not isdir(self.app_dir): if not isdir(self.app_dir):
makedirs(self.app_dir) makedirs(self.app_dir)
self.home = expanduser('~')
self.filename = join(getenv('XDG_CONFIG_HOME', expanduser('~/.config')), appname, '%s.ini' % appname) self.filename = join(getenv('XDG_CONFIG_HOME', expanduser('~/.config')), appname, '%s.ini' % appname)
if not isdir(dirname(self.filename)): if not isdir(dirname(self.filename)):
makedirs(dirname(self.filename)) makedirs(dirname(self.filename))

View File

@ -1,7 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from os.path import dirname, expanduser, isdir, sep from os.path import dirname, expanduser, isdir, join, sep
from sys import platform from sys import platform
import Tkinter as tk import Tkinter as tk
@ -136,8 +136,8 @@ class PreferencesDialog(tk.Toplevel):
self.outdir_label = nb.Label(outframe, text=_('File location')) # Section heading in settings self.outdir_label = nb.Label(outframe, text=_('File location')) # Section heading in settings
self.outdir_label.grid(padx=BUTTONX, sticky=tk.W) self.outdir_label.grid(padx=BUTTONX, sticky=tk.W)
self.outdir = nb.Entry(outframe, takefocus=False) self.outdir = nb.Entry(outframe, takefocus=False)
if config.get('outdir').startswith(expanduser('~')): if config.get('outdir').startswith(config.home):
self.outdir.insert(0, '~' + config.get('outdir')[len(expanduser('~')):]) self.outdir.insert(0, '~' + config.get('outdir')[len(config.home):])
else: else:
self.outdir.insert(0, config.get('outdir')) self.outdir.insert(0, config.get('outdir'))
self.outdir.grid(row=20, padx=(PADX,0), sticky=tk.EW) self.outdir.grid(row=20, padx=(PADX,0), sticky=tk.EW)
@ -286,7 +286,7 @@ class PreferencesDialog(tk.Toplevel):
browseInfo.lpszTitle = _('File location') browseInfo.lpszTitle = _('File location')
browseInfo.ulFlags = BIF_RETURNONLYFSDIRS|BIF_USENEWUI browseInfo.ulFlags = BIF_RETURNONLYFSDIRS|BIF_USENEWUI
browseInfo.lpfn = BrowseCallbackProc(browsecallback) browseInfo.lpfn = BrowseCallbackProc(browsecallback)
browseInfo.lParam = expanduser(self.outdir.get()) browseInfo.lParam = self.outdir.get().startswith('~') and join(config.home, self.outdir.get()[1:]) or self.outdir.get()
ctypes.windll.ole32.CoInitialize(None) ctypes.windll.ole32.CoInitialize(None)
pidl = ctypes.windll.shell32.SHBrowseForFolderW(ctypes.byref(browseInfo)) pidl = ctypes.windll.shell32.SHBrowseForFolderW(ctypes.byref(browseInfo))
if pidl: if pidl:
@ -300,8 +300,8 @@ class PreferencesDialog(tk.Toplevel):
if d: if d:
self.outdir['state'] = tk.NORMAL # must be writable to update self.outdir['state'] = tk.NORMAL # must be writable to update
self.outdir.delete(0, tk.END) self.outdir.delete(0, tk.END)
if d.startswith(expanduser('~')): if d.startswith(config.home):
self.outdir.insert(0, '~' + d[len(expanduser('~')):]) self.outdir.insert(0, '~' + d[len(config.home):])
else: else:
self.outdir.insert(0, d) self.outdir.insert(0, d)
self.outdir['state'] = 'readonly' self.outdir['state'] = 'readonly'
@ -362,7 +362,7 @@ class PreferencesDialog(tk.Toplevel):
(self.out_ship_coriolis.get() and config.OUT_SHIP_CORIOLIS) + (self.out_ship_coriolis.get() and config.OUT_SHIP_CORIOLIS) +
(self.out_log_edsm.get() and config.OUT_LOG_EDSM) + (self.out_log_edsm.get() and config.OUT_LOG_EDSM) +
(self.out_log_auto.get() and config.OUT_LOG_AUTO)) (self.out_log_auto.get() and config.OUT_LOG_AUTO))
config.set('outdir', expanduser(self.outdir.get())) config.set('outdir', self.outdir.get().startswith('~') and join(config.home, self.outdir.get()[1:]) or self.outdir.get())
config.set('edsm_cmdrname', self.edsm_cmdr.get().strip()) config.set('edsm_cmdrname', self.edsm_cmdr.get().strip())
config.set('edsm_apikey', self.edsm_apikey.get().strip()) config.set('edsm_apikey', self.edsm_apikey.get().strip())