Skip to content

Commit

Permalink
feat: use argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarIthawi committed Nov 19, 2023
1 parent 7805789 commit 24673d2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
49 changes: 42 additions & 7 deletions scripts/sync_translations.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -15,22 +34,23 @@ 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

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}'
Expand Down Expand Up @@ -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
20 changes: 14 additions & 6 deletions scripts/tests/test_sync_translations.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down

0 comments on commit 24673d2

Please sign in to comment.