aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWaylan Limberg <waylan@gmail.com>2009-01-28 19:21:28 -0500
committerWaylan Limberg <waylan@gmail.com>2009-01-28 19:21:47 -0500
commitbe6891a22f6888af302392bc2dfa1e4a6099fd4f (patch)
tree787715967cfb2852f9e7bd20c572954ecc72c1df
parentc89c1263798eaedffa09077819e769b019801556 (diff)
downloadmarkdown-be6891a22f6888af302392bc2dfa1e4a6099fd4f.tar.gz
markdown-be6891a22f6888af302392bc2dfa1e4a6099fd4f.tar.bz2
markdown-be6891a22f6888af302392bc2dfa1e4a6099fd4f.zip
Fixed error handling to work when the logger is not being used.
Previously, when using as a module and not setting up your own logger, the logger would fail and Markdown would choke on the unresolved problem. Now exceptions or warnings are raised as appropriate (based on log level). However, if the user has configured the logger, the errors are logged, not raised and Markdown either continues or exits based on log level.
-rw-r--r--markdown/__init__.py66
1 files changed, 43 insertions, 23 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py
index f1ddcde..4db94f2 100644
--- a/markdown/__init__.py
+++ b/markdown/__init__.py
@@ -45,12 +45,10 @@ version_info = (2,0,0, "beta-2")
import re
import codecs
import sys
+import warnings
import logging
from logging import DEBUG, INFO, WARN, ERROR, CRITICAL
-def message(level, text):
- """ A wrapper method for logging debug messages. """
- logging.getLogger('MARKDOWN').log(level, text)
"""
CONSTANTS
@@ -81,22 +79,6 @@ INLINE_PLACEHOLDER_PREFIX = STX+"klzzwxh:"
INLINE_PLACEHOLDER = INLINE_PLACEHOLDER_PREFIX + "%s" + ETX
AMP_SUBSTITUTE = STX+"amp"+ETX
-import preprocessors
-import blockprocessors
-import treeprocessors
-import inlinepatterns
-import postprocessors
-import blockparser
-import etree_loader
-import odict
-
-# Extensions should use "markdown.etree" instead of "etree" (or do `from
-# markdown import etree`). Do not import it by yourself.
-
-etree = etree_loader.importETree()
-
-# Adds the ability to output html4
-import html4
"""
Constants you probably do not need to change
@@ -111,13 +93,24 @@ RTL_BIDI_RANGES = ( (u'\u0590', u'\u07FF'),
)
-
"""
AUXILIARY GLOBAL FUNCTIONS
=============================================================================
"""
+def message(level, text):
+ """ A wrapper method for logging debug messages. """
+ logger = logging.getLogger('MARKDOWN')
+ if logger.handlers:
+ # The logger is configured
+ logger.log(level, text)
+ if level > WARN:
+ sys.exit(0)
+ elif level > WARN:
+ raise MarkdownException, text
+ else:
+ warnings.warn(text, MarkdownWarning)
def isBlockLevel(tag):
@@ -134,6 +127,16 @@ class AtomicString(unicode):
pass
+class MarkdownException(Exception):
+ """ A Markdown Exception. """
+ pass
+
+
+class MarkdownWarning(Warning):
+ """ A Markdown Warning. """
+ pass
+
+
"""
OVERALL DESIGN
=============================================================================
@@ -154,6 +157,23 @@ Those steps are put together by the Markdown() class.
"""
+import preprocessors
+import blockprocessors
+import treeprocessors
+import inlinepatterns
+import postprocessors
+import blockparser
+import etree_loader
+import odict
+
+# Extensions should use "markdown.etree" instead of "etree" (or do `from
+# markdown import etree`). Do not import it by yourself.
+
+etree = etree_loader.importETree()
+
+# Adds the ability to output html4
+import html4
+
class Markdown:
"""Convert Markdown to HTML."""
@@ -327,7 +347,6 @@ class Markdown:
self.serializer = etree.tostring
else:
message(CRITICAL, 'Invalid Output Format: "%s". Use one of "xhtml1" or "html4".' % format)
- sys.exit()
def convert(self, source):
"""
@@ -496,8 +515,9 @@ def load_extension(ext_name, configs = []):
try: # Old style (mdx.<extension>)
module = __import__(module_name_old_style)
except ImportError:
- message(CRITICAL, "Failed loading extension '%s' from '%s' or '%s'"
+ message(WARN, "Failed loading extension '%s' from '%s' or '%s'"
% (ext_name, module_name_new_style, module_name_old_style))
+ # Return None so we don't try to initiate none-existant extension
return None
# If the module is loaded successfully, we expect it to define a
@@ -505,7 +525,7 @@ def load_extension(ext_name, configs = []):
try:
return module.makeExtension(configs.items())
except AttributeError:
- message(CRITICAL, "Failed to instantiate extension '%s'" % ext_name)
+ message(CRITICAL, "Failed to initiate extension '%s'" % ext_name)
def load_extensions(ext_names):