1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-16 17:12:21 +03:00

Merge pull request #738 from EDCD/fix/revert-unicode-changes

Fix/revert unicode changes
This commit is contained in:
Athanasius 2020-10-06 16:22:32 +01:00 committed by GitHub
commit 97d42e8806
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 22 deletions

View File

@ -1094,27 +1094,47 @@ if __name__ == "__main__":
edmclogger.set_channels_loglevel(logging.DEBUG)
logger.info(f'Startup v{appversion} : Running on Python v{sys.version}')
logger.debug(f'''Platform: {sys.platform}
logger.debug(f'''Platform: {sys.platform} {sys.platform == "win32" and sys.getwindowsversion()}
argv[0]: {sys.argv[0]}
exec_prefix: {sys.exec_prefix}
executable: {sys.executable}
sys.path: {sys.path}'''
)
# Change locale to a utf8 one
# Log what we have at startup
# We prefer a UTF-8 encoding gets set, but older Windows versions have
# issues with this. From Windows 10 1903 onwards we can rely on the
# manifest ActiveCodePage to set this, but that is silently ignored on
# all previous Windows versions.
# Trying to set a UTF-8 encoding on those older versions will fail with
# locale.Error: unsupported locale setting
# but we do need to make the attempt for when we're running from source.
log_locale('Initial Locale')
# Make sure the local is actually set as per locale's idea of defaults
locale.setlocale(locale.LC_ALL, '')
log_locale('After LC_ALL defaults set')
# Now find out the current locale, mostly the language
locale_startup = locale.getlocale(locale.LC_CTYPE)
logger.debug(f'Locale LC_CTYPE: {locale_startup}')
# Now set that same language, but utf8 encoding (it was probably cp1252
# or equivalent for other languages).
# UTF-8, not utf8: <https://en.wikipedia.org/wiki/UTF-8#Naming>
locale.setlocale(locale.LC_ALL, (locale_startup[0], 'UTF-8'))
log_locale('After switching to UTF-8 encoding (same language)')
try:
locale.setlocale(locale.LC_ALL, '')
except locale.Error as e:
logger.error("Could not set LC_ALL to ''", exc_info=e)
else:
log_locale('After LC_ALL defaults set')
locale_startup = locale.getlocale(locale.LC_CTYPE)
logger.debug(f'Locale LC_CTYPE: {locale_startup}')
# Set that same language, but utf8 encoding (it was probably cp1252
# or equivalent for other languages).
# UTF-8, not utf8: <https://en.wikipedia.org/wiki/UTF-8#Naming>
try:
# locale_startup[0] is the 'language' portion
locale.setlocale(locale.LC_ALL, (locale_startup[0], 'UTF-8'))
except locale.Error as e:
logger.error(f"Could not set LC_ALL to ({locale_startup[0]}, 'UTF_8')", exc_info=e)
else:
log_locale('After switching to UTF-8 encoding (same language)')
# TODO: unittests in place of these
# logger.debug('Test from __main__')

View File

@ -183,7 +183,7 @@ if platform == 'darwin':
elif platform == 'win32':
import ctypes
import winreg
from ctypes.wintypes import HINSTANCE, HWND, LPCWSTR, LPWSTR, MAX_PATH, POINT, RECT, SIZE, UINT
from ctypes.wintypes import HINSTANCE, HWND, LPARAM, LPCWSTR, LPVOID, LPWSTR, MAX_PATH, POINT, RECT, SIZE, UINT
is_wine = False
try:
WINE_REGISTRY_KEY = r'HKEY_LOCAL_MACHINE\Software\Wine'
@ -194,6 +194,16 @@ elif platform == 'win32':
except OSError:
pass
# https://msdn.microsoft.com/en-us/library/windows/desktop/bb762115
BIF_RETURNONLYFSDIRS = 0x00000001
BIF_USENEWUI = 0x00000050
BFFM_INITIALIZED = 1
BFFM_SETSELECTION = 0x00000467
BrowseCallbackProc = ctypes.WINFUNCTYPE(ctypes.c_int, HWND, ctypes.c_uint, LPARAM, LPARAM)
class BROWSEINFO(ctypes.Structure):
_fields_ = [("hwndOwner", HWND), ("pidlRoot", LPVOID), ("pszDisplayName", LPWSTR), ("lpszTitle", LPCWSTR), ("ulFlags", UINT), ("lpfn", BrowseCallbackProc), ("lParam", LPCWSTR), ("iImage", ctypes.c_int)]
CalculatePopupWindowPosition = None
if not is_wine:
CalculatePopupWindowPosition = ctypes.windll.user32.CalculatePopupWindowPosition
@ -868,13 +878,43 @@ class PreferencesDialog(tk.Toplevel):
:param title: Title of the window
:param pathvar: the path to start the dialog on
"""
import tkinter.filedialog
directory = tkinter.filedialog.askdirectory(
parent=self,
initialdir=expanduser(pathvar.get()),
title=title,
mustexist=tk.TRUE
)
import locale
# If encoding isn't UTF-8 we can't use the tkinter dialog
current_locale = locale.getlocale(locale.LC_CTYPE)
from sys import platform as sys_platform
directory = None
if sys_platform == 'win32' and current_locale[1] not in ('utf8', 'UTF8', 'utf-8', 'UTF-8'):
def browsecallback(hwnd, uMsg, lParam, lpData):
# set initial folder
if uMsg == BFFM_INITIALIZED and lpData:
ctypes.windll.user32.SendMessageW(hwnd, BFFM_SETSELECTION, 1, lpData);
return 0
browseInfo = BROWSEINFO()
browseInfo.lpszTitle = title
browseInfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI
browseInfo.lpfn = BrowseCallbackProc(browsecallback)
browseInfo.lParam = pathvar.get().startswith('~') and join(config.home,
pathvar.get()[2:]) or pathvar.get()
ctypes.windll.ole32.CoInitialize(None)
pidl = ctypes.windll.shell32.SHBrowseForFolderW(ctypes.byref(browseInfo))
if pidl:
path = ctypes.create_unicode_buffer(MAX_PATH)
ctypes.windll.shell32.SHGetPathFromIDListW(pidl, path)
ctypes.windll.ole32.CoTaskMemFree(pidl)
directory = path.value
else:
directory = None
else:
import tkinter.filedialog
directory = tkinter.filedialog.askdirectory(
parent=self,
initialdir=expanduser(pathvar.get()),
title=title,
mustexist=tk.TRUE
)
if directory:
pathvar.set(directory)