From 24673d29b745425962caffede280f8f2a3c80751 Mon Sep 17 00:00:00 2001 From: Omar Al-Ithawi Date: Sun, 19 Nov 2023 16:59:59 +0300 Subject: [PATCH] feat: use argparse --- scripts/sync_translations.py | 49 +++++++++++++++++++++---- scripts/tests/test_sync_translations.py | 20 +++++++--- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/scripts/sync_translations.py b/scripts/sync_translations.py index 333a6f930c5..f8c29a4e4fd 100644 --- a/scripts/sync_translations.py +++ b/scripts/sync_translations.py @@ -1,6 +1,25 @@ +""" +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/ + + +Variable names meaning: + + - current_translation: translation in the new "open-edx/openedx-translations" project + - translation_from_old_project: translation in the old "open-edx/edx-platform" or "open-edx/xblocks" projects + +""" + +import argparse import configparser +from datetime import datetime import os -import sys from os.path import expanduser import yaml @@ -15,8 +34,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 @@ -24,13 +44,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}' @@ -213,6 +233,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 5f752c4c943..3dfd7ccf3d9 100644 --- a/scripts/tests/test_sync_translations.py +++ b/scripts/tests/test_sync_translations.py @@ -1,9 +1,14 @@ """ Tests for sync_translations.py """ +from dataclasses import dataclass +from datetime import datetime, timezone import types +from typing import Union +import pytest import responses + from transifex.api import transifex_api, Project from transifex.api.jsonapi import Resource from transifex.api.jsonapi.auth import BearerAuthentication @@ -14,14 +19,17 @@ HOST = transifex_api.HOST -def sync_command(): - result = Command( - argv=[], - tx_api=transifex_api, - environ={ +def sync_command(**kwargs): + command_args = { + 'tx_api': transifex_api, + 'dry_run': True, + 'simulate_github_workflow': False, + 'environ': { 'TX_API_TOKEN': 'dummy-token' } - ) + } + command_args.update(kwargs) + result = Command(**command_args) result.tx_api.make_auth_headers = BearerAuthentication('dummy-token') return result