1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-10-01 08:40:11 +02:00

load_localized_config() returns a dict in a stable order

I renamed the variables while I was at it, to make it clearer.
This commit is contained in:
Hans-Christoph Steiner 2023-05-10 13:16:53 +02:00
parent 74a23284e1
commit b04c7ff539

View File

@ -496,31 +496,41 @@ def file_entry(filename, hash_value=None):
def load_localized_config(name, repodir): def load_localized_config(name, repodir):
lst = {} """Load localized config files and put them into internal dict format.
This will maintain the order as came from the data files, e.g
YAML. The locale comes from unsorted paths on the filesystem, so
that is separately sorted.
"""
ret = dict()
for f in Path().glob("config/**/{name}.yml".format(name=name)): for f in Path().glob("config/**/{name}.yml".format(name=name)):
locale = f.parts[1] locale = f.parts[1]
if len(f.parts) == 2: if len(f.parts) == 2:
locale = "en-US" locale = "en-US"
with open(f, encoding="utf-8") as fp: with open(f, encoding="utf-8") as fp:
elem = yaml.safe_load(fp) elem = yaml.safe_load(fp)
for akey, avalue in elem.items(): for afname, field_dict in elem.items():
if akey not in lst: if afname not in ret:
lst[akey] = {} ret[afname] = dict()
for key, value in avalue.items(): for key, value in field_dict.items():
if key not in lst[akey]: if key not in ret[afname]:
lst[akey][key] = {} ret[afname][key] = dict()
if key == "icon": if key == "icon":
icons_dir = os.path.join(repodir, 'icons') icons_dir = os.path.join(repodir, 'icons')
if not os.path.exists(icons_dir): if not os.path.exists(icons_dir):
os.mkdir(icons_dir) os.mkdir(icons_dir)
shutil.copy(os.path.join("config", value), icons_dir) shutil.copy(os.path.join("config", value), icons_dir)
lst[akey][key][locale] = file_entry( ret[afname][key][locale] = file_entry(
os.path.join(icons_dir, value) os.path.join(icons_dir, value)
) )
else: else:
lst[akey][key][locale] = value ret[afname][key][locale] = value
return lst for elem in ret.values():
for afname in elem:
elem[afname] = {locale: v for locale, v in sorted(elem[afname].items())}
return ret
def parse_human_readable_size(size): def parse_human_readable_size(size):