GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ishell.py
1 #! /usr/bin/env python
2 #
3 # Adapted from:
4 #
5 # Backend to the console plugin.
6 # @author: Eitan Isaacson
7 # @organization: IBM Corporation
8 # @copyright: Copyright (c) 2007 IBM Corporation
9 # @license: BSD
10 #
11 # All rights reserved. This program and the accompanying materials are made
12 # available under the terms of the BSD which accompanies this distribution, and
13 # is available at U{http://www.opensource.org/licenses/bsd-license.php}
14 #
15 
16 import os
17 import sys
18 import re
19 from StringIO import StringIO
20 try:
21  import IPython
22  from IPython import ipapi
23 except Exception,e:
24  raise "Error importing IPython (%s)" % str(e)
25 
26 
27 # ------------------------------------------------------------------ class Shell
28 class Shell:
29  """ """
30 
31  def __init__(self,argv=None,user_ns=None,user_global_ns=None,
32  cin=None, cout=None,cerr=None, input_func=None):
33  """ """
34  if input_func:
35  IPython.iplib.raw_input_original = input_func
36  if cin:
37  IPython.Shell.Term.cin = cin
38  if cout:
39  IPython.Shell.Term.cout = cout
40  if cerr:
41  IPython.Shell.Term.cerr = cerr
42  if argv is None:
43  argv=[]
44  IPython.iplib.raw_input = lambda x: None
45  self.term = IPython.genutils.IOTerm(cin=cin, cout=cout, cerr=cerr)
46  os.environ['TERM'] = 'dumb'
47  excepthook = sys.excepthook
48  self.IP = IPython.Shell.make_IPython(argv,
49  user_ns=user_ns,
50  user_global_ns=user_global_ns,
51  embedded=True,
52  shell_class=IPython.Shell.InteractiveShell)
53  self.IP.system = lambda cmd: self.shell(self.IP.var_expand(cmd),
54  header='IPython system call: ',
55  verbose=self.IP.rc.system_verbose)
56  # Get a hold of the public IPython API object and use it
57  self.ip = ipapi.get()
58  self.ip.magic('colors LightBG')
59  sys.excepthook = excepthook
60  self.iter_more = 0
61  self.complete_sep = re.compile('[\s\{\}\[\]\(\)]')
62 
63 
64  def namespace(self):
65  return self.IP.user_ns
66 
67  def eval(self, console):
68  console.write ('\n')
69  orig_stdout = sys.stdout
70  sys.stdout = IPython.Shell.Term.cout
71  try:
72  line = self.IP.raw_input(None, self.iter_more)
73  if self.IP.autoindent:
74  self.IP.readline_startup_hook(None)
75  except KeyboardInterrupt:
76  self.IP.write('\nKeyboardInterrupt\n')
77  self.IP.resetbuffer()
78  self.IP.outputcache.prompt_count -= 1
79  if self.IP.autoindent:
80  self.IP.indent_current_nsp = 0
81  self.iter_more = 0
82  except:
83  self.IP.showtraceback()
84  else:
85  self.iter_more = self.IP.push(line)
86  if (self.IP.SyntaxTB.last_syntax_error and self.IP.rc.autoedit_syntax):
87  self.IP.edit_syntax_error()
88  if self.iter_more:
89  self.prompt = str(self.IP.outputcache.prompt2).strip()
90  if self.IP.autoindent:
91  self.IP.readline_startup_hook(self.IP.pre_readline)
92  else:
93  self.prompt = str(self.IP.outputcache.prompt1).strip()
94  sys.stdout = orig_stdout
95 
96  # System output (if any)
97  while True:
98  try:
99  buf = os.read(console.piperead, 256)
100  except:
101  break
102  else:
103  console.write (buf)
104  if len(buf) < 256: break
105 
106  # Command output
107  rv = console.cout.getvalue()
108  if rv:
109  rv = rv.strip('\n')
110  console.write (rv)
111  if rv:
112  console.write ('\n')
113  console.cout.truncate(0)
114  console.prompt()
115 
116  def complete(self, line):
117  split_line = self.complete_sep.split(line)
118  possibilities = self.IP.complete(split_line[-1])
119  if possibilities:
120  common_prefix = os.path.commonprefix (possibilities)
121  completed = line[:-len(split_line[-1])]+common_prefix
122  else:
123  completed = line
124  return completed, possibilities
125 
126  def shell(self, cmd,verbose=0,debug=0,header=''):
127  stat = 0
128  if verbose or debug: print header+cmd
129  if not debug:
130  input, output = os.popen4(cmd)
131  print output.read()
132  output.close()
133  input.close()
134