Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/styles/__init__.py: 25%

28 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 06:09 +0000

1""" 

2 pygments.styles 

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

4 

5 Contains built-in styles. 

6 

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

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11from pygments.plugin import find_plugin_styles 

12from pygments.util import ClassNotFound 

13from pygments.styles._mapping import STYLES 

14 

15#: A dictionary of built-in styles, mapping style names to 

16#: ``'submodule::classname'`` strings. 

17#: This list is deprecated. Use `pygments.styles.STYLES` instead 

18STYLE_MAP = {v[1]: v[0].split('.')[-1] + '::' + k for k, v in STYLES.items()} 

19 

20#: Internal reverse mapping to make `get_style_by_name` more efficient 

21_STYLE_NAME_TO_MODULE_MAP = {v[1]: (v[0], k) for k, v in STYLES.items()} 

22 

23 

24def get_style_by_name(name): 

25 """ 

26 Return a style class by its short name. The names of the builtin styles 

27 are listed in :data:`pygments.styles.STYLE_MAP`. 

28 

29 Will raise :exc:`pygments.util.ClassNotFound` if no style of that name is 

30 found. 

31 """ 

32 if name in _STYLE_NAME_TO_MODULE_MAP: 

33 mod, cls = _STYLE_NAME_TO_MODULE_MAP[name] 

34 builtin = "yes" 

35 else: 

36 for found_name, style in find_plugin_styles(): 

37 if name == found_name: 

38 return style 

39 # perhaps it got dropped into our styles package 

40 builtin = "" 

41 mod = 'pygments.styles.' + name 

42 cls = name.title() + "Style" 

43 

44 try: 

45 mod = __import__(mod, None, None, [cls]) 

46 except ImportError: 

47 raise ClassNotFound("Could not find style module %r" % mod + 

48 (builtin and ", though it should be builtin") 

49 + ".") 

50 try: 

51 return getattr(mod, cls) 

52 except AttributeError: 

53 raise ClassNotFound("Could not find style class %r in style module." % cls) 

54 

55 

56def get_all_styles(): 

57 """Return a generator for all styles by name, both builtin and plugin.""" 

58 for v in STYLES.values(): 

59 yield v[1] 

60 for name, _ in find_plugin_styles(): 

61 yield name