Skip to content

Commit

Permalink
chore: use argparse on validate_translation_files.py
Browse files Browse the repository at this point in the history
allow passing arguments in a pythonic manner
  • Loading branch information
OmarIthawi committed Nov 4, 2023
1 parent a47d09f commit 42c9bcf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
10 changes: 5 additions & 5 deletions scripts/tests/test_validate_translation_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"""

import os.path
import re

from ..validate_translation_files import (
get_translation_files,
main,
validate_translation_files,
)

SCRIPT_DIR = os.path.dirname(__file__)
Expand Down Expand Up @@ -35,7 +36,7 @@ def test_main_on_invalid_files(capsys):
Integration test for the `main` function on some invalid files.
"""
mock_translations_dir = os.path.join(SCRIPT_DIR, 'mock_translations_dir')
exit_code = main(mock_translations_dir)
exit_code = validate_translation_files(mock_translations_dir)
out, err = capsys.readouterr()

assert 'VALID:' in out, 'Valid files should be printed in stdout'
Expand All @@ -44,8 +45,7 @@ def test_main_on_invalid_files(capsys):
assert 'hi/LC_MESSAGES/django.po' not in out, 'Invalid file should be printed in stderr'
assert 'en/LC_MESSAGES/django.po' not in out, 'Source file should not be validated'

assert 'INVALID:' in err
assert 'hi/LC_MESSAGES/django.po' in err
assert re.match(r'INVALID: .*hi/LC_MESSAGES/django.po', err)
assert '\'msgstr\' is not a valid Python brace format string, unlike \'msgid\'' in err
assert 'FAILURE: Some translations are invalid.' in err

Expand All @@ -57,7 +57,7 @@ def test_main_on_valid_files(capsys):
Integration test for the `main` function but only for the Arabic translations which is valid.
"""
mock_translations_dir = os.path.join(SCRIPT_DIR, 'mock_translations_dir/demo-xblock/conf/locale/ar')
exit_code = main(mock_translations_dir)
exit_code = validate_translation_files(mock_translations_dir)
out, err = capsys.readouterr()

assert 'VALID:' in out, 'Valid files should be printed in stdout'
Expand Down
25 changes: 22 additions & 3 deletions scripts/validate_translation_files.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import sys
"""
Validate translation files using GNU gettext `msgfmt` command.
This script is used to validate translation files in the Open edX platform and mark
invalid entries as fuzzy.
"""

import argparse
import os
import os.path
import subprocess
import sys


def get_translation_files(translation_directory):
Expand Down Expand Up @@ -39,7 +47,9 @@ def validate_translation_file(po_file):
}


def main(translations_dir='translations'):
def validate_translation_files(
translations_dir='translations',
):
"""
Run GNU gettext `msgfmt` and print errors to stderr.
Expand Down Expand Up @@ -81,5 +91,14 @@ def main(translations_dir='translations'):
return exit_code


def main(): # pragma: no cover
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--dir', action='store', type=str, default='translations')
args = parser.parse_args()
sys.exit(validate_translation_files(
translations_dir=args.dir,
))


if __name__ == '__main__':
sys.exit(main()) # pragma: no cover
main() # pragma: no cover

0 comments on commit 42c9bcf

Please sign in to comment.