factor logging out of library calls
This commit is contained in:
@@ -76,4 +76,3 @@ class Configuration:
|
||||
|
||||
|
||||
CONFIGURATION = Configuration(LOCALE_DIR.joinpath('config').normpath())
|
||||
|
||||
|
||||
@@ -1,18 +1,9 @@
|
||||
import os, subprocess, logging
|
||||
import os, subprocess
|
||||
|
||||
from logger import get_logger
|
||||
from config import CONFIGURATION, BASE_DIR
|
||||
|
||||
|
||||
def get_default_logger():
|
||||
"""Returns a default logger"""
|
||||
log = logging.getLogger(__name__)
|
||||
log.setLevel(logging.INFO)
|
||||
log_handler = logging.StreamHandler()
|
||||
log_handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s'))
|
||||
log.addHandler(log_handler)
|
||||
return log
|
||||
|
||||
LOG = get_default_logger()
|
||||
|
||||
LOG = get_logger(__name__)
|
||||
|
||||
def execute(command, working_directory=BASE_DIR, log=LOG):
|
||||
"""
|
||||
|
||||
@@ -18,8 +18,10 @@ See https://edx-wiki.atlassian.net/wiki/display/ENG/PO+File+workflow
|
||||
import os
|
||||
from datetime import datetime
|
||||
from polib import pofile
|
||||
|
||||
from logger import get_logger
|
||||
from config import BASE_DIR, LOCALE_DIR, CONFIGURATION
|
||||
from execute import execute, create_dir_if_necessary, remove_file, LOG
|
||||
from execute import execute, create_dir_if_necessary, remove_file
|
||||
|
||||
# BABEL_CONFIG contains declarations for Babel to extract strings from mako template files
|
||||
# Use relpath to reduce noise in logs
|
||||
@@ -32,6 +34,7 @@ BABEL_OUT = BASE_DIR.relpathto(CONFIGURATION.source_messages_dir.joinpath('mako.
|
||||
SOURCE_WARN = 'This English source file is machine-generated. Do not check it into github'
|
||||
|
||||
def main ():
|
||||
log = get_logger(__name__)
|
||||
create_dir_if_necessary(LOCALE_DIR)
|
||||
source_msgs_dir = CONFIGURATION.source_messages_dir
|
||||
|
||||
@@ -60,7 +63,7 @@ def main ():
|
||||
execute(make_djangojs_cmd, working_directory=BASE_DIR)
|
||||
|
||||
for filename in generated_files:
|
||||
LOG.info('Cleaning %s' % filename)
|
||||
log.info('Cleaning %s' % filename)
|
||||
po = pofile(source_msgs_dir.joinpath(filename))
|
||||
# replace default headers with edX headers
|
||||
fix_header(po)
|
||||
|
||||
@@ -16,10 +16,11 @@
|
||||
import os
|
||||
from polib import pofile
|
||||
|
||||
from logger import get_logger
|
||||
from config import BASE_DIR, CONFIGURATION
|
||||
from execute import execute, remove_file, LOG
|
||||
from execute import execute, remove_file
|
||||
|
||||
def merge(locale, target='django.po', fail_if_missing=True):
|
||||
def merge(locale, target='django.po', fail_if_missing=True, log=None):
|
||||
"""
|
||||
For the given locale, merge django-partial.po, messages.po, mako.po -> django.po
|
||||
target is the resulting filename
|
||||
@@ -28,7 +29,8 @@ def merge(locale, target='django.po', fail_if_missing=True):
|
||||
If fail_if_missing is False, and the files to be merged are missing,
|
||||
just return silently.
|
||||
"""
|
||||
LOG.info('Merging locale={0}'.format(locale))
|
||||
if log:
|
||||
log.info('Merging locale={0}'.format(locale))
|
||||
locale_directory = CONFIGURATION.get_messages_dir(locale)
|
||||
files_to_merge = ('django-partial.po', 'messages.po', 'mako.po')
|
||||
try:
|
||||
@@ -70,10 +72,12 @@ def validate_files(dir, files_to_merge):
|
||||
raise Exception("I18N: Cannot generate because file not found: {0}".format(pathname))
|
||||
|
||||
def main ():
|
||||
log = get_logger(__name__)
|
||||
|
||||
for locale in CONFIGURATION.locales:
|
||||
merge(locale)
|
||||
# Dummy text is not required. Don't raise exception if files are missing.
|
||||
merge(CONFIGURATION.dummy_locale, fail_if_missing=False)
|
||||
merge(CONFIGURATION.dummy_locale, fail_if_missing=False, log=log)
|
||||
compile_cmd = 'django-admin.py compilemessages'
|
||||
execute(compile_cmd, working_directory=BASE_DIR)
|
||||
|
||||
|
||||
13
i18n/logger.py
Normal file
13
i18n/logger.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import logging
|
||||
|
||||
def get_logger(name):
|
||||
"""
|
||||
Returns a default logger.
|
||||
logging.basicConfig does not render to the console
|
||||
"""
|
||||
log = logging.getLogger()
|
||||
log.setLevel(logging.INFO)
|
||||
log_handler = logging.StreamHandler()
|
||||
log_handler.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s'))
|
||||
log.addHandler(log_handler)
|
||||
return log
|
||||
@@ -2,24 +2,27 @@ import os
|
||||
from unittest import TestCase
|
||||
from nose.plugins.skip import SkipTest
|
||||
|
||||
from logger import get_logger
|
||||
from config import LOCALE_DIR
|
||||
from execute import call, LOG
|
||||
from execute import call
|
||||
|
||||
def test_po_files():
|
||||
def test_po_files(root=LOCALE_DIR):
|
||||
"""
|
||||
This is a generator. It yields all of the .po files under root, and tests each one.
|
||||
"""
|
||||
for (dirpath, dirnames, filenames) in os.walk(LOCALE_DIR):
|
||||
log = get_logger(__name__)
|
||||
for (dirpath, dirnames, filenames) in os.walk(root):
|
||||
for name in filenames:
|
||||
print name
|
||||
(base, ext) = os.path.splitext(name)
|
||||
if ext.lower() == '.po':
|
||||
yield validate_po_file, os.path.join(dirpath, name)
|
||||
yield validate_po_file, os.path.join(dirpath, name), log
|
||||
|
||||
|
||||
def validate_po_file(filename):
|
||||
def validate_po_file(filename, log):
|
||||
"""
|
||||
Call GNU msgfmt -c on each .po file to validate its format.
|
||||
Any errors caught by msgfmt are logged to log.
|
||||
"""
|
||||
# Skip this test for now because it's very noisy
|
||||
raise SkipTest()
|
||||
@@ -27,5 +30,5 @@ def validate_po_file(filename):
|
||||
rfile = os.path.relpath(filename, LOCALE_DIR)
|
||||
(out, err) = call(['msgfmt','-c', rfile], log=None, working_directory=LOCALE_DIR)
|
||||
if err != '':
|
||||
LOG.warn('\n'+err)
|
||||
log.warn('\n'+err)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user