Skip to content

Commit

Permalink
Rework system locale
Browse files Browse the repository at this point in the history
  • Loading branch information
codefiles committed Sep 12, 2022
1 parent ea71566 commit 5ed38b7
Showing 1 changed file with 34 additions and 43 deletions.
77 changes: 34 additions & 43 deletions archinstall/lib/locale_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, name: str, encoding: str = 'UTF-8'):
self.encoding = encoding
self.modifier = ''

# Make sure we extract the modifier, that way we can put it in if needed.
# Extract the modifier if found.
if '@' in name:
name, potential_modifier = name.split('@')

Expand All @@ -44,7 +44,7 @@ def __init__(self, name: str, encoding: str = 'UTF-8'):
if '.' in name:
self.language, potential_encoding = name.split('.')

# Override encoding if the name contains an encoding that differs.
# Override encoding if name contains an encoding that differs.
if encoding != potential_encoding:
self.encoding = potential_encoding
else:
Expand Down Expand Up @@ -99,53 +99,36 @@ def __init__(self, locales: List[Locale] = [], target: str = ''):
self.locale_gen = f'{target}/etc/locale.gen'
self.locale_conf = f'{target}/etc/locale.conf'

def list_locales(self) -> List[Locale]:
def list_supported(self) -> List[Locale]:
"""
Get a list of all the locales in the locale-gen configuration file.
Get a list of supported locales.
:return: A list of all the locales.
:return: A list of supported locales.
:rtype: List[Locale]
"""
locales = []

try:
with open(self.locale_gen, 'r') as fh:
entries = fh.readlines()
except FileNotFoundError:
log(f"Configuration file for locale-gen not found: '{self.locale_gen}'", fg="red", level=logging.ERROR)
else:
# Before the list of locales begins there is an empty line with a '#' in front
# so collect the locales from bottom up and halt when done.
entries.reverse()

for entry in entries:
text = entry.replace('#', '').strip()

if text == '':
break

locales.append(Locale(*text.split()))

locales.reverse()
for locale in list_locales(self.target):
locales.append(Locale(*locale.split()))

return locales

def verify_locales(self) -> bool:
"""
Check if the locales match entries in the locale-gen configuration file.
Check if the locales match supported locales.
If a match is found then update the name of the locale to the name of the matching entry in case they differ.
:return: If matched return True else False.
:rtype: bool
"""
list_locales = self.list_locales()
supported = self.list_supported()
found_all = True

for locale in self.locales:
found = False
for entry_locale in list_locales:
if locale == entry_locale:
locale.name = entry_locale.name
for entry in supported:
if locale == entry:
locale.name = entry.name
found = True
break

Expand Down Expand Up @@ -327,7 +310,8 @@ def get_system_locale(self) -> str:
except FileNotFoundError:
pass
else:
# Set up a regular expression pattern of a line beginning with 'LANG=' followed by and ending in a locale in optional double quotes.
# Set up a regular expression pattern of a line beginning with 'LANG='
# followed by and ending in a locale in optional double quotes.
pattern = re.compile(rf'^LANG="?(.+?)"?$')

for line in lines:
Expand Down Expand Up @@ -405,22 +389,29 @@ def run(self) -> bool:
return True


def list_locales() -> List[str]:
with open('/etc/locale.gen', 'r') as fp:
locales = []
# Before the list of locales begins there is an empty line with a '#' in front
# so collect the locales from bottom up and halt when done.
entries = fp.readlines()
entries.reverse()
def list_locales(target: str = '') -> List[str]:
supported = f'{target}/usr/share/i18n/SUPPORTED'
locales = []

for entry in entries:
text = entry.replace('#', '').strip()
if text == '':
try:
with open(supported, 'r') as fh:
entries = fh.readlines()
except FileNotFoundError:
log(f"Supported locale file not found: '{supported}'", fg="red", level=logging.ERROR)
else:
# Remove lines that do not contain locales.
for index, line in enumerate(entries):
if line == 'SUPPORTED-LOCALES=\\\n':
entries = entries[index + 1:]
break
locales.append(text)

locales.reverse()
return locales
# Remove C.UTF-8 since it is provided by the glibc package.
entries.remove('C.UTF-8/UTF-8 \\\n')

for entry in entries:
locales.append(entry[:-3].replace('/', ' '))

return locales

def get_locale_mode_text(mode):
if mode == 'LC_ALL':
Expand Down

0 comments on commit 5ed38b7

Please sign in to comment.