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

1""" 

2 pygments.plugin 

3 ~~~~~~~~~~~~~~~ 

4 

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. 

11 

12 lexer plugins:: 

13 

14 [pygments.lexers] 

15 yourlexer = yourmodule:YourLexer 

16 

17 formatter plugins:: 

18 

19 [pygments.formatters] 

20 yourformatter = yourformatter:YourFormatter 

21 /.ext = yourformatter:YourFormatter 

22 

23 As you can see, you can define extensions for the formatter 

24 with a leading slash. 

25 

26 syntax plugins:: 

27 

28 [pygments.styles] 

29 yourstyle = yourstyle:YourStyle 

30 

31 filter plugin:: 

32 

33 [pygments.filter] 

34 yourfilter = yourfilter:YourFilter 

35 

36 

37 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS. 

38 :license: BSD, see LICENSE for details. 

39""" 

40 

41LEXER_ENTRY_POINT = 'pygments.lexers' 

42FORMATTER_ENTRY_POINT = 'pygments.formatters' 

43STYLE_ENTRY_POINT = 'pygments.styles' 

44FILTER_ENTRY_POINT = 'pygments.filters' 

45 

46 

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, []) 

69 

70 

71def find_plugin_lexers(): 

72 for entrypoint in iter_entry_points(LEXER_ENTRY_POINT): 

73 yield entrypoint.load() 

74 

75 

76def find_plugin_formatters(): 

77 for entrypoint in iter_entry_points(FORMATTER_ENTRY_POINT): 

78 yield entrypoint.name, entrypoint.load() 

79 

80 

81def find_plugin_styles(): 

82 for entrypoint in iter_entry_points(STYLE_ENTRY_POINT): 

83 yield entrypoint.name, entrypoint.load() 

84 

85 

86def find_plugin_filters(): 

87 for entrypoint in iter_entry_points(FILTER_ENTRY_POINT): 

88 yield entrypoint.name, entrypoint.load()