From 4c6d789732c4971ca507b03c638132ef8aedc1f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Germ=C3=A1n=20Carrillo?= Date: Thu, 24 Oct 2024 22:22:48 -0500 Subject: [PATCH] Set preferred language in generator; get available languages in gpkg connector --- modelbaker/dbconnector/db_connector.py | 8 +++++++- modelbaker/dbconnector/gpkg_connector.py | 22 ++++++++++++++++++---- modelbaker/generator/generator.py | 3 +++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/modelbaker/dbconnector/db_connector.py b/modelbaker/dbconnector/db_connector.py index 3305917..f07ba19 100644 --- a/modelbaker/dbconnector/db_connector.py +++ b/modelbaker/dbconnector/db_connector.py @@ -401,7 +401,7 @@ def set_preferred_translation(self, lang: str) -> bool: """ Returns whether the preferred translation language was successfully set """ - if len(lang) == 2: + if len(lang) == 2 and lang != "__": self._lang = lang return True @@ -417,6 +417,12 @@ def get_translation_handling(self) -> tuple[bool, str]: """ return False, "" + def get_available_languages(self) -> list[str]: + """ + Returns a list of available languages in the t_ili2db_nls table. + """ + return [] + class DBConnectorError(Exception): """This error is raised when DbConnector could not connect to database. diff --git a/modelbaker/dbconnector/gpkg_connector.py b/modelbaker/dbconnector/gpkg_connector.py index eac1bf6..5c9b8f0 100644 --- a/modelbaker/dbconnector/gpkg_connector.py +++ b/modelbaker/dbconnector/gpkg_connector.py @@ -106,10 +106,7 @@ def _get_tables_info(self): interlis_joins = "" tr_enabled, lang = self.get_translation_handling() if tr_enabled: - self.new_message.emit( - Qgis.Info, - f"Getting tables info with preferred language {lang}.", - ) + self.stdout.emit(f"Getting tables info with preferred language {lang}.") if self.metadata_exists(): interlis_fields = """p.setting AS kind_settings, @@ -1213,3 +1210,20 @@ def set_ili2db_sequence_value(self, value): def get_translation_handling(self) -> tuple[bool, str]: return self._table_exists(GPKG_NLS_TABLE) and self._lang != "", self._lang + + def get_available_languages(self): + if not self._table_exists(GPKG_NLS_TABLE): + return [] + + cursor = self.conn.cursor() + cursor.execute( + """SELECT DISTINCT + lang + FROM "{}"; + """.format( + GPKG_NLS_TABLE + ) + ) + records = cursor.fetchall() + cursor.close() + return [record["lang"] for record in records] diff --git a/modelbaker/generator/generator.py b/modelbaker/generator/generator.py index 373563e..28c0936 100644 --- a/modelbaker/generator/generator.py +++ b/modelbaker/generator/generator.py @@ -53,6 +53,7 @@ def __init__( mgmt_uri: Optional[str] = None, consider_basket_handling: bool = False, optimize_strategy: OptimizeStrategy = OptimizeStrategy.NONE, + preferred_language: str = "", ) -> None: """ Creates a new Generator objects. @@ -76,6 +77,8 @@ def __init__( self.basket_handling = consider_basket_handling and self.get_basket_handling() self.optimize_strategy = optimize_strategy + self._db_connector.set_preferred_translation(preferred_language) + self._additional_ignored_layers = ( [] ) # List of layers to ignore set by 3rd parties