def func_wrapper(self, *args, **kwargs): answer = user_input.UserInput() if self.interactive: print 'Are you sure?' answer.ask_yes_or_no() if answer.state == 'yes': return func(self, *args, **kwargs) else: pass else: return func(self, *args, **kwargs)
def update(self): """ Update info about database and current items in trash :return: """ try: verification.check_for_trash_files(self.arr_json_files, self.path_of_trash) except my_exceptions.TrashSetError as e: self.rootLogger.error('Error: Unknown %s' % termcolor.colored(e.get_list(), 'green')) answer = user_input.UserInput() list_of_elems = e.get_list() for elem in list_of_elems[:]: self.delete_for_name_from_trash(list_of_elems) except my_exceptions.DatabaseSetError as e: list_of_elems = e.get_list() for elem in list_of_elems: for index, json_dict in enumerate(self.arr_json_files): if elem == str(json_dict['hash']): self.arr_json_files.remove(self.arr_json_files[index]) serialization.push_json(self.arr_json_files, self.database)
def gui_frame_keypress(self, size, key): lb = self.gui_body_get() if key == self.config.get_keybind('quit'): self.gui_switch_frame_body(None) elif key == self.config.get_keybind('help'): self.gui_switch_frame_body(self.view_help) elif key == self.config.get_keybind('sync'): self.ndb.last_sync = 0 self.ndb.sync_worker_go() elif key == self.config.get_keybind('view_log'): self.view_log.update_log() self.gui_switch_frame_body(self.view_log) elif key == self.config.get_keybind('down'): if len(lb.body.positions()) <= 0: return None last = len(lb.body.positions()) if lb.focus_position == (last - 1): return None lb.focus_position += 1 lb.render(size) elif key == self.config.get_keybind('up'): if len(lb.body.positions()) <= 0: return None if lb.focus_position == 0: return None lb.focus_position -= 1 lb.render(size) elif key == self.config.get_keybind('page_down'): if len(lb.body.positions()) <= 0: return None last = len(lb.body.positions()) next_focus = lb.focus_position + size[1] if next_focus >= last: next_focus = last - 1 lb.change_focus(size, next_focus, offset_inset=0, coming_from='above') elif key == self.config.get_keybind('page_up'): if len(lb.body.positions()) <= 0: return None if 'bottom' in lb.ends_visible(size): last = len(lb.body.positions()) next_focus = last - size[1] - size[1] else: next_focus = lb.focus_position - size[1] if next_focus < 0: next_focus = 0 lb.change_focus(size, next_focus, offset_inset=0, coming_from='below') elif key == self.config.get_keybind('half_page_down'): if len(lb.body.positions()) <= 0: return None last = len(lb.body.positions()) next_focus = lb.focus_position + (size[1] / 2) if next_focus >= last: next_focus = last - 1 lb.change_focus(size, next_focus, offset_inset=0, coming_from='above') elif key == self.config.get_keybind('half_page_up'): if len(lb.body.positions()) <= 0: return None if 'bottom' in lb.ends_visible(size): last = len(lb.body.positions()) next_focus = last - size[1] - (size[1] / 2) else: next_focus = lb.focus_position - (size[1] / 2) if next_focus < 0: next_focus = 0 lb.change_focus(size, next_focus, offset_inset=0, coming_from='below') elif key == self.config.get_keybind('bottom'): if len(lb.body.positions()) <= 0: return None lb.change_focus(size, (len(lb.body.positions()) - 1), offset_inset=0, coming_from='above') elif key == self.config.get_keybind('top'): if len(lb.body.positions()) <= 0: return None lb.change_focus(size, 0, offset_inset=0, coming_from='below') elif key == self.config.get_keybind('view_next_note'): if self.gui_body_get().__class__ != view_note.ViewNote: return key if len(self.view_titles.body.positions()) <= 0: return None last = len(self.view_titles.body.positions()) if self.view_titles.focus_position == (last - 1): return None self.view_titles.focus_position += 1 lb.update_note_view(self.view_titles.note_list[ self.view_titles.focus_position].note['key']) self.gui_switch_frame_body(self.view_note) elif key == self.config.get_keybind('view_prev_note'): if self.gui_body_get().__class__ != view_note.ViewNote: return key if len(self.view_titles.body.positions()) <= 0: return None if self.view_titles.focus_position == 0: return None self.view_titles.focus_position -= 1 lb.update_note_view(self.view_titles.note_list[ self.view_titles.focus_position].note['key']) self.gui_switch_frame_body(self.view_note) elif key == self.config.get_keybind('prev_version') or \ key == self.config.get_keybind('next_version'): if self.gui_body_get().__class__ != view_note.ViewNote: return key diff = -1 if key == self.config.get_keybind('prev_version') else 1 version = diff + (self.view_note.old_note['version'] if self.view_note.old_note else self.view_note.note['version']) lb.update_note_view(version=version) elif key == self.config.get_keybind('diff_version'): if self.gui_body_get().__class__ != view_note.ViewNote: return key if not self.view_note.old_note: self.log(u'Already at latest version (key={0})'.format( self.view_note.key)) return None self.gui_clear() self.exec_diff_on_note(self.view_note.note, self.view_note.old_note) self.gui_reset() elif key == self.config.get_keybind('restore_version'): if self.gui_body_get().__class__ != view_note.ViewNote: return key if not self.view_note.old_note: self.log(u'Already at latest version (key={0})'.format( self.view_note.key)) return None self.gui_footer_input_set( urwid.AttrMap( user_input.UserInput( self.config, 'Restore v{0} (y/n): '.format( self.view_note.old_note['version']), '', self.gui_yes_no_input, [self.restore_note_callback, self.view_note.key]), 'user_input_bar')) self.gui_footer_focus_input() self.master_frame.keypress = self.gui_footer_input_get().keypress elif key == self.config.get_keybind('latest_version'): if self.gui_body_get().__class__ != view_note.ViewNote: return key lb.update_note_view(version=None) elif key == self.config.get_keybind('select_version'): if self.gui_body_get().__class__ != view_note.ViewNote: return key self.gui_footer_input_set( urwid.AttrMap( user_input.UserInput(self.config, key, '', self.gui_version_input, None), 'user_input_bar')) self.gui_footer_focus_input() self.master_frame.keypress = self.gui_footer_input_get().keypress elif key == self.config.get_keybind('status'): if self.status_bar == 'yes': self.status_bar = 'no' else: self.status_bar = self.config.get_config('status_bar') elif key == self.config.get_keybind('create_note'): if self.gui_body_get().__class__ != view_titles.ViewTitles: return key self.gui_clear() content = self.exec_cmd_on_note(None) self.gui_reset() if content: self.log(u'New note created') self.ndb.create_note(content) self.gui_update_view() self.ndb.sync_worker_go() elif key == self.config.get_keybind('edit_note') or \ key == self.config.get_keybind('view_note_ext') or \ key == self.config.get_keybind('view_note_json'): if self.gui_body_get().__class__ != view_titles.ViewTitles and \ self.gui_body_get().__class__ != view_note.ViewNote: return key if self.gui_body_get().__class__ == view_titles.ViewTitles: if len(lb.body.positions()) <= 0: return None note = lb.note_list[lb.focus_position].note else: # self.gui_body_get().__class__ == view_note.ViewNote: if key == self.config.get_keybind('edit_note'): note = lb.note else: note = lb.old_note if lb.old_note else lb.note self.gui_clear() if key == self.config.get_keybind('edit_note'): content = self.exec_cmd_on_note(note) elif key == self.config.get_keybind('view_note_ext'): content = self.exec_cmd_on_note(note, cmd=self.get_pager()) else: # key == self.config.get_keybind('view_note_json') content = self.exec_cmd_on_note(note, cmd=self.get_pager(), raw=True) self.gui_reset() if not content: return None md5_old = md5.new(self.encode_utf_8(note['content'])).digest() md5_new = md5.new(self.encode_utf_8(content)).digest() if md5_old != md5_new: self.log(u'Note updated') self.ndb.set_note_content(note['key'], content) if self.gui_body_get().__class__ == view_titles.ViewTitles: lb.update_note_title() else: # self.gui_body_get().__class__ == view_note.ViewNote: lb.update_note_view() self.ndb.sync_worker_go() else: self.log(u'Note unchanged') elif key == self.config.get_keybind('view_note'): if self.gui_body_get().__class__ != view_titles.ViewTitles: return key if len(lb.body.positions()) <= 0: return None self.view_note.update_note_view( lb.note_list[lb.focus_position].note['key']) self.gui_switch_frame_body(self.view_note) elif key == self.config.get_keybind('pipe_note'): if self.gui_body_get().__class__ != view_titles.ViewTitles and \ self.gui_body_get().__class__ != view_note.ViewNote: return key if self.gui_body_get().__class__ == view_titles.ViewTitles: if len(lb.body.positions()) <= 0: return None note = lb.note_list[lb.focus_position].note else: # self.gui_body_get().__class__ == view_note.ViewNote: note = lb.old_note if lb.old_note else lb.note self.gui_footer_input_set( urwid.AttrMap( user_input.UserInput(self.config, key, '', self.gui_pipe_input, None), 'user_input_bar')) self.gui_footer_focus_input() self.master_frame.keypress = self.gui_footer_input_get().keypress elif key == self.config.get_keybind('note_trash'): if self.gui_body_get().__class__ != view_titles.ViewTitles and \ self.gui_body_get().__class__ != view_note.ViewNote: return key if self.gui_body_get().__class__ == view_titles.ViewTitles: if len(lb.body.positions()) <= 0: return None note = lb.note_list[lb.focus_position].note else: # self.gui_body_get().__class__ == view_note.ViewNote: note = lb.note self.gui_footer_input_set( urwid.AttrMap( user_input.UserInput( self.config, '{0} (y/n): '.format( 'Untrash' if note['deleted'] else 'Trash'), '', self.gui_yes_no_input, [self.trash_note_callback, note['key']]), 'user_input_bar')) self.gui_footer_focus_input() self.master_frame.keypress = self.gui_footer_input_get().keypress elif key == self.config.get_keybind('note_pin'): if self.gui_body_get().__class__ != view_titles.ViewTitles and \ self.gui_body_get().__class__ != view_note.ViewNote: return key if self.gui_body_get().__class__ == view_titles.ViewTitles: if len(lb.body.positions()) <= 0: return None note = lb.note_list[lb.focus_position].note else: # self.gui_body_get().__class__ == view_note.ViewNote: note = lb.note pin = 1 if 'systemtags' in note: if 'pinned' in note['systemtags']: pin = 0 else: pin = 1 self.ndb.set_note_pinned(note['key'], pin) if self.gui_body_get().__class__ == view_titles.ViewTitles: lb.update_note_title() self.ndb.sync_worker_go() elif key == self.config.get_keybind('note_markdown'): if self.gui_body_get().__class__ != view_titles.ViewTitles and \ self.gui_body_get().__class__ != view_note.ViewNote: return key if self.gui_body_get().__class__ == view_titles.ViewTitles: if len(lb.body.positions()) <= 0: return None note = lb.note_list[lb.focus_position].note else: # self.gui_body_get().__class__ == view_note.ViewNote: note = lb.note md = 1 if 'systemtags' in note: if 'markdown' in note['systemtags']: md = 0 else: md = 1 self.ndb.set_note_markdown(note['key'], md) if self.gui_body_get().__class__ == view_titles.ViewTitles: lb.update_note_title() self.ndb.sync_worker_go() elif key == self.config.get_keybind('note_tags'): if self.gui_body_get().__class__ != view_titles.ViewTitles and \ self.gui_body_get().__class__ != view_note.ViewNote: return key if self.gui_body_get().__class__ == view_titles.ViewTitles: if len(lb.body.positions()) <= 0: return None note = lb.note_list[lb.focus_position].note else: # self.gui_body_get().__class__ == view_note.ViewNote: note = lb.note self.gui_footer_input_set( urwid.AttrMap( user_input.UserInput(self.config, 'Tags: ', '%s' % ','.join(note['tags']), self.gui_tags_input, None), 'user_input_bar')) self.gui_footer_focus_input() self.master_frame.keypress = self.gui_footer_input_get().keypress elif key == self.config.get_keybind('search_gstyle') or \ key == self.config.get_keybind('search_regex') or \ key == self.config.get_keybind('search_prev_gstyle') or \ key == self.config.get_keybind('search_prev_regex'): if self.gui_body_get().__class__ != view_titles.ViewTitles and \ self.gui_body_get().__class__ != view_note.ViewNote: return key if self.gui_body_get().__class__ == view_note.ViewNote: if key == self.config.get_keybind('search_prev_gstyle') or \ key == self.config.get_keybind('search_prev_regex'): self.view_note.search_direction = 'backward' else: self.view_note.search_direction = 'forward' self.gui_footer_input_set( urwid.AttrMap( user_input. UserInput(self.config, key, '', self.gui_search_input, [ 'gstyle' if key == self.config.get_keybind('search_gstyle') or key == self.config.get_keybind('search_prev_gstyle') else 'regex', 'backward' if key == self.config.get_keybind('search_prev_gstyle') or key == self.config.get_keybind('search_prev_regex') else 'forward' ]), 'user_input_bar')) self.gui_footer_focus_input() self.master_frame.keypress = self.gui_footer_input_get().keypress elif key == self.config.get_keybind('search_next'): if self.gui_body_get().__class__ != view_note.ViewNote: return key self.view_note.search_note_view_next() elif key == self.config.get_keybind('search_prev'): if self.gui_body_get().__class__ != view_note.ViewNote: return key self.view_note.search_note_view_prev() elif key == self.config.get_keybind('clear_search'): if self.gui_body_get().__class__ != view_titles.ViewTitles: return key self.view_titles.update_note_list(None) self.gui_body_set(self.view_titles) elif key == self.config.get_keybind('sort_date'): if self.gui_body_get().__class__ != view_titles.ViewTitles: return key self.view_titles.sort_note_list('date') elif key == self.config.get_keybind('sort_alpha'): if self.gui_body_get().__class__ != view_titles.ViewTitles: return key self.view_titles.sort_note_list('alpha') elif key == self.config.get_keybind('copy_note_text'): if self.gui_body_get().__class__ != view_note.ViewNote: return key self.view_note.copy_note_text() else: return lb.keypress(size, key) self.gui_update_status_bar() return None
called report_progress """ import inspect import os import sys import time currentdir = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe()))) parentdir = os.path.dirname(currentdir) sys.path.insert(0, parentdir) from settings import db, socketio import models import user_input user_in = user_input.UserInput() def prompt_in(): """ Method for receiving input """ text = user_in.read_input() while not text: time.sleep(1) # wait x seconds for user input text = user_in.read_input() return text def send_out(msg, char_id): """ Method for sending reply """ print(msg) socketio.emit("chatlog updated", {"text": msg})