diff options
author | Craig de Stigter <craig.destigter@koordinates.com> | 2011-04-18 16:51:12 +1200 |
---|---|---|
committer | Craig de Stigter <craig.destigter@koordinates.com> | 2011-04-18 16:51:12 +1200 |
commit | 8bc4135947d9a2fc1749ebc4a8cdbd4d72d7a74e (patch) | |
tree | 7faa86d9ba1d0de861202f5f80739b8b677b50b4 /markdown/__init__.py | |
parent | 80aa9a2d52f2fcbffd6e15d208b8ead5886900b1 (diff) | |
download | markdown-8bc4135947d9a2fc1749ebc4a8cdbd4d72d7a74e.tar.gz markdown-8bc4135947d9a2fc1749ebc4a8cdbd4d72d7a74e.tar.bz2 markdown-8bc4135947d9a2fc1749ebc4a8cdbd4d72d7a74e.zip |
sane logging. remove sys.exit() calls (libraries should never ever call sys.exit()) and raise appropriate exceptions instead. backwards incompatible insofar as custom extensions may need tweaks if they're using old markdown logging (but they shouldn't, it was gross)
Diffstat (limited to 'markdown/__init__.py')
-rw-r--r-- | markdown/__init__.py | 42 |
1 files changed, 19 insertions, 23 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py index 0774174..b397f1b 100644 --- a/markdown/__init__.py +++ b/markdown/__init__.py @@ -35,8 +35,7 @@ version_info = (2,1,0, "Dev") import re import codecs -from logging import DEBUG, INFO, WARN, ERROR, CRITICAL -from md_logging import message +import logging import util from preprocessors import build_preprocessors from blockprocessors import build_block_parser @@ -49,7 +48,10 @@ import html4 # For backwards compatibility in the 2.0.x series # The things defined in these modules started off in __init__.py so third # party code might need to access them here. -from util import * +#from util import * + + +logger = logging.getLogger('MARKDOWN') class Markdown: @@ -139,13 +141,10 @@ class Markdown: if isinstance(ext, basestring): ext = self.build_extension(ext, configs.get(ext, [])) if isinstance(ext, Extension): - try: - ext.extendMarkdown(self, globals()) - except NotImplementedError, e: - message(ERROR, e) + # might raise NotImplementedError, but that's the extension author's problem + ext.extendMarkdown(self, globals()) else: - message(ERROR, - 'Extension "%s.%s" must be of type: "markdown.Extension".' \ + raise ValueError('Extension "%s.%s" must be of type: "markdown.Extension".' \ % (ext.__class__.__module__, ext.__class__.__name__)) return self @@ -179,17 +178,18 @@ class Markdown: try: # Old style (mdx_<extension>) module = __import__(module_name_old_style) except ImportError: - 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 + logger.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 # function called makeExtension() try: return module.makeExtension(configs.items()) except AttributeError, e: - message(CRITICAL, "Failed to initiate extension '%s': %s" % (ext_name, e)) + logger.warn("Failed to initiate extension '%s': %s" % (ext_name, e)) + return None def registerExtension(self, extension): """ This gets called by the extension """ @@ -214,8 +214,7 @@ class Markdown: try: self.serializer = self.output_formats[format.lower()] except KeyError: - message(CRITICAL, - 'Invalid Output Format: "%s". Use one of %s.' \ + raise KeyError('Invalid Output Format: "%s". Use one of %s.' \ % (format, self.output_formats.keys())) return self @@ -244,12 +243,9 @@ class Markdown: # Fixup the source text if not source.strip(): return u"" # a blank unicode string - try: - source = unicode(source) - except UnicodeDecodeError: - message(CRITICAL, - 'UnicodeDecodeError: Markdown only accepts unicode or ascii input.') - return u"" + + # if this raises UnicodeDecodeError, that's the caller's fault. + source = unicode(source) source = source.replace(util.STX, "").replace(util.ETX, "") source = source.replace("\r\n", "\n").replace("\r", "\n") + "\n\n" @@ -283,7 +279,7 @@ class Markdown: output = '' else: # We have a serious problem - message(CRITICAL, 'Failed to strip top level tags.') + raise ValueError('Markdown failed to strip top-level tags. Document=%r' % output.strip()) # Run the text post-processors for pp in self.postprocessors.values(): |