From f32eda6f5a38fed83426309c7e232d0285b7ef24 Mon Sep 17 00:00:00 2001 From: Quinten Steenhuis Date: Sat, 23 Sep 2023 15:10:02 -0400 Subject: [PATCH 1/3] Safer loading of YAML file with language names --- docassemble/AssemblyLine/language.py | 33 +++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/docassemble/AssemblyLine/language.py b/docassemble/AssemblyLine/language.py index 7a8af1ca..abee6bef 100644 --- a/docassemble/AssemblyLine/language.py +++ b/docassemble/AssemblyLine/language.py @@ -1,4 +1,5 @@ # coding=utf-8 +import os from typing import List, Optional, Tuple from docassemble.base.util import url_action, path_and_mimetype import yaml @@ -13,6 +14,32 @@ ] +def _package_name(package_name: Optional[str] = None): + """Get package name without the name of the current module""" + if not package_name: + package_name = __name__ + try: + return ".".join(package_name.split(".")[:-1]) + except: + return package_name + + +def get_local_languages_yaml(): + current_package_name = _package_name() + + this_yaml = path_and_mimetype(f"{current_package_name}:data/sources/languages.yml")[ + 0 + ] + try: + local_yaml = path_and_mimetype("data/sources/languages.yml")[0] + except: + local_yaml = None + + if local_yaml and os.path.isfile(local_yaml): + return local_yaml + return this_yaml + + def get_tuples( lang_codes: List[str], languages_path: Optional[str] = None ) -> List[Tuple[str, str]]: @@ -31,7 +58,7 @@ def get_tuples( """ if not languages_path: - languages_path = path_and_mimetype("data/sources/languages.yml")[0] + languages_path = get_local_languages_yaml() if languages_path is not None: with open(languages_path, "r", encoding="utf-8") as stream: @@ -91,7 +118,7 @@ def get_language_list_dropdown( """ if not languages_path: - languages_path = path_and_mimetype("data/sources/languages.yml")[0] + languages_path = get_local_languages_yaml() languages = get_tuples(lang_codes, languages_path=languages_path) for language in languages: @@ -152,7 +179,7 @@ def get_language_list( A string containing the HTML for an unordered inline list of language selection. """ if not languages_path: - languages_path = path_and_mimetype("data/sources/languages.yml")[0] + languages_path = get_local_languages_yaml() if not languages: if not lang_codes: raise ValueError("Either languages or lang_codes must be provided") From 02d5c3a970bbd0faea772707cf1573a108f06755 Mon Sep 17 00:00:00 2001 From: Quinten Steenhuis Date: Sat, 23 Sep 2023 15:47:04 -0400 Subject: [PATCH 2/3] Update docassemble/AssemblyLine/language.py Co-authored-by: Bryce Willey --- docassemble/AssemblyLine/language.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docassemble/AssemblyLine/language.py b/docassemble/AssemblyLine/language.py index abee6bef..eab398b5 100644 --- a/docassemble/AssemblyLine/language.py +++ b/docassemble/AssemblyLine/language.py @@ -15,7 +15,7 @@ def _package_name(package_name: Optional[str] = None): - """Get package name without the name of the current module""" + """Get package name without the name of the given module. By default this is `docassemble.AssemblyLine.language`""" if not package_name: package_name = __name__ try: From 8233d50110d1ee038f76ee0381ca1cfed6ef8997 Mon Sep 17 00:00:00 2001 From: Quinten Steenhuis Date: Sat, 23 Sep 2023 15:47:56 -0400 Subject: [PATCH 3/3] Update docassemble/AssemblyLine/language.py Co-authored-by: Bryce Willey --- docassemble/AssemblyLine/language.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docassemble/AssemblyLine/language.py b/docassemble/AssemblyLine/language.py index eab398b5..ad994807 100644 --- a/docassemble/AssemblyLine/language.py +++ b/docassemble/AssemblyLine/language.py @@ -25,11 +25,6 @@ def _package_name(package_name: Optional[str] = None): def get_local_languages_yaml(): - current_package_name = _package_name() - - this_yaml = path_and_mimetype(f"{current_package_name}:data/sources/languages.yml")[ - 0 - ] try: local_yaml = path_and_mimetype("data/sources/languages.yml")[0] except: @@ -37,6 +32,11 @@ def get_local_languages_yaml(): if local_yaml and os.path.isfile(local_yaml): return local_yaml + + al_package_name = _package_name() + this_yaml = path_and_mimetype(f"{al_package_name}:data/sources/languages.yml")[ + 0 + ] return this_yaml