def __addUndo(self, items): self.__undo.append([]) for item in items: if isinstance(item, Menu): file_path = item.path elif isinstance(item, tuple): if item[0] == 'Item': file_path = os.path.join(util.getUserItemPath(), item[1]) if not os.path.isfile(file_path): file_path = util.getItemPath(item[1]) elif item[0] == 'Menu': file_path = os.path.join(util.getUserDirectoryPath(), item[1]) if not os.path.isfile(file_path): file_path = util.getDirectoryPath(item[1]) else: continue elif isinstance(item, MateMenu.TreeDirectory): if item.get_desktop_file_path() is None: continue file_path = os.path.join(util.getUserDirectoryPath(), os.path.split(item.get_desktop_file_path())[1]) if not os.path.isfile(file_path): file_path = item.get_desktop_file_path() elif isinstance(item, MateMenu.TreeEntry): file_path = os.path.join(util.getUserItemPath(), item.get_desktop_file_id()) if not os.path.isfile(file_path): file_path = item.get_desktop_file_path() else: continue with codecs.open(file_path, 'r', 'utf-8') as f: data = f.read() undo_path = util.getUniqueUndoFile(file_path) with codecs.open(undo_path, 'w', 'utf-8') as f: f.write(data) self.__undo[-1].append(undo_path)
def __addUndo(self, items): self.__undo.append([]) for item in items: if isinstance(item, Menu): file_path = item.path elif isinstance(item, tuple): if item[0] == 'Item': file_path = os.path.join(util.getUserItemPath(), item[1]) if not os.path.isfile(file_path): file_path = util.getItemPath(item[1]) elif item[0] == 'Menu': file_path = os.path.join(util.getUserDirectoryPath(), item[1]) if not os.path.isfile(file_path): file_path = util.getDirectoryPath(item[1]) else: continue elif isinstance(item, MateMenu.TreeDirectory): if item.get_desktop_file_path() is None: continue file_path = os.path.join(util.getUserDirectoryPath(), os.path.split(item.get_desktop_file_path())[1]) if not os.path.isfile(file_path): file_path = item.get_desktop_file_path() elif isinstance(item, MateMenu.TreeEntry): file_path = os.path.join(util.getUserItemPath(), item.get_desktop_file_id()) if not os.path.isfile(file_path): file_path = item.get_desktop_file_path() else: continue with codecs.open(file_path, 'r', 'utf-8') as f: data = f.read() undo_path = util.getUniqueUndoFile(file_path) with codecs.open(undo_path, 'w', 'utf-8') as f: f.write(data) self.__undo[-1].append(undo_path) self.update_undo_redo_button_state()
def redo(self): if len(self.__redo) == 0: return files = self.__redo.pop() undo = [] for file_path in files: new_path = file_path.rsplit('.', 1)[0] undo_path = util.getUniqueUndoFile(new_path) f_file_path = codecs.open(new_path, 'r', 'utf-8') f_new_path = codecs.open(new_path, 'rw', 'utf-8') f_undo_path = codecs.open(undo_path, 'rw', 'utf-8') data = f_new_path.read() f_undo_path.write(data) data = f_file_path.read() f_new_path.write(data) os.unlink(file_path) undo.append(undo_path) f_file_path.close() f_new_path.close() f_undo_path.close() #reload DOM to make changes stick for name in ('applications', 'settings'): menu = getattr(self, name) try: menu.dom = xml.dom.minidom.parse(menu.path) except (IOError, xml.parsers.expat.ExpatError): menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) util.removeWhitespaceNodes(menu.dom) self.__undo.append(undo)
def __addUndo(self, items): self.__undo.append([]) for item in items: if isinstance(item, Menu): file_path = item.path elif isinstance(item, tuple): if item[0] == 'Item': file_path = os.path.join(util.getUserItemPath(), item[1]) if not os.path.isfile(file_path): file_path = util.getItemPath(item[1]) elif item[0] == 'Menu': file_path = os.path.join(util.getUserDirectoryPath(), item[1]) if not os.path.isfile(file_path): file_path = util.getDirectoryPath(item[1]) else: continue elif item.get_type() == matemenu.TYPE_DIRECTORY: if item.get_desktop_file_path() == None: continue file_path = os.path.join(util.getUserDirectoryPath(), os.path.split(item.get_desktop_file_path())[1]) if not os.path.isfile(file_path): file_path = item.get_desktop_file_path() elif item.get_type() == matemenu.TYPE_ENTRY: file_path = os.path.join(util.getUserItemPath(), item.get_desktop_file_id()) if not os.path.isfile(file_path): file_path = item.get_desktop_file_path() else: continue data = open(file_path).read() undo_path = util.getUniqueUndoFile(file_path) open(undo_path, 'w').write(data) self.__undo[-1].append(undo_path)
def redo(self): if len(self.__redo) == 0: return files = self.__redo.pop() undo = [] for redo_path in files[::-1]: new_path = redo_path.rsplit('.', 1)[0] if not os.path.exists(redo_path): continue undo_path = util.getUniqueUndoFile(new_path) # create undo file try: with codecs.open(new_path, 'r', 'utf-8') as f_new: with codecs.open(undo_path, 'w', 'utf-8') as f_undo: f_undo.write(f_new.read()) undo.append(undo_path) except FileNotFoundError: pass # restore redo file try: with codecs.open(redo_path, 'r', 'utf-8') as f_redo: with codecs.open(new_path, 'w', 'utf-8') as f_new: f_new.write(f_redo.read()) os.unlink(redo_path) except FileNotFoundError: pass #reload DOM to make changes stick for name in ('applications', 'settings'): menu = getattr(self, name) try: menu.dom = xml.dom.minidom.parse(menu.path) except (IOError, xml.parsers.expat.ExpatError): menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) util.removeWhitespaceNodes(menu.dom) if undo: self.__undo.append(undo) self.update_undo_redo_button_state()
def redo(self): if len(self.__redo) == 0: return files = self.__redo.pop() undo = [] for file_path in files: new_path = file_path.rsplit('.', 1)[0] undo_path = util.getUniqueUndoFile(new_path) data = open(new_path).read() open(undo_path, 'w').write(data) data = open(file_path).read() open(new_path, 'w').write(data) os.unlink(file_path) undo.append(undo_path) #reload DOM to make changes stick for name in ('applications', 'settings'): menu = getattr(self, name) if not os.path.isfile(menu.path): menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) else: menu.dom = xml.dom.minidom.parse(menu.path) self.__remove_whilespace_nodes(menu.dom) self.__undo.append(undo)
def __addUndo(self, items): self.__undo.append([]) for item in items: if isinstance(item, Menu): file_path = item.path elif isinstance(item, tuple): if item[0] == 'Item': file_path = os.path.join(util.getUserItemPath(), item[1]) if not os.path.isfile(file_path): file_path = util.getItemPath(item[1]) elif item[0] == 'Menu': file_path = os.path.join(util.getUserDirectoryPath(), item[1]) if not os.path.isfile(file_path): file_path = util.getDirectoryPath(item[1]) else: continue elif item.get_type() == matemenu.TYPE_DIRECTORY: if item.get_desktop_file_path() == None: continue file_path = os.path.join( util.getUserDirectoryPath(), os.path.split(item.get_desktop_file_path())[1]) if not os.path.isfile(file_path): file_path = item.get_desktop_file_path() elif item.get_type() == matemenu.TYPE_ENTRY: file_path = os.path.join(util.getUserItemPath(), item.get_desktop_file_id()) if not os.path.isfile(file_path): file_path = item.get_desktop_file_path() else: continue data = open(file_path).read() undo_path = util.getUniqueUndoFile(file_path) open(undo_path, 'w').write(data) self.__undo[-1].append(undo_path)
def redo(self): if len(self.__redo) == 0: return files = self.__redo.pop() undo = [] for file_path in files: new_path = file_path.rsplit('.', 1)[0] undo_path = util.getUniqueUndoFile(new_path) data = open(new_path).read() open(undo_path, 'w').write(data) data = open(file_path).read() open(new_path, 'w').write(data) os.unlink(file_path) undo.append(undo_path) #reload DOM to make changes stick for name in ('applications', 'settings'): menu = getattr(self, name) if not os.path.isfile(menu.path): menu.dom = xml.dom.minidom.parseString( util.getUserMenuXml(menu.tree)) else: menu.dom = xml.dom.minidom.parse(menu.path) self.__remove_whilespace_nodes(menu.dom) self.__undo.append(undo)