From d5fed3681455d1e38aaa4867315c49db313ee8ce Mon Sep 17 00:00:00 2001 From: Omar Al-Ithawi Date: Thu, 9 Nov 2023 14:13:41 +0300 Subject: [PATCH] fixes: refactor to prep for tests --- scripts/sync_translations.py | 99 +++++++++++++++++-------- scripts/tests/test_sync_translations.py | 6 +- 2 files changed, 72 insertions(+), 33 deletions(-) diff --git a/scripts/sync_translations.py b/scripts/sync_translations.py index b98b289e2f3..bf13fa81f51 100644 --- a/scripts/sync_translations.py +++ b/scripts/sync_translations.py @@ -1,7 +1,18 @@ +""" +Sync translations from the deprecated Transifex projects into the new openedx-translations project. + + - Old projects links: + * edX Platform Core: https://app.transifex.com/open-edx/edx-platform/ + * XBlocks: https://app.transifex.com/open-edx/xblocks/ + + - New project link: + * https://app.transifex.com/open-edx/openedx-translations/ +""" + +import argparse import configparser from datetime import datetime import os -import sys from os.path import expanduser import yaml @@ -16,8 +27,9 @@ class Command: workflow_file_path = '.github/workflows/sync-translations.yml' - def __init__(self, argv, tx_api, environ): - self.argv = argv + def __init__(self, tx_api, dry_run, simulate_github_workflow, environ): + self.dry_run = dry_run + self.simulate_github_workflow = simulate_github_workflow self.tx_api = tx_api self.environ = environ @@ -25,13 +37,13 @@ def is_dry_run(self): """ Check if the script is running in dry-run mode. """ - return '--dry-run' in self.argv + return self.dry_run def is_simulated_github_actions(self): """ Check if the script is running in simulated GitHub Actions mode. """ - return '--simulate-github-workflow' in self.argv + return self.simulate_github_workflow def get_resource_url(self, resource, project_slug): return f'https://www.transifex.com/{ORGANIZATION_SLUG}/{project_slug}/{resource.slug}' @@ -107,30 +119,40 @@ def sync_translations(self, language_code, old_resource, new_resource): for new_translation in self.get_translations(language_code=language_code, resource=new_resource): translation_id = self.get_translation_id(new_translation) if translation_from_old_project := translations_from_old_project.get(translation_id): - updates = {} - for attr in ['reviewed', 'proofread', 'strings']: - if old_attr_value := getattr(translation_from_old_project, attr, None): - if old_attr_value != getattr(new_translation, attr, None): - updates[attr] = old_attr_value - - # Avoid overwriting more recent translations in the open-edx/openedx-translations project - newer_translation_found = False - if translation_from_old_project.datetime_translated and new_translation.datetime_translated: - old_project_translation_time = datetime.fromisoformat( - translation_from_old_project.datetime_translated.replace('Z', '+00:00')) - new_project_translation_time = datetime.fromisoformat( - new_translation.datetime_translated.replace('Z', '+00:00')) - - newer_translation_found = new_project_translation_time > old_project_translation_time - - if updates: - if newer_translation_found: - print(translation_id, updates, '[Skipped: new project translation is more recent]') - import pdb; pdb.set_trace() - else: - print(translation_id, updates, '[Dry run]' if self.is_dry_run() else '') - if not self.is_dry_run(): - new_translation.save(**updates) + self.sync_translation_entry( + translation_id=translation_id, + translation_from_old_project=translation_from_old_project, + new_translation=new_translation, + ) + + def sync_translation_entry(self, translation_id, translation_from_old_project, new_translation): + """ + Sync a single translation entry from the old project to the new one. + """ + updates = {} + for attr in ['reviewed', 'proofread', 'strings']: + if old_attr_value := getattr(translation_from_old_project, attr, None): + if old_attr_value != getattr(new_translation, attr, None): + updates[attr] = old_attr_value + + # Avoid overwriting more recent translations in the open-edx/openedx-translations project + newer_translation_found = False + if translation_from_old_project.datetime_translated and new_translation.datetime_translated: + old_project_translation_time = datetime.fromisoformat( + translation_from_old_project.datetime_translated.replace('Z', '+00:00')) + new_project_translation_time = datetime.fromisoformat( + new_translation.datetime_translated.replace('Z', '+00:00')) + + newer_translation_found = new_project_translation_time > old_project_translation_time + + if updates: + if newer_translation_found: + print(translation_id, updates, '[Skipped: new project translation is more recent]') + import pdb; pdb.set_trace() + else: + print(translation_id, updates, '[Dry run]' if self.is_dry_run() else '') + if not self.is_dry_run(): + new_translation.save(**updates) def sync_tags(self, old_resource, new_resource): """ @@ -227,6 +249,21 @@ def run(self): ) -if __name__ == '__main__': - command = Command(sys.argv, environ=os.environ, tx_api=transifex_api) +def main(): # pragma: no cover + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--simulate-github-workflow', action='store_true', + dest='simulate_github_workflow') + parser.add_argument('--dry-run', action='store_true', dest='dry_run') + argparse_args = parser.parse_args() + + command = Command( + tx_api=transifex_api, + environ=os.environ, + dry_run=argparse_args.dry_run, + simulate_github_workflow=argparse_args.simulate_github_workflow, + ) command.run() + + +if __name__ == '__main__': + main() # pragma: no cover diff --git a/scripts/tests/test_sync_translations.py b/scripts/tests/test_sync_translations.py index 338cfc4bd7a..49068d768da 100644 --- a/scripts/tests/test_sync_translations.py +++ b/scripts/tests/test_sync_translations.py @@ -14,10 +14,11 @@ HOST = transifex_api.HOST -def sync_command(): +def sync_command(args=None): result = Command( - argv=[], tx_api=transifex_api, + dry_run=True, + simulate_github_workflow=False, environ={ 'TX_API_TOKEN': 'dummy-token' } @@ -95,3 +96,4 @@ def test_more_recent_translations_not_overridden(): """ Verify that the more recent translations in the open-edx/openedx-translations project are not overridden. """ + assert False, 'Not implemented yet'