[PATCH 5 of 7] [debug] syntax highlight SQL and RQL debug output

Laurent Peuch cortex at worlddomination.be
Sun Sep 1 04:07:01 CEST 2019


# HG changeset patch
# User Laurent Peuch <cortex at worlddomination.be>
# Date 1566346212 -7200
#      Wed Aug 21 02:10:12 2019 +0200
# Node ID 17465c0ba7998d52ab5b2b2155b411c24da039e9
# Parent  b581c9996564877909b47221ce9d8d6f4f35638c
[debug] syntax highlight SQL and RQL debug output

This prevent DBG_RQL and DBG_SQL output to looks like a gigantic blob of white
text.

Do this only if we detect that we are in a tty and that pygments is installed.

Closes #17222885

diff --git a/cubicweb/misc/source_highlight.py b/cubicweb/misc/source_highlight.py
new file mode 100644
--- /dev/null
+++ b/cubicweb/misc/source_highlight.py
@@ -0,0 +1,21 @@
+"""This module provide syntaxe highlight functions"""
+
+from logilab.common.logging_ext import _colorable_terminal
+
+try:
+    from pygments import highlight as pygments_highlight
+    from pygments.lexers import get_lexer_by_name
+    from pygments.formatters.terminal import TerminalFormatter
+    has_pygments = True
+except ImportError:
+    has_pygments = False
+
+
+def highlight(code, language):
+    if not has_pygments:
+        return code
+
+    if not _colorable_terminal():
+        return code
+
+    return pygments_highlight(code, get_lexer_by_name(language), TerminalFormatter())
diff --git a/cubicweb/server/querier.py b/cubicweb/server/querier.py
--- a/cubicweb/server/querier.py
+++ b/cubicweb/server/querier.py
@@ -31,6 +31,7 @@ from cubicweb import Binary, server
 from cubicweb.rset import ResultSet
 
 from cubicweb.utils import QueryCache, RepeatList
+from cubicweb.misc.source_highlight import highlight
 from cubicweb.server.rqlannotation import SQLGenAnnotator, set_qdata
 from cubicweb.server.ssplanner import READ_ONLY_RTYPES, add_types_restriction
 from cubicweb.server.edition import EditedEntity
@@ -519,7 +520,7 @@ class QuerierHelper(object):
         if server.DEBUG & (server.DBG_RQL | server.DBG_SQL):
             if server.DEBUG & (server.DBG_MORE | server.DBG_SQL):
                 print('*'*80)
-            print('querier input', repr(rql), repr(args))
+            print(f"querier input '{highlight(repr(rql)[1:-1], 'RQL')} '{repr(args)}'")
         try:
             rqlst, cachekey = self.rql_cache.get(cnx, rql, args)
         except UnknownEid:
diff --git a/cubicweb/server/sources/__init__.py b/cubicweb/server/sources/__init__.py
--- a/cubicweb/server/sources/__init__.py
+++ b/cubicweb/server/sources/__init__.py
@@ -27,12 +27,13 @@ from yams.schema import role_name
 
 from cubicweb import ValidationError, set_log_methods, server, _
 from cubicweb.server import SOURCE_TYPES
+from cubicweb.misc.source_highlight import highlight
 
 
 def dbg_st_search(uri, union, args, cachekey=None, prefix='rql for'):
     if server.DEBUG & server.DBG_RQL:
         global t
-        print('  %s %s source: %s' % (prefix, uri, repr(union.as_string())))
+        print(f"  {prefix} {uri} source: '{highlight(repr(union.as_string())[1:-1], 'RQL')}'")
         t = time()
         if server.DEBUG & server.DBG_MORE:
             print('    args', repr(args))
diff --git a/cubicweb/server/sources/native.py b/cubicweb/server/sources/native.py
--- a/cubicweb/server/sources/native.py
+++ b/cubicweb/server/sources/native.py
@@ -50,6 +50,7 @@ from cubicweb.server.hook import Cleanup
 from cubicweb.server.edition import EditedEntity
 from cubicweb.server.sources import AbstractSource, dbg_st_search, dbg_results
 from cubicweb.server.sources.rql2sql import SQLGenerator
+from cubicweb.misc.source_highlight import highlight
 from cubicweb.statsd_logger import statsd_timeit
 
 
@@ -67,12 +68,12 @@ class LogCursor(object):
         it's a function just so that it shows up in profiling
         """
         if server.DEBUG & server.DBG_SQL:
-            print('exec', query, args)
+            print('exec', highlight(query, "SQL"), args)
         try:
             self.cu.execute(str(query), args)
         except Exception as ex:
             print("sql: %r\n args: %s\ndbms message: %r" % (
-                query, args, ex.args[0]))
+                highlight(query, "SQL"), args, ex.args[0]))
             raise
 
     def fetchall(self):
@@ -673,7 +674,7 @@ class NativeSQLSource(SQLAdapterMixIn, A
         """
         cursor = cnx.cnxset.cu
         if server.DEBUG & server.DBG_SQL:
-            print('exec', query, args, cnx.cnxset.cnx)
+            print('exec', highlight(query, "SQL"), args, cnx.cnxset.cnx)
         try:
             # str(query) to avoid error if it's a unicode string
             cursor.execute(str(query), args)
@@ -736,7 +737,7 @@ class NativeSQLSource(SQLAdapterMixIn, A
         it's a function just so that it shows up in profiling
         """
         if server.DEBUG & server.DBG_SQL:
-            print('execmany', query, 'with', len(args), 'arguments', cnx.cnxset.cnx)
+            print('execmany', highlight(query, "SQL"), 'with', len(args), 'arguments', cnx.cnxset.cnx)
         cursor = cnx.cnxset.cu
         try:
             # str(query) to avoid error if it's a unicode string



More information about the cubicweb-devel mailing list