eric6/ThirdParty/Pygments/pygments/formatters/__init__.py

changeset 7547
21b0534faebc
parent 6942
2602857055c5
child 7701
25f42e208e08
equal deleted inserted replaced
7546:bf5f777260a6 7547:21b0534faebc
3 pygments.formatters 3 pygments.formatters
4 ~~~~~~~~~~~~~~~~~~~ 4 ~~~~~~~~~~~~~~~~~~~
5 5
6 Pygments formatters. 6 Pygments formatters.
7 7
8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS. 8 :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details. 9 :license: BSD, see LICENSE for details.
10 """ 10 """
11 11
12 import re 12 import re
13 import sys 13 import sys
15 import fnmatch 15 import fnmatch
16 from os.path import basename 16 from os.path import basename
17 17
18 from pygments.formatters._mapping import FORMATTERS 18 from pygments.formatters._mapping import FORMATTERS
19 from pygments.plugin import find_plugin_formatters 19 from pygments.plugin import find_plugin_formatters
20 from pygments.util import ClassNotFound, itervalues 20 from pygments.util import ClassNotFound
21 21
22 __all__ = ['get_formatter_by_name', 'get_formatter_for_filename', 22 __all__ = ['get_formatter_by_name', 'get_formatter_for_filename',
23 'get_all_formatters', 'load_formatter_from_file'] + list(FORMATTERS) 23 'get_all_formatters', 'load_formatter_from_file'] + list(FORMATTERS)
24 24
25 _formatter_cache = {} # classes by name 25 _formatter_cache = {} # classes by name
43 43
44 44
45 def get_all_formatters(): 45 def get_all_formatters():
46 """Return a generator for all formatter classes.""" 46 """Return a generator for all formatter classes."""
47 # NB: this returns formatter classes, not info like get_all_lexers(). 47 # NB: this returns formatter classes, not info like get_all_lexers().
48 for info in itervalues(FORMATTERS): 48 for info in FORMATTERS.values():
49 if info[1] not in _formatter_cache: 49 if info[1] not in _formatter_cache:
50 _load_formatters(info[0]) 50 _load_formatters(info[0])
51 yield _formatter_cache[info[1]] 51 yield _formatter_cache[info[1]]
52 for _, formatter in find_plugin_formatters(): 52 for _, formatter in find_plugin_formatters():
53 yield formatter 53 yield formatter
56 def find_formatter_class(alias): 56 def find_formatter_class(alias):
57 """Lookup a formatter by alias. 57 """Lookup a formatter by alias.
58 58
59 Returns None if not found. 59 Returns None if not found.
60 """ 60 """
61 for module_name, name, aliases, _, _ in itervalues(FORMATTERS): 61 for module_name, name, aliases, _, _ in FORMATTERS.values():
62 if alias in aliases: 62 if alias in aliases:
63 if name not in _formatter_cache: 63 if name not in _formatter_cache:
64 _load_formatters(module_name) 64 _load_formatters(module_name)
65 return _formatter_cache[name] 65 return _formatter_cache[name]
66 for _, cls in find_plugin_formatters(): 66 for _, cls in find_plugin_formatters():
96 .. versionadded:: 2.2 96 .. versionadded:: 2.2
97 """ 97 """
98 try: 98 try:
99 # This empty dict will contain the namespace for the exec'd file 99 # This empty dict will contain the namespace for the exec'd file
100 custom_namespace = {} 100 custom_namespace = {}
101 exec(open(filename, 'rb').read(), custom_namespace) 101 with open(filename, 'rb') as f:
102 exec(f.read(), custom_namespace)
102 # Retrieve the class `formattername` from that namespace 103 # Retrieve the class `formattername` from that namespace
103 if formattername not in custom_namespace: 104 if formattername not in custom_namespace:
104 raise ClassNotFound('no valid %s class found in %s' % 105 raise ClassNotFound('no valid %s class found in %s' %
105 (formattername, filename)) 106 (formattername, filename))
106 formatter_class = custom_namespace[formattername] 107 formatter_class = custom_namespace[formattername]
107 # And finally instantiate it with the options 108 # And finally instantiate it with the options
108 return formatter_class(**options) 109 return formatter_class(**options)
109 except IOError as err: 110 except IOError as err:
110 raise ClassNotFound('cannot read %s' % filename) 111 raise ClassNotFound('cannot read %s: %s' % (filename, err))
111 except ClassNotFound as err: 112 except ClassNotFound:
112 raise 113 raise
113 except Exception as err: 114 except Exception as err:
114 raise ClassNotFound('error when loading custom formatter: %s' % err) 115 raise ClassNotFound('error when loading custom formatter: %s' % err)
115 116
116 117
118 """Lookup and instantiate a formatter by filename pattern. 119 """Lookup and instantiate a formatter by filename pattern.
119 120
120 Raises ClassNotFound if not found. 121 Raises ClassNotFound if not found.
121 """ 122 """
122 fn = basename(fn) 123 fn = basename(fn)
123 for modname, name, _, filenames, _ in itervalues(FORMATTERS): 124 for modname, name, _, filenames, _ in FORMATTERS.values():
124 for filename in filenames: 125 for filename in filenames:
125 if _fn_matches(fn, filename): 126 if _fn_matches(fn, filename):
126 if name not in _formatter_cache: 127 if name not in _formatter_cache:
127 _load_formatters(modname) 128 _load_formatters(modname)
128 return _formatter_cache[name](**options) 129 return _formatter_cache[name](**options)

eric ide

mercurial