GnuCash  5.6-150-g038405b370+
ishell.py
1 #! /usr/bin/env python3
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{https://www.opensource.org/licenses/bsd-license.php}
14 #
15 
16 import os
17 import sys
18 import re
19 from io import StringIO
20 try:
21  import IPython
22 except Exception as e:
23  raise Exception("Error importing IPython (%s)" % str(e))
24 
25 
26 # ------------------------------------------------------------------ class Shell
27 class Shell:
28  """ """
29 
30  def __init__(self,argv=None,user_ns=None,user_global_ns=None,
31  cin=None, cout=None,cerr=None, input_func=None):
32  """ """
33  if input_func:
34  IPython.iplib.raw_input_original = input_func
35  if cin:
36  IPython.Shell.Term.cin = cin
37  if cout:
38  IPython.Shell.Term.cout = cout
39  if cerr:
40  IPython.Shell.Term.cerr = cerr
41  if argv is None:
42  argv=[]
43  IPython.iplib.raw_input = lambda x: None
44  self.term = IPython.genutils.IOTerm(cin=cin, cout=cout, cerr=cerr)
45  os.environ['TERM'] = 'dumb'
46  excepthook = sys.excepthook
47  self.IP = IPython.Shell.make_IPython(argv,
48  user_ns=user_ns,
49  user_global_ns=user_global_ns,
50  embedded=True,
51  shell_class=IPython.Shell.InteractiveShell)
52  self.IP.system = lambda cmd: self.shell(self.IP.var_expand(cmd),
53  header='IPython system call: ',
54  verbose=self.IP.rc.system_verbose)
55  # Get a hold of the public IPython API object and use it
56  self.ip = IPython.core.getipython.get_ipython()
57  self.ip.magic('colors LightBG')
58  sys.excepthook = excepthook
59  self.iter_more = 0
60  self.complete_sep = re.compile(r'[\s\{\}\[\]\(\)]')
61 
62 
63  def namespace(self):
64  return self.IP.user_ns
65 
66  def eval(self, console):
67  console.write ('\n')
68  orig_stdout = sys.stdout
69  sys.stdout = IPython.Shell.Term.cout
70  try:
71  line = self.IP.raw_input(None, self.iter_more)
72  if self.IP.autoindent:
73  self.IP.readline_startup_hook(None)
74  except KeyboardInterrupt:
75  self.IP.write('\nKeyboardInterrupt\n')
76  self.IP.resetbuffer()
77  self.IP.outputcache.prompt_count -= 1
78  if self.IP.autoindent:
79  self.IP.indent_current_nsp = 0
80  self.iter_more = 0
81  except:
82  self.IP.showtraceback()
83  else:
84  self.iter_more = self.IP.push(line)
85  if (self.IP.SyntaxTB.last_syntax_error and self.IP.rc.autoedit_syntax):
86  self.IP.edit_syntax_error()
87  if self.iter_more:
88  self.prompt = str(self.IP.outputcache.prompt2).strip()
89  if self.IP.autoindent:
90  self.IP.readline_startup_hook(self.IP.pre_readline)
91  else:
92  self.prompt = str(self.IP.outputcache.prompt1).strip()
93  sys.stdout = orig_stdout
94 
95  # System output (if any)
96  while True:
97  try:
98  buf = os.read(console.piperead, 256)
99  except:
100  break
101  else:
102  console.write (buf)
103  if len(buf) < 256: break
104 
105  # Command output
106  rv = console.cout.getvalue()
107  if rv:
108  rv = rv.strip('\n')
109  console.write (rv)
110  if rv:
111  console.write ('\n')
112  console.cout.truncate(0)
113  console.prompt()
114 
115  def complete(self, line):
116  split_line = self.complete_sep.split(line)
117  possibilities = self.IP.complete(split_line[-1])
118  if possibilities:
119  common_prefix = os.path.commonprefix (possibilities)
120  completed = line[:-len(split_line[-1])]+common_prefix
121  else:
122  completed = line
123  return completed, possibilities
124 
125  def shell(self, cmd,verbose=0,debug=0,header=''):
126  stat = 0
127  if verbose or debug: print(header+cmd)
128  if not debug:
129  input, output = os.popen4(cmd)
130  print(output.read())
131  output.close()
132  input.close()
133 
def shell(self, cmd, verbose=0, debug=0, header='')
Definition: ishell.py:125
def __init__(self, argv=None, user_ns=None, user_global_ns=None, cin=None, cout=None, cerr=None, input_func=None)
Definition: ishell.py:31