codekingpro/portable-devtools
114k
1"""2 pygments.styles3 ~~~~~~~~~~~~~~~4 5 Contains built-in styles.6 7 :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.8 :license: BSD, see LICENSE for details.9"""10 11from pygments.plugin import find_plugin_styles12from pygments.util import ClassNotFound13from pygments.styles._mapping import STYLES14 15#: A dictionary of built-in styles, mapping style names to16#: ``'submodule::classname'`` strings.17#: This list is deprecated. Use `pygments.styles.STYLES` instead18STYLE_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 efficient21_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 styles27 are listed in :data:`pygments.styles.STYLE_MAP`.28 29 Will raise :exc:`pygments.util.ClassNotFound` if no style of that name is30 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 style39 # perhaps it got dropped into our styles package40 builtin = ""41 mod = 'pygments.styles.' + name42 cls = name.title() + "Style"43 44 try:45 mod = __import__(mod, None, None, [cls])46 except ImportError:47 raise ClassNotFound(f"Could not find style module {mod!r}" +48 (builtin and ", though it should be builtin")49 + ".")50 try:51 return getattr(mod, cls)52 except AttributeError:53 raise ClassNotFound(f"Could not find style class {cls!r} in style module.")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 name62 