Commit 337a012c by David Baumgold

Make all i18n scripts use argparse, handle --verbose flag

parent c08a73f0
...@@ -25,6 +25,7 @@ where $DUMMY_LOCALE is the dummy_locale value set in the i18n config ...@@ -25,6 +25,7 @@ where $DUMMY_LOCALE is the dummy_locale value set in the i18n config
from __future__ import print_function from __future__ import print_function
import re import re
import sys import sys
import argparse
import polib import polib
from path import path from path import path
...@@ -204,7 +205,7 @@ def main(verbosity=1): ...@@ -204,7 +205,7 @@ def main(verbosity=1):
SOURCE_MSGS_DIR = CONFIGURATION.source_messages_dir SOURCE_MSGS_DIR = CONFIGURATION.source_messages_dir
for locale, converter in zip(CONFIGURATION.dummy_locales, [Dummy(), Dummy2()]): for locale, converter in zip(CONFIGURATION.dummy_locales, [Dummy(), Dummy2()]):
if verbosity: if verbosity:
print("Processing source language files into dummy strings, locale {}:".format(locale)) print('Processing source language files into dummy strings, locale "{}"'.format(locale))
for source_file in CONFIGURATION.source_messages_dir.walkfiles('*.po'): for source_file in CONFIGURATION.source_messages_dir.walkfiles('*.po'):
if verbosity: if verbosity:
print(' ', source_file.relpath()) print(' ', source_file.relpath())
...@@ -214,4 +215,8 @@ def main(verbosity=1): ...@@ -214,4 +215,8 @@ def main(verbosity=1):
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--verbose", "-v", action="count", default=0)
args = parser.parse_args()
ret = main(verbosity=args.verbose)
sys.exit(ret)
...@@ -210,7 +210,8 @@ def is_key_string(string): ...@@ -210,7 +210,8 @@ def is_key_string(string):
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--verbose', '-v', action='count', default=0) parser.add_argument('--verbose', '-v', action='count', default=0)
args = parser.parse_args() args = parser.parse_args()
main(verbosity=args.verbose) ret = main(verbosity=args.verbose)
sys.exit(ret)
...@@ -111,23 +111,27 @@ def validate_files(dir, files_to_merge): ...@@ -111,23 +111,27 @@ def validate_files(dir, files_to_merge):
raise Exception("I18N: Cannot generate because file not found: {0}".format(pathname)) raise Exception("I18N: Cannot generate because file not found: {0}".format(pathname))
def main(argv=None): def main(strict=True, verbosity=1):
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
parser = argparse.ArgumentParser(description="Generate merged and compiled message files.")
parser.add_argument("--strict", action='store_true', help="Complain about missing files.")
args = parser.parse_args(argv or [])
for locale in CONFIGURATION.translated_locales: for locale in CONFIGURATION.translated_locales:
merge_files(locale, fail_if_missing=args.strict) merge_files(locale, fail_if_missing=strict)
# Dummy text is not required. Don't raise exception if files are missing. # Dummy text is not required. Don't raise exception if files are missing.
for locale in CONFIGURATION.dummy_locales: for locale in CONFIGURATION.dummy_locales:
merge_files(locale, fail_if_missing=False) merge_files(locale, fail_if_missing=False)
compile_cmd = 'django-admin.py compilemessages -v0' compile_cmd = 'django-admin.py compilemessages -v{}'.format(verbosity)
execute(compile_cmd, working_directory=BASE_DIR, stderr=DEVNULL) if verbosity:
stderr = None
else:
stderr = DEVNULL
execute(compile_cmd, working_directory=BASE_DIR, stderr=stderr)
if __name__ == '__main__': if __name__ == '__main__':
main(sys.argv[1:]) logging.basicConfig(stream=sys.stdout, level=logging.INFO)
parser = argparse.ArgumentParser(description="Generate merged and compiled message files.")
parser.add_argument("--strict", action='store_true', help="Complain about missing files.")
parser.add_argument("--verbose", "-v", action="count", default=0)
args = parser.parse_args()
main(strict=args.strict, verbosity=args.verbose)
...@@ -8,8 +8,9 @@ import copy ...@@ -8,8 +8,9 @@ import copy
import fnmatch import fnmatch
import logging import logging
import sys import sys
import argparse
import polib import polib
import textwrap
from i18n.config import CONFIGURATION from i18n.config import CONFIGURATION
...@@ -116,27 +117,28 @@ def segment_pofile(filename, segments): ...@@ -116,27 +117,28 @@ def segment_pofile(filename, segments):
return files_written return files_written
def main(argv): def main(locales=None, verbosity=1):
"""
$ segment.py LOCALE [...]
Segment the .po files in LOCALE(s) based on the segmenting rules in
config.yaml.
Note that segmenting is *not* idempotent: it modifies the input file, so
be careful that you don't run it twice on the same file.
"""
# This is used as a tool only to segment translation files when adding a # This is used as a tool only to segment translation files when adding a
# new segment. In the regular workflow, the work is done by the extract # new segment. In the regular workflow, the work is done by the extract
# phase calling the functions above. # phase calling the functions above.
locales = locales or []
logging.basicConfig(stream=sys.stdout, level=logging.INFO) for locale in locales:
if len(argv) < 2:
sys.exit("Need a locale to segment")
for locale in argv[1:]:
segment_pofiles(locale) segment_pofiles(locale)
if __name__ == "__main__": if __name__ == "__main__":
main(sys.argv) logging.basicConfig(stream=sys.stdout, level=logging.INFO)
description = textwrap.dedent("""
Segment the .po files in LOCALE(s) based on the segmenting rules in
config.yaml.
Note that segmenting is *not* idempotent: it modifies the input file, so
be careful that you don't run it twice on the same file.
""".strip())
parser = argparse.ArgumentParser(description=description)
parser.add_argument("locale", nargs="+", help="a locale to segment")
parser.add_argument("--verbose", "-v", action="count", default=0)
args = parser.parse_args()
main(locales=args.locale, verbosity=args.verbose)
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
import sys import sys
from polib import pofile from polib import pofile
import argparse
from i18n.config import CONFIGURATION from i18n.config import CONFIGURATION
from i18n.execute import execute from i18n.execute import execute
from i18n.extract import EDX_MARKER from i18n.extract import EDX_MARKER
TRANSIFEX_HEADER = 'edX community translations have been downloaded from %s' TRANSIFEX_HEADER = 'edX community translations have been downloaded from {}'
TRANSIFEX_URL = 'https://www.transifex.com/projects/p/edx-platform/' TRANSIFEX_URL = 'https://www.transifex.com/projects/p/edx-platform/'
...@@ -16,7 +17,7 @@ def push(): ...@@ -16,7 +17,7 @@ def push():
def pull(): def pull():
print "Pulling languages from transifex..." print("Pulling languages from transifex...")
execute('tx pull --mode=reviewed --all') execute('tx pull --mode=reviewed --all')
clean_translated_locales() clean_translated_locales()
...@@ -57,18 +58,20 @@ def clean_file(filename): ...@@ -57,18 +58,20 @@ def clean_file(filename):
def get_new_header(po): def get_new_header(po):
team = po.metadata.get('Language-Team', None) team = po.metadata.get('Language-Team', None)
if not team: if not team:
return TRANSIFEX_HEADER % TRANSIFEX_URL return TRANSIFEX_HEADER.format(TRANSIFEX_URL)
else: else:
return TRANSIFEX_HEADER % team return TRANSIFEX_HEADER.format(team)
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) < 2: parser = argparse.ArgumentParser()
raise Exception("missing argument: push or pull") parser.add_argument("command", help="push or pull")
arg = sys.argv[1] parser.add_argument("--verbose", "-v")
if arg == 'push': args = parser.parse_args()
if args.command == "push":
push() push()
elif arg == 'pull': elif args.command == "pull":
pull() pull()
else: else:
raise Exception("unknown argument: (%s)" % arg) raise Exception("unknown command ({cmd})".format(cmd=args.command))
...@@ -149,20 +149,14 @@ def check_messages(filename, report_empty=False): ...@@ -149,20 +149,14 @@ def check_messages(filename, report_empty=False):
log.info(" No problems found in {0}".format(filename)) log.info(" No problems found in {0}".format(filename))
def parse_args(argv): def get_parser():
""" """
Parse command line arguments, returning a dict of Returns an argument parser for this script.
valid options:
{
'empty': BOOLEAN,
'verbose': BOOLEAN,
'language': str
}
where 'language' is a language code, eg "fr"
""" """
parser = argparse.ArgumentParser(description="Automatically finds translation errors in all edx-platform *.po files, for all languages, unless one or more language(s) is specified to check.") parser = argparse.ArgumentParser(description=(
"Automatically finds translation errors in all edx-platform *.po files, "
"for all languages, unless one or more language(s) is specified to check."
))
parser.add_argument( parser.add_argument(
'-l', '--language', '-l', '--language',
...@@ -179,39 +173,39 @@ def parse_args(argv): ...@@ -179,39 +173,39 @@ def parse_args(argv):
parser.add_argument( parser.add_argument(
'-v', '--verbose', '-v', '--verbose',
action='store_true', action='count', default=0,
help="Turns on info-level logging." help="Turns on info-level logging."
) )
return vars(parser.parse_args(argv)) return parser
def main():
"""Main entry point for the tool."""
args_dict = parse_args(sys.argv[1:]) def main(languages=None, empty=False, verbosity=1):
if args_dict['verbose']: languages = languages or []
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
else:
logging.basicConfig(stream=sys.stdout, level=logging.WARNING)
langs = args_dict['language'] if not languages:
root = LOCALE_DIR
validate_po_files(root, empty)
return
if langs is not None: # languages will be a list of language codes; test each language.
# lang will be a list of language codes; test each language. for language in languages:
for lang in langs: root = LOCALE_DIR / language
root = LOCALE_DIR / lang
# Assert that a directory for this language code exists on the system # Assert that a directory for this language code exists on the system
if not os.path.isdir(root): if not root.isdir():
log.error(" {0} is not a valid directory.\nSkipping language '{1}'".format(root, lang)) log.error(" {0} is not a valid directory.\nSkipping language '{1}'".format(root, language))
continue continue
# If we found the language code's directory, validate the files. # If we found the language code's directory, validate the files.
validate_po_files(root, args_dict['empty']) validate_po_files(root, empty)
else:
# If lang is None, we walk all of the .po files under root, and test each one.
root = LOCALE_DIR
validate_po_files(root, args_dict['empty'])
if __name__ == '__main__': if __name__ == '__main__':
main() parser = get_parser()
args = parser.parse_args()
if args.verbose:
log_level = logging.INFO
else:
log_level = logging.WARNING
logging.basicConfig(stream=sys.stdout, level=log_level)
main(languages=args.language, empty=args.empty, verbosity=args.verbose)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment