1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""AutoComplete.py - An IDLE extension for automatically completing names.
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThis extension can complete either attribute names of file names. It can pop
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepa window with all available names, for the user to select from.
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep"""
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport os
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport sys
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport string
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom idlelib.configHandler import idleConf
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# This string includes all chars that may be in a file name (without a path
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# separator)
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepFILENAME_CHARS = string.ascii_letters + string.digits + os.curdir + "._~#$:-"
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# This string includes all chars that may be in an identifier
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepID_CHARS = string.ascii_letters + string.digits + "_"
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# These constants represent the two different types of completions
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepCOMPLETE_ATTRIBUTES, COMPLETE_FILES = range(1, 2+1)
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom idlelib import AutoCompleteWindow
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom idlelib.HyperParser import HyperParser
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport __main__
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSEPS = os.sep
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif os.altsep:  # e.g. '/' on Windows...
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    SEPS += os.altsep
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass AutoComplete:
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    menudefs = [
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('edit', [
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            ("Show Completions", "<<force-open-completions>>"),
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ])
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ]
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    popupwait = idleConf.GetOption("extensions", "AutoComplete",
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                   "popupwait", type="int", default=0)
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, editwin=None):
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.editwin = editwin
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if editwin is None:  # subprocess and test
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.text = editwin.text
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.autocompletewindow = None
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # id of delayed call, and the index of the text insert when the delayed
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # call was issued. If _delayed_completion_id is None, there is no
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # delayed call.
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._delayed_completion_id = None
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._delayed_completion_index = None
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def _make_autocomplete_window(self):
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return AutoCompleteWindow.AutoCompleteWindow(self.text)
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def _remove_autocomplete_window(self, event=None):
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if self.autocompletewindow:
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.autocompletewindow.hide_window()
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.autocompletewindow = None
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def force_open_completions_event(self, event):
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """Happens when the user really wants to open a completion list, even
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if a function call is needed.
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.open_completions(True, False, True)
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def try_open_completions_event(self, event):
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """Happens when it would be nice to open a completion list, but not
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        really necessary, for example after an dot, so function
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        calls won't be made.
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        lastchar = self.text.get("insert-1c")
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if lastchar == ".":
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._open_completions_later(False, False, False,
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                         COMPLETE_ATTRIBUTES)
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        elif lastchar in SEPS:
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._open_completions_later(False, False, False,
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                         COMPLETE_FILES)
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def autocomplete_event(self, event):
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """Happens when the user wants to complete his word, and if necessary,
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        open a completion list after that (if there is more than one
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        completion)
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if hasattr(event, "mc_state") and event.mc_state:
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # A modifier was pressed along with the tab, continue as usual.
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if self.autocompletewindow and self.autocompletewindow.is_active():
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.autocompletewindow.complete()
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return "break"
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            opened = self.open_completions(False, True, True)
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if opened:
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return "break"
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def _open_completions_later(self, *args):
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._delayed_completion_index = self.text.index("insert")
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if self._delayed_completion_id is not None:
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.text.after_cancel(self._delayed_completion_id)
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._delayed_completion_id = \
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.text.after(self.popupwait, self._delayed_open_completions,
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            *args)
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def _delayed_open_completions(self, *args):
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self._delayed_completion_id = None
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if self.text.index("insert") != self._delayed_completion_index:
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.open_completions(*args)
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def open_completions(self, evalfuncs, complete, userWantsWin, mode=None):
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """Find the completions and create the AutoCompleteWindow.
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        Return True if successful (no syntax error or so found).
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if complete is True, then if there's nothing to complete and no
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        start of completion, won't open completions and return False.
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        If mode is given, will open a completion list only in this mode.
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Cancel another delayed call, if it exists.
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if self._delayed_completion_id is not None:
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.text.after_cancel(self._delayed_completion_id)
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._delayed_completion_id = None
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        hp = HyperParser(self.editwin, "insert")
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        curline = self.text.get("insert linestart", "insert")
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        i = j = len(curline)
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if hp.is_in_string() and (not mode or mode==COMPLETE_FILES):
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._remove_autocomplete_window()
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            mode = COMPLETE_FILES
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            while i and curline[i-1] in FILENAME_CHARS:
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                i -= 1
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            comp_start = curline[i:j]
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            j = i
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            while i and curline[i-1] in FILENAME_CHARS + SEPS:
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                i -= 1
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            comp_what = curline[i:j]
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        elif hp.is_in_code() and (not mode or mode==COMPLETE_ATTRIBUTES):
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self._remove_autocomplete_window()
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            mode = COMPLETE_ATTRIBUTES
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            while i and curline[i-1] in ID_CHARS:
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                i -= 1
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            comp_start = curline[i:j]
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if i and curline[i-1] == '.':
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                hp.set_index("insert-%dc" % (len(curline)-(i-1)))
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                comp_what = hp.get_expression()
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if not comp_what or \
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                   (not evalfuncs and comp_what.find('(') != -1):
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return
148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                comp_what = ""
150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if complete and not comp_what and not comp_start:
154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        comp_lists = self.fetch_completions(comp_what, mode)
156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if not comp_lists[0]:
157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return
158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.autocompletewindow = self._make_autocomplete_window()
159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.autocompletewindow.show_window(comp_lists,
160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                            "insert-%dc" % len(comp_start),
161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                            complete,
162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                            mode,
163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                            userWantsWin)
164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return True
165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def fetch_completions(self, what, mode):
167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """Return a pair of lists of completions for something. The first list
168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        is a sublist of the second. Both are sorted.
169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        If there is a Python subprocess, get the comp. list there.  Otherwise,
171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        either fetch_completions() is running in the subprocess itself or it
172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        was called in an IDLE EditorWindow before any script had been run.
173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        The subprocess environment is that of the most recently run script.  If
175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        two unrelated modules are being edited some calltips in the current
176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        module may be inoperative if the module was not the last to run.
177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            rpcclt = self.editwin.flist.pyshell.interp.rpcclt
180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except:
181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            rpcclt = None
182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if rpcclt:
183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return rpcclt.remotecall("exec", "get_the_completion_list",
184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                     (what, mode), {})
185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if mode == COMPLETE_ATTRIBUTES:
187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if what == "":
188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    namespace = __main__.__dict__.copy()
189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    namespace.update(__main__.__builtins__.__dict__)
190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    bigl = eval("dir()", namespace)
191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    bigl.sort()
192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if "__all__" in bigl:
193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        smalll = sorted(eval("__all__", namespace))
194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    else:
195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        smalll = [s for s in bigl if s[:1] != '_']
196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    try:
198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        entity = self.get_entity(what)
199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        bigl = dir(entity)
200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        bigl.sort()
201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        if "__all__" in bigl:
202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            smalll = sorted(entity.__all__)
203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        else:
204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            smalll = [s for s in bigl if s[:1] != '_']
205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    except:
206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return [], []
207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            elif mode == COMPLETE_FILES:
209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if what == "":
210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    what = "."
211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                try:
212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    expandedpath = os.path.expanduser(what)
213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    bigl = os.listdir(expandedpath)
214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    bigl.sort()
215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    smalll = [s for s in bigl if s[:1] != '.']
216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                except OSError:
217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return [], []
218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if not smalll:
220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                smalll = bigl
221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return smalll, bigl
222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def get_entity(self, name):
224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """Lookup name in a namespace spanning sys.modules and __main.dict__"""
225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        namespace = sys.modules.copy()
226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        namespace.update(__main__.__dict__)
227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return eval(name, namespace)
228