3 pygments.formatters |
3 pygments.formatters |
4 ~~~~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Pygments formatters. |
6 Pygments formatters. |
7 |
7 |
8 :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
11 from __future__ import unicode_literals |
|
12 |
11 |
13 import os.path |
12 import re |
|
13 import sys |
|
14 import types |
14 import fnmatch |
15 import fnmatch |
|
16 from os.path import basename |
15 |
17 |
16 from pygments.formatters._mapping import FORMATTERS |
18 from pygments.formatters._mapping import FORMATTERS |
17 from pygments.plugin import find_plugin_formatters |
19 from pygments.plugin import find_plugin_formatters |
18 from pygments.util import ClassNotFound |
20 from pygments.util import ClassNotFound, itervalues |
19 |
|
20 ns = globals() |
|
21 for fcls in FORMATTERS: |
|
22 ns[fcls.__name__] = fcls |
|
23 del fcls |
|
24 |
21 |
25 __all__ = ['get_formatter_by_name', 'get_formatter_for_filename', |
22 __all__ = ['get_formatter_by_name', 'get_formatter_for_filename', |
26 'get_all_formatters'] + [cls.__name__ for cls in FORMATTERS] |
23 'get_all_formatters'] + list(FORMATTERS) |
|
24 |
|
25 _formatter_cache = {} # classes by name |
|
26 _pattern_cache = {} |
27 |
27 |
28 |
28 |
29 _formatter_alias_cache = {} |
29 def _fn_matches(fn, glob): |
30 _formatter_filename_cache = [] |
30 """Return whether the supplied file name fn matches pattern filename.""" |
31 |
31 if glob not in _pattern_cache: |
32 def _init_formatter_cache(): |
32 pattern = _pattern_cache[glob] = re.compile(fnmatch.translate(glob)) |
33 if _formatter_alias_cache: |
33 return pattern.match(fn) |
34 return |
34 return _pattern_cache[glob].match(fn) |
35 for cls in get_all_formatters(): |
|
36 for alias in cls.aliases: |
|
37 _formatter_alias_cache[alias] = cls |
|
38 for fn in cls.filenames: |
|
39 _formatter_filename_cache.append((fn, cls)) |
|
40 |
35 |
41 |
36 |
42 def find_formatter_class(name): |
37 def _load_formatters(module_name): |
43 _init_formatter_cache() |
38 """Load a formatter (and all others in the module too).""" |
44 cls = _formatter_alias_cache.get(name, None) |
39 mod = __import__(module_name, None, None, ['__all__']) |
45 return cls |
40 for formatter_name in mod.__all__: |
|
41 cls = getattr(mod, formatter_name) |
|
42 _formatter_cache[cls.name] = cls |
46 |
43 |
47 |
44 |
48 def get_formatter_by_name(name, **options): |
45 def get_all_formatters(): |
49 _init_formatter_cache() |
46 """Return a generator for all formatter classes.""" |
50 cls = _formatter_alias_cache.get(name, None) |
47 # NB: this returns formatter classes, not info like get_all_lexers(). |
51 if not cls: |
48 for info in itervalues(FORMATTERS): |
52 raise ClassNotFound("No formatter found for name %r" % name) |
49 if info[1] not in _formatter_cache: |
|
50 _load_formatters(info[0]) |
|
51 yield _formatter_cache[info[1]] |
|
52 for _, formatter in find_plugin_formatters(): |
|
53 yield formatter |
|
54 |
|
55 |
|
56 def find_formatter_class(alias): |
|
57 """Lookup a formatter by alias. |
|
58 |
|
59 Returns None if not found. |
|
60 """ |
|
61 for module_name, name, aliases, _, _ in itervalues(FORMATTERS): |
|
62 if alias in aliases: |
|
63 if name not in _formatter_cache: |
|
64 _load_formatters(module_name) |
|
65 return _formatter_cache[name] |
|
66 for _, cls in find_plugin_formatters(): |
|
67 if alias in cls.aliases: |
|
68 return cls |
|
69 |
|
70 |
|
71 def get_formatter_by_name(_alias, **options): |
|
72 """Lookup and instantiate a formatter by alias. |
|
73 |
|
74 Raises ClassNotFound if not found. |
|
75 """ |
|
76 cls = find_formatter_class(_alias) |
|
77 if cls is None: |
|
78 raise ClassNotFound("no formatter found for name %r" % _alias) |
53 return cls(**options) |
79 return cls(**options) |
54 |
80 |
55 |
81 |
56 def get_formatter_for_filename(fn, **options): |
82 def get_formatter_for_filename(fn, **options): |
57 _init_formatter_cache() |
83 """Lookup and instantiate a formatter by filename pattern. |
58 fn = os.path.basename(fn) |
84 |
59 for pattern, cls in _formatter_filename_cache: |
85 Raises ClassNotFound if not found. |
60 if fnmatch.fnmatch(fn, pattern): |
86 """ |
61 return cls(**options) |
87 fn = basename(fn) |
62 raise ClassNotFound("No formatter found for file name %r" % fn) |
88 for modname, name, _, filenames, _ in itervalues(FORMATTERS): |
|
89 for filename in filenames: |
|
90 if _fn_matches(fn, filename): |
|
91 if name not in _formatter_cache: |
|
92 _load_formatters(modname) |
|
93 return _formatter_cache[name](**options) |
|
94 for cls in find_plugin_formatters(): |
|
95 for filename in cls.filenames: |
|
96 if _fn_matches(fn, filename): |
|
97 return cls(**options) |
|
98 raise ClassNotFound("no formatter found for file name %r" % fn) |
63 |
99 |
64 |
100 |
65 def get_all_formatters(): |
101 class _automodule(types.ModuleType): |
66 """Return a generator for all formatters.""" |
102 """Automatically import formatters.""" |
67 for formatter in FORMATTERS: |
103 |
68 yield formatter |
104 def __getattr__(self, name): |
69 for _, formatter in find_plugin_formatters(): |
105 info = FORMATTERS.get(name) |
70 yield formatter |
106 if info: |
|
107 _load_formatters(info[0]) |
|
108 cls = _formatter_cache[info[1]] |
|
109 setattr(self, name, cls) |
|
110 return cls |
|
111 raise AttributeError(name) |
|
112 |
|
113 |
|
114 oldmod = sys.modules[__name__] |
|
115 newmod = _automodule(__name__) |
|
116 newmod.__dict__.update(oldmod.__dict__) |
|
117 sys.modules[__name__] = newmod |
|
118 del newmod.newmod, newmod.oldmod, newmod.sys, newmod.types |