From b0eea7bf0bc4e746c6054803616807e0fea2e32f Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Wed, 20 Jan 2016 20:10:02 +0100 Subject: [PATCH] Use prompt_toolkit.token everywhere. (Make pygments more optional.) --- .../contrib/regular_languages/lexer.py | 2 +- prompt_toolkit/layout/toolbars.py | 5 +--- prompt_toolkit/shortcuts.py | 8 +++++-- prompt_toolkit/styles.py | 23 +++++++++++++------ prompt_toolkit/token.py | 7 +++++- tools/debug_vt100_input.py | 4 ++-- 6 files changed, 32 insertions(+), 17 deletions(-) diff --git a/prompt_toolkit/contrib/regular_languages/lexer.py b/prompt_toolkit/contrib/regular_languages/lexer.py index 5901eb90f..3412aa9a8 100644 --- a/prompt_toolkit/contrib/regular_languages/lexer.py +++ b/prompt_toolkit/contrib/regular_languages/lexer.py @@ -3,8 +3,8 @@ the input using a regular grammar with token annotations. """ from __future__ import unicode_literals -from pygments.token import Token from prompt_toolkit.layout.lexers import Lexer +from prompt_toolkit.token import Token from .compiler import _CompiledGrammar diff --git a/prompt_toolkit/layout/toolbars.py b/prompt_toolkit/layout/toolbars.py index 56c174f12..4c27c52d5 100644 --- a/prompt_toolkit/layout/toolbars.py +++ b/prompt_toolkit/layout/toolbars.py @@ -1,12 +1,10 @@ from __future__ import unicode_literals -from pygments.lexers import BashLexer - from ..enums import IncrementalSearchDirection from .processors import BeforeInput -from .lexers import PygmentsLexer, SimpleLexer +from .lexers import SimpleLexer from .dimension import LayoutDimension from .controls import BufferControl, TokenListControl, UIControl from .containers import Window, ConditionalContainer @@ -38,7 +36,6 @@ def __init__(self, get_tokens, filter=Always(), **kw): class SystemToolbarControl(BufferControl): def __init__(self): super(SystemToolbarControl, self).__init__( - lexer=PygmentsLexer(BashLexer), buffer_name=SYSTEM_BUFFER, input_processors=[BeforeInput.static('Shell command: ', Token.Toolbar.System)],) diff --git a/prompt_toolkit/shortcuts.py b/prompt_toolkit/shortcuts.py index 4135b5343..93a150eaf 100644 --- a/prompt_toolkit/shortcuts.py +++ b/prompt_toolkit/shortcuts.py @@ -49,10 +49,14 @@ from six import text_type, exec_, PY2 import os -import pygments.lexer import sys import textwrap +try: + from pygments.lexer import Lexer as pygments_Lexer +except ImportError: + pygments_Lexer = None + if is_windows(): from .terminal.win32_output import Win32Output from .terminal.conemu_output import ConEmuOutput @@ -209,7 +213,7 @@ def create_prompt_layout(message='', lexer=None, is_password=False, # class is given, turn it into a PygmentsLexer. (Important for # backwards-compatibility.) try: - if issubclass(lexer, pygments.lexer.Lexer): + if pygments_Lexer and issubclass(lexer, pygments.lexer.Lexer): lexer = PygmentsLexer(lexer) except TypeError: # Happens when lexer is `None` or an instance of something else. pass diff --git a/prompt_toolkit/styles.py b/prompt_toolkit/styles.py index bca4292a9..0564509b4 100644 --- a/prompt_toolkit/styles.py +++ b/prompt_toolkit/styles.py @@ -1,14 +1,23 @@ """ Styling for prompt_toolkit applications. + +Pygments needs to be installed for usage of ``PygmentsStyle``. """ from __future__ import unicode_literals from abc import ABCMeta, abstractmethod from collections import namedtuple -from pygments.token import Token from six import with_metaclass -import pygments.style -import pygments.styles.default +from .token import Token + +# Following imports are only needed when a ``PygmentsStyle`` class is used. +try: + from pygments.style import Style as pygments_Style + from pygments.styles.default import DefaultStyle as pygments_DefaultStyle +except ImportError: + pygments_Style = None + pygments_DefaultStyle = None + __all__ = ( 'Style', @@ -97,7 +106,7 @@ class PygmentsStyle(Style): :param pygments_style_cls: Pygments ``Style`` class. """ def __init__(self, pygments_style_cls): - assert issubclass(pygments_style_cls, pygments.style.Style) + assert issubclass(pygments_style_cls, pygments_Style) self.pygments_style_cls = pygments_style_cls self._token_to_attrs_dict = None @@ -120,7 +129,7 @@ def invalidation_hash(self): @classmethod def from_defaults(cls, style_dict=None, - pygments_style_cls=pygments.styles.default.DefaultStyle, + pygments_style_cls=pygments_DefaultStyle, include_extensions=True): """ Shortcut to create a :class:`.PygmentsStyle` instance from a Pygments @@ -131,9 +140,9 @@ def from_defaults(cls, style_dict=None, :param include_extensions: (`bool`) Include prompt_toolkit extensions. """ assert style_dict is None or isinstance(style_dict, dict) - assert pygments_style_cls is None or issubclass(pygments_style_cls, pygments.style.Style) + assert pygments_style_cls is None or issubclass(pygments_style_cls, pygments_Style) - class _CustomStyle(pygments.styles.default.DefaultStyle): + class _CustomStyle(pygments_DefaultStyle): background_color = None styles = {} diff --git a/prompt_toolkit/token.py b/prompt_toolkit/token.py index 83e12a701..aa3c4bced 100644 --- a/prompt_toolkit/token.py +++ b/prompt_toolkit/token.py @@ -30,4 +30,9 @@ def __repr__(self): return 'Token' + (self and '.' or '') + '.'.join(self) -Token = _TokenType() +# Prefer the Token class from Pygments. If Pygments is not installed, use our +# minimalistic Token class. +try: + from pygments.token import Token +except ImportError: + Token = _TokenType() diff --git a/tools/debug_vt100_input.py b/tools/debug_vt100_input.py index 53faff482..6dd4c280b 100755 --- a/tools/debug_vt100_input.py +++ b/tools/debug_vt100_input.py @@ -3,11 +3,11 @@ Parse vt100 input and print keys. For testing terminal input. """ - from __future__ import unicode_literals +import sys + from prompt_toolkit.terminal.vt100_input import InputStream, raw_mode from prompt_toolkit.keys import Keys -import sys def callback(key_press):