def create_new_file(self): selected_node_id = self.get_selected_node() if selected_node_id: selected_path = self.tree.set(selected_node_id, "path") selected_kind = self.tree.set(selected_node_id, "kind") if selected_kind == "dir": parent_path = selected_path else: parent_id = self.tree.parent(selected_node_id) parent_path = self.tree.set(parent_id, "path") else: parent_path = self.current_focus name = ask_string("File name", "Provide filename", initial_value="", master=self.winfo_toplevel()) if not name: return None path = self.join(parent_path, name) if name in self._cached_child_data[parent_path]: # TODO: ignore case in windows messagebox.showerror("Error", "The file '" + path + "' already exists", master=self) return self.create_new_file() else: self.open_file(path) return path
def mkdir(self): parent = self.get_selected_path() if parent is None: parent = self.current_focus else: if self.get_selected_kind() == "file": # dirname does the right thing even if parent is Linux path and runnning on Windows parent = os.path.dirname(parent) name = ask_string("New directory", "Enter name for new directory under\n%s" % parent) if not name or not name.strip(): return self.perform_mkdir(parent, name.strip()) self.refresh_tree()
def set_content_as_bytes(self, data, keep_undo=False): encoding = self.detect_encoding(data) while True: try: chars = data.decode(encoding) if self.looks_like_text(chars): self.set_content(chars, keep_undo) return True except UnicodeDecodeError: pass encoding = ask_string( "Bad encoding", "Could not read as %s text.\nYou could try another encoding" % encoding, initial_value=encoding, options=get_proposed_encodings(), master=self.winfo_toplevel(), ) if not encoding: return False