nncli

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

commit 2b079569276349951f56ec5d0a7445bba465e71c
parent 079b2aab1e6f99d5d31e1b3313f4cb094ae0f4cc
Author: Eric Davis <edavis@insanum.com>
Date:   Mon, 14 Jul 2014 00:14:26 -0700

unified/simplified command execution on a note (editor/pager)

Diffstat:
Msncli.py | 167+++++++++++++++++++++++++------------------------------------------------------
1 file changed, 52 insertions(+), 115 deletions(-)

diff --git a/sncli.py b/sncli.py @@ -62,6 +62,29 @@ def get_pager(self): return None return pager + def exec_cmd_on_note(self, note, cmd=None): + + if not cmd: + cmd = self.get_editor() + if not cmd: + return None + + tf = temp.tempfile_create(note if note else None) + + try: + subprocess.check_call(cmd + u' ' + temp.tempfile_name(tf), shell=True) + except Exception, e: + self.log(u'Command error: ' + str(e)) + temp.tempfile_delete(tf) + return None + + content = ''.join(temp.tempfile_content(tf)) + if not content or content == u'\n': + content = None + + temp.tempfile_delete(tf) + return content + def gui_header_clear(self): self.master_frame.contents['header'] = ( None, None ) self.sncli_loop.draw_screen() @@ -382,29 +405,16 @@ def gui_frame_keypress(self, size, key): if self.gui_body_get().__class__ != view_titles.ViewTitles: return key - editor = self.get_editor() - if not editor: return None - - tf = temp.tempfile_create(None) - - try: - self.gui_clear() - subprocess.check_call(editor + u' ' + temp.tempfile_name(tf), shell=True) - except Exception, e: - self.log(u'Editor error: ' + str(e)) - temp.tempfile_delete(tf) - return None - finally: - self.gui_reset() + self.gui_clear() + content = self.exec_cmd_on_note(None) + self.gui_reset() - content = ''.join(temp.tempfile_content(tf)) - if content: + if content and content != u'\n': self.log(u'New note created') self.ndb.create_note(content) - temp.tempfile_delete(tf) - - elif key == self.config.get_keybind('edit_note'): + elif key == self.config.get_keybind('edit_note') or \ + key == self.config.get_keybind('view_note_ext'): if self.gui_body_get().__class__ != view_titles.ViewTitles and \ self.gui_body_get().__class__ != view_note.ViewNote: return key @@ -416,33 +426,25 @@ def gui_frame_keypress(self, size, key): else: # self.gui_body_get().__class__ == view_note.ViewNote: note = lb.note - editor = self.get_editor() - if not editor: return None + self.gui_clear() + if key == self.config.get_keybind('edit_note'): + content = self.exec_cmd_on_note(note) + else: + content = self.exec_cmd_on_note(note, cmd=self.get_pager()) + self.gui_reset() md5_old = md5.new(note['content']).digest() - tf = temp.tempfile_create(note) + md5_new = md5.new(content).digest() - try: - self.gui_clear() - subprocess.check_call(editor + u' ' + temp.tempfile_name(tf), shell=True) - except Exception, e: - self.log(u'Editor error: ' + str(e)) - temp.tempfile_delete(tf) - return None - finally: - self.gui_reset() - - new_content = ''.join(temp.tempfile_content(tf)) - md5_new = md5.new(new_content).digest() if md5_old != md5_new: self.log(u'Note updated') - self.ndb.set_note_content(note['key'], new_content) + self.ndb.set_note_content(note['key'], content) if self.gui_body_get().__class__ == view_titles.ViewTitles: lb.update_note_title(None) else: # self.gui_body_get().__class__ == view_note.ViewNote: lb.update_note(note['key']) - - temp.tempfile_delete(tf) + else: + self.log(u'Note unchanged') elif key == self.config.get_keybind('view_note'): if self.gui_body_get().__class__ != view_titles.ViewTitles: @@ -454,43 +456,6 @@ def gui_frame_keypress(self, size, key): self.view_note.update_note(note['key']) self.gui_switch_frame_body(self.view_note) - elif key == self.config.get_keybind('view_note_ext'): - 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 - - pager = self.get_pager() - if not pager: return None - - md5_old = md5.new(note['content']).digest() - tf = temp.tempfile_create(note) - - try: - self.gui_clear() - subprocess.check_call(pager + u' ' + temp.tempfile_name(tf), shell=True) - except Exception, e: - self.log(u'Pager error: ' + str(e)) - temp.tempfile_delete(tf) - return None - finally: - self.gui_reset() - - new_content = ''.join(temp.tempfile_content(tf)) - md5_new = md5.new(new_content).digest() - if md5_old != md5_new: - self.log(u'Note updated') - self.ndb.set_note_content(note['key'], new_content) - lb.update_note_title(None) - - temp.tempfile_delete(tf) - 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: @@ -824,36 +789,18 @@ def cli_note_dump(self, key): def cli_note_create(self, from_stdin, title): - def save_new_note(title, content): - if title or (content and content != u'\n'): - if title: - content = title + '\n\n' + content if content else u'' - self.log(u'New note created') - self.ndb.create_note(content) - self.sync_notes() - if from_stdin: - content = ''.join(sys.stdin) - save_new_note(title, content) - return - - editor = self.get_editor() - if not editor: return - - tf = temp.tempfile_create(None) - - try: - subprocess.check_call(editor + u' ' + temp.tempfile_name(tf), shell=True) - except Exception, e: - self.log(u'Editor error: ' + str(e)) - temp.tempfile_delete(tf) - return + else: + content = self.exec_cmd_on_note(None) - content = ''.join(temp.tempfile_content(tf)) - save_new_note(title, content) + if title: + content = title + '\n\n' + content if content else u'' - temp.tempfile_delete(tf) + if content and content != u'\n': + self.log(u'New note created') + self.ndb.create_note(content) + self.sync_notes() def cli_note_edit(self, key): @@ -862,30 +809,20 @@ def cli_note_edit(self, key): self.log(u'ERROR: Key does not exist') return - editor = self.get_editor() - if not editor: return None + content = self.exec_cmd_on_note(note) + if not content: + return md5_old = md5.new(note['content']).digest() - tf = temp.tempfile_create(note) - - try: - subprocess.check_call(editor + u' ' + temp.tempfile_name(tf), shell=True) - except Exception, e: - self.log(u'Editor error: ' + str(e)) - temp.tempfile_delete(tf) - return + md5_new = md5.new(content).digest() - new_content = ''.join(temp.tempfile_content(tf)) - md5_new = md5.new(new_content).digest() if md5_old != md5_new: self.log(u'Note updated') - self.ndb.set_note_content(note['key'], new_content) + self.ndb.set_note_content(note['key'], content) self.sync_notes() else: self.log(u'Note unchanged') - temp.tempfile_delete(tf) - def cli_note_trash(self, key, trash): note = self.ndb.get_note(key)