Add support for more encodings for profiles.ini

This commit is contained in:
2Shirt 2021-10-27 20:01:05 -06:00
parent 49e8d5c894
commit 545cc20e48
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -80,6 +80,15 @@ from wk.std import (
# STATIC VARIABLES # STATIC VARIABLES
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
CONEMU_EXE = get_tool_path('ConEmu', 'ConEmu', check=False) CONEMU_EXE = get_tool_path('ConEmu', 'ConEmu', check=False)
KNOWN_ENCODINGS = (
'utf-8',
'utf-16',
'utf-32',
'utf-16-be',
'utf-16-le',
'utf-32-be',
'utf-32-le',
)
IN_CONEMU = 'ConEmuPID' in os.environ IN_CONEMU = 'ConEmuPID' in os.environ
MENU_PRESETS = Menu() MENU_PRESETS = Menu()
PROGRAMFILES_32 = os.environ.get( PROGRAMFILES_32 = os.environ.get(
@ -644,6 +653,7 @@ def install_firefox():
profile then it is reverted so the original existing profile instead. profile then it is reverted so the original existing profile instead.
""" """
current_default_profile = None current_default_profile = None
encoding = None
firefox_exe = get_path_obj( firefox_exe = get_path_obj(
f'{os.environ["PROGRAMFILES"]}/Mozilla Firefox/firefox.exe', f'{os.environ["PROGRAMFILES"]}/Mozilla Firefox/firefox.exe',
) )
@ -664,7 +674,9 @@ def install_firefox():
# Save current default profile # Save current default profile
if profiles_ini.exists(): if profiles_ini.exists():
current_default_profile = get_firefox_default_profile(profiles_ini) current_default_profile, encoding = get_firefox_default_profile(
profiles_ini,
)
# Uninstall Firefox if needed # Uninstall Firefox if needed
if ARCH == '64' and program_path_32bit_exists: if ARCH == '64' and program_path_32bit_exists:
@ -680,13 +692,13 @@ def install_firefox():
# Check if default profile changed # Check if default profile changed
if current_default_profile: if current_default_profile:
new_default = get_firefox_default_profile(profiles_ini) new_default, encoding = get_firefox_default_profile(profiles_ini)
revert_default = new_default and new_default != current_default_profile revert_default = new_default and new_default != current_default_profile
# Revert default profile if needed # Revert default profile if needed
if revert_default: if revert_default:
out = [] out = []
for line in profiles_ini.read_text(encoding='utf-8').splitlines(): for line in profiles_ini.read_text(encoding=encoding).splitlines():
if 'Default=Profile' in line: if 'Default=Profile' in line:
out.append(f'Default={current_default_profile}') out.append(f'Default={current_default_profile}')
else: else:
@ -817,21 +829,31 @@ def uninstall_firefox():
# Misc Functions # Misc Functions
def get_firefox_default_profile(profiles_ini): def get_firefox_default_profile(profiles_ini):
"""Get Firefox default profile, returns pathlib.Path or None.""" """Get Firefox default profile, returns(pathlib.Path, encoding) or None."""
default_profile = None default_profile = None
encoding = None
parser = None parser = None
# Bail early # Bail early
if not profiles_ini.exists(): if not profiles_ini.exists():
return None return (default_profile, encoding)
# Determine encoding (if possible)
for enc in KNOWN_ENCODINGS:
try:
profiles_ini.read_text(encoding=enc)
except UnicodeError:
# Ignore and keep trying
pass
else:
encoding = enc
break
if not encoding:
raise UnicodeError('Failed to determine encoding of profiles.ini')
# Parse INI # Parse INI
parser = configparser.ConfigParser() parser = configparser.ConfigParser()
try: parser.read(profiles_ini, encoding=encoding)
parser.read(profiles_ini)
except (configparser.ParsingError, UnicodeError):
# Assuming we have the wrong encoding
parser.read(profiles_ini, encoding='utf-16')
for section in parser.sections(): for section in parser.sections():
if section.lower().startswith('install'): if section.lower().startswith('install'):
default_profile = parser[section].get('default') default_profile = parser[section].get('default')
@ -841,7 +863,7 @@ def get_firefox_default_profile(profiles_ini):
default_profile = parser[section].get('path') default_profile = parser[section].get('path')
# Done # Done
return default_profile return (default_profile, encoding)
def get_storage_status(): def get_storage_status():