Skip to content

Commit

Permalink
fixes: refactor to prep for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarIthawi committed Nov 9, 2023
1 parent b7d3bb2 commit d5fed36
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 33 deletions.
99 changes: 68 additions & 31 deletions scripts/sync_translations.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -16,22 +27,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 @@ -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):
"""
Expand Down Expand Up @@ -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
6 changes: 4 additions & 2 deletions scripts/tests/test_sync_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down Expand Up @@ -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'

0 comments on commit d5fed36

Please sign in to comment.