|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.formatters |
|
4 ~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Pygments formatters. |
|
7 |
|
8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 import re |
|
13 import sys |
|
14 import types |
|
15 import fnmatch |
|
16 from os.path import basename |
|
17 |
|
18 from pygments.formatters._mapping import FORMATTERS |
|
19 from pygments.plugin import find_plugin_formatters |
|
20 from pygments.util import ClassNotFound, itervalues |
|
21 |
|
22 __all__ = ['get_formatter_by_name', 'get_formatter_for_filename', |
|
23 'get_all_formatters', 'load_formatter_from_file'] + list(FORMATTERS) |
|
24 |
|
25 _formatter_cache = {} # classes by name |
|
26 _pattern_cache = {} |
|
27 |
|
28 |
|
29 def _fn_matches(fn, glob): |
|
30 """Return whether the supplied file name fn matches pattern filename.""" |
|
31 if glob not in _pattern_cache: |
|
32 pattern = _pattern_cache[glob] = re.compile(fnmatch.translate(glob)) |
|
33 return pattern.match(fn) |
|
34 return _pattern_cache[glob].match(fn) |
|
35 |
|
36 |
|
37 def _load_formatters(module_name): |
|
38 """Load a formatter (and all others in the module too).""" |
|
39 mod = __import__(module_name, None, None, ['__all__']) |
|
40 for formatter_name in mod.__all__: |
|
41 cls = getattr(mod, formatter_name) |
|
42 _formatter_cache[cls.name] = cls |
|
43 |
|
44 |
|
45 def get_all_formatters(): |
|
46 """Return a generator for all formatter classes.""" |
|
47 # NB: this returns formatter classes, not info like get_all_lexers(). |
|
48 for info in itervalues(FORMATTERS): |
|
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) |
|
79 return cls(**options) |
|
80 |
|
81 |
|
82 def load_formatter_from_file(filename, formattername="CustomFormatter", |
|
83 **options): |
|
84 """Load a formatter from a file. |
|
85 |
|
86 This method expects a file located relative to the current working |
|
87 directory, which contains a class named CustomFormatter. By default, |
|
88 it expects the Formatter to be named CustomFormatter; you can specify |
|
89 your own class name as the second argument to this function. |
|
90 |
|
91 Users should be very careful with the input, because this method |
|
92 is equivalent to running eval on the input file. |
|
93 |
|
94 Raises ClassNotFound if there are any problems importing the Formatter. |
|
95 |
|
96 .. versionadded:: 2.2 |
|
97 """ |
|
98 try: |
|
99 # This empty dict will contain the namespace for the exec'd file |
|
100 custom_namespace = {} |
|
101 exec(open(filename, 'rb').read(), custom_namespace) |
|
102 # Retrieve the class `formattername` from that namespace |
|
103 if formattername not in custom_namespace: |
|
104 raise ClassNotFound('no valid %s class found in %s' % |
|
105 (formattername, filename)) |
|
106 formatter_class = custom_namespace[formattername] |
|
107 # And finally instantiate it with the options |
|
108 return formatter_class(**options) |
|
109 except IOError as err: |
|
110 raise ClassNotFound('cannot read %s' % filename) |
|
111 except ClassNotFound as err: |
|
112 raise |
|
113 except Exception as err: |
|
114 raise ClassNotFound('error when loading custom formatter: %s' % err) |
|
115 |
|
116 |
|
117 def get_formatter_for_filename(fn, **options): |
|
118 """Lookup and instantiate a formatter by filename pattern. |
|
119 |
|
120 Raises ClassNotFound if not found. |
|
121 """ |
|
122 fn = basename(fn) |
|
123 for modname, name, _, filenames, _ in itervalues(FORMATTERS): |
|
124 for filename in filenames: |
|
125 if _fn_matches(fn, filename): |
|
126 if name not in _formatter_cache: |
|
127 _load_formatters(modname) |
|
128 return _formatter_cache[name](**options) |
|
129 for cls in find_plugin_formatters(): |
|
130 for filename in cls.filenames: |
|
131 if _fn_matches(fn, filename): |
|
132 return cls(**options) |
|
133 raise ClassNotFound("no formatter found for file name %r" % fn) |
|
134 |
|
135 |
|
136 class _automodule(types.ModuleType): |
|
137 """Automatically import formatters.""" |
|
138 |
|
139 def __getattr__(self, name): |
|
140 info = FORMATTERS.get(name) |
|
141 if info: |
|
142 _load_formatters(info[0]) |
|
143 cls = _formatter_cache[info[1]] |
|
144 setattr(self, name, cls) |
|
145 return cls |
|
146 raise AttributeError(name) |
|
147 |
|
148 |
|
149 oldmod = sys.modules[__name__] |
|
150 newmod = _automodule(__name__) |
|
151 newmod.__dict__.update(oldmod.__dict__) |
|
152 sys.modules[__name__] = newmod |
|
153 del newmod.newmod, newmod.oldmod, newmod.sys, newmod.types |