def pick_module(w, size, key):
    from os.path import splitext
    import sys

    def mod_exists(mod):
        if (not hasattr(mod, '__file__')):
            return False
        if (mod.__file__ is None):
            return False
        filename = mod.__file__
        (base, ext) = splitext(filename)
        ext = ext.lower()
        from os.path import exists
        if (ext == '.pyc'):
            return exists((base + '.py'))
        else:
            return (ext == '.py')
    new_mod_text = SelectableText('-- update me --')
    new_mod_entry = urwid.AttrMap(new_mod_text, None, 'focused selectable')

    def build_filtered_mod_list(filt_string=''):
        modules = sorted((name for (name, mod) in list(sys.modules.items()) if mod_exists(mod)))
        result = [urwid.AttrMap(SelectableText(mod), None, 'focused selectable') for mod in modules if (filt_string in mod)]
        new_mod_text.set_text(("<<< IMPORT MODULE '%s' >>>" % filt_string))
        result.append(new_mod_entry)
        return result

    def show_mod(mod):
        filename = self.debugger.canonic(mod.__file__)
        (base, ext) = splitext(filename)
        if (ext == '.pyc'):
            ext = '.py'
            filename = (base + '.py')
        self.set_source_code_provider(FileSourceCodeProvider(self.debugger, filename))
        self.source_list.set_focus(0)

    class FilterEdit(urwid.Edit):

        def keypress(self, size, key):
            result = urwid.Edit.keypress(self, size, key)
            if (result is None):
                mod_list[:] = build_filtered_mod_list(self.get_edit_text())
            return result
    filt_edit = FilterEdit([('label', 'Filter: ')], self.last_module_filter)
    mod_list = urwid.SimpleListWalker(build_filtered_mod_list(filt_edit.get_edit_text()))
    lb = urwid.ListBox(mod_list)
    w = urwid.Pile([('flow', urwid.AttrMap(filt_edit, 'value')), ('fixed', 1, urwid.SolidFill()), urwid.AttrMap(lb, 'selectable')])
    while True:
        result = self.dialog(w, [('OK', True), ('Cancel', False), ('Reload', 'reload')], title='Pick Module')
        self.last_module_filter = filt_edit.get_edit_text()
        if (result is True):
            (widget, pos) = lb.get_focus()
            if (widget is new_mod_entry):
                new_mod_name = filt_edit.get_edit_text()
                try:
                    __import__(str(new_mod_name))
                except Exception:
                    from traceback import format_exception
                    self.message("Could not import module '{}':\n\n{}".format(new_mod_name, ''.join(format_exception(*sys.exc_info()))), title='Import Error')
                else:
                    show_mod(__import__(str(new_mod_name)))
                    break
            else:
                show_mod(sys.modules[widget.base_widget.get_text()[0]])
                break
        elif (result is False):
            break
        elif (result == 'reload'):
            (widget, pos) = lb.get_focus()
            if (widget is not new_mod_entry):
                mod_name = widget.base_widget.get_text()[0]
                mod = sys.modules[mod_name]
                import importlib
                importlib.reload(mod)
                self.message(("'%s' was successfully reloaded." % mod_name))
                if (self.source_code_provider is not None):
                    self.source_code_provider.clear_cache()
                self.set_source_code_provider(self.source_code_provider, force_update=True)
                (_, pos) = self.stack_list._w.get_focus()
                self.debugger.set_frame_index(self.translate_ui_stack_index(pos))
def pick_module(w, size, key):
    from os.path import splitext
    import sys

    def mod_exists(mod):
        if (not hasattr(mod, '__file__')):
            return False
        if (mod.__file__ is None):
            return False
        filename = mod.__file__
        (base, ext) = splitext(filename)
        ext = ext.lower()
        from os.path import exists
        if (ext == '.pyc'):
            return exists((base + '.py'))
        else:
            return (ext == '.py')
    new_mod_text = SelectableText('-- update me --')
    new_mod_entry = urwid.AttrMap(new_mod_text, None, 'focused selectable')

    def build_filtered_mod_list(filt_string=):
        modules = sorted((name for (name, mod) in list(sys.modules.items()) if mod_exists(mod)))
        result = [urwid.AttrMap(SelectableText(mod), None, 'focused selectable') for mod in modules if (filt_string in mod)]
        new_mod_text.set_text(("<<< IMPORT MODULE '%s' >>>" % filt_string))
        result.append(new_mod_entry)
        return result

    def show_mod(mod):
        filename = self.debugger.canonic(mod.__file__)
        (base, ext) = splitext(filename)
        if (ext == '.pyc'):
            ext = '.py'
            filename = (base + '.py')
        self.set_source_code_provider(FileSourceCodeProvider(self.debugger, filename))
        self.source_list.set_focus(0)

    class FilterEdit(urwid.Edit):

        def keypress(self, size, key):
            result = urwid.Edit.keypress(self, size, key)
            if (result is None):
                mod_list[:] = build_filtered_mod_list(self.get_edit_text())
            return result
    filt_edit = FilterEdit([('label', 'Filter: ')], self.last_module_filter)
    mod_list = urwid.SimpleListWalker(build_filtered_mod_list(filt_edit.get_edit_text()))
    lb = urwid.ListBox(mod_list)
    w = urwid.Pile([('flow', urwid.AttrMap(filt_edit, 'value')), ('fixed', 1, urwid.SolidFill()), urwid.AttrMap(lb, 'selectable')])
    while True:
        result = self.dialog(w, [('OK', True), ('Cancel', False), ('Reload', 'reload')], title='Pick Module')
        self.last_module_filter = filt_edit.get_edit_text()
        if (result is True):
            (widget, pos) = lb.get_focus()
            if (widget is new_mod_entry):
                new_mod_name = filt_edit.get_edit_text()
                try:
                    __import__(str(new_mod_name))
                except Exception:
                    from traceback import format_exception
                    self.message("Could not import module '{}':\n\n{}".format(new_mod_name, .join(format_exception(*sys.exc_info()))), title='Import Error')
                else:
                    show_mod(__import__(str(new_mod_name)))
                    break
            else:
                show_mod(sys.modules[widget.base_widget.get_text()[0]])
                break
        elif (result is False):
            break
        elif (result == 'reload'):
            (widget, pos) = lb.get_focus()
            if (widget is not new_mod_entry):
                mod_name = widget.base_widget.get_text()[0]
                mod = sys.modules[mod_name]
                import importlib
                importlib.reload(mod)
                self.message(("'%s' was successfully reloaded." % mod_name))
                if (self.source_code_provider is not None):
                    self.source_code_provider.clear_cache()
                self.set_source_code_provider(self.source_code_provider, force_update=True)
                (_, pos) = self.stack_list._w.get_focus()
                self.debugger.set_frame_index(self.translate_ui_stack_index(pos))