diff options
| author | wiemann <wiemann@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2007-03-01 17:58:13 +0000 |
|---|---|---|
| committer | wiemann <wiemann@929543f6-e4f2-0310-98a6-ba3bd3dd1d04> | 2007-03-01 17:58:13 +0000 |
| commit | 071fa6d2a41da4e5b9998c18b9b7e2fa15a0476c (patch) | |
| tree | c20874611c86dee6b3bfd82e5905ab57c728a033 /docutils | |
| parent | 70c8dc88aaa4fcdbf081e1625c5505567265a105 (diff) | |
| download | docutils-071fa6d2a41da4e5b9998c18b9b7e2fa15a0476c.tar.gz | |
moved writers.get_writer_class to generic utils.get_entry_point
git-svn-id: http://svn.code.sf.net/p/docutils/code/branches/plugins@4973 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils')
| -rw-r--r-- | docutils/core.py | 2 | ||||
| -rw-r--r-- | docutils/utils.py | 32 | ||||
| -rw-r--r-- | docutils/writers/__init__.py | 25 |
3 files changed, 33 insertions, 26 deletions
diff --git a/docutils/core.py b/docutils/core.py index f68f74cea..8c6baa80e 100644 --- a/docutils/core.py +++ b/docutils/core.py @@ -82,7 +82,7 @@ class Publisher: def set_writer(self, writer_name): """Set `self.writer` by name.""" - writer_class = writers.get_writer_class(writer_name) + writer_class = utils.get_entry_point('docutils.writers', writer_name) self.writer = writer_class() def set_components(self, reader_name, parser_name, writer_name): diff --git a/docutils/utils.py b/docutils/utils.py index ce9d2868e..139efbe1b 100644 --- a/docutils/utils.py +++ b/docutils/utils.py @@ -15,10 +15,10 @@ import types import warnings import unicodedata from types import StringType, UnicodeType +import pkg_resources from docutils import ApplicationError, DataError from docutils import nodes - class SystemMessage(ApplicationError): def __init__(self, system_message, level): @@ -577,3 +577,33 @@ class DependencyList: else: output_file = None return '%s(%r, %s)' % (self.__class__.__name__, output_file, self.list) + + +class EntryPointNotFoundError(ApplicationError): pass +class DuplicateEntryPointError(ApplicationError): pass + +def get_entry_point(group, name): + """ + Load the entry point with name `name` in entry point group `group` + and return the associated Python object. If no such entry point + is found, raise `EntryPointNotFoundError`. If more than one entry + point is found, raise `DuplicateEntryPointError`. + """ + entry_points = list(pkg_resources.iter_entry_points(group, name)) + if not len(entry_points): + # Retrieving components used to be case-insensitive, but that's + # probably a bad idea. + if name != name.lower(): + entry_point = get_entry_point(group, name.lower()) + print >>sys.stderr, 'Warning: Entry point names are case-'\ + 'sensitive; using lower-case name "%s.%s"' \ + % (group, name.lower()) + return entry_point + else: + raise EntryPointNotFoundError( + 'Entry point "%s.%s" not found.' % (group, name)) + if len(entry_points) > 1: + raise DuplicateEntryPointError('More than one entry point "%s.%s" ' + 'installed on system.' % (group, name)) + entry_point = entry_points[0] + return entry_point.load() diff --git a/docutils/writers/__init__.py b/docutils/writers/__init__.py index e2b15a74f..17d5e4d87 100644 --- a/docutils/writers/__init__.py +++ b/docutils/writers/__init__.py @@ -11,7 +11,7 @@ __docformat__ = 'reStructuredText' import os.path import docutils -from docutils import languages, Component +from docutils import languages, utils, Component from docutils.transforms import universal @@ -113,26 +113,3 @@ class UnfilteredWriter(Writer): # later, the then-used writer will add the appropriate # transforms. return Component.get_transforms(self) - - -_writer_aliases = { - 'html': 'html4css1', - 'latex': 'latex2e', - 'pprint': 'pseudoxml', - 'pformat': 'pseudoxml', - 'pdf': 'rlpdf', - 'xml': 'docutils_xml', - 's5': 's5_html'} - -def get_writer_class(writer_name): - """Return the Writer class from the `writer_name` module.""" - writer_name = writer_name.lower() - if _writer_aliases.has_key(writer_name): - writer_name = _writer_aliases[writer_name] - import pkg_resources - writer = None - for entrypoint in pkg_resources.iter_entry_points('docutils.writers'): - if entrypoint.name == writer_name: - writer = entrypoint.load() - assert writer is not None, 'writer "%s" not found' % writer_name - return writer |
