Repository navigation
Process objects resource to csv with context, fix process_all command #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f88f0b6
813d191
5d6b147
218db7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,14 @@ | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import strictyaml | ||
| import typer | ||
|
|
||
| from automation.models import Config | ||
|
|
||
|
|
||
| def load_config(config_path: Path) -> Config: | ||
| yaml = strictyaml.load(config_path.read_text()) | ||
| return Config.model_validate(yaml.data) | ||
|
|
||
|
|
||
| def main(config_path: Path) -> None: | ||
| from pprint import pprint # noqa: PLC0415 | ||
|
|
||
| pprint(load_config(config_path).model_dump(), width=120) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| typer.run(main) | ||
| def load_json_config(config_path: Path) -> Config: | ||
| config_data = json.loads(config_path.read_text(encoding="utf-8")) | ||
| return Config.model_validate( | ||
| { | ||
| "source": {"project": "dwarf-fortress-steam"}, | ||
| "languages": config_data["include"], | ||
| }, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import codecs | ||
| import io | ||
| import shutil | ||
| from pathlib import Path | ||
|
|
||
| import alternative_encodings | ||
| import typer | ||
| from babel.messages.pofile import read_po | ||
| from df_translation_toolkit.convert import objects_po_to_csv | ||
| from df_translation_toolkit.utils.csv_utils import writer | ||
| from df_translation_toolkit.validation.validation_models import Diagnostics | ||
| from loguru import logger | ||
|
|
||
| from automation.models import Config, Context, LanguageInfo, SourceInfo | ||
| from automation.process_utils import ( | ||
| check_possibility_to_encode_translations, | ||
| get_po_file_path, | ||
| validate_language_name, | ||
| ) | ||
|
|
||
| alternative_encodings.register_all() | ||
|
|
||
|
|
||
| def load_po_file_with_context(file_path: Path) -> list[tuple[str, str, str]]: | ||
| with file_path.open(encoding="utf-8") as file: | ||
| return [ | ||
| (str(item.id), str(item.context or ""), str(item.string)) | ||
| for item in read_po(file) | ||
| if item.id and item.string | ||
| ] | ||
|
|
||
|
|
||
| def process_objects_with_context( | ||
| *, | ||
| csv_file_path: Path, | ||
| language: LanguageInfo, | ||
| context: Context, | ||
| resource_name: str, | ||
| ) -> None: | ||
| logger.info(f"Processing {resource_name} resource with context") | ||
| errors_file_path = csv_file_path.parent / "errors.txt" | ||
| if errors_file_path.exists(): | ||
| errors_file_path.unlink() | ||
|
|
||
| po_file_path = get_po_file_path( | ||
| working_directory=context.working_directory, | ||
| project_name=context.config.source.project, | ||
| resource_name=resource_name, | ||
| language_code=language.code, | ||
| ) | ||
| po_data = load_po_file_with_context(file_path=po_file_path) | ||
| if not po_data: | ||
| logger.warning(f"Empty {resource_name} resource, skipping.") | ||
| return | ||
|
|
||
| diagnostics = Diagnostics() | ||
| filtered_dictionary: dict[tuple[str, str], str] = {} | ||
| for original, object_context, translation in po_data: | ||
| prepared_entries = objects_po_to_csv.prepare_dictionary([(original, translation)], diagnostics) | ||
| for source, prepared_translation in prepared_entries: | ||
| filtered_dictionary[source, object_context] = prepared_translation | ||
|
|
||
| if diagnostics.contains_problems(): | ||
| with errors_file_path.open("w", encoding="utf-8") as errors_file: | ||
| errors_file.write(str(diagnostics)) | ||
|
|
||
| if not filtered_dictionary: | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| logger.warning(f"Empty filtered dictionary from {resource_name} resource, skipping.") | ||
| return | ||
|
|
||
| logger.info(f"Filtered lines: {len(filtered_dictionary)=}") | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| check_possibility_to_encode_translations( | ||
| ((source, translation) for (source, _), translation in filtered_dictionary.items()), | ||
| language, | ||
| resource_name, | ||
| ) | ||
|
|
||
| csv_data_buffer = io.StringIO(newline="") | ||
| csv_writer = writer(csv_data_buffer) | ||
| rows = [ | ||
| [source, object_context, translation] | ||
| for (source, object_context), translation in filtered_dictionary.items() | ||
| ] | ||
| csv_writer.writerows(rows) | ||
| csv_data_buffer.flush() | ||
| csv_data_buffer.seek(0) | ||
|
|
||
| with csv_file_path.open("wb") as csv_file: | ||
| data = csv_data_buffer.read() | ||
| csv_file.write(codecs.encode(data, encoding=language.encoding)) | ||
| logger.info(f"Written data size: {len(data)=}") | ||
| assert len(data) > 0 | ||
|
|
||
|
|
||
| def process_objects_only(language: LanguageInfo, context: Context) -> None: | ||
| validate_language_name(language.name) | ||
| translation_build_directory = context.working_directory / "translation_build" | ||
| csv_objects_directory = translation_build_directory / "csv_objects" / language.name | ||
| shutil.rmtree(csv_objects_directory, ignore_errors=True) | ||
| csv_objects_directory.mkdir(parents=True) | ||
| objects_csv_file_path = csv_objects_directory / "dfint_dictionary.csv" | ||
|
Comment on lines
+99
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| process_objects_with_context( | ||
| csv_file_path=objects_csv_file_path, | ||
| language=language, | ||
| context=context, | ||
| resource_name="objects", | ||
| ) | ||
| logger.info(f"{objects_csv_file_path.relative_to(context.working_directory)} written") | ||
|
|
||
| app = typer.Typer() | ||
|
|
||
|
|
||
| @app.command() | ||
| def main( | ||
| working_directory: Path, | ||
| language_name: str, | ||
| language_code: str, | ||
| encoding: str, | ||
| project: str = "dwarf-fortress-steam", | ||
| ) -> None: | ||
| try: | ||
| validate_language_name(language_name) | ||
| except ValueError as ex: | ||
| raise typer.BadParameter(str(ex)) from ex | ||
|
|
||
| context = Context( | ||
| config=Config(source=SourceInfo(project=project), languages=[]), | ||
| working_directory=working_directory, | ||
| ) | ||
|
|
||
| language_info = LanguageInfo( | ||
| name=language_name, | ||
| code=language_code, | ||
| encoding=encoding, | ||
| ) | ||
| process_objects_only(language_info, context) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import codecs | ||
| from collections.abc import Iterable | ||
| from pathlib import Path | ||
|
|
||
| from automation.models import LanguageInfo | ||
|
|
||
|
|
||
| def validate_language_name(language_name: str) -> None: | ||
| if ( | ||
| not language_name | ||
| or language_name in {".", ".."} | ||
| or Path(language_name).name != language_name | ||
| or "\\" in language_name | ||
| ): | ||
| error_message = "language_name must be a single path component" | ||
| raise ValueError(error_message) | ||
|
|
||
|
|
||
| def get_po_file_path(*, working_directory: Path, project_name: str, resource_name: str, language_code: str) -> Path: | ||
| return ( | ||
| working_directory | ||
| / "translations-backup" | ||
| / "translations" | ||
| / project_name | ||
| / resource_name | ||
| / f"{language_code}.po" | ||
| ) | ||
|
|
||
|
|
||
| def check_possibility_to_encode_translations( | ||
| dictionary: Iterable[tuple[str, str]], | ||
| language: LanguageInfo, | ||
| resource_name: str, | ||
| ) -> None: | ||
| for original, translation in dictionary: | ||
| try: | ||
| codecs.encode(translation, encoding=language.encoding) | ||
| except UnicodeEncodeError as ex: | ||
| msg = f"Error trying encode translation: {translation!r}; original: {original!r}, resource: {resource_name}" | ||
| raise ValueError(msg) from ex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: dfint/autobuild
Length of output: 3152
Script executed:
Repository: dfint/autobuild
Length of output: 9512
Script executed:
Repository: dfint/autobuild
Length of output: 3664
Validate objects-only output without the hardcoded exclusions.
process_objects_only()checks every objects translation for encodability.process_objects()skips sources listed inexclude. An unencodable translation for a source that also appears in the hardcoded resource makes the new call raiseValueError. That exception stopsprocess()beforecsv_with_objectsis written, andprocess_all()skips the remaining languages.Suggested fix
Committable suggestion