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