Skip to content

Instantly share code, notes, and snippets.

@avalanchy
Last active August 29, 2015 13:56
Show Gist options
  • Save avalanchy/8862584 to your computer and use it in GitHub Desktop.
Save avalanchy/8862584 to your computer and use it in GitHub Desktop.
pybabel errors logging. runs pybabel and check if files with jinja2 wrappers are in messages.pot.
"""runs pybabel and check if files with jinja2 wrappers are in messages.pot"""
import os
# config some constants
ROOT_DIR = 'templates'
FILES_EXTENSIONS = ('.html', '.txt')
SEARCH_TAGS = ('{% trans', '_(')
OUTPUT_FILE = 'locales/messages.pot'
# run babel extracting
os.system(
'pybabel -q extract -F babel.conf -o {output_file} ./ '
'--sort-by-file --no-wrap'.format(
output_file=OUTPUT_FILE,
)
)
# collect files that contains SEARCH_TAGS tags
translated_files = []
for root, _, files in os.walk(ROOT_DIR):
for path in files:
_, extension = os.path.splitext(path)
if extension not in FILES_EXTENSIONS:
continue
full_path = os.path.join(root, path)
with open(full_path, 'r') as f:
file_body = f.read()
if any((file_body.find(tag) != -1 for tag in SEARCH_TAGS)):
translated_files.append(full_path)
# check if translated files exist in message.pot
error_files = []
with open(OUTPUT_FILE, 'r') as f:
messages = f.read()
for path in translated_files:
if messages.find(path) == -1:
error_files.append(path)
# std output
BAD = '%d templates contain {tags} and NOT exist in {output_file}:'.format(
tags=[tag.replace('%', '%%') for tag in SEARCH_TAGS],
output_file=OUTPUT_FILE,
)
GOOD = 'Files extraction was successful!'
if error_files:
print BAD % len(error_files)
for path in sorted(error_files):
print 4*' ' + path
else:
print GOOD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment