Beispiel #1
0
    def addfolder(self,
                  dirpath=None,
                  parent="",
                  watch=True,
                  is_rootfolder=False,
                  exec_file=True):
        dirpath = dirpath or filedialog.askdirectory(initialdir=".")
        if not dirpath: return

        name = os.path.basename(dirpath)
        parent = "" if parent == self.rootfolder else parent

        if is_rootfolder:
            self.rootfolder = dirpath
        else:
            parent = self.insert(parent,
                                 "end",
                                 dirpath,
                                 text="      " + name,
                                 open=(not parent),
                                 image=getimage("folder"),
                                 tag="folder" if not parent else "",
                                 values=dirpath)

        items = [(i, os.path.isdir(os.path.join(dirpath, i)))
                 for i in os.listdir(dirpath)]
        items.sort()
        for name, is_dir in items:
            if is_dir and name not in EXCLUDED_DIR:
                self.addfolder(os.path.join(dirpath, name),
                               parent,
                               watch=False,
                               exec_file=exec_file)

        for name, is_dir in items:
            if not is_dir and name not in EXCLUDED_FILES:
                fullpath = os.path.join(dirpath, name)
                self.addfile(fullpath,
                             parent,
                             watch=False,
                             exec_file=exec_file)

        if watch:
            if not self.observer:
                self.observer = Observer()
                self.observer.start()

            self.watchers[name] = self.observer.schedule(FileHandler(
                self, dirpath=dirpath),
                                                         path=dirpath,
                                                         recursive=True)
Beispiel #2
0
    def setmodule(self, name, module, parent=None, file=None):
        if not parent:
            if self.exists(module.__name__):
                self.delete(module.__name__)
            self.store[name] = module
            parent = self.insert("",
                                 'end',
                                 module.__name__,
                                 text="      " + name,
                                 values=name,
                                 image=getimage(".py"),
                                 open=True,
                                 tags="file" if file else "module")

        if len(self.store) == 1:
            self.app.menu.setview("modules", True)

        functions = []
        builtin_fn = []
        classes = []
        constants = []
        submodules = []
        for fn in dir(module):
            try:
                attr = getattr(module, fn)
            except:
                fn = "_" + fn

            if fn[:1] != "_":
                if inspect.isclass(attr):
                    classes.append(fn)
                elif inspect.isfunction(attr):
                    functions.append(fn)
                elif callable(attr):
                    builtin_fn.append(fn)
                elif inspect.ismodule(attr):
                    if attr.__name__ not in EXCLUDED_MODULES:  # + BUILTIN_PKGS + INSTALLED_PKGS:
                        submodules.append((fn, attr))
                else:
                    constants.append(fn)

        if builtin_fn:
            folder = self.insert(parent, "end", text="builtins")
            for i in builtin_fn:
                self.insert(folder,
                            "end",
                            text=i,
                            values=module.__name__ + "." + i)

        if functions:
            folder = self.insert(parent, "end", text="functions")
            for i in functions:
                self.insert(folder,
                            "end",
                            text=i,
                            values=module.__name__ + "." + i)

        if classes:
            folder = self.insert(parent, "end", text="classes")
            for j in classes:
                self.insert(folder,
                            "end",
                            text=j,
                            values=module.__name__ + "." + j)

        if constants:
            folder = self.insert(parent, "end", text="objects")
            for k in constants:
                if not re.match(r"^[A-Z_]*$", k):
                    self.insert(folder,
                                "end",
                                text=k,
                                values=module.__name__ + "." + k)

        if submodules:
            for fn, attr in submodules:
                if not self.exists(attr.__name__):
                    folder = self.insert(parent,
                                         "end",
                                         attr.__name__,
                                         text="      " + fn,
                                         values=attr.__name__,
                                         image=getimage(".py"))
                    self.setmodule(attr.__name__, attr, folder)
        return False
Beispiel #3
0
 def disable_file(self, key):
     self.item(key, image=getimage("python-disabled"))
     self.add_tag(key, "disabled")
     children = self.get_children(key)
     for i in children:
         self.delete(i)
