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 _run_move(self, parsed_kwargs, config): uuid = parsed_kwargs['uuid'] nb_name = parsed_kwargs['notebook'] try: NoteManager(config).move_note(uuid, nb_name) except StorageCheckException as ex: raise SafeExitException(str(ex))
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 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 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 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 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 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 _run_selected(self, parsed_kwargs, config): nb_name = parsed_kwargs['notebook'] try: NoteManager(config).move_note_from_selected(nb_name) except StorageCheckException as ex: raise SafeExitException(str(ex))
def edit_note(self, note, editor_name=None): from hashlib import md5 as func_md5 editor = select_editor(self.config, editor_name) old_utime = note.update_time old_content = note.content.to_format(str) hash_old = func_md5(old_content.encode()).hexdigest() fn_tmp = self.config.editor.fn_tempfile init_content = old_content + '\n'*2 + '^^^%s^^^' % str(note.tags) new_content = editor.open(fn_tmp=fn_tmp, init_content=init_content) # - parse title title = parse_title(new_content, self.config) # - auto parsing tags from note content tags, new_content = parse_tags(new_content, self.config) # - remove leading and trailing spaces again new_content = new_content.strip() try: if new_content == '': msg = 'Empty content detected, do you want to save this note?' if not query_yes_no(msg, default='no', back_n_lines=1): raise KeyboardInterrupt except KeyboardInterrupt: print() sys.exit(1) continue_editing_tags = False tags_changed = set(tags) != set(note.tags) if tags_changed: try: msg_tags = str(tags) if len(tags) > 0 else '(no tag)' print('Current tags: %s' % msg_tags) msg = ( 'Tags are modified, do you want to edit them? ' '(press ctrl+c to abort changes)' ) continue_editing_tags = query_yes_no(msg, default='yes', back_n_lines=1) except KeyboardInterrupt: print() pass if continue_editing_tags: try: raw_tags = editor.open(fn_tmp=fn_tmp, init_content=str(tags)) except UserCancelledException: sys.exit(1) try: tags = Tags.from_string_content(raw_tags) tags_changed = set(tags) != set(note.tags) except: print('Failed to parse new tags, fallback to orignal ones.') hash_new = func_md5(new_content.encode()).hexdigest() if hash_old == hash_new and not tags_changed: msg = 'Content and tags were not changed.' raise SafeExitException(msg) note.update_content(new_content) note.tags = tags return note