nncli

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

commit 0d0908430f46cdc0b39c19db7a2074da936c9767
parent bcce0d9ca8859e8113b9db36dfc00a3063aac0b6
Author: Eric Davis <edavis@insanum.com>
Date:   Tue, 15 Jul 2014 14:42:35 -0700

new keybind command to view the raw json of a note

Diffstat:
Mconfig.py | 2++
Msncli.py | 27++++++++++++++++++---------
Mtemp.py | 24+++++++++++++++---------
3 files changed, 35 insertions(+), 18 deletions(-)

diff --git a/config.py b/config.py @@ -39,6 +39,7 @@ def __init__(self): 'kb_edit_note' : 'e', 'kb_view_note' : 'enter', 'kb_view_note_ext' : 'meta enter', + 'kb_view_note_json' : 'R', 'kb_pipe_note' : '|', 'kb_view_next_note' : 'J', 'kb_view_prev_note' : 'K', @@ -151,6 +152,7 @@ def __init__(self): self.keybinds['edit_note'] = [ cp.get(cfg_sec, 'kb_edit_note'), [ 'titles', 'notes' ], 'Edit note' ] self.keybinds['view_note'] = [ cp.get(cfg_sec, 'kb_view_note'), [ 'titles' ], 'View note' ] self.keybinds['view_note_ext'] = [ cp.get(cfg_sec, 'kb_view_note_ext'), [ 'titles', 'notes' ], 'View note with pager' ] + self.keybinds['view_note_json'] = [ cp.get(cfg_sec, 'kb_view_note_json'), [ 'titles', 'notes' ], 'View note raw json' ] self.keybinds['pipe_note'] = [ cp.get(cfg_sec, 'kb_pipe_note'), [ 'titles', 'notes' ], 'Pipe note contents' ] self.keybinds['view_next_note'] = [ cp.get(cfg_sec, 'kb_view_next_note'), [ 'notes' ], 'View next note' ] self.keybinds['view_prev_note'] = [ cp.get(cfg_sec, 'kb_view_prev_note'), [ 'notes' ], 'View previous note' ] diff --git a/sncli.py b/sncli.py @@ -62,14 +62,14 @@ def get_pager(self): return None return pager - def exec_cmd_on_note(self, note, cmd=None): + def exec_cmd_on_note(self, note, cmd=None, raw=False): if not cmd: cmd = self.get_editor() if not cmd: return None - tf = temp.tempfile_create(note if note else None) + tf = temp.tempfile_create(note if note else None, raw=raw) try: subprocess.check_call(cmd + u' ' + temp.tempfile_name(tf), shell=True) @@ -78,9 +78,11 @@ def exec_cmd_on_note(self, note, cmd=None): temp.tempfile_delete(tf) return None - content = ''.join(temp.tempfile_content(tf)) - if not content or content == u'\n': - content = None + content = None + if not raw: + content = ''.join(temp.tempfile_content(tf)) + if not content or content == u'\n': + content = None temp.tempfile_delete(tf) return content @@ -460,13 +462,14 @@ def gui_frame_keypress(self, size, key): content = self.exec_cmd_on_note(None) self.gui_reset() - if content and content != u'\n': + if content: self.log(u'New note created') self.ndb.create_note(content) self.gui_update_view() elif key == self.config.get_keybind('edit_note') or \ - key == self.config.get_keybind('view_note_ext'): + 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 @@ -481,10 +484,16 @@ def gui_frame_keypress(self, size, key): self.gui_clear() if key == self.config.get_keybind('edit_note'): content = self.exec_cmd_on_note(note) - else: + 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(note['content']).digest() md5_new = md5.new(content).digest() @@ -863,7 +872,7 @@ def cli_note_create(self, from_stdin, title): if title: content = title + '\n\n' + content if content else u'' - if content and content != u'\n': + if content: self.log(u'New note created') self.ndb.create_note(content) self.sync_notes() diff --git a/temp.py b/temp.py @@ -1,14 +1,20 @@ -import os, tempfile +import os, json, tempfile -def tempfile_create(note): - ext = '.txt' - if note and 'markdown' in note['systemtags']: - ext = '.mkd' - tf = tempfile.NamedTemporaryFile(suffix=ext, delete=False) - if note: - tf.write(note['content']) - tf.flush() +def tempfile_create(note, raw=False): + if raw: + # dump the raw json of the note + tf = tempfile.NamedTemporaryFile(suffix='.json', delete=False) + json.dump(note, tf, indent=2) + tf.flush() + else: + ext = '.txt' + if note and 'markdown' in note['systemtags']: + ext = '.mkd' + tf = tempfile.NamedTemporaryFile(suffix=ext, delete=False) + if note: + tf.write(note['content']) + tf.flush() return tf def tempfile_delete(tf):