Beispiel #4
0
    def addfile(self,
                file=None,
                parent="",
                index="end",
                watch=True,
                is_open=True,
                exec_file=True):
        file = file or filedialog.askopenfilename()
        if not file: return

        name, ext = name_ext(file)
        if os.path.basename(file) in EXCLUDED_FILES or ext in EXCLUDED_EXT:
            return

        if self.exists(name):
            self.delete(name, del_objs=False)

        parent = self.insert(
            parent if (self.rootfolder and parent != self.rootfolder) else "",
            index,
            name,
            text="      " + name,
            image=getimage(ext),
            tags="local" if parent else ("local", "file"),
            open=is_open,
            values=file)

        if ext != ".py": return

        if name in sys.modules:
            del sys.modules[name]

        if watch:
            if not self.observer:
                self.observer = Observer()
                self.observer.start()

            self.watchers[name] = self.observer.schedule(
                FileHandler(self, file=file),
                path=os.path.dirname(file),
                recursive=False)

        expanded = self.expanded()
        spec = importlib.util.spec_from_file_location(name, file)
        module = importlib.util.module_from_spec(spec)
        try:
            spec.loader.exec_module(module)
        except Exception as err:
            traceback.print_exc()
            self.disable_file(parent)
            return

        if exec_file:
            for i in dir(module):
                attr = getattr(module, i)
                if not inspect.ismodule(
                        attr
                ) and i in self.app.objects and i not in self.locals[name]:
                    self.disable_file(parent)
                    print(
                        Exception("ImportError: object with name " + i +
                                  " already exists.\n  File \"" + file +
                                  "\", line 1, in <module>"))
                    builtin_print("\a")
                    return

        if name not in self.store:
            self.store[name] = module

        self.setmodule(name, module, parent, file)
        self.expanded(expanded)

        if exec_file:
            prev = self.app.objects.store.copy()
            source = open(file, "r").read()
            try:
                self.app.console.exec(source, file)
            except:
                print("TODO - exec failed")
                return

            new = self.app.objects.store.copy()

            if name in self.locals:
                self.locals[name].update(
                    {k: new[k]
                     for k in set(new) - set(prev)})
            else:
                self.locals[name] = {k: new[k] for k in set(new) - set(prev)}

            objects = [i for i in dir(module)]
            keys = list(self.locals[name].keys())
            for i in keys:
                if i not in objects:
                    del self.locals[name][i]
                    del self.app.objects[i]
Beispiel #5
0
    def show(self, obj, clear_nav=True, display_only=False):
        self.obj = obj

        if display_only:
            pass
        elif clear_nav:
            name = self.getname(obj)
            self.nav.delete("1.0", "end")
            self.nav.insert("end", name, ("root", "basepath"))
            self.rootmodule = self.obj
            self.nav_count = 0
            self.objects.clear()
        else:
            name = self.getname(obj).split(".")[-1]
            tag_ranges = self.nav.tag_ranges("subpath")
            if tag_ranges:
                self.nav.delete(*tag_ranges)
            self.nav.insert("end", " > ",
                            ("blue", "nav_count=" + str(self.nav_count)))
            self.nav.insert("end", name,
                            ("module_nav", "basepath",
                             "nav_count=" + str(len(self.objects))))
            self.objects.append(obj)

        self.clear()

        if isinstance(obj, str) and os.path.isfile(obj):
            content = open(obj).read()
            show_textfile(self.text, content)
            return

        for i in dir(self.obj):
            attr = getattr(self.obj, i)
            if i[0] == "_":
                pass
            elif inspect.isclass(attr):
                self.classes[i] = attr
            elif inspect.ismodule(
                    attr
            ):  # and i not in INSTALLED_PKGS + BUILTIN_PKGS + EXCLUDED_MODULES + ["os", "sys"]:
                self.submodules[i] = attr
            elif inspect.isfunction(attr):
                self.functions[i] = attr
            elif callable(attr):
                self.builtins[i] = attr
            else:
                pass

        if self.classes:
            classes = self.tree.insert("", "end", text="classes", open=True)
            for k in self.classes:
                temp = self.tree.insert(classes,
                                        "end",
                                        k,
                                        text=k,
                                        tags="class")

        if self.builtins:
            if inspect.isclass(self.obj):
                parent = self.tree.insert("", "end", text="methods", open=True)
            else:
                parent = self.tree.insert("",
                                          "end",
                                          text="builtins",
                                          open=True)
            for j in self.builtins:
                self.tree.insert(parent, "end", j, text=j)

        if self.functions:
            functions = self.tree.insert("",
                                         "end",
                                         text="functions",
                                         open=True)
            for j in self.functions:
                self.tree.insert(functions, "end", j, text=j, open=True)

        if self.submodules:
            for i in self.submodules:
                self.tree.insert("",
                                 "end",
                                 i,
                                 image=getimage(".py"),
                                 text="      " + i,
                                 tags="submodule")

        if self.has_sidebar:
            if not self.builtins and not self.functions and not self.classes and not self.submodules:
                if self.paned_window.winfo_width() > 1:
                    self.paned_window.sashpos(0, 0)
                else:
                    self.paned_window.bind(
                        "<Configure>",
                        lambda event: self.paned_window.sashpos(0, 0))
            elif self.paned_window.sashpos(0) < 20:
                self.paned_window.sashpos(0, 220)

        self.display_doc(obj)