Testing whether a reader class is enabled *before* calling it, otherwise the test is never reached.

For example, if Markdown hasn't been installed, and you attempt read_file('foo.md') then an exception is raised in MarkdownReader.__init__ as it tries to call Markdown, which is False.  The class field "enabled" was not checked before attempting to instantiate MarkdownReader.  Instead, there is a later test on the instance, which won't be reached.
This commit is contained in:
Richard Brooksby 2013-05-21 23:47:18 +01:00
commit 150478625b

View file

@ -347,15 +347,16 @@ def read_file(path, fmt=None, settings=None):
if settings is None:
settings = {}
reader = EXTENSIONS[fmt](settings)
cls = EXTENSIONS[fmt]
if not cls.enabled:
raise ValueError("Missing dependencies for %s" % fmt)
reader = cls(settings)
settings_key = '%s_EXTENSIONS' % fmt.upper()
if settings and settings_key in settings:
reader.extensions = settings[settings_key]
if not reader.enabled:
raise ValueError("Missing dependencies for %s" % fmt)
metadata = parse_path_metadata(
path=path, settings=settings, process=reader.process_metadata)
content, reader_metadata = reader.read(path)