From 17123eaf6eb72a60bf6c5b8a1ad0457187387879 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Sat, 31 Jan 2015 11:55:37 -0500 Subject: Only log warnings from commandline script. I need to remember this is a lib first and not configure logging from within the lib. Also, from the script we are now actually displaying deprecation warnings. For some reason I don't understnad deprecation warnings are hidden by default in Python. And who remembers to run Python with the `-Wd` flag every time they upgrade a lib just to make sure there's no new deprecations? Fixes #384. --- markdown/__init__.py | 2 +- markdown/__main__.py | 40 +++++++++++++++++++++++++++------------- markdown/extensions/headerid.py | 4 ---- tests/test_apis.py | 5 ++--- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/markdown/__init__.py b/markdown/__init__.py index 19c4fc7..fc51288 100644 --- a/markdown/__init__.py +++ b/markdown/__init__.py @@ -49,8 +49,8 @@ from .serializers import to_html_string, to_xhtml_string __all__ = ['Markdown', 'markdown', 'markdownFromFile'] + logger = logging.getLogger('MARKDOWN') -logging.captureWarnings(True) class Markdown(object): diff --git a/markdown/__main__.py b/markdown/__main__.py index 3586ead..17bfa9f 100644 --- a/markdown/__main__.py +++ b/markdown/__main__.py @@ -4,17 +4,18 @@ COMMAND-LINE SPECIFIC STUFF """ -import markdown import sys import optparse import codecs +import warnings +import markdown try: import yaml except ImportError: # pragma: no cover import json as yaml import logging -from logging import DEBUG, INFO, CRITICAL +from logging import DEBUG, WARNING, CRITICAL logger = logging.getLogger('MARKDOWN') @@ -62,7 +63,7 @@ def parse_options(args=None, values=None): action="store_const", const=CRITICAL+10, dest="verbose", help="Suppress all warnings.") parser.add_option("-v", "--verbose", - action="store_const", const=INFO, dest="verbose", + action="store_const", const=WARNING, dest="verbose", help="Print all warnings.") parser.add_option("--noisy", action="store_const", const=DEBUG, dest="verbose", @@ -91,14 +92,21 @@ def parse_options(args=None, values=None): e.args = (message,) + e.args[1:] raise - return {'input': input_file, - 'output': options.filename, - 'safe_mode': options.safe, - 'extensions': options.extensions, - 'extension_configs': extension_configs, - 'encoding': options.encoding, - 'output_format': options.output_format, - 'lazy_ol': options.lazy_ol}, options.verbose + opts = { + 'input': input_file, + 'output': options.filename, + 'extensions': options.extensions, + 'extension_configs': extension_configs, + 'encoding': options.encoding, + 'output_format': options.output_format, + 'lazy_ol': options.lazy_ol + } + + if options.safe: + # Avoid deprecation warning if user didn't set option + opts['safe_mode'] = options.safe + + return opts, options.verbose def run(): # pragma: no cover @@ -109,7 +117,14 @@ def run(): # pragma: no cover if not options: sys.exit(2) logger.setLevel(logging_level) - logger.addHandler(logging.StreamHandler()) + console_handler = logging.StreamHandler() + logger.addHandler(console_handler) + if logging_level <= WARNING: + # Ensure deprecation warnings get displayed + warnings.filterwarnings('default') + logging.captureWarnings(True) + warn_logger = logging.getLogger('py.warnings') + warn_logger.addHandler(console_handler) # Run markdown.markdownFromFile(**options) @@ -117,6 +132,5 @@ def run(): # pragma: no cover if __name__ == '__main__': # pragma: no cover # Support running module as a commandline command. - # Python 2.5 & 2.6 do: `python -m markdown.__main__ [options] [args]`. # Python 2.7 & 3.x do: `python -m markdown [options] [args]`. run() diff --git a/markdown/extensions/headerid.py b/markdown/extensions/headerid.py index 9340a1b..67c9b7d 100644 --- a/markdown/extensions/headerid.py +++ b/markdown/extensions/headerid.py @@ -21,12 +21,8 @@ from . import Extension from ..treeprocessors import Treeprocessor from ..util import parseBoolValue from .toc import slugify, unique, stashedHTML2text -import logging import warnings -logger = logging.getLogger('MARKDOWN') -logging.captureWarnings(True) - class HeaderIdTreeprocessor(Treeprocessor): """ Assign IDs to headers. """ diff --git a/tests/test_apis.py b/tests/test_apis.py index 4ed2990..e3de779 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -15,7 +15,7 @@ import types import markdown import warnings from markdown.__main__ import parse_options -from logging import DEBUG, INFO, CRITICAL +from logging import DEBUG, WARNING, CRITICAL import yaml import tempfile @@ -613,7 +613,6 @@ class TestCliOptionParsing(unittest.TestCase): 'input': None, 'output': None, 'encoding': None, - 'safe_mode': False, 'output_format': 'xhtml1', 'lazy_ol': True, 'extensions': [], @@ -636,7 +635,7 @@ class TestCliOptionParsing(unittest.TestCase): def testVerboseOption(self): options, logging_level = parse_options(['-v']) - self.assertEqual(logging_level, INFO) + self.assertEqual(logging_level, WARNING) def testNoisyOption(self): options, logging_level = parse_options(['--noisy']) -- cgit v1.2.3