nncli

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

commit e0334bd39cf47fe353777f1f5f4b5afa82a0fc0e
parent d62f234a4744ad8ae26b643ccfba01dbe76c08c5
Author: Samuel Walladge <samuel@swalladge.id.au>
Date:   Fri, 23 Sep 2016 18:30:25 +0930

Merge branch 'import' of https://github.com/retrography/sncli into retrography-import

Diffstat:
MREADME.md | 1+
Msimplenote_cli/notes_db.py | 25+++++++++++++++++++++++++
Msimplenote_cli/sncli.py | 28++++++++++++++++++++++++++++
3 files changed, 54 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -90,6 +90,7 @@ Usage: list [search_string] - list notes (refined with search string) dump [search_string] - dump notes (refined with search string) create [-] - create a note ('-' content from stdin) + import [-] - import a note in JSON format ('-' JSON from stdin) dump - dump a note (specified by <key>) edit - edit a note (specified by <key>) < trash | untrash > - trash/untrash a note (specified by <key>) diff --git a/simplenote_cli/notes_db.py b/simplenote_cli/notes_db.py @@ -271,6 +271,31 @@ def filter_notes_regex(self, search_string=None): match_regexp = search_string if sspat else '' return filtered_notes, match_regexp, active_notes + def import_note(self, note): + # need to get a key unique to this database. not really important + # what it is, as long as it's unique. + new_key = note['key'] if note.get('key') else utils.generate_random_key() + while new_key in self.notes: + new_key = utils.generate_random_key() + + timestamp = time.time() + + # note has no internal key yet. + new_note = { + 'content' : note['content'] if note.get('content') else '', + 'deleted' : note['deleted'] if note.get('deleted') else 0, + 'modifydate' : note['modifydate'] if note.get('modifydate') else timestamp, + 'createdate' : note['createdate'] if note.get('createdate') else timestamp, + 'savedate' : 0, # never been written to disc + 'syncdate' : 0, # never been synced with server + 'tags' : note['tags'] if note.get('tags') else [], + 'systemtags' : note['systemtags'] if note.get('systemtags') else [] + } + + self.notes[new_key] = new_note + + return new_key + def create_note(self, content): # need to get a key unique to this database. not really important # what it is, as long as it's unique. diff --git a/simplenote_cli/sncli.py b/simplenote_cli/sncli.py @@ -1081,6 +1081,22 @@ def cli_note_create(self, from_stdin, title): self.ndb.create_note(content) self.sync_notes() + def cli_note_import(self, from_stdin): + + if from_stdin: + raw = ''.join(sys.stdin) + else: + raw = self.exec_cmd_on_note(None) + + if raw: + try: + note = json.loads(raw) + self.log('New note created') + self.ndb.import_note(note) + self.sync_notes() + except ValueError: + self.log('Decoding JSON has failed') + def cli_note_edit(self, key): note = self.ndb.get_note(key) @@ -1159,6 +1175,7 @@ def usage(): list [search_string] - list notes (refined with search string) dump [search_string] - dump notes (refined with search string) create [-] - create a note ('-' content from stdin) + import [-] - import a note in JSON format ('-' JSON from stdin) dump - dump a note (specified by <key>) edit - edit a note (specified by <key>) < trash | untrash > - trash/untrash a note (specified by <key>) @@ -1238,6 +1255,17 @@ def sncli_start(sync=sync, verbose=verbose, config=config): else: usage() + elif args[0] == 'import': + + if len(args) == 1: + sn = sncli_start() + sn.cli_note_import(False) + elif len(args) == 2 and args[1] == '-': + sn = sncli_start() + sn.cli_note_import(True) + else: + usage() + elif args[0] == 'edit': if not key: