home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __version__ = 0.9
- __author__ = 'Laurent Dufrechou'
- __email__ = 'laurent.dufrechou _at_ gmail.com'
- __license__ = 'BSD'
- import re
- import sys
- import os
- import locale
- from thread_ex import ThreadEx
-
- try:
- import IPython
- except Exception:
- e = None
- print 'Error importing IPython (%s)' % str(e)
- raise Exception, e
-
-
- class _Helper(object):
-
- def __init__(self, pager):
- self._pager = pager
-
-
- def __repr__(self):
- return 'Type help() for interactive help, or help(object) for help about object.'
-
-
- def __call__(self, *args, **kwds):
-
- class DummyWriter(object):
-
- def __init__(self, pager):
- self._pager = pager
-
-
- def write(self, data):
- self._pager(data)
-
-
- import pydoc
- pydoc.help.output = DummyWriter(self._pager)
-
- pydoc.help.interact = lambda : 1
- return pydoc.help(*args, **kwds)
-
-
-
- class _CodeExecutor(ThreadEx):
-
- def __init__(self, instance):
- ThreadEx.__init__(self)
- self.instance = instance
-
-
- def run(self):
-
- try:
- self.instance._doc_text = None
- self.instance._help_text = None
- self.instance._execute()
- self.instance._after_execute()
- except KeyboardInterrupt:
- pass
-
-
-
-
- class NonBlockingIPShell(object):
-
- def __init__(self, argv = [], user_ns = { }, user_global_ns = None, cin = None, cout = None, cerr = None, ask_exit_handler = None):
- self._IP = None
- self.init_ipython0(argv, user_ns, user_global_ns, cin, cout, cerr, ask_exit_handler)
- self._iter_more = 0
- self._history_level = 0
- self._complete_sep = re.compile('[\\s\\{\\}\\[\\]\\(\\)\\=]')
- self._prompt = str(self._IP.outputcache.prompt1).strip()
- self._line_to_execute = ''
- self._threading = True
- self._doc_text = None
- self._help_text = None
- self._add_button = None
-
-
- def init_ipython0(self, argv = [], user_ns = { }, user_global_ns = None, cin = None, cout = None, cerr = None, ask_exit_handler = None):
- if cin:
- IPython.genutils.Term.cin = cin
-
- if cout:
- IPython.genutils.Term.cout = cout
-
- if cerr:
- IPython.genutils.Term.cerr = cerr
-
- excepthook = sys.excepthook
- self.sys_displayhook_ori = sys.displayhook
- self._IP = IPython.Shell.make_IPython(argv, user_ns = user_ns, user_global_ns = user_global_ns, embedded = True, shell_class = IPython.Shell.InteractiveShell)
- self.displayhook = sys.displayhook
- sys.displayhook = self.sys_displayhook_ori
- loc = locale.getpreferredencoding()
- if loc:
- self._IP.stdin_encoding = loc
-
- self._IP.set_hook('show_in_pager', self._pager)
- self._IP.set_hook('shell_hook', self._shell)
- IPython.iplib.raw_input_original = self._raw_input_original
- self._IP.exit = ask_exit_handler
- self._IP.user_ns['help'] = _Helper(self._pager_help)
- ip = IPython.ipapi.get()
-
- def bypass_magic(self, arg):
- print '%this magic is currently disabled.'
-
- ip.expose_magic('cpaste', bypass_magic)
- import __builtin__
- __builtin__.raw_input = self._raw_input
- sys.excepthook = excepthook
-
-
- def do_execute(self, line):
- self._line_to_execute = line
- if self._threading:
- self.ce = _CodeExecutor(self)
- self.ce.start()
- else:
-
- try:
- self._doc_text = None
- self._help_text = None
- self._execute()
- self._after_execute()
- except KeyboardInterrupt:
- pass
-
-
-
- def get_threading(self):
- return self._threading
-
-
- def set_threading(self, state):
- self._threading = state
-
-
- def get_doc_text(self):
- return self._doc_text
-
-
- def get_help_text(self):
- return self._help_text
-
-
- def get_banner(self):
- return self._IP.BANNER
-
-
- def get_prompt_count(self):
- return self._IP.outputcache.prompt_count
-
-
- def get_prompt(self):
- return self._prompt
-
-
- def get_indentation(self):
- return self._IP.indent_current_nsp
-
-
- def update_namespace(self, ns_dict):
- self._IP.user_ns.update(ns_dict)
-
-
- def complete(self, line):
- split_line = self._complete_sep.split(line)
- possibilities = self._IP.complete(split_line[-1])
- if possibilities:
-
- def _common_prefix(str1, str2):
- for i in range(len(str1)):
- if not str2.startswith(str1[:i + 1]):
- return str1[:i]
-
- return str1
-
- common_prefix = reduce(_common_prefix, possibilities)
- completed = line[:-len(split_line[-1])] + common_prefix
- else:
- completed = line
- return (completed, possibilities)
-
-
- def history_back(self):
- history = ''
- while (history == '' or history == '\n') and self._history_level > 0:
- if self._history_level >= 1:
- self._history_level -= 1
-
- history = self._get_history()
- return history
-
-
- def history_forward(self):
- history = ''
- while (history == '' or history == '\n') and self._history_level <= self._get_history_max_index():
- if self._history_level < self._get_history_max_index():
- self._history_level += 1
- history = self._get_history()
- continue
- self
- if self._history_level == self._get_history_max_index():
- history = self._get_history()
- self._history_level += 1
- continue
- self
- history = ''
- return history
-
-
- def init_history_index(self):
- self._history_level = self._get_history_max_index() + 1
-
-
- def _after_execute(self):
- pass
-
-
- def _ask_exit(self):
- pass
-
-
- def _get_history_max_index(self):
- return len(self._IP.input_hist_raw) - 1
-
-
- def _get_history(self):
- rv = self._IP.input_hist_raw[self._history_level].strip('\n')
- return rv
-
-
- def _pager_help(self, text):
- pass
-
-
- def _pager(self, IP, text):
- self._doc_text = text
-
-
- def _raw_input_original(self, prompt = ''):
- return self._line_to_execute
-
-
- def _raw_input(self, prompt = ''):
- raise NotImplementedError
-
-
- def _execute(self):
- orig_stdout = sys.stdout
- sys.stdout = IPython.Shell.Term.cout
-
- try:
- line = self._IP.raw_input(None, self._iter_more)
- if self._IP.autoindent:
- self._IP.readline_startup_hook(None)
- except KeyboardInterrupt:
- self._IP.write('\nKeyboardInterrupt\n')
- self._IP.resetbuffer()
- self._IP.outputcache.prompt_count -= 1
- if self._IP.autoindent:
- self._IP.indent_current_nsp = 0
-
- self._iter_more = 0
- except:
- self._IP.showtraceback()
-
- self._IP.write(str(self._IP.outputcache.prompt_out).strip())
- self._iter_more = self._IP.push(line)
- if self._IP.SyntaxTB.last_syntax_error and self._IP.rc.autoedit_syntax:
- self._IP.edit_syntax_error()
-
- if self._iter_more:
- self._prompt = str(self._IP.outputcache.prompt2).strip()
- if self._IP.autoindent:
- self._IP.readline_startup_hook(self._IP.pre_readline)
-
- else:
- self._prompt = str(self._IP.outputcache.prompt1).strip()
- self._IP.indent_current_nsp = 0
- sys.stdout = orig_stdout
-
-
- def _shell(self, ip, cmd):
- (stdin, stdout) = os.popen4(cmd)
- result = stdout.read().decode('cp437').encode(locale.getpreferredencoding())
- print '\x01\x1b[1;36m\x02' + result
- stdout.close()
- stdin.close()
-
-
-