commit 0207204bdf1e4906d3cafa271ee04ef3c9d56797
parent 26169e2cb99be4fd5ca14011d06998a71a2f516b
Author: Samuel Walladge <samuel@swalladge.id.au>
Date: Sat, 24 Sep 2016 12:32:25 +0930
improve import functions
- don't use assert, since that doesn't run if the -O flag is passed to python
- better sanity checking with feedback messages to log
- program exits with error status if failed
Diffstat:
3 files changed, 29 insertions(+), 12 deletions(-)
diff --git a/README.md b/README.md
@@ -58,6 +58,7 @@ Check your OS distribution for installation packages.
- search for notes using a Google style search pattern or Regular Expression
- dump note contents
- create a new note (via stdin or editor)
+ - import a note with raw json data (stdin or editor)
- edit a note (via editor)
- trash/untrash a note
- pin/unpin a note
diff --git a/simplenote_cli/notes_db.py b/simplenote_cli/notes_db.py
@@ -280,12 +280,18 @@ def import_note(self, note):
timestamp = time.time()
+ try:
+ modifydate = float(note.get('modifydate', timestamp))
+ createdate = float(note.get('createdate', timestamp))
+ except ValueError:
+ raise ValueError('date fields must be numbers or string representations of numbers')
+
# note has no internal key yet.
new_note = {
'content' : note.get('content', ''),
'deleted' : note.get('deleted', 0),
- 'modifydate' : note.get('modifydate', timestamp),
- 'createdate' : note.get('createdate', timestamp),
+ 'modifydate' : modifydate,
+ 'createdate' : createdate,
'savedate' : 0, # never been written to disc
'syncdate' : 0, # never been synced with server
'tags' : note.get('tags', []),
@@ -293,20 +299,26 @@ def import_note(self, note):
}
# sanity check all note values
- assert isinstance(new_note['content'], str)
- assert new_note['deleted'] in (0, 1)
+ if not isinstance(new_note['content'], str):
+ raise ValueError('"content" must be a string')
+ if not new_note['deleted'] in (0, 1):
+ raise ValueError('"deleted" must be 0 or 1')
for n in (new_note['modifydate'], new_note['createdate']):
- assert isinstance(n, float) or isinstance(n, int)
- assert 0 <= n <= timestamp
+ if not 0 <= n <= timestamp:
+ raise ValueError('date fields must be real')
- assert isinstance(new_note['tags'], list)
+ if not isinstance(new_note['tags'], list):
+ raise ValueError('"tags" must be an array')
for tag in new_note['tags']:
- assert isinstance(tag, str)
+ if not isinstance(tag, str):
+ raise ValueError('items in the "tags" array must be strings')
- assert isinstance(new_note['systemtags'], list)
+ if not isinstance(new_note['systemtags'], list):
+ raise ValueError('"systemtags" must be an array')
for tag in new_note['systemtags']:
- assert isinstance(tag, str)
+ if not isinstance(tag, str):
+ raise ValueError('items in the "systemtags" array must be strings')
self.notes[new_key] = new_note
diff --git a/simplenote_cli/sncli.py b/simplenote_cli/sncli.py
@@ -1094,8 +1094,12 @@ def cli_note_import(self, from_stdin):
self.log('New note created')
self.ndb.import_note(note)
self.sync_notes()
- except ValueError:
- self.log('Decoding JSON has failed')
+ except json.decoder.JSONDecodeError as e:
+ self.log('(IMPORT) Decoding JSON has failed: {}'.format(e))
+ sys.exit(1)
+ except ValueError as e:
+ self.log('(IMPORT) ValueError: {}'.format(e))
+ sys.exit(1)
def cli_note_edit(self, key):