def saveNote(self, note_content, use_current_time = True): """ Save XML and create a git commit. """ # Save XML. xml = ElementTree.Element("note") note_content.contentToXML(xml, use_current_time) misc.pSaveXML(self.fullname, xml) # git commit. misc.gitAddCommit(self.notebook.getDirectory(), self.filename, "update " + self.name) # Append current version to the list of versions. self.versions.append(misc.gitGetLastCommitId(self.notebook.getDirectory())) self.setText(self.name + " (" + str(len(self.versions)) +")") # Check if the keywords have changed. self.checkKeywords(note_content) # For now, keywords are always from the most recently saved # version of the note. This may need improvement down the road? self.keywords = list(note_content.getKeywords()) self.notebook.setUnpushed()
def rename(self, new_name): self.name = str(new_name) self.setText(new_name) # Update the XML and commit. xml = ElementTree.Element("notebook") name_xml = ElementTree.SubElement(xml, "name") name_xml.text = self.name misc.pSaveXML(self.directory + "notebook.xml", xml) misc.gitAddCommit(self.directory, self.directory + "notebook.xml", "rename notebook.") self.setUnpushed()
def moveNote(self, new_notebook): """ Notebooks are just directories, so update the filename accordingly. new_notebook is a NotebookStandardItem. """ # Load all versions of the note in the old notebook # and all of the (unique) attachments. attachments = {} note_contents = [] for version in self.versions: note_content = self.loadNoteContent(version) note_contents.append(note_content) for fullname in note_content.getAttachments(): attachments[fullname] = True # Move attachments. old_dir = self.notebook.getDirectory() new_dir = new_notebook.getDirectory() for fullname in attachments.keys(): filename = os.path.basename(fullname) os.makedirs(new_dir + os.path.dirname(fullname)) shutil.copy(old_dir + fullname, new_dir + fullname) misc.gitRemove(old_dir, fullname, "remove attachment " + filename) misc.gitAddCommit(new_dir, fullname, "attachment " + filename) # Delete old note. self.deleteNote() # Move to new notebook. # FIXME: If this gets move to a new notebook, then back to the old # notebook it will appear as completely new note. However if we # don't do this then the version history of the note will get all # messed up. self.notebook = new_notebook self.filename = "note_" + str(uuid.uuid1()) + ".xml" self.fullname = self.notebook.getDirectory() + self.filename self.versions = [] # Replay history in the new notebook. for content in note_contents: self.saveNote(content, use_current_time = False) self.notebook.setUnpushed()
def createWithFile(self, directory, a_file): """ This is called to create a new attachment from a file. """ self.directory = directory self.filename = os.path.basename(a_file) self.fullname = "attach_" + str(uuid.uuid1()) + "/" os.makedirs(self.directory + self.fullname) self.fullname += self.filename shutil.copy(a_file, self.directory + self.fullname) self.setText(self.filename) misc.gitAddCommit(directory, self.fullname, "attachment " + self.filename) self.imageCheck()
def createWithName(self, notebook_name, username, email): self.name = notebook_name self.uuid = str(uuid.uuid1()) self.setText(self.name) self.directory += self.uuid os.makedirs(self.directory) xml = ElementTree.Element("notebook") name_xml = ElementTree.SubElement(xml, "name") name_xml.text = self.name self.directory += "/" misc.pSaveXML(self.directory + "notebook.xml", xml) # Create a new git repository for this notebook. misc.gitInit(self.directory, username, email) # Commit the notebook name. misc.gitAddCommit(self.directory, self.directory + "notebook.xml", "add notebook.") self.has_unpushed = True self.setToolTip(self.directory)