nncli

NextCloud Notes Command Line Interface
git clone git://git.danielmoch.com/nncli.git
Log | Files | Refs | LICENSE

commit 5b43fbdb33374063eb2c2ea5f4ac15ed08dd1ef7
parent b7a3a6bff444765174c4838ac909134b6d8f1891
Author: Eric Davis <edavis@insanum.com>
Date:   Tue, 15 Jul 2014 12:28:12 -0700

simplified code dealing with note versions

Diffstat:
Msncli.py | 82++++++++++++++++++++++++++-----------------------------------------------------
Mview_note.py | 37+++++++++++++++++++++++++++++++------
Mview_titles.py | 2+-
3 files changed, 59 insertions(+), 62 deletions(-)

diff --git a/sncli.py b/sncli.py @@ -205,7 +205,7 @@ def gui_update_view(self): self.view_titles.focus_note(cur_key) if self.gui_body_get().__class__ == view_note.ViewNote: - self.view_note.update_note(self.view_note.note) + self.view_note.update_note_view() self.gui_update_status_bar() @@ -236,18 +236,18 @@ def gui_search_input(self, args, search_string): self.view_titles.update_note_list(search_string, args[0]) self.gui_body_set(self.view_titles) - def gui_version_input(self, args, next_note_version): + def gui_version_input(self, args, version): self.gui_footer_input_clear() self.gui_body_focus() self.master_frame.keypress = self.gui_frame_keypress - if next_note_version: - # XXX verify version number - self.log(u'Fetching version v{0} of note (key={1})'. - format(next_note_version, self.view_note.key)) - next_note = self.ndb.get_note_version(self.view_note.key, next_note_version) - self.view_note.old_note_version = next_note_version - self.view_note.old_note = next_note - self.view_note.update_note() + if version: + try: + # verify input is a number + int(version) + except ValueError, e: + self.log(u'ERROR: Invalid version value') + return + self.view_note.update_note_view(version=version) self.gui_update_status_bar() def gui_tags_input(self, args, tags): @@ -263,9 +263,9 @@ def gui_tags_input(self, args, tags): self.ndb.set_note_tags(note['key'], tags) if self.gui_body_get().__class__ == view_titles.ViewTitles: - self.view_titles.update_note_title(None) + self.view_titles.update_note_title() else: # self.gui_body_get().__class__ == view_note.ViewNote: - self.view_note.update_note(note['key']) + self.view_note.update_note_view() self.gui_update_status_bar() @@ -395,7 +395,7 @@ def gui_frame_keypress(self, size, key): if self.view_titles.focus_position == (last - 1): return None self.view_titles.focus_position += 1 - lb.update_note( + lb.update_note_view( self.view_titles.note_list[self.view_titles.focus_position].note['key']) self.gui_switch_frame_body(self.view_note) @@ -408,7 +408,7 @@ def gui_frame_keypress(self, size, key): if self.view_titles.focus_position == 0: return None self.view_titles.focus_position -= 1 - lb.update_note( + lb.update_note_view( self.view_titles.note_list[self.view_titles.focus_position].note['key']) self.gui_switch_frame_body(self.view_note) @@ -417,47 +417,19 @@ def gui_frame_keypress(self, size, key): if self.gui_body_get().__class__ != view_note.ViewNote: return key - diff = -1 if key == self.config.get_keybind('prev_version') \ - else 1 - limit = 0 if key == self.config.get_keybind('prev_version') \ - else self.view_note.note['version'] + 1 - - if not self.view_note.old_note_version: - next_note_version = self.view_note.note['version'] + diff - else: - next_note_version = self.view_note.old_note_version + diff - - if next_note_version == limit: - self.log(u'Version v{0} is unavailable (key={1})'. - format(next_note_version, self.view_note.key)) - return None - - self.log(u'Fetching version v{0} of note (key={1})'. - format(next_note_version, self.view_note.key)) - next_note = self.ndb.get_note_version(self.view_note.key, next_note_version) + diff = -1 if key == self.config.get_keybind('prev_version') else 1 - if not next_note: - self.log(u'Failed to get version v{0} of note (key={1})'. - format(next_note_version, self.view_note.key)) - return None + version = diff + (self.view_note.old_note['version'] + if self.view_note.old_note else + self.view_note.note['version']) - if next_note_version != self.view_note.note['version']: - self.view_note.old_note_version = next_note_version - self.view_note.old_note = next_note - else: - self.view_note.old_note_version = None - self.view_note.old_note = None - - lb.update_note() + lb.update_note_view(version=version) elif key == self.config.get_keybind('latest_version'): if self.gui_body_get().__class__ != view_note.ViewNote: return key - self.view_note.old_note_version = None - self.view_note.old_note = None - - lb.update_note() + lb.update_note_view(version=None) elif key == self.config.get_keybind('select_version'): if self.gui_body_get().__class__ != view_note.ViewNote: @@ -520,9 +492,9 @@ def gui_frame_keypress(self, size, key): 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(None) + lb.update_note_title() else: # self.gui_body_get().__class__ == view_note.ViewNote: - lb.update_note(note['key']) + lb.update_note_view() else: self.log(u'Note unchanged') @@ -532,8 +504,8 @@ def gui_frame_keypress(self, size, key): if len(lb.body.positions()) <= 0: return None - note = lb.note_list[lb.focus_position].note - self.view_note.update_note(note['key']) + 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'): @@ -575,7 +547,7 @@ def gui_frame_keypress(self, size, key): 1 if not note['deleted'] else 0) if self.gui_body_get().__class__ == view_titles.ViewTitles: - lb.update_note_title(None) + lb.update_note_title() elif key == self.config.get_keybind('note_pin'): if self.gui_body_get().__class__ != view_titles.ViewTitles and \ @@ -597,7 +569,7 @@ def gui_frame_keypress(self, size, key): self.ndb.set_note_pinned(note['key'], pin) if self.gui_body_get().__class__ == view_titles.ViewTitles: - lb.update_note_title(None) + lb.update_note_title() elif key == self.config.get_keybind('note_markdown'): if self.gui_body_get().__class__ != view_titles.ViewTitles and \ @@ -619,7 +591,7 @@ def gui_frame_keypress(self, size, key): self.ndb.set_note_markdown(note['key'], md) if self.gui_body_get().__class__ == view_titles.ViewTitles: - lb.update_note_title(None) + lb.update_note_title() elif key == self.config.get_keybind('note_tags'): if self.gui_body_get().__class__ != view_titles.ViewTitles and \ diff --git a/view_note.py b/view_note.py @@ -9,9 +9,8 @@ def __init__(self, config, args): self.ndb = args['ndb'] self.key = args['key'] self.log = args['log'] - self.old_note_version = None - self.old_note = None self.note = self.ndb.get_note(self.key) if self.key else None + self.old_note = None self.tabstop = int(self.config.get_config('tabstop')) super(ViewNote, self).__init__( urwid.SimpleFocusListWalker(self.get_note_content_as_list())) @@ -35,10 +34,36 @@ def get_note_content_as_list(self): lines.append(urwid.AttrMap(urwid.Divider(u'-'), 'default')) return lines - def update_note(self, key=None): - if key: - self.key = key - self.note = self.ndb.get_note(self.key) if self.key else None + def update_note_view(self, key=None, version=None): + if key: # setting a new note + self.key = key + self.note = self.ndb.get_note(self.key) + self.old_note = None + + if self.key and version: + # verify version is within range + if int(version) <= 0 or int(version) >= self.note['version'] + 1: + self.log(u'Version v{0} is unavailable (key={1})'. + format(version, self.key)) + return + + if (not version and self.old_note) or \ + (self.key and version and version == self.note['version']): + self.log(u'Displaying latest version v{0} of note (key={1})'. + format(self.note['version'], self.key)) + self.old_note = None + elif self.key and version: + # get a previous version of the note + self.log(u'Fetching version v{0} of note (key={1})'. + format(version, self.key)) + version_note = self.ndb.get_note_version(self.key, version) + if not version_note: + self.log(u'Failed to get version v{0} of note (key={1})'. + format(version, self.key)) + # don't do anything, keep current note/version + else: + self.old_note = version_note + self.body[:] = \ urwid.SimpleFocusListWalker(self.get_note_content_as_list()) self.focus_position = 0 diff --git a/view_titles.py b/view_titles.py @@ -165,7 +165,7 @@ def get_status_bar(self): urwid.AttrMap(urwid.Columns([ status_title, status_index ]), 'status_bar') - def update_note_title(self, key): + def update_note_title(self, key=None): if not key: self.body[self.focus_position] = \ self.get_note_title(self.note_list[self.focus_position].note)