from_pygments.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """
  2. Adaptor for building prompt_toolkit styles, starting from a Pygments style.
  3. Usage::
  4. from pygments.styles.tango import TangoStyle
  5. style = style_from_pygments(pygments_style_cls=TangoStyle)
  6. """
  7. from __future__ import unicode_literals
  8. from .base import Style
  9. from .from_dict import style_from_dict
  10. __all__ = (
  11. 'PygmentsStyle',
  12. 'style_from_pygments',
  13. )
  14. # Following imports are only needed when a ``PygmentsStyle`` class is used.
  15. try:
  16. from pygments.style import Style as pygments_Style
  17. from pygments.styles.default import DefaultStyle as pygments_DefaultStyle
  18. except ImportError:
  19. pygments_Style = None
  20. pygments_DefaultStyle = None
  21. def style_from_pygments(style_cls=pygments_DefaultStyle,
  22. style_dict=None,
  23. include_defaults=True):
  24. """
  25. Shortcut to create a :class:`.Style` instance from a Pygments style class
  26. and a style dictionary.
  27. Example::
  28. from prompt_toolkit.styles.from_pygments import style_from_pygments
  29. from pygments.styles import get_style_by_name
  30. style = style_from_pygments(get_style_by_name('monokai'))
  31. :param style_cls: Pygments style class to start from.
  32. :param style_dict: Dictionary for this style. `{Token: style}`.
  33. :param include_defaults: (`bool`) Include prompt_toolkit extensions.
  34. """
  35. assert style_dict is None or isinstance(style_dict, dict)
  36. assert style_cls is None or issubclass(style_cls, pygments_Style)
  37. styles_dict = {}
  38. if style_cls is not None:
  39. styles_dict.update(style_cls.styles)
  40. if style_dict is not None:
  41. styles_dict.update(style_dict)
  42. return style_from_dict(styles_dict, include_defaults=include_defaults)
  43. class PygmentsStyle(Style):
  44. " Deprecated. "
  45. def __new__(cls, pygments_style_cls):
  46. assert issubclass(pygments_style_cls, pygments_Style)
  47. return style_from_dict(pygments_style_cls.styles)
  48. def invalidation_hash(self):
  49. pass
  50. @classmethod
  51. def from_defaults(cls, style_dict=None,
  52. pygments_style_cls=pygments_DefaultStyle,
  53. include_extensions=True):
  54. " Deprecated. "
  55. return style_from_pygments(
  56. style_cls=pygments_style_cls,
  57. style_dict=style_dict,
  58. include_defaults=include_extensions)