Back to index

moin  1.9.0~rc2
__init__.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     pygments.formatters
00004     ~~~~~~~~~~~~~~~~~~~
00005 
00006     Pygments formatters.
00007 
00008     :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
00009     :license: BSD, see LICENSE for details.
00010 """
00011 import os.path
00012 import fnmatch
00013 
00014 from pygments.formatters._mapping import FORMATTERS
00015 from pygments.plugin import find_plugin_formatters
00016 from pygments.util import docstring_headline, ClassNotFound
00017 
00018 ns = globals()
00019 for fcls in FORMATTERS:
00020     ns[fcls.__name__] = fcls
00021 del fcls
00022 
00023 __all__ = ['get_formatter_by_name', 'get_formatter_for_filename',
00024            'get_all_formatters'] + [cls.__name__ for cls in FORMATTERS]
00025 
00026 
00027 _formatter_alias_cache = {}
00028 _formatter_filename_cache = []
00029 
00030 def _init_formatter_cache():
00031     if _formatter_alias_cache:
00032         return
00033     for cls in get_all_formatters():
00034         for alias in cls.aliases:
00035             _formatter_alias_cache[alias] = cls
00036         for fn in cls.filenames:
00037             _formatter_filename_cache.append((fn, cls))
00038 
00039 
00040 def find_formatter_class(name):
00041     _init_formatter_cache()
00042     cls = _formatter_alias_cache.get(name, None)
00043     return cls
00044 
00045 
00046 def get_formatter_by_name(name, **options):
00047     _init_formatter_cache()
00048     cls = _formatter_alias_cache.get(name, None)
00049     if not cls:
00050         raise ClassNotFound("No formatter found for name %r" % name)
00051     return cls(**options)
00052 
00053 
00054 def get_formatter_for_filename(fn, **options):
00055     _init_formatter_cache()
00056     fn = os.path.basename(fn)
00057     for pattern, cls in _formatter_filename_cache:
00058         if fnmatch.fnmatch(fn, pattern):
00059             return cls(**options)
00060     raise ClassNotFound("No formatter found for file name %r" % fn)
00061 
00062 
00063 def get_all_formatters():
00064     """Return a generator for all formatters."""
00065     for formatter in FORMATTERS:
00066         yield formatter
00067     for _, formatter in find_plugin_formatters():
00068         yield formatter