cubicweb/misc/source_highlight.py
author Philippe Pepiot <ph@itsalwaysdns.eu>
Tue, 31 Mar 2020 19:15:03 +0200
changeset 12957 0c973204033a
parent 12770 be0864a2eec8
permissions -rw-r--r--
[server] prevent returning closed cursor to the database pool In since c8c6ad8 init_repository use repo.internal_cnx() instead of repo.system_source.get_connection() so it use the pool and we should not close cursors from the pool before returning it back. Otherwise we may have "connection already closed" error. This bug only trigger when connection-pool-size = 1. Since we are moving to use a dynamic pooler we need to get this fixed. This does not occur with sqlite since the connection wrapper instantiate new cursor everytime, but this occur with other databases.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12739
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     1
"""This module provide syntaxe highlight functions"""
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     2
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     3
from logilab.common.logging_ext import _colorable_terminal
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     4
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     5
try:
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     6
    from pygments import highlight as pygments_highlight
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     7
    from pygments.lexers import get_lexer_by_name
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     8
    from pygments.formatters.terminal import TerminalFormatter
12757
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
     9
    from pygments.formatters.html import HtmlFormatter
12739
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    10
    has_pygments = True
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    11
except ImportError:
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    12
    has_pygments = False
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    13
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    14
12758
db95a417a5ec [debug/source_highlight] rename highlight to highlight_terminal
Laurent Peuch <cortex@worlddomination.be>
parents: 12757
diff changeset
    15
def highlight_terminal(code, language):
12739
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    16
    if not has_pygments:
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    17
        return code
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    18
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    19
    if not _colorable_terminal():
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    20
        return code
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    21
c6f8ca03718f [debug] syntax highlight SQL and RQL debug output
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    22
    return pygments_highlight(code, get_lexer_by_name(language), TerminalFormatter())
12757
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    23
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    24
12770
be0864a2eec8 [mod] allow to pass generic additional arguments to pygments HtmlFormatter
Laurent Peuch <cortex@worlddomination.be>
parents: 12769
diff changeset
    25
def highlight_html(code, language, linenos=False, linenostart=1, **kwargs):
12757
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    26
    if not has_pygments:
12769
a61e0fe17a69 [debug/fix] ensure that not syntax highlighted code is a string
Laurent Peuch <cortex@worlddomination.be>
parents: 12758
diff changeset
    27
        return str(code)
12757
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    28
12770
be0864a2eec8 [mod] allow to pass generic additional arguments to pygments HtmlFormatter
Laurent Peuch <cortex@worlddomination.be>
parents: 12769
diff changeset
    29
    return pygments_highlight(str(code),
be0864a2eec8 [mod] allow to pass generic additional arguments to pygments HtmlFormatter
Laurent Peuch <cortex@worlddomination.be>
parents: 12769
diff changeset
    30
                              get_lexer_by_name(language),
be0864a2eec8 [mod] allow to pass generic additional arguments to pygments HtmlFormatter
Laurent Peuch <cortex@worlddomination.be>
parents: 12769
diff changeset
    31
                              HtmlFormatter(wrapcode=True, linenos=linenos, linenostart=linenostart, **kwargs))
12757
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    32
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    33
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    34
def generate_css():
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    35
    if has_pygments:
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    36
        return HtmlFormatter().get_style_defs()
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    37
    else:
e55f6f6a8d28 [debug/source_highlight] add highlight_html and generate_css for debugtool panels
Laurent Peuch <cortex@worlddomination.be>
parents: 12739
diff changeset
    38
        return ""