def __make_note_ref(self, doc, docpath, bodycopy=False): '''Make note path: create and associate a note with current file and return it''' # this function is used in save-note methods: # we must have an associated note if we wish to save marks to memobook. # This function sets up the note so that we can perform the save. try: note = Note() note.title = os.path.basename(docpath) note.ID = docpath note.mime = NoteMime.TEXT docbody = doc.get_text(doc.get_start_iter(), doc.get_end_iter(), True) if bodycopy: note.body = docbody note.tags = Tag(parse.parse(docbody)) note.tags.silent = index_search(self.index, docpath, None) except Exception as e: dprint(1, "\nError: " + str(e)) return None else: self.__open_notes.append(note) return note
def __process_file(self, f): try: suffices = self.__ctrl["mime"]["Text"]["suff"] if isinstance(suffices, str): if str(f).rfind(suffices) < len(str(f)) - len(suffices) - 1: return else: success = False for suff in suffices: if str(f).rfind(suff) < len(str(f)) - len(suffices) - 1: continue else: success = True break if not success: return handle_text = "" with open(f, "r") as handle: for l in handle.readlines(): handle_text += l except Exception as e: wrapper = Exception("< " + str(f) + " > " + str(e)) self._error.append(wrapper) return return parse.parse(handle_text)
def __process_file(self, f): '''Helper function for populate: if file is text, load its marks''' dprint(3, "\nDatabaseBinding::__process_file") try: suffices = self.__ctrl["mime"]["Text"]["suff"] if isinstance(suffices, str): if str(f).rfind(suffices) < len(str(f)) - len(suffices) - 1: return else: success = False for suff in suffices: if str(f).rfind(suff) < len(str(f)) - len(suffices) - 1: continue else: success = True break if not success: return handle_text = "" with open(f, "r") as handle: for l in handle.readlines(): handle_text += l except Exception as e: wrapper = Exception("< " + str(f) + " > " + str(e)) self._error.append(wrapper) return marks = parse.parse(handle_text) dprint(4, " MARKS: " + ", ".join(marks)) for m in marks: self.__cursor.execute( '''SELECT mark,file FROM bookmarks WHERE mark=? AND file=? AND type=?''', (m, str(f), NoteMime.TEXT.value)) hit_list = self.__cursor.fetchall() if hit_list: continue else: self.__cursor.execute( '''INSERT INTO bookmarks(mark,file,type) VALUES(?,?,?)''', (m, str(f), NoteMime.TEXT.value)) return
def save_note(self, nt): '''Write a note to the source. Only text files are written; others silently fail.''' dprint(3, "\nDatabaseBinding::save_note") # this could be problematic for default/unknown mime loading. # ...if an unk type is displayed as text, why wouldn't the user expect # ...the write operation to succeed? if nt is None: e = Exception("save_note argument requires note object") self._error.append(e) return True if nt.mime is not NoteMime.TEXT: # silent fail: writing is not supported for images or pdfs return False ### compare/update marks ### nt.tags = Tag(parse.parse(nt.body)) self.__cursor.execute( '''select rowid,mark from bookmarks where file=?''', (nt.ID, )) db_hits = self.__cursor.fetchall() for item in db_hits: if not (item[1] in nt.tags): self.__cursor.execute( '''delete from bookmarks where rowid=?''', (item[0], )) self._src.commit() for tag in nt.tags: if not (tag in [item[1] for item in db_hits]): self.__cursor.execute( '''insert into bookmarks (mark,file,type) values (?,?,?)''', (tag, nt.ID, nt.mime.value)) self._src.commit() try: handle = open(nt.ID, "w") handle.write(nt.body) handle.close() except Exception as e: self._error.append(e) return True else: return False
def parse(self): '''Scan note body for bookmarks via parse function''' dprint(3, "\nNote::parse") for t in parse.parse(self.body): self.tags.append(t)