• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.10.1/pyobjc-45/2.5/pyobjc/pyobjc-core/Examples/NonFunctional/RemotePyInterpreter/
1import sys
2import os
3import __builtin__
4import traceback
5import keyword
6import time
7from code import InteractiveConsole, softspace
8from StringIO import StringIO
9try:
10    set
11except NameError:
12    from sets import Set as set
13
14class RemoteConsole(InteractiveConsole):
15    def __init__(self, pipe, **kw):
16        self.pipe = pipe
17        self.buffer = None
18        InteractiveConsole.__init__(self, **kw)
19        self.locals['__interpreter__'] = self
20
21    def raw_input(self, prompt=''):
22        return self.pipe.expect('RemoteConsole.raw_input', prompt)
23
24    def write(self, msg):
25        return self.pipe.expect('RemoteConsole.write', msg)
26
27    def resetbuffer(self):
28        self.lastbuffer = self.buffer
29        InteractiveConsole.resetbuffer(self)
30
31    def displayhook(self, value):
32        if value is not None:
33            __builtin__._ = value
34        return self.pipe.expect('RemoteConsole.displayhook', value)
35
36    def excepthook(self, type, value, traceback):
37        return self.pipe.expect('RemoteConsole.excepthook', type, value, traceback)
38
39    def runcode(self, code):
40        try:
41            exec code in self.locals
42        except SystemExit:
43            raise
44        except:
45            self.showtraceback()
46        else:
47            if softspace(sys.stdout, 0):
48                print
49
50    def interact(self):
51        old_raw_input = __builtin__.raw_input
52        old_displayhook = sys.displayhook
53        old_excepthook = sys.excepthook
54        old_stdin = sys.stdin
55        old_stdout = sys.stdout
56        old_stderr = sys.stderr
57        old_help = __builtin__.help
58        old_quit = __builtin__.quit
59        __builtin__.raw_input = self.raw_input
60        __builtin__.help = "Close window to exit."
61        __builtin__.quit = "Close window to exit."
62        sys.displayhook = self.displayhook
63        sys.excepthook = self.excepthook
64        sys.stdin = self.pipe.stdin
65        sys.stdout = self.pipe.stdout
66        sys.stderr = self.pipe.stderr
67        try:
68            self.pipe.expect('RemoteConsole.initialize', repr(sys.version_info), sys.executable, os.getpid())
69            InteractiveConsole.interact(self)
70        finally:
71            __builtin__.raw_input = old_raw_input
72            __builtin__.help = old_help
73            __builtin__.quit = old_quit
74            sys.displayhook = old_displayhook
75            sys.excepthook = old_excepthook
76            sys.stdin = old_stdin
77            sys.stdout = old_stdout
78            sys.stderr = old_stderr
79
80    def recommendCompletionsFor(self, word):
81        parts = word.split('.')
82        if len(parts) > 1:
83            # has a . so it must be a module or class or something
84            # using eval, which shouldn't normally have side effects
85            # unless there's descriptors/metaclasses doing some nasty
86            # get magic
87            objname = '.'.join(parts[:-1])
88            try:
89                obj = eval(objname, self.locals)
90            except:
91                return None, 0
92            wordlower = parts[-1].lower()
93            if wordlower == '':
94                # they just punched in a dot, so list all attributes
95                # that don't look private or special
96                prefix = '.'.join(parts[-2:])
97                check = [
98                    (prefix+_method)
99                    for _method
100                    in dir(obj)
101                    if _method[:1] != '_' and _method.lower().startswith(wordlower)
102                ]
103            else:
104                # they started typing the method name
105                check = filter(lambda s:s.lower().startswith(wordlower), dir(obj))
106        else:
107            # no dots, must be in the normal namespaces.. no eval necessary
108            check = sets.Set(dir(__builtins__))
109            check.update(keyword.kwlist)
110            check.update(self.locals)
111            wordlower = parts[-1].lower()
112            check = filter(lambda s:s.lower().startswith(wordlower), check)
113        check.sort()
114        return check, 0
115