Show More
@@ -1,473 +1,472 b'' | |||||
1 | #!/usr/bin/python |
|
1 | #!/usr/bin/python | |
2 | # -*- coding: iso-8859-15 -*- |
|
2 | # -*- coding: iso-8859-15 -*- | |
3 | ''' |
|
3 | ''' | |
4 | Provides IPython remote instance. |
|
4 | Provides IPython remote instance. | |
5 |
|
5 | |||
6 | @author: Laurent Dufrechou |
|
6 | @author: Laurent Dufrechou | |
7 | laurent.dufrechou _at_ gmail.com |
|
7 | laurent.dufrechou _at_ gmail.com | |
8 | @license: BSD |
|
8 | @license: BSD | |
9 |
|
9 | |||
10 | All rights reserved. This program and the accompanying materials are made |
|
10 | All rights reserved. This program and the accompanying materials are made | |
11 | available under the terms of the BSD which accompanies this distribution, and |
|
11 | available under the terms of the BSD which accompanies this distribution, and | |
12 | is available at U{http://www.opensource.org/licenses/bsd-license.php} |
|
12 | is available at U{http://www.opensource.org/licenses/bsd-license.php} | |
13 | ''' |
|
13 | ''' | |
14 |
|
14 | |||
15 | __version__ = 0.9 |
|
15 | __version__ = 0.9 | |
16 | __author__ = "Laurent Dufrechou" |
|
16 | __author__ = "Laurent Dufrechou" | |
17 | __email__ = "laurent.dufrechou _at_ gmail.com" |
|
17 | __email__ = "laurent.dufrechou _at_ gmail.com" | |
18 | __license__ = "BSD" |
|
18 | __license__ = "BSD" | |
19 |
|
19 | |||
20 | import re |
|
20 | import re | |
21 | import sys |
|
21 | import sys | |
22 | import os |
|
22 | import os | |
23 | import locale |
|
23 | import locale | |
24 | import time |
|
24 | import time | |
25 | import pydoc,__builtin__,site |
|
25 | import pydoc,__builtin__,site | |
26 | from thread_ex import ThreadEx |
|
26 | from thread_ex import ThreadEx | |
27 | from StringIO import StringIO |
|
27 | from StringIO import StringIO | |
28 |
|
28 | |||
29 | try: |
|
29 | try: | |
30 | import IPython |
|
30 | import IPython | |
31 | except Exception,e: |
|
31 | except Exception,e: | |
32 | raise "Error importing IPython (%s)" % str(e) |
|
32 | raise "Error importing IPython (%s)" % str(e) | |
33 |
|
33 | |||
34 | ############################################################################## |
|
34 | ############################################################################## | |
35 | class _Helper(object): |
|
35 | class _Helper(object): | |
36 | """Redefine the built-in 'help'. |
|
36 | """Redefine the built-in 'help'. | |
37 | This is a wrapper around pydoc.help (with a twist). |
|
37 | This is a wrapper around pydoc.help (with a twist). | |
38 | """ |
|
38 | """ | |
39 |
|
39 | |||
40 | def __init__(self,pager): |
|
40 | def __init__(self,pager): | |
41 | self._pager = pager |
|
41 | self._pager = pager | |
42 |
|
42 | |||
43 | def __repr__(self): |
|
43 | def __repr__(self): | |
44 | return "Type help() for interactive help, " \ |
|
44 | return "Type help() for interactive help, " \ | |
45 | "or help(object) for help about object." |
|
45 | "or help(object) for help about object." | |
46 |
|
46 | |||
47 | def __call__(self, *args, **kwds): |
|
47 | def __call__(self, *args, **kwds): | |
48 | class DummyWriter(object): |
|
48 | class DummyWriter(object): | |
49 | def __init__(self,pager): |
|
49 | def __init__(self,pager): | |
50 | self._pager = pager |
|
50 | self._pager = pager | |
51 |
|
51 | |||
52 | def write(self,data): |
|
52 | def write(self,data): | |
53 | self._pager(data) |
|
53 | self._pager(data) | |
54 |
|
54 | |||
55 | import pydoc |
|
55 | import pydoc | |
56 | pydoc.help.output = DummyWriter(self._pager) |
|
56 | pydoc.help.output = DummyWriter(self._pager) | |
57 | pydoc.help.interact = lambda :1 |
|
57 | pydoc.help.interact = lambda :1 | |
58 |
|
58 | |||
59 | #helper.output.write = self.doc.append |
|
|||
60 | return pydoc.help(*args, **kwds) |
|
59 | return pydoc.help(*args, **kwds) | |
61 |
|
60 | |||
62 |
|
61 | |||
63 | ############################################################################## |
|
62 | ############################################################################## | |
64 | class _CodeExecutor(ThreadEx): |
|
63 | class _CodeExecutor(ThreadEx): | |
65 |
|
64 | |||
66 | def __init__(self, instance, after): |
|
65 | def __init__(self, instance, after): | |
67 | ThreadEx.__init__(self) |
|
66 | ThreadEx.__init__(self) | |
68 | self.instance = instance |
|
67 | self.instance = instance | |
69 | self._afterExecute=after |
|
68 | self._afterExecute=after | |
70 |
|
69 | |||
71 | def run(self): |
|
70 | def run(self): | |
72 | try: |
|
71 | try: | |
73 | self.instance._doc_text = None |
|
72 | self.instance._doc_text = None | |
74 | self.instance._help_text = None |
|
73 | self.instance._help_text = None | |
75 | self.instance._execute() |
|
74 | self.instance._execute() | |
76 | # used for uper class to generate event after execution |
|
75 | # used for uper class to generate event after execution | |
77 | self._afterExecute() |
|
76 | self._afterExecute() | |
78 |
|
77 | |||
79 | except KeyboardInterrupt: |
|
78 | except KeyboardInterrupt: | |
80 | pass |
|
79 | pass | |
81 |
|
80 | |||
82 |
|
81 | |||
83 | ############################################################################## |
|
82 | ############################################################################## | |
84 | class NonBlockingIPShell(object): |
|
83 | class NonBlockingIPShell(object): | |
85 | ''' |
|
84 | ''' | |
86 | Create an IPython instance, running the commands in a separate, |
|
85 | Create an IPython instance, running the commands in a separate, | |
87 | non-blocking thread. |
|
86 | non-blocking thread. | |
88 | This allows embedding in any GUI without blockage. |
|
87 | This allows embedding in any GUI without blockage. | |
89 |
|
88 | |||
90 | Note: The ThreadEx class supports asynchroneous function call |
|
89 | Note: The ThreadEx class supports asynchroneous function call | |
91 | via raise_exc() |
|
90 | via raise_exc() | |
92 | ''' |
|
91 | ''' | |
93 |
|
92 | |||
94 | def __init__(self,argv |
|
93 | def __init__(self,argv | |
95 | =[],user_ns={},user_global_ns=None, |
|
94 | =[],user_ns={},user_global_ns=None, | |
96 | cin=None, cout=None, cerr=None, |
|
95 | cin=None, cout=None, cerr=None, | |
97 | ask_exit_handler=None): |
|
96 | ask_exit_handler=None): | |
98 | ''' |
|
97 | ''' | |
99 | @param argv: Command line options for IPython |
|
98 | @param argv: Command line options for IPython | |
100 | @type argv: list |
|
99 | @type argv: list | |
101 | @param user_ns: User namespace. |
|
100 | @param user_ns: User namespace. | |
102 | @type user_ns: dictionary |
|
101 | @type user_ns: dictionary | |
103 | @param user_global_ns: User global namespace. |
|
102 | @param user_global_ns: User global namespace. | |
104 | @type user_global_ns: dictionary. |
|
103 | @type user_global_ns: dictionary. | |
105 | @param cin: Console standard input. |
|
104 | @param cin: Console standard input. | |
106 | @type cin: IO stream |
|
105 | @type cin: IO stream | |
107 | @param cout: Console standard output. |
|
106 | @param cout: Console standard output. | |
108 | @type cout: IO stream |
|
107 | @type cout: IO stream | |
109 | @param cerr: Console standard error. |
|
108 | @param cerr: Console standard error. | |
110 | @type cerr: IO stream |
|
109 | @type cerr: IO stream | |
111 | @param exit_handler: Replacement for builtin exit() function |
|
110 | @param exit_handler: Replacement for builtin exit() function | |
112 | @type exit_handler: function |
|
111 | @type exit_handler: function | |
113 | @param time_loop: Define the sleep time between two thread's loop |
|
112 | @param time_loop: Define the sleep time between two thread's loop | |
114 | @type int |
|
113 | @type int | |
115 | ''' |
|
114 | ''' | |
116 | #first we redefine in/out/error functions of IPython |
|
115 | #first we redefine in/out/error functions of IPython | |
117 | if cin: |
|
116 | if cin: | |
118 | IPython.Shell.Term.cin = cin |
|
117 | IPython.Shell.Term.cin = cin | |
119 | if cout: |
|
118 | if cout: | |
120 | IPython.Shell.Term.cout = cout |
|
119 | IPython.Shell.Term.cout = cout | |
121 | if cerr: |
|
120 | if cerr: | |
122 | IPython.Shell.Term.cerr = cerr |
|
121 | IPython.Shell.Term.cerr = cerr | |
123 |
|
122 | |||
124 | # This is to get rid of the blockage that accurs during |
|
123 | # This is to get rid of the blockage that accurs during | |
125 | # IPython.Shell.InteractiveShell.user_setup() |
|
124 | # IPython.Shell.InteractiveShell.user_setup() | |
126 | IPython.iplib.raw_input = lambda x: None |
|
125 | IPython.iplib.raw_input = lambda x: None | |
127 |
|
126 | |||
128 | self._term = IPython.genutils.IOTerm(cin=cin, cout=cout, cerr=cerr) |
|
127 | self._term = IPython.genutils.IOTerm(cin=cin, cout=cout, cerr=cerr) | |
129 |
|
128 | |||
130 | excepthook = sys.excepthook |
|
129 | excepthook = sys.excepthook | |
131 |
|
130 | |||
132 | self._IP = IPython.Shell.make_IPython( |
|
131 | self._IP = IPython.Shell.make_IPython( | |
133 | argv,user_ns=user_ns, |
|
132 | argv,user_ns=user_ns, | |
134 | user_global_ns=user_global_ns, |
|
133 | user_global_ns=user_global_ns, | |
135 | embedded=True, |
|
134 | embedded=True, | |
136 | shell_class=IPython.Shell.InteractiveShell) |
|
135 | shell_class=IPython.Shell.InteractiveShell) | |
137 |
|
136 | |||
138 | #we replace IPython default encoding by wx locale encoding |
|
137 | #we replace IPython default encoding by wx locale encoding | |
139 | loc = locale.getpreferredencoding() |
|
138 | loc = locale.getpreferredencoding() | |
140 | if loc: |
|
139 | if loc: | |
141 | self._IP.stdin_encoding = loc |
|
140 | self._IP.stdin_encoding = loc | |
142 | #we replace the ipython default pager by our pager |
|
141 | #we replace the ipython default pager by our pager | |
143 | self._IP.set_hook('show_in_pager',self._pager) |
|
142 | self._IP.set_hook('show_in_pager',self._pager) | |
144 |
|
143 | |||
145 | #we replace the ipython default shell command caller by our shell handler |
|
144 | #we replace the ipython default shell command caller by our shell handler | |
146 | self._IP.set_hook('shell_hook',self._shell) |
|
145 | self._IP.set_hook('shell_hook',self._shell) | |
147 |
|
146 | |||
148 | #we replace the ipython default input command caller by our method |
|
147 | #we replace the ipython default input command caller by our method | |
149 | IPython.iplib.raw_input_original = self._raw_input |
|
148 | IPython.iplib.raw_input_original = self._raw_input | |
150 | #we replace the ipython default exit command by our method |
|
149 | #we replace the ipython default exit command by our method | |
151 | self._IP.exit = self._setAskExit |
|
150 | self._IP.exit = self._setAskExit | |
152 | #we modify Exit and Quit Magic |
|
151 | #we modify Exit and Quit Magic | |
153 | ip = IPython.ipapi.get() |
|
152 | ip = IPython.ipapi.get() | |
154 | ip.expose_magic('Exit', self._setDoExit) |
|
153 | ip.expose_magic('Exit', self._setDoExit) | |
155 | ip.expose_magic('Quit', self._setDoExit) |
|
154 | ip.expose_magic('Quit', self._setDoExit) | |
|
155 | #we replace the help command | |||
|
156 | self._IP.user_ns['help'] = _Helper(self._pager_help) | |||
156 |
|
157 | |||
157 | sys.excepthook = excepthook |
|
158 | sys.excepthook = excepthook | |
158 |
|
159 | |||
|
160 | #vars used by _execute | |||
159 | self._iter_more = 0 |
|
161 | self._iter_more = 0 | |
160 | self._history_level = 0 |
|
162 | self._history_level = 0 | |
161 | self._complete_sep = re.compile('[\s\{\}\[\]\(\)]') |
|
163 | self._complete_sep = re.compile('[\s\{\}\[\]\(\)]') | |
162 | self._prompt = str(self._IP.outputcache.prompt1).strip() |
|
164 | self._prompt = str(self._IP.outputcache.prompt1).strip() | |
163 |
|
165 | |||
164 | #thread working vars |
|
166 | #thread working vars | |
165 | self._line_to_execute = '' |
|
167 | self._line_to_execute = '' | |
166 |
|
168 | |||
167 | #vars that will be checked by GUI loop to handle thread states... |
|
169 | #vars that will be checked by GUI loop to handle thread states... | |
168 | #will be replaced later by PostEvent GUI funtions... |
|
170 | #will be replaced later by PostEvent GUI funtions... | |
169 | self._doc_text = None |
|
171 | self._doc_text = None | |
170 | self._help_text = None |
|
172 | self._help_text = None | |
171 | self._ask_exit = False |
|
173 | self._ask_exit = False | |
172 | self._add_button = None |
|
174 | self._add_button = None | |
173 |
|
175 | |||
174 | #we replace the help command |
|
|||
175 | self._IP.user_ns['help'] = _Helper(self._pager_help) |
|
|||
176 |
|
||||
177 | #----------------------- Thread management section ---------------------- |
|
176 | #----------------------- Thread management section ---------------------- | |
178 | def doExecute(self,line): |
|
177 | def doExecute(self,line): | |
179 | """ |
|
178 | """ | |
180 | Tell the thread to process the 'line' command |
|
179 | Tell the thread to process the 'line' command | |
181 | """ |
|
180 | """ | |
182 |
|
181 | |||
183 | self._line_to_execute = line |
|
182 | self._line_to_execute = line | |
184 |
|
183 | |||
185 | self.ce = _CodeExecutor(self,self._afterExecute) |
|
184 | self.ce = _CodeExecutor(self,self._afterExecute) | |
186 | self.ce.start() |
|
185 | self.ce.start() | |
187 |
|
186 | |||
188 | #----------------------- IPython management section ---------------------- |
|
187 | #----------------------- IPython management section ---------------------- | |
189 | def getAskExit(self): |
|
188 | def getAskExit(self): | |
190 | ''' |
|
189 | ''' | |
191 | returns the _ask_exit variable that can be checked by GUI to see if |
|
190 | returns the _ask_exit variable that can be checked by GUI to see if | |
192 | IPython request an exit handling |
|
191 | IPython request an exit handling | |
193 | ''' |
|
192 | ''' | |
194 | return self._ask_exit |
|
193 | return self._ask_exit | |
195 |
|
194 | |||
196 | def clearAskExit(self): |
|
195 | def clearAskExit(self): | |
197 | ''' |
|
196 | ''' | |
198 | clear the _ask_exit var when GUI as handled the request. |
|
197 | clear the _ask_exit var when GUI as handled the request. | |
199 | ''' |
|
198 | ''' | |
200 | self._ask_exit = False |
|
199 | self._ask_exit = False | |
201 |
|
200 | |||
202 | def getDocText(self): |
|
201 | def getDocText(self): | |
203 | """ |
|
202 | """ | |
204 | Returns the output of the processing that need to be paged (if any) |
|
203 | Returns the output of the processing that need to be paged (if any) | |
205 |
|
204 | |||
206 | @return: The std output string. |
|
205 | @return: The std output string. | |
207 | @rtype: string |
|
206 | @rtype: string | |
208 | """ |
|
207 | """ | |
209 | return self._doc_text |
|
208 | return self._doc_text | |
210 |
|
209 | |||
211 | def getHelpText(self): |
|
210 | def getHelpText(self): | |
212 | """ |
|
211 | """ | |
213 | Returns the output of the processing that need to be paged via help pager(if any) |
|
212 | Returns the output of the processing that need to be paged via help pager(if any) | |
214 |
|
213 | |||
215 | @return: The std output string. |
|
214 | @return: The std output string. | |
216 | @rtype: string |
|
215 | @rtype: string | |
217 | """ |
|
216 | """ | |
218 | return self._help_text |
|
217 | return self._help_text | |
219 |
|
218 | |||
220 | def getBanner(self): |
|
219 | def getBanner(self): | |
221 | """ |
|
220 | """ | |
222 | Returns the IPython banner for useful info on IPython instance |
|
221 | Returns the IPython banner for useful info on IPython instance | |
223 |
|
222 | |||
224 | @return: The banner string. |
|
223 | @return: The banner string. | |
225 | @rtype: string |
|
224 | @rtype: string | |
226 | """ |
|
225 | """ | |
227 | return self._IP.BANNER |
|
226 | return self._IP.BANNER | |
228 |
|
227 | |||
229 | def getPromptCount(self): |
|
228 | def getPromptCount(self): | |
230 | """ |
|
229 | """ | |
231 | Returns the prompt number. |
|
230 | Returns the prompt number. | |
232 | Each time a user execute a line in the IPython shell the prompt count is increased |
|
231 | Each time a user execute a line in the IPython shell the prompt count is increased | |
233 |
|
232 | |||
234 | @return: The prompt number |
|
233 | @return: The prompt number | |
235 | @rtype: int |
|
234 | @rtype: int | |
236 | """ |
|
235 | """ | |
237 | return self._IP.outputcache.prompt_count |
|
236 | return self._IP.outputcache.prompt_count | |
238 |
|
237 | |||
239 | def getPrompt(self): |
|
238 | def getPrompt(self): | |
240 | """ |
|
239 | """ | |
241 | Returns current prompt inside IPython instance |
|
240 | Returns current prompt inside IPython instance | |
242 | (Can be In [...]: ot ...:) |
|
241 | (Can be In [...]: ot ...:) | |
243 |
|
242 | |||
244 | @return: The current prompt. |
|
243 | @return: The current prompt. | |
245 | @rtype: string |
|
244 | @rtype: string | |
246 | """ |
|
245 | """ | |
247 | return self._prompt |
|
246 | return self._prompt | |
248 |
|
247 | |||
249 | def getIndentation(self): |
|
248 | def getIndentation(self): | |
250 | """ |
|
249 | """ | |
251 | Returns the current indentation level |
|
250 | Returns the current indentation level | |
252 | Usefull to put the caret at the good start position if we want to do autoindentation. |
|
251 | Usefull to put the caret at the good start position if we want to do autoindentation. | |
253 |
|
252 | |||
254 | @return: The indentation level. |
|
253 | @return: The indentation level. | |
255 | @rtype: int |
|
254 | @rtype: int | |
256 | """ |
|
255 | """ | |
257 | return self._IP.indent_current_nsp |
|
256 | return self._IP.indent_current_nsp | |
258 |
|
257 | |||
259 | def updateNamespace(self, ns_dict): |
|
258 | def updateNamespace(self, ns_dict): | |
260 | ''' |
|
259 | ''' | |
261 | Add the current dictionary to the shell namespace. |
|
260 | Add the current dictionary to the shell namespace. | |
262 |
|
261 | |||
263 | @param ns_dict: A dictionary of symbol-values. |
|
262 | @param ns_dict: A dictionary of symbol-values. | |
264 | @type ns_dict: dictionary |
|
263 | @type ns_dict: dictionary | |
265 | ''' |
|
264 | ''' | |
266 | self._IP.user_ns.update(ns_dict) |
|
265 | self._IP.user_ns.update(ns_dict) | |
267 |
|
266 | |||
268 | def complete(self, line): |
|
267 | def complete(self, line): | |
269 | ''' |
|
268 | ''' | |
270 | Returns an auto completed line and/or posibilities for completion. |
|
269 | Returns an auto completed line and/or posibilities for completion. | |
271 |
|
270 | |||
272 | @param line: Given line so far. |
|
271 | @param line: Given line so far. | |
273 | @type line: string |
|
272 | @type line: string | |
274 |
|
273 | |||
275 | @return: Line completed as for as possible, |
|
274 | @return: Line completed as for as possible, | |
276 | and possible further completions. |
|
275 | and possible further completions. | |
277 | @rtype: tuple |
|
276 | @rtype: tuple | |
278 | ''' |
|
277 | ''' | |
279 | split_line = self._complete_sep.split(line) |
|
278 | split_line = self._complete_sep.split(line) | |
280 | possibilities = self._IP.complete(split_line[-1]) |
|
279 | possibilities = self._IP.complete(split_line[-1]) | |
281 | if possibilities: |
|
280 | if possibilities: | |
282 |
|
281 | |||
283 | def _commonPrefix(str1, str2): |
|
282 | def _commonPrefix(str1, str2): | |
284 | ''' |
|
283 | ''' | |
285 | Reduction function. returns common prefix of two given strings. |
|
284 | Reduction function. returns common prefix of two given strings. | |
286 |
|
285 | |||
287 | @param str1: First string. |
|
286 | @param str1: First string. | |
288 | @type str1: string |
|
287 | @type str1: string | |
289 | @param str2: Second string |
|
288 | @param str2: Second string | |
290 | @type str2: string |
|
289 | @type str2: string | |
291 |
|
290 | |||
292 | @return: Common prefix to both strings. |
|
291 | @return: Common prefix to both strings. | |
293 | @rtype: string |
|
292 | @rtype: string | |
294 | ''' |
|
293 | ''' | |
295 | for i in range(len(str1)): |
|
294 | for i in range(len(str1)): | |
296 | if not str2.startswith(str1[:i+1]): |
|
295 | if not str2.startswith(str1[:i+1]): | |
297 | return str1[:i] |
|
296 | return str1[:i] | |
298 | return str1 |
|
297 | return str1 | |
299 | common_prefix = reduce(_commonPrefix, possibilities) |
|
298 | common_prefix = reduce(_commonPrefix, possibilities) | |
300 | completed = line[:-len(split_line[-1])]+common_prefix |
|
299 | completed = line[:-len(split_line[-1])]+common_prefix | |
301 | else: |
|
300 | else: | |
302 | completed = line |
|
301 | completed = line | |
303 | return completed, possibilities |
|
302 | return completed, possibilities | |
304 |
|
303 | |||
305 | def historyBack(self): |
|
304 | def historyBack(self): | |
306 | ''' |
|
305 | ''' | |
307 | Provides one history command back. |
|
306 | Provides one history command back. | |
308 |
|
307 | |||
309 | @return: The command string. |
|
308 | @return: The command string. | |
310 | @rtype: string |
|
309 | @rtype: string | |
311 | ''' |
|
310 | ''' | |
312 | history = '' |
|
311 | history = '' | |
313 | #the below while loop is used to suppress empty history lines |
|
312 | #the below while loop is used to suppress empty history lines | |
314 | while((history == '' or history == '\n') and self._history_level >0): |
|
313 | while((history == '' or history == '\n') and self._history_level >0): | |
315 | if self._history_level>=1: |
|
314 | if self._history_level>=1: | |
316 | self._history_level -= 1 |
|
315 | self._history_level -= 1 | |
317 | history = self._getHistory() |
|
316 | history = self._getHistory() | |
318 | return history |
|
317 | return history | |
319 |
|
318 | |||
320 | def historyForward(self): |
|
319 | def historyForward(self): | |
321 | ''' |
|
320 | ''' | |
322 | Provides one history command forward. |
|
321 | Provides one history command forward. | |
323 |
|
322 | |||
324 | @return: The command string. |
|
323 | @return: The command string. | |
325 | @rtype: string |
|
324 | @rtype: string | |
326 | ''' |
|
325 | ''' | |
327 | history = '' |
|
326 | history = '' | |
328 | #the below while loop is used to suppress empty history lines |
|
327 | #the below while loop is used to suppress empty history lines | |
329 | while((history == '' or history == '\n') and self._history_level <= self._getHistoryMaxIndex()): |
|
328 | while((history == '' or history == '\n') and self._history_level <= self._getHistoryMaxIndex()): | |
330 | if self._history_level < self._getHistoryMaxIndex(): |
|
329 | if self._history_level < self._getHistoryMaxIndex(): | |
331 | self._history_level += 1 |
|
330 | self._history_level += 1 | |
332 | history = self._getHistory() |
|
331 | history = self._getHistory() | |
333 | else: |
|
332 | else: | |
334 | if self._history_level == self._getHistoryMaxIndex(): |
|
333 | if self._history_level == self._getHistoryMaxIndex(): | |
335 | history = self._getHistory() |
|
334 | history = self._getHistory() | |
336 | self._history_level += 1 |
|
335 | self._history_level += 1 | |
337 | else: |
|
336 | else: | |
338 | history = '' |
|
337 | history = '' | |
339 | return history |
|
338 | return history | |
340 |
|
339 | |||
341 | def initHistoryIndex(self): |
|
340 | def initHistoryIndex(self): | |
342 | ''' |
|
341 | ''' | |
343 | set history to last command entered |
|
342 | set history to last command entered | |
344 | ''' |
|
343 | ''' | |
345 | self._history_level = self._getHistoryMaxIndex()+1 |
|
344 | self._history_level = self._getHistoryMaxIndex()+1 | |
346 |
|
345 | |||
347 | #----------------------- IPython PRIVATE management section -------------- |
|
346 | #----------------------- IPython PRIVATE management section -------------- | |
348 | def _afterExecute(self): |
|
347 | def _afterExecute(self): | |
349 | ''' |
|
348 | ''' | |
350 | Can be redefined to generate post event after excution is done |
|
349 | Can be redefined to generate post event after excution is done | |
351 | ''' |
|
350 | ''' | |
352 | pass |
|
351 | pass | |
353 |
|
352 | |||
354 | def _setAskExit(self): |
|
353 | def _setAskExit(self): | |
355 | ''' |
|
354 | ''' | |
356 | set the _ask_exit variable that can be checked by GUI to see if |
|
355 | set the _ask_exit variable that can be checked by GUI to see if | |
357 | IPython request an exit handling |
|
356 | IPython request an exit handling | |
358 | ''' |
|
357 | ''' | |
359 | self._ask_exit = True |
|
358 | self._ask_exit = True | |
360 |
|
359 | |||
361 | def _setDoExit(self, toto, arg): |
|
360 | def _setDoExit(self, toto, arg): | |
362 | ''' |
|
361 | ''' | |
363 | set the _do_exit variable that can be checked by GUI to see if |
|
362 | set the _do_exit variable that can be checked by GUI to see if | |
364 | IPython do a direct exit of the app |
|
363 | IPython do a direct exit of the app | |
365 | ''' |
|
364 | ''' | |
366 | self._do_exit = True |
|
365 | self._do_exit = True | |
367 |
|
366 | |||
368 | def _getHistoryMaxIndex(self): |
|
367 | def _getHistoryMaxIndex(self): | |
369 | ''' |
|
368 | ''' | |
370 | returns the max length of the history buffer |
|
369 | returns the max length of the history buffer | |
371 |
|
370 | |||
372 | @return: history length |
|
371 | @return: history length | |
373 | @rtype: int |
|
372 | @rtype: int | |
374 | ''' |
|
373 | ''' | |
375 | return len(self._IP.input_hist_raw)-1 |
|
374 | return len(self._IP.input_hist_raw)-1 | |
376 |
|
375 | |||
377 | def _getHistory(self): |
|
376 | def _getHistory(self): | |
378 | ''' |
|
377 | ''' | |
379 | Get's the command string of the current history level. |
|
378 | Get's the command string of the current history level. | |
380 |
|
379 | |||
381 | @return: Historic command stri |
|
380 | @return: Historic command stri | |
382 | @rtype: string |
|
381 | @rtype: string | |
383 | ''' |
|
382 | ''' | |
384 | rv = self._IP.input_hist_raw[self._history_level].strip('\n') |
|
383 | rv = self._IP.input_hist_raw[self._history_level].strip('\n') | |
385 | return rv |
|
384 | return rv | |
386 |
|
385 | |||
387 | def _pager_help(self,text): |
|
386 | def _pager_help(self,text): | |
388 | ''' |
|
387 | ''' | |
389 | This function is used as a callback replacment to IPython help pager function |
|
388 | This function is used as a callback replacment to IPython help pager function | |
390 |
|
389 | |||
391 | It puts the 'text' value inside the self._help_text string that can be retrived via getHelpText |
|
390 | It puts the 'text' value inside the self._help_text string that can be retrived via getHelpText | |
392 | function. |
|
391 | function. | |
393 | ''' |
|
392 | ''' | |
394 | if self._help_text == None: |
|
393 | if self._help_text == None: | |
395 | self._help_text = text |
|
394 | self._help_text = text | |
396 | else: |
|
395 | else: | |
397 | self._help_text += text |
|
396 | self._help_text += text | |
398 |
|
397 | |||
399 | def _pager(self,IP,text): |
|
398 | def _pager(self,IP,text): | |
400 | ''' |
|
399 | ''' | |
401 | This function is used as a callback replacment to IPython pager function |
|
400 | This function is used as a callback replacment to IPython pager function | |
402 |
|
401 | |||
403 | It puts the 'text' value inside the self._doc_text string that can be retrived via getDocText |
|
402 | It puts the 'text' value inside the self._doc_text string that can be retrived via getDocText | |
404 | function. |
|
403 | function. | |
405 | ''' |
|
404 | ''' | |
406 | self._doc_text = text |
|
405 | self._doc_text = text | |
407 |
|
406 | |||
408 | def _raw_input(self, prompt=''): |
|
407 | def _raw_input(self, prompt=''): | |
409 | ''' |
|
408 | ''' | |
410 | Custom raw_input() replacement. Get's current line from console buffer. |
|
409 | Custom raw_input() replacement. Get's current line from console buffer. | |
411 |
|
410 | |||
412 | @param prompt: Prompt to print. Here for compatability as replacement. |
|
411 | @param prompt: Prompt to print. Here for compatability as replacement. | |
413 | @type prompt: string |
|
412 | @type prompt: string | |
414 |
|
413 | |||
415 | @return: The current command line text. |
|
414 | @return: The current command line text. | |
416 | @rtype: string |
|
415 | @rtype: string | |
417 | ''' |
|
416 | ''' | |
418 | return self._line_to_execute |
|
417 | return self._line_to_execute | |
419 |
|
418 | |||
420 | def _execute(self): |
|
419 | def _execute(self): | |
421 | ''' |
|
420 | ''' | |
422 | Executes the current line provided by the shell object. |
|
421 | Executes the current line provided by the shell object. | |
423 | ''' |
|
422 | ''' | |
424 | orig_stdout = sys.stdout |
|
423 | orig_stdout = sys.stdout | |
425 | sys.stdout = IPython.Shell.Term.cout |
|
424 | sys.stdout = IPython.Shell.Term.cout | |
426 |
|
425 | |||
427 | try: |
|
426 | try: | |
428 | line = self._IP.raw_input(None, self._iter_more) |
|
427 | line = self._IP.raw_input(None, self._iter_more) | |
429 | if self._IP.autoindent: |
|
428 | if self._IP.autoindent: | |
430 | self._IP.readline_startup_hook(None) |
|
429 | self._IP.readline_startup_hook(None) | |
431 |
|
430 | |||
432 | except KeyboardInterrupt: |
|
431 | except KeyboardInterrupt: | |
433 | self._IP.write('\nKeyboardInterrupt\n') |
|
432 | self._IP.write('\nKeyboardInterrupt\n') | |
434 | self._IP.resetbuffer() |
|
433 | self._IP.resetbuffer() | |
435 | # keep cache in sync with the prompt counter: |
|
434 | # keep cache in sync with the prompt counter: | |
436 | self._IP.outputcache.prompt_count -= 1 |
|
435 | self._IP.outputcache.prompt_count -= 1 | |
437 |
|
436 | |||
438 | if self._IP.autoindent: |
|
437 | if self._IP.autoindent: | |
439 | self._IP.indent_current_nsp = 0 |
|
438 | self._IP.indent_current_nsp = 0 | |
440 | self._iter_more = 0 |
|
439 | self._iter_more = 0 | |
441 | except: |
|
440 | except: | |
442 | self._IP.showtraceback() |
|
441 | self._IP.showtraceback() | |
443 | else: |
|
442 | else: | |
444 | self._iter_more = self._IP.push(line) |
|
443 | self._iter_more = self._IP.push(line) | |
445 | if (self._IP.SyntaxTB.last_syntax_error and |
|
444 | if (self._IP.SyntaxTB.last_syntax_error and | |
446 | self._IP.rc.autoedit_syntax): |
|
445 | self._IP.rc.autoedit_syntax): | |
447 | self._IP.edit_syntax_error() |
|
446 | self._IP.edit_syntax_error() | |
448 | if self._iter_more: |
|
447 | if self._iter_more: | |
449 | self._prompt = str(self._IP.outputcache.prompt2).strip() |
|
448 | self._prompt = str(self._IP.outputcache.prompt2).strip() | |
450 | if self._IP.autoindent: |
|
449 | if self._IP.autoindent: | |
451 | self._IP.readline_startup_hook(self._IP.pre_readline) |
|
450 | self._IP.readline_startup_hook(self._IP.pre_readline) | |
452 | else: |
|
451 | else: | |
453 | self._prompt = str(self._IP.outputcache.prompt1).strip() |
|
452 | self._prompt = str(self._IP.outputcache.prompt1).strip() | |
454 | self._IP.indent_current_nsp = 0 #we set indentation to 0 |
|
453 | self._IP.indent_current_nsp = 0 #we set indentation to 0 | |
455 | sys.stdout = orig_stdout |
|
454 | sys.stdout = orig_stdout | |
456 |
|
455 | |||
457 | def _shell(self, ip, cmd): |
|
456 | def _shell(self, ip, cmd): | |
458 | ''' |
|
457 | ''' | |
459 | Replacement method to allow shell commands without them blocking. |
|
458 | Replacement method to allow shell commands without them blocking. | |
460 |
|
459 | |||
461 | @param ip: Ipython instance, same as self._IP |
|
460 | @param ip: Ipython instance, same as self._IP | |
462 | @type cmd: Ipython instance |
|
461 | @type cmd: Ipython instance | |
463 | @param cmd: Shell command to execute. |
|
462 | @param cmd: Shell command to execute. | |
464 | @type cmd: string |
|
463 | @type cmd: string | |
465 | ''' |
|
464 | ''' | |
466 | stdin, stdout = os.popen4(cmd) |
|
465 | stdin, stdout = os.popen4(cmd) | |
467 | result = stdout.read().decode('cp437').encode(locale.getpreferredencoding()) |
|
466 | result = stdout.read().decode('cp437').encode(locale.getpreferredencoding()) | |
468 | #we use print command because the shell command is called inside IPython instance and thus is |
|
467 | #we use print command because the shell command is called inside IPython instance and thus is | |
469 | #redirected to thread cout |
|
468 | #redirected to thread cout | |
470 | #"\x01\x1b[1;36m\x02" <-- add colour to the text... |
|
469 | #"\x01\x1b[1;36m\x02" <-- add colour to the text... | |
471 | print "\x01\x1b[1;36m\x02"+result |
|
470 | print "\x01\x1b[1;36m\x02"+result | |
472 | stdout.close() |
|
471 | stdout.close() | |
473 | stdin.close() |
|
472 | stdin.close() |
General Comments 0
You need to be logged in to leave comments.
Login now