From e0d69db04c38c07848a8b461f035006a9e16eb21 Mon Sep 17 00:00:00 2001
From: Danilo Piparo <danilo.piparo@cern.ch>
Date: Wed, 26 Aug 2015 11:43:42 +0200
Subject: [PATCH] Detect drawn canvases in the list of canvases, move comment
 stripper

---
 .../pyroot/ROOTaaS/iPyROOT/cpptransformer.py  | 21 +--------
 bindings/pyroot/ROOTaaS/iPyROOT/utils.py      | 44 ++++++++++++++++++-
 2 files changed, 43 insertions(+), 22 deletions(-)

diff --git a/bindings/pyroot/ROOTaaS/iPyROOT/cpptransformer.py b/bindings/pyroot/ROOTaaS/iPyROOT/cpptransformer.py
index efcdeb02119..6c58a2573e0 100644
--- a/bindings/pyroot/ROOTaaS/iPyROOT/cpptransformer.py
+++ b/bindings/pyroot/ROOTaaS/iPyROOT/cpptransformer.py
@@ -1,5 +1,3 @@
-import re
-
 from IPython.core.inputtransformer import InputTransformer
 from IPython import get_ipython
 
@@ -9,22 +7,7 @@ import cppcompleter
 from IPython.core import display
 
 
-def commentRemover( text ):
-   def blotOutNonNewlines( strIn ) :  # Return a string containing only the newline chars contained in strIn
-      return "" + ("\n" * strIn.count('\n'))
-
-   def replacer( match ) :
-      s = match.group(0)
-      if s.startswith('/'):  # Matched string is //...EOL or /*...*/  ==> Blot out all non-newline chars
-         return blotOutNonNewlines(s)
-      else:                  # Matched string is '...' or "..."  ==> Keep unchanged
-         return s
-
-   pattern = re.compile(\
-        r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
-        re.DOTALL | re.MULTILINE)
 
-   return re.sub(pattern, replacer, text)
 
 class CppTransformer(InputTransformer):
 
@@ -52,9 +35,7 @@ class CppTransformer(InputTransformer):
                 utils.declareCppCode(self.cell)
                 self.mustDeclare = False
             else:
-                cell = self.cell
-                code = commentRemover(self.cell)
-                utils.processCppCode(code)
+                utils.processCppCode(self.cell)
             self.cell = ""
         if self.mustSwitchToPython:
             ip = get_ipython()
diff --git a/bindings/pyroot/ROOTaaS/iPyROOT/utils.py b/bindings/pyroot/ROOTaaS/iPyROOT/utils.py
index 029c32e2425..a6b6ec256c3 100644
--- a/bindings/pyroot/ROOTaaS/iPyROOT/utils.py
+++ b/bindings/pyroot/ROOTaaS/iPyROOT/utils.py
@@ -5,6 +5,7 @@ import time
 import tempfile
 import itertools
 import ctypes
+import re
 from contextlib import contextmanager
 from IPython import get_ipython
 from IPython.display import HTML
@@ -101,6 +102,23 @@ def _setIgnoreLevel(level):
     yield
     ROOT.gErrorIgnoreLevel = originalLevel
 
+def commentRemover( text ):
+   def blotOutNonNewlines( strIn ) :  # Return a string containing only the newline chars contained in strIn
+      return "" + ("\n" * strIn.count('\n'))
+
+   def replacer( match ) :
+      s = match.group(0)
+      if s.startswith('/'):  # Matched string is //...EOL or /*...*/  ==> Blot out all non-newline chars
+         return blotOutNonNewlines(s)
+      else:                  # Matched string is '...' or "..."  ==> Keep unchanged
+         return s
+
+   pattern = re.compile(\
+        r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
+        re.DOTALL | re.MULTILINE)
+
+   return re.sub(pattern, replacer, text)
+
 class StreamCapture(object):
     def __init__(self, stream, ip=get_ipython()):
         streamsFileNo={sys.stderr:2,sys.stdout:1}
@@ -271,10 +289,30 @@ class CanvasCapture(object):
         self.shell.events.unregister('pre_execute', self._pre_execute)
         self.shell.events.unregister('post_execute', self._post_execute)
 
+class CaptureDrawnCanvases(object):
+    '''
+    Capture the canvas which is drawn to display it.
+    '''
+    def __init__(self, ip=get_ipython()):
+        self.shell = ip
+
+    def _pre_execute(self):
+        pass
+
+    def _post_execute(self):
+        for can in ROOT.gROOT.GetListOfCanvases():
+            if can.IsDrawn():
+               can.Draw()
+               can.ResetDrawn()
+
+    def register(self):
+        self.shell.events.register('pre_execute', self._pre_execute)
+        self.shell.events.register('post_execute', self._post_execute)
+
 
 captures = [StreamCapture(sys.stderr),
-            StreamCapture(sys.stdout)]
-            #CanvasCapture()]
+            StreamCapture(sys.stdout),
+            CaptureDrawnCanvases()]
 
 def toCpp():
     '''
@@ -392,9 +430,11 @@ def setStyle():
 
 # Here functions are defined to process C++ code
 def processCppCodeImpl(cell):
+    cell = commentRemover(cell)
     ROOT.gInterpreter.ProcessLine(cell)
 
 def declareCppCodeImpl(cell):
+    cell = commentRemover(cell)
     ROOT.gInterpreter.Declare(cell)
 
 def processCppCode(cell):
-- 
GitLab