Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/plugin.py: 28%
32 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-20 06:09 +0000
1"""
2 pygments.plugin
3 ~~~~~~~~~~~~~~~
5 Pygments plugin interface. By default, this tries to use
6 ``importlib.metadata``, which is in the Python standard
7 library since Python 3.8, or its ``importlib_metadata``
8 backport for earlier versions of Python. It falls back on
9 ``pkg_resources`` if not found. Finally, if ``pkg_resources``
10 is not found either, no plugins are loaded at all.
12 lexer plugins::
14 [pygments.lexers]
15 yourlexer = yourmodule:YourLexer
17 formatter plugins::
19 [pygments.formatters]
20 yourformatter = yourformatter:YourFormatter
21 /.ext = yourformatter:YourFormatter
23 As you can see, you can define extensions for the formatter
24 with a leading slash.
26 syntax plugins::
28 [pygments.styles]
29 yourstyle = yourstyle:YourStyle
31 filter plugin::
33 [pygments.filter]
34 yourfilter = yourfilter:YourFilter
37 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
38 :license: BSD, see LICENSE for details.
39"""
41LEXER_ENTRY_POINT = 'pygments.lexers'
42FORMATTER_ENTRY_POINT = 'pygments.formatters'
43STYLE_ENTRY_POINT = 'pygments.styles'
44FILTER_ENTRY_POINT = 'pygments.filters'
47def iter_entry_points(group_name):
48 try:
49 from importlib.metadata import entry_points
50 except ImportError:
51 try:
52 from importlib_metadata import entry_points
53 except ImportError:
54 try:
55 from pkg_resources import iter_entry_points
56 except (ImportError, OSError):
57 return []
58 else:
59 return iter_entry_points(group_name)
60 groups = entry_points()
61 if hasattr(groups, 'select'):
62 # New interface in Python 3.10 and newer versions of the
63 # importlib_metadata backport.
64 return groups.select(group=group_name)
65 else:
66 # Older interface, deprecated in Python 3.10 and recent
67 # importlib_metadata, but we need it in Python 3.8 and 3.9.
68 return groups.get(group_name, [])
71def find_plugin_lexers():
72 for entrypoint in iter_entry_points(LEXER_ENTRY_POINT):
73 yield entrypoint.load()
76def find_plugin_formatters():
77 for entrypoint in iter_entry_points(FORMATTER_ENTRY_POINT):
78 yield entrypoint.name, entrypoint.load()
81def find_plugin_styles():
82 for entrypoint in iter_entry_points(STYLE_ENTRY_POINT):
83 yield entrypoint.name, entrypoint.load()
86def find_plugin_filters():
87 for entrypoint in iter_entry_points(FILTER_ENTRY_POINT):
88 yield entrypoint.name, entrypoint.load()