def remove_note(self, uuid): storer = get_storer(self.config) if uuid is None: # Enter interactive mode and let user select note from current notebook nb_name = HEAD.get() notes = storer.get_notes_from_notebook(nb_name, n_limit=None) try: selected_notes = NotebookOperator(self.config).select_notes( notes, multiple=False, show_date=True, show_uuid=True, clear_after_exit=True, ) assert len(selected_notes) == 1 note = selected_notes[0] except UserCancelledException: raise SafeExitException() else: # Check whether specific note exists note = storer.get_note(uuid) n_removed = storer.remove_note_by_uuid(note.uuid) msg = '%s note%s ha%s been removed to trash can.' % ( n_removed, 's' if n_removed > 1 else '', 've' if n_removed > 1 else 's') print(msg)
def show_note(self, uuid): storer = get_storer(self.config) if uuid is None: # Enter interactive mode and let user select note from current notebook nb_name = HEAD.get() notes = storer.get_notes_from_notebook(nb_name, n_limit=None) try: selected_notes = NotebookOperator(self.config).select_notes( notes, multiple=False, show_date=True, show_uuid=True, clear_after_exit=True, ) assert len(selected_notes) == 1 note = selected_notes[0] except UserCancelledException: raise SafeExitException() else: note = storer.get_note(uuid) tw_config = {'max_lines': None} # show all content utils_show_notes([note], self.config, tw_config)
def edit_note(self, uuid, editor_name=None): storer = get_storer(self.config) if uuid is None: # Enter interactive mode and let user select note from current notebook nb_name = HEAD.get() notes = storer.get_notes_from_notebook(nb_name, n_limit=None) try: selected_notes = NotebookOperator(self.config).select_notes( notes, multiple=False, show_date=True, show_uuid=True, clear_after_exit=True, ) assert len(selected_notes) == 1 note = selected_notes[0] except UserCancelledException: raise SafeExitException() else: note = storer.get_note(uuid) edited_note = NoteOperator(self.config).edit_note( note, editor_name=editor_name) storer.update_note(note)
def list_tags(self): storer = get_storer(self.config) tags, counts = storer.get_all_tags_with_count() lines = [ '%4s %s' % (count, tag) for (count, tag) in zip(counts, tags) ] msg = '\n'.join(lines) print(msg)
def search_note_by_tags(self, raw_tags): n_desired_tags = len(raw_tags.split(',')) tags = Tags.from_string_content(raw_tags) if len(tags) != n_desired_tags: msg = 'Tags should be in the format of "#tag_name_1, #tag_name_2", ...' raise SafeExitException(msg) storer = get_storer(self.config) notes = storer.get_notes_by_tags(tags) tw_config = {'max_lines': 3} utils_show_notes(notes, self.config, tw_config)
def rename_tag(self, old_name, new_name): storer = get_storer(self.config) if not storer.check_tag_exist(old_name): msg = 'Tag "%s" does not exist, so that we cannot rename it' % old_name raise SafeExitException(msg) if storer.check_tag_exist(new_name): msg = 'Tag "%s" already exist, please choose another name' % new_name raise SafeExitException(msg) storer.rename_tag(old_name, new_name) msg = 'Tag "%s" has been renamed "%s"' % (old_name, new_name) print(msg)
def remove_note_from_selected(self): storer = get_storer(self.config) uuids = CachedNoteUUIDs.get() if len(uuids) == 0: raise SafeExitException('No selected note.') n_removed = storer.remove_note_by_uuid(uuids) msg = '%s note%s ha%s been removed to trash can.' % ( n_removed, 's' if n_removed > 1 else '', 've' if n_removed > 1 else 's') print(msg)
def show_note_from_selected(self): uuids = CachedNoteUUIDs.get() if len(uuids) == 0: raise SafeExitException('No selected note.') if len(uuids) > 1: # TODO: enter interactive mode raise NotImplementedError else: uuid = uuids[0] storer = get_storer(self.config) note = storer.get_note(uuid) tw_config = {'max_lines': None} # show all content utils_show_notes([note], self.config, tw_config)
def clear_empty_tags(self): """Clear those tags which no notes are tagged by.""" storer = get_storer(self.config) tags, counts = storer.get_all_tags_with_count() tags_to_remove = [tags[i] for i, v in enumerate(counts) if v == 0] try: TagOperator(self.config).confirm_to_remove_tags(tags_to_remove) except KeyboardInterrupt as ex: raise SafeExitException() from ex n_deleted = storer.delete_tags_by_name(tags_to_remove) msg = '%s tag%s ha%s been deleted.' % (n_deleted, 's' if n_deleted > 1 else '', 've' if n_deleted > 1 else 's') print(msg)
def create_note(self, raw_title, raw_content, raw_tags): nb_name = HEAD.get() storer = get_storer(self.config) if not storer.check_notebook_exist(nb_name): msg = 'Notebook `%s` does not exist' % nb_name raise StorageCheckException(msg) if raw_content is None: note = NoteOperator(self.config).create_note() else: tags = [] if raw_tags is None else Tags.from_string_content( raw_tags) note = Note.create(raw_title, raw_content, tags) storer.create_note(note, nb_name)
def edit_note_from_selected(self, editor_name=None): uuids = CachedNoteUUIDs.get() if len(uuids) == 0: raise SafeExitException('No selected note.') if len(uuids) > 1: # TODO: enter interactive mode raise NotImplementedError else: uuid = uuids[0] storer = get_storer(self.config) note = storer.get_note(uuid) edited_note = NoteOperator(self.config).edit_note( note, editor_name=editor_name) storer.update_note(note)
def move_note_from_selected(self, nb_name): # TODO: update update_time of notebook uuids = CachedNoteUUIDs.get() if len(uuids) == 0: raise SafeExitException('No selected note.') storer = get_storer(self.config) if not storer.check_notebook_exist(nb_name): msg = 'Notebook `%s` does not exist' % nb_name raise StorageCheckException(msg) n_moved = storer.move_note_by_uuid(uuids, nb_name) msg = '%s note%s ha%s been moved to notebook "%s".' % ( n_moved, 's' if n_moved > 1 else '', 've' if n_moved > 1 else 's', nb_name) print(msg)
def search_note_by_content(self, pattern_content): storer = get_storer(self.config) notes = storer.get_notes_by_content(pattern_content) tw_config = {'max_lines': 3} utils_show_notes(notes, self.config, tw_config)