[debug] syntax highlight SQL and RQL debug output
authorLaurent Peuch <cortex@worlddomination.be>
Wed, 21 Aug 2019 02:10:12 +0200
changeset 12739 c6f8ca03718f
parent 12738 a54037a68b14
child 12740 6e98699d3a9a
[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
cubicweb/misc/source_highlight.py
cubicweb/server/querier.py
cubicweb/server/sources/__init__.py
cubicweb/server/sources/native.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cubicweb/misc/source_highlight.py	Wed Aug 21 02:10:12 2019 +0200
@@ -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())
--- a/cubicweb/server/querier.py	Tue Aug 20 22:45:46 2019 +0200
+++ b/cubicweb/server/querier.py	Wed Aug 21 02:10:12 2019 +0200
@@ -31,6 +31,7 @@
 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 @@
         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("querier input", highlight(repr(rql)[1:-1], 'RQL'), repr(args))
         try:
             rqlst, cachekey = self.rql_cache.get(cnx, rql, args)
         except UnknownEid:
--- a/cubicweb/server/sources/__init__.py	Tue Aug 20 22:45:46 2019 +0200
+++ b/cubicweb/server/sources/__init__.py	Wed Aug 21 02:10:12 2019 +0200
@@ -27,12 +27,13 @@
 
 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(" ", prefix, uri, "source:", highlight(repr(union.as_string())[1:-1], 'RQL'))
         t = time()
         if server.DEBUG & server.DBG_MORE:
             print('    args', repr(args))
--- a/cubicweb/server/sources/native.py	Tue Aug 20 22:45:46 2019 +0200
+++ b/cubicweb/server/sources/native.py	Wed Aug 21 02:10:12 2019 +0200
@@ -50,6 +50,7 @@
 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 @@
         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):
@@ -685,7 +686,7 @@
         """
         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)
@@ -748,7 +749,8 @@
         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