ROOT  6.06/09
Reference Guide
cpptransformer.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import sys
3 import re
4 from IPython.core.inputtransformer import InputTransformer
5 from IPython import get_ipython
6 from IPython.core import display
7 import utils
8 
9 
10 _cppDcl=re.compile("\s*\.cpp\s+-d")
11 _cppAclic=re.compile("\s*\.cpp\s+-a")
12 _bash=re.compile("\s*\.bash\d*")
13 class CppTransformer(InputTransformer):
14 
15  def __init__(self):
16  self.cell = ""
17  self.mustSwitchToPython = False
18  self.runAsDecl = False
19  self.runAsAclic = False
20  self.runAsBash = False
21 
22  def push(self, line):
23  '''
24  >>> import ROOT
25  >>> t = CppTransformer()
26  >>> t.push("int i=3;")
27  >>> t.reset()
28  >>> ROOT.i
29  3
30  >>> t.push('.cpp -a')
31  >>> t.push('int q(int i){return i+i;};')
32  >>> t.reset()
33  >>> ROOT.q(2)
34  4
35  >>> t.push(' .cpp -a\t\t ')
36  >>> t.push('int qq(int i){return i+i;};')
37  >>> t.reset()
38  >>> ROOT.qq(2)
39  4
40  >>> t.push('.cpp -d')
41  >>> t.push('int f(int i){return i+i;}')
42  >>> t.reset()
43  >>> ROOT.f(3)
44  6
45  >>> t.push('.cpp -d')
46  >>> t.push('int ff(int i){return i+i;}')
47  >>> t.reset()
48  >>> ROOT.ff(3)
49  6
50  >>> t.push('.bash echo Hello ')
51  >>> t.reset()
52  Hello
53  >>> t.push(' \t .bash \t echo Hello ')
54  >>> t.reset()
55  Hello
56  >>> t.push('.bash')
57  >>> t.push('echo Hello')
58  >>> t.reset()
59  Hello
60  '''
61  # FIXME: must be in a single line
62  fcnName="toPython()"
63  if line == "%s;"%fcnName or line == fcnName:
64  self.mustSwitchToPython = True
65  elif _cppDcl.match(line) and self.cell == "":
66  self.runAsDecl = True
67  elif _cppAclic.match(line) and self.cell == "":
68  self.runAsAclic = True
69  elif _bash.match(line) and self.cell == "":
70  self.cell += line.replace(".bash","")+"\n"
71  self.runAsBash = True
72  else:
73  line+="\n"
74  self.cell += line
75  return None
76 
77  def reset(self):
78  out = None
79  if self.cell != "":
80  if self.runAsDecl:
81  utils.declareCppCode(self.cell)
82  self.runAsDecl = False
83  elif self.runAsAclic:
84  utils.invokeAclic(self.cell)
85  self.runAsAclic = False
86  elif self.runAsBash:
87  cellNoEndNewLine = self.cell[:-1]
88  out = utils._checkOutput(cellNoEndNewLine,"Error running shell command")
89  if out: sys.stdout.write(out)
90  self.runAsBash = False
91  else:
92  utils.processCppCode(self.cell)
93  self.cell = ""
94  if self.mustSwitchToPython:
95  ip = get_ipython()
97  self.mustSwitchToPython = False
98  cppcompleter.unload_ipython_extension(ip)
99  # Change highlight mode
100  display.display_javascript(utils.jsDefaultHighlight.format(mimeType = utils.ipyMIME), raw=True)
101  print("Notebook is in Python mode")
102 
103 _transformer = CppTransformer()
104 
106  ipython.input_splitter.logical_line_transforms.remove(_transformer)
107  ipython.input_transformer_manager.logical_line_transforms.remove(_transformer)
108 
110  ipython.input_splitter.logical_line_transforms.insert(0,_transformer)
111  ipython.input_transformer_manager.logical_line_transforms.insert(0,_transformer)