ThirdParty/Pygments/pygments/formatters/__init__.py

changeset 0
de9c2efb9d02
child 684
2f29a0b6e1c7
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 # -*- coding: utf-8 -*-
2 """
3 pygments.formatters
4 ~~~~~~~~~~~~~~~~~~~
5
6 Pygments formatters.
7
8 :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11 import os.path
12 import fnmatch
13
14 from pygments.formatters._mapping import FORMATTERS
15 from pygments.plugin import find_plugin_formatters
16 from pygments.util import docstring_headline, ClassNotFound
17
18 ns = globals()
19 for fcls in FORMATTERS:
20 ns[fcls.__name__] = fcls
21 del fcls
22
23 __all__ = ['get_formatter_by_name', 'get_formatter_for_filename',
24 'get_all_formatters'] + [cls.__name__ for cls in FORMATTERS]
25
26
27 _formatter_alias_cache = {}
28 _formatter_filename_cache = []
29
30 def _init_formatter_cache():
31 if _formatter_alias_cache:
32 return
33 for cls in get_all_formatters():
34 for alias in cls.aliases:
35 _formatter_alias_cache[alias] = cls
36 for fn in cls.filenames:
37 _formatter_filename_cache.append((fn, cls))
38
39
40 def find_formatter_class(name):
41 _init_formatter_cache()
42 cls = _formatter_alias_cache.get(name, None)
43 return cls
44
45
46 def get_formatter_by_name(name, **options):
47 _init_formatter_cache()
48 cls = _formatter_alias_cache.get(name, None)
49 if not cls:
50 raise ClassNotFound("No formatter found for name %r" % name)
51 return cls(**options)
52
53
54 def get_formatter_for_filename(fn, **options):
55 _init_formatter_cache()
56 fn = os.path.basename(fn)
57 for pattern, cls in _formatter_filename_cache:
58 if fnmatch.fnmatch(fn, pattern):
59 return cls(**options)
60 raise ClassNotFound("No formatter found for file name %r" % fn)
61
62
63 def get_all_formatters():
64 """Return a generator for all formatters."""
65 for formatter in FORMATTERS:
66 yield formatter
67 for _, formatter in find_plugin_formatters():
68 yield formatter

eric ide

mercurial