From cf8d6b89657295ed55fc7ca8a70bf3a7de3db17f Mon Sep 17 00:00:00 2001 From: Steve Strassmann Date: Tue, 7 May 2013 14:10:59 -0400 Subject: [PATCH] factor logging out of library calls --- i18n/config.py | 1 - i18n/execute.py | 17 ++++------------- i18n/extract.py | 7 +++++-- i18n/generate.py | 12 ++++++++---- i18n/logger.py | 13 +++++++++++++ i18n/tests/test_validate.py | 15 +++++++++------ 6 files changed, 39 insertions(+), 26 deletions(-) create mode 100644 i18n/logger.py diff --git a/i18n/config.py b/i18n/config.py index 461b0dfd15..d78fc0ca45 100644 --- a/i18n/config.py +++ b/i18n/config.py @@ -76,4 +76,3 @@ class Configuration: CONFIGURATION = Configuration(LOCALE_DIR.joinpath('config').normpath()) - diff --git a/i18n/execute.py b/i18n/execute.py index 6a7e12ac5d..e3f3478d12 100644 --- a/i18n/execute.py +++ b/i18n/execute.py @@ -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): """ diff --git a/i18n/extract.py b/i18n/extract.py index 57da0bd76d..9b0ad3829c 100755 --- a/i18n/extract.py +++ b/i18n/extract.py @@ -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) diff --git a/i18n/generate.py b/i18n/generate.py index 0c7179b2c6..ffc88b64d0 100755 --- a/i18n/generate.py +++ b/i18n/generate.py @@ -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) diff --git a/i18n/logger.py b/i18n/logger.py new file mode 100644 index 0000000000..20d767a032 --- /dev/null +++ b/i18n/logger.py @@ -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 diff --git a/i18n/tests/test_validate.py b/i18n/tests/test_validate.py index 7f0cdd7a25..6bb7164a50 100644 --- a/i18n/tests/test_validate.py +++ b/i18n/tests/test_validate.py @@ -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)