GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions | Data Fields
pycons.shell.Shell Class Reference

Public Member Functions

 __init__ (self, ns_globals={}, ns_locals={})
 
 namespace (self)
 
 is_balanced (self, line)
 
 complete (self, line)
 
 eval (self, console)
 
 execute (self, console)
 

Data Fields

 completer
 
 command
 
 globals
 
 locals
 
 complete_sep
 
 prompt
 

Detailed Description

 

Definition at line 40 of file shell.py.

Constructor & Destructor Documentation

◆ __init__()

pycons.shell.Shell.__init__ (   self,
  ns_globals = {},
  ns_locals = {} 
)
 

Definition at line 43 of file shell.py.

43 def __init__(self, ns_globals={}, ns_locals={}):
44 """ """
45
46 self.completer = rlcompleter.Completer (ns_globals)
47 self.command = ''
48 self.globals = ns_globals
49 self.locals = ns_locals
50 self.complete_sep = re.compile(r'[\s\{\}\[\]\‍(\‍)]')
51 self.prompt = sys.ps1
52
53

Member Function Documentation

◆ complete()

pycons.shell.Shell.complete (   self,
  line 
)

Definition at line 96 of file shell.py.

96 def complete(self, line):
97 split_line = self.complete_sep.split(line)
98 possibilities = []
99 i = 0
100 c = self.completer.complete (split_line[-1], i)
101 while c:
102 possibilities.append(c)
103 i = i+1
104 c = self.completer.complete (split_line[-1], i)
105 if possibilities:
106 common_prefix = os.path.commonprefix (possibilities)
107 completed = line[:-len(split_line[-1])]+common_prefix
108 else:
109 completed = line
110 return completed, possibilities
111
112

◆ eval()

pycons.shell.Shell.eval (   self,
  console 
)

Definition at line 113 of file shell.py.

113 def eval (self, console):
114 line = console.last_line()
115 console.write ('\n')
116 if line == '':
117 self.execute (console)
118 self.command = ''
119 self.prompt = sys.ps1
120 console.prompt('prompt')
121 return
122 self.command = self.command + line + '\n'
123 if not self.is_balanced (self.command):
124 self.prompt = sys.ps2
125 console.prompt('prompt')
126 return
127 line = line.rstrip()
128 if len(line) > 0:
129 if line[-1] == ':' or line[-1] == '\\' or line[0] in ' \11':
130 self.prompt = sys.ps2
131 console.prompt('prompt')
132 return
133 self.execute (console)
134 self.command = ''
135 self.prompt = sys.ps1
136 console.prompt('prompt')
137
138

◆ execute()

pycons.shell.Shell.execute (   self,
  console 
)

Definition at line 139 of file shell.py.

139 def execute (self, console):
140 if not self.command:
141 return
142 try:
143 try:
144 r = eval (self.command, self.globals, self.locals)
145 if r is not None:
146 # System output (if any)
147 while True:
148 try:
149 buf = os.read(console.piperead, 256)
150 except:
151 break
152 else:
153 console.write (buf, 'output')
154 if len(buf) < 256: break
155 # Command output
156 print(repr(r))
157 except SyntaxError:
158 exec(str(self.command), self.globals)
159 except:
160 if hasattr (sys, 'last_type') and sys.last_type == SystemExit:
161 console.quit()
162 elif hasattr (sys, 'exc_type') and sys.exc_type == SystemExit:
163 console.quit()
164 else:
165 try:
166 tb = sys.exc_info()[2]
167 if tb:
168 tb=tb.tb_next
169 traceback.print_exception(sys.exc_info()[0], sys.exc_info()[1], tb)
170 except:
171 sys.stderr, console.stderr = console.stderr, sys.stderr
172 traceback.print_exc()
173

◆ is_balanced()

pycons.shell.Shell.is_balanced (   self,
  line 
)
 Checks line balance for brace, bracket, parentheses and string quote

This helper function checks for the balance of brace, bracket,
parentheses and string quote. Any unbalanced line means to wait until
some other lines are fed to the console.

Definition at line 57 of file shell.py.

57 def is_balanced (self, line):
58 """ Checks line balance for brace, bracket, parentheses and string quote
59
60 This helper function checks for the balance of brace, bracket,
61 parentheses and string quote. Any unbalanced line means to wait until
62 some other lines are fed to the console.
63 """
64
65 s = line
66 s = list(filter(lambda x: x in '()[]{}"\'', s))
67 # s = s.replace ("'''", "'")
68 # s = s.replace ('"""', '"')
69 instring = False
70 brackets = {'(':')', '[':']', '{':'}', '"':'"', '\'':'\''}
71 stack = []
72 while len(s):
73 if not instring:
74 if s[0] in ')]}':
75 if stack and brackets[stack[-1]] == s[0]:
76 del stack[-1]
77 else:
78 return False
79 elif s[0] in '"\'':
80 if stack and brackets[stack[-1]] == s[0]:
81 del stack[-1]
82 instring = False
83 else:
84 stack.append(s[0])
85 instring = True
86 else:
87 stack.append(s[0])
88 else:
89 if s[0] in '"\'' and stack and brackets[stack[-1]] == s[0]:
90 del stack[-1]
91 instring = False
92 s = s[1:]
93 return len(stack) == 0
94
95

◆ namespace()

pycons.shell.Shell.namespace (   self)

Definition at line 54 of file shell.py.

54 def namespace(self):
55 return self.globals
56

Field Documentation

◆ command

pycons.shell.Shell.command

Definition at line 47 of file shell.py.

◆ complete_sep

pycons.shell.Shell.complete_sep

Definition at line 50 of file shell.py.

◆ completer

pycons.shell.Shell.completer

Definition at line 46 of file shell.py.

◆ globals

pycons.shell.Shell.globals

Definition at line 48 of file shell.py.

◆ locals

pycons.shell.Shell.locals

Definition at line 49 of file shell.py.

◆ prompt

pycons.shell.Shell.prompt

Definition at line 51 of file shell.py.


The documentation for this class was generated from the following file: