compilemessages: Add mypy annotations.

This commit is contained in:
Tim Abbott 2016-07-29 20:03:57 -07:00
parent a787dee48b
commit 641aa08721
1 changed files with 19 additions and 11 deletions

View File

@ -3,6 +3,8 @@ from __future__ import print_function
import os import os
import re import re
import ujson import ujson
from six import text_type
from typing import Any, Dict, List from typing import Any, Dict, List
from django.core.management.commands import compilemessages from django.core.management.commands import compilemessages
@ -13,39 +15,43 @@ import polib
class Command(compilemessages.Command): class Command(compilemessages.Command):
def handle(self, *args, **options): def handle(self, *args, **options):
# type: (*Any, **Any) -> None
super(Command, self).handle(*args, **options) super(Command, self).handle(*args, **options)
self.extract_language_options() self.extract_language_options()
def get_po_filename(self, locale_path, locale): def get_po_filename(self, locale_path, locale):
# type: (text_type, text_type) -> text_type
po_template = '{}/{}/LC_MESSAGES/django.po' po_template = '{}/{}/LC_MESSAGES/django.po'
return po_template.format(locale_path, locale) return po_template.format(locale_path, locale)
def get_json_filename(self, locale_path, locale): def get_json_filename(self, locale_path, locale):
return "{}/{}/translations.json".format(locale_path, locale) # type: (text_type, text_type) -> text_type
return "{}/{}/translations.json".format(locale_path, locale)
def extract_language_options(self): def extract_language_options(self):
locale_path = "{}/locale".format(settings.STATIC_ROOT) # type: () -> None
output_path = "{}/language_options.json".format(locale_path) locale_path = u"{}/locale".format(settings.STATIC_ROOT)
output_path = u"{}/language_options.json".format(locale_path)
data = {'languages': []} # type: Dict[str, List[Dict[str, str]]] data = {'languages': []} # type: Dict[str, List[Dict[str, text_type]]]
lang_name_re = re.compile('"Language-Team: (.*?) \(') lang_name_re = re.compile('"Language-Team: (.*?) \(')
locales = os.listdir(locale_path) locales = os.listdir(locale_path)
locales.append('en') locales.append(u'en')
locales = list(set(locales)) locales = list(set(locales))
for locale in locales: for locale in locales:
info = {} info = {}
if locale == 'en': if locale == u'en':
data['languages'].append({ data['languages'].append({
'code': 'en', 'code': u'en',
'name': 'English', 'name': u'English',
}) })
continue continue
if locale == 'zh-CN': if locale == u'zh-CN':
continue continue
if locale == 'zh_CN': if locale == u'zh_CN':
name = 'Simplified Chinese' name = u'Simplified Chinese'
else: else:
filename = self.get_po_filename(locale_path, locale) filename = self.get_po_filename(locale_path, locale)
if not os.path.exists(filename): if not os.path.exists(filename):
@ -75,6 +81,8 @@ class Command(compilemessages.Command):
ujson.dump(data, writer, indent=2) ujson.dump(data, writer, indent=2)
def get_translation_percentage(self, locale_path, locale): def get_translation_percentage(self, locale_path, locale):
# type: (text_type, text_type) -> text_type
# backend stats # backend stats
po = polib.pofile(self.get_po_filename(locale_path, locale)) po = polib.pofile(self.get_po_filename(locale_path, locale))
not_translated = len(po.untranslated_entries()) not_translated = len(po.untranslated_entries())