Locale: Correct remaining pylint issues in maclocale.py.

This commit is contained in:
John Ralls 2023-07-11 15:54:50 -07:00 committed by Nick Hall
parent e3a2797a33
commit 48d4196739

View File

@ -74,7 +74,9 @@ locale, leaving $LANGUAGE unset (which is the same as setting it to
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
import os, subprocess, locale import os
import subprocess
import locale
import logging import logging
LOG = logging.getLogger(".gramps.gen.utils.grampslocale.mac") LOG = logging.getLogger(".gramps.gen.utils.grampslocale.mac")
LOG.propagate = True LOG.propagate = True
@ -93,10 +95,12 @@ def mac_setup_localization(glocale):
args = ('/usr/bin/defaults', 'read', args = ('/usr/bin/defaults', 'read',
'org.gramps-project.gramps', key) 'org.gramps-project.gramps', key)
answer = subprocess.Popen( answer = None
args, with subprocess.Popen(
stderr=open("/dev/null"), args,
stdout=subprocess.PIPE).communicate()[0] stderr=open("/dev/null", encoding='utf8'),
stdout=subprocess.PIPE) as proc:
answer = proc.communicate()[0]
if not answer: if not answer:
LOG.debug("No prefs found for %s:%s", domain, key) LOG.debug("No prefs found for %s:%s", domain, key)
return None return None
@ -210,46 +214,22 @@ def mac_setup_localization(glocale):
return None return None
return apple_collation return apple_collation
#The action starts here def _set_from_environment(glocale):
_locale = None _locale = None
glocale.calendar = None glocale.calendar = None
try:
locale.setlocale(locale.LC_ALL, '')
_locale = locale.getlocale()
except locale.Error as err:
LOG.warning("Environment locale not usable: %s", str(err))
if not glocale.lang and _locale:
if glocale.check_available_translations(_locale[0]):
glocale.lang = '.'.join(_locale)
else:
LOG.debug("Environment locale %s not supported", _locale)
if not glocale.lang:
LOG.debug("Setting from the environment didn't work out, checking "
"defaults")
(glocale.lang, glocale.calendar) = _mac_get_locale()
glocale.coll_qualifier = None
glocale.collation = _mac_get_collation()
if not glocale.lang and glocale.collation:
coll_parts = glocale.collation.split('@')
glocale.lang = glocale.check_available_translations(coll_parts[0])
if not glocale.lang:
glocale.lang = "en_US.UTF-8"
glocale.lang = locale.normalize(glocale.lang)
if not glocale.lang.split('.')[1].lower() in ['utf8', 'utf-8']:
LOG.debug('Forcing locale encoding to UTF-8')
glocale.lang = '.'.join([glocale.lang.split('.')[0], 'UTF-8'])
try: try:
locale.setlocale(locale.LC_ALL, glocale.lang) locale.setlocale(locale.LC_ALL, '')
except locale.Error: _locale = locale.getlocale()
LOG.debug('Attempt failed, locale %s unsupported', glocale.lang) except locale.Error as err:
glocale.encoding = glocale.lang.split('.')[1] LOG.warning("Environment locale not usable: %s", str(err))
if not glocale.language:
if not glocale.lang and _locale:
if glocale.check_available_translations(_locale[0]):
glocale.lang = '.'.join(_locale)
else:
LOG.debug("Environment locale %s not supported", _locale)
def _find_language(glocale):
lang = locale.getlocale(locale.LC_MESSAGES)[0] lang = locale.getlocale(locale.LC_MESSAGES)[0]
language = [glocale.check_available_translations(lang)] language = [glocale.check_available_translations(lang)]
if not language: if not language:
@ -283,15 +263,49 @@ def mac_setup_localization(glocale):
if not glocale.lang: if not glocale.lang:
glocale.lang = locale.normalize(glocale.language[0]) glocale.lang = locale.normalize(glocale.language[0])
glocale.encoding = glocale.lang.split('.')[1] glocale.encoding = glocale.lang.split('.')[1]
LOG.debug("Ended check for languages with glocale.language %s", LOG.debug("Ended check for languages with glocale.language %s",
glocale.language) glocale.language)
if not glocale.collation: def _force_encoding(glocale):
glocale.collation = (locale.getlocale(locale.LC_COLLATE)[0] or LOG.debug('Forcing locale encoding to UTF-8')
glocale.lang) glocale.lang = '.'.join([glocale.lang.split('.')[0], 'UTF-8'])
if not glocale.calendar: try:
locale.setlocale(locale.LC_ALL, glocale.lang)
except locale.Error:
LOG.debug('Attempt failed, locale %s unsupported', glocale.lang)
def _set_calendar_from_translation(glocale):
time = locale.getlocale(locale.LC_TIME)[0] time = locale.getlocale(locale.LC_TIME)[0]
if glocale.check_available_translations(time): if glocale.check_available_translations(time):
glocale.calendar = time glocale.calendar = time
else: else:
glocale.calendar = glocale.lang[:5] glocale.calendar = glocale.lang[:5]
#The action starts here
_set_from_environment(glocale)
if not glocale.lang:
LOG.debug("Setting from the environment didn't work out, checking "
"defaults")
(glocale.lang, glocale.calendar) = _mac_get_locale()
glocale.coll_qualifier = None
glocale.collation = _mac_get_collation()
if not glocale.lang and glocale.collation:
coll_parts = glocale.collation.split('@')
glocale.lang = glocale.check_available_translations(coll_parts[0])
if not glocale.lang:
glocale.lang = "en_US.UTF-8"
glocale.lang = locale.normalize(glocale.lang)
if not glocale.lang.split('.')[1].lower() in ['utf8', 'utf-8']:
_force_encoding(glocale)
glocale.encoding = glocale.lang.split('.')[1]
if not glocale.language:
_find_language(glocale)
if not glocale.collation:
glocale.collation = (locale.getlocale(locale.LC_COLLATE)[0] or
glocale.lang)
if not glocale.calendar:
_set_calendar_from_translation(glocale)