Skip to content

Commit 26080c5

Browse files
committed
Version 2.1.0 - Adds since and data parameters
- Supports since as a Simperium cursor, not a date string - Keeps track of cursor in `self.current` - Allows data=False for just getting keys and versions. Defaults to True and adds a dummy/empty note data object if using data=False - Adds in another call to __add_simplenote_api_fields that was missed last time - Add a test to check data=False doesn't blow up - Fix a million instances of success being spelt wrong
1 parent f3d2b89 commit 26080c5

File tree

5 files changed

+73
-31
lines changed

5 files changed

+73
-31
lines changed

Diff for: HISTORY.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
History
22
========
33

4+
2.1.0 (2018-11-04)
5+
------------------
6+
7+
* Adds since paramter back in (as Simperium cursor, not date)
8+
*
9+
410
2.0.3 (2018-10-19)
511
------------------
612

Diff for: README.rst

+16-12
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,22 @@ simplenote.py can be imported into any python module::
4545

4646
The object then provides the following API methods::
4747

48-
sn.get_note_list(tags=[]) # Supports optional `tags` parameter that takes a list of tags
49-
# to return only notes that contain at least one of these tags.
50-
51-
sn.get_note(note_id) # note id is value of key `key` in note dict as returned
52-
# by get_note_list. Supports optional version integer as
53-
# argument to return previous versions
54-
55-
sn.add_note(note) # A ``note`` object is a dictionary with at least a
56-
# ``content`` property, containing the note text.
57-
58-
sn.update_note(note) # The ``update_note`` method needs a note object which
59-
# also has a ``key`` property.
48+
sn.get_note_list(data=True, since=cursor, tags=[]) # Supports optional `tags` parameter that takes a list of tags
49+
# to return only notes that contain at least one of these tags.
50+
# Also supports a `since` parameter, but as per the Simperium
51+
# API this is no longer a date, rather a cursor.
52+
# Lastly, also supports a `data` parameter (defaults to True)
53+
# to only return keys/ids and versions
54+
55+
sn.get_note(note_id) # note id is value of key `key` in note dict as returned
56+
# by get_note_list. Supports optional version integer as
57+
# argument to return previous versions
58+
59+
sn.add_note(note) # A ``note`` object is a dictionary with at least a
60+
# ``content`` property, containing the note text.
61+
62+
sn.update_note(note) # The ``update_note`` method needs a note object which
63+
# also has a ``key`` property.
6064
sn.trash_note(note_id)
6165

6266
simplenote.delete_note(note_id)

Diff for: simplenote/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
from .simplenote import Simplenote, SimplenoteLoginFailed
44

55
__author__ = "Daniel Schauenberg"
6-
__version__ = "2.0.3"
6+
__version__ = "2.1.0"
77
__license__ = "MIT"

Diff for: simplenote/simplenote.py

+39-18
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(self, username, password):
5858
self.password = password
5959
self.header = 'X-Simperium-Token'
6060
self.token = None
61-
self.mark = "mark"
61+
self.current = ""
6262

6363
def authenticate(self, user, password):
6464
""" Method to get simplenote auth token
@@ -115,7 +115,7 @@ def get_note(self, noteid, version=None):
115115
A tuple `(note, status)`
116116
117117
- note (dict): note object
118-
- status (int): 0 on sucesss and -1 otherwise
118+
- status (int): 0 on success and -1 otherwise
119119
120120
"""
121121
# request note
@@ -151,7 +151,7 @@ def update_note(self, note):
151151
Returns:
152152
A tuple `(note, status)`
153153
- note (dict): note object
154-
- status (int): 0 on sucesss and -1 otherwise
154+
- status (int): 0 on success and -1 otherwise
155155
156156
"""
157157
# determine whether to create a new note or update an existing one
@@ -202,7 +202,7 @@ def add_note(self, note):
202202
A tuple `(note, status)`
203203
204204
- note (dict): the newly created note
205-
- status (int): 0 on sucesss and -1 otherwise
205+
- status (int): 0 on success and -1 otherwise
206206
207207
"""
208208

@@ -213,23 +213,30 @@ def add_note(self, note):
213213
else:
214214
return "No string or valid note.", -1
215215

216-
def get_note_list(self, tags=[]):
216+
def get_note_list(self, data=True, since=None, tags=[]):
217217
""" Method to get the note list
218218
219-
The method can be passed optional arguments to limit the
220-
the list to notes containing a certain tag. If omitted a list
221-
of all notes is returned.
219+
The method can be passed optional arguments to limit the list to
220+
notes containing a certain tag, or only updated since a certain
221+
Simperium cursor. If omitted a list of all notes is returned.
222+
223+
By default data objects are returned. If data is set to false only
224+
keys/ids and versions are returned. An empty data object is inserted
225+
for compatibility.
222226
223227
Arguments:
224228
- tags=[] list of tags as string: return notes that have
225229
at least one of these tags
230+
- since=cursor Simperium cursor as string: return only changes
231+
since this cursor
232+
- data=True If false only return keys/ids and versions
226233
227234
Returns:
228235
A tuple `(notes, status)`
229236
230237
- notes (list): A list of note objects with all properties set except
231238
`content`.
232-
- status (int): 0 on sucesss and -1 otherwise
239+
- status (int): 0 on success and -1 otherwise
233240
234241
"""
235242
# initialize data
@@ -239,8 +246,13 @@ def get_note_list(self, tags=[]):
239246
notes = { "index" : [] }
240247

241248
# get the note index
242-
# TODO: Using data=false is actually fine with simplenote.vim - sadly no faster though
243-
params = '/index?limit=%s&data=true' % (str(NOTE_FETCH_LENGTH))
249+
params = '/index?limit=%s' % (str(NOTE_FETCH_LENGTH))
250+
251+
if since is not None:
252+
params += '&since=%s' % (since)
253+
# Fetching data is the default
254+
if data:
255+
params += '&data=true'
244256

245257
# perform initial HTTP request
246258
request = Request(DATA_URL+params)
@@ -251,6 +263,9 @@ def get_note_list(self, tags=[]):
251263
# re-write for v1 consistency
252264
note_objects = []
253265
for n in response_notes["index"]:
266+
# If data=False then can't do this bit... or not all of it, just have id and version. Add empty data object.
267+
if not data:
268+
n['d'] = {}
254269
note_object = self.__add_simplenote_api_fields(n['d'], n['id'], n['v'])
255270
note_objects.append(note_object)
256271
notes["index"].extend(note_objects)
@@ -270,14 +285,16 @@ def get_note_list(self, tags=[]):
270285
# re-write for v1 consistency
271286
note_objects = []
272287
for n in response_notes["index"]:
288+
if not data:
289+
n['d'] = {}
273290
note_object = n['d']
274-
note_object['version'] = n['v']
275-
note_object['key'] = n['id']
291+
note_object = self.__add_simplenote_api_fields(n['d'], n['id'], n['v'])
276292
note_objects.append(note_object)
277293
notes["index"].extend(note_objects)
278294
except IOError:
279295
status = -1
280296
note_list = notes["index"]
297+
self.current = response_notes["current"]
281298
# Can only filter for tags at end, once all notes have been retrieved.
282299
if (len(tags) > 0):
283300
note_list = [n for n in note_list if (len(set(n["tags"]).intersection(tags)) > 0)]
@@ -293,7 +310,7 @@ def trash_note(self, note_id):
293310
A tuple `(note, status)`
294311
295312
- note (dict): the newly created note or an error message
296-
- status (int): 0 on sucesss and -1 otherwise
313+
- status (int): 0 on success and -1 otherwise
297314
298315
"""
299316
# get note
@@ -321,7 +338,7 @@ def delete_note(self, note_id):
321338
A tuple `(note, status)`
322339
323340
- note (dict): an empty dict or an error message
324-
- status (int): 0 on sucesss and -1 otherwise
341+
- status (int): 0 on success and -1 otherwise
325342
326343
"""
327344
# notes have to be trashed before deletion
@@ -344,9 +361,13 @@ def __add_simplenote_api_fields(self, note, noteid, version):
344361
# Compatibility with original Simplenote API v2.1.5
345362
note[u'key'] = noteid
346363
note[u'version'] = version
347-
note[u'modifydate'] = note["modificationDate"]
348-
note[u'createdate'] = note["creationDate"]
349-
note[u'systemtags'] = note["systemTags"]
364+
try:
365+
note[u'modifydate'] = note["modificationDate"]
366+
note[u'createdate'] = note["creationDate"]
367+
note[u'systemtags'] = note["systemTags"]
368+
except KeyError:
369+
# For when data=False
370+
pass
350371
return note
351372

352373
def __remove_simplenote_api_fields(self, note):

Diff for: tests/test_simplenote.py

+11
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ def test_simplenote_get_list_with_tags(self):
7373
else:
7474
self.assertEqual(0, len(res))
7575

76+
# TODO: I can't think of a good way to test this yet since test period is
77+
# relatively quick
78+
# def test_simplenote_get_list_with_since(self)
79+
80+
def test_simplenote_get_list_without_data(self):
81+
res, status = self.simplenote_instance.get_note_list(data=False)
82+
if status == 0:
83+
self.assertEqual(self.initial_note_count, len(res))
84+
else:
85+
self.assertEqual(0, len(res))
86+
7687
def test_simplenote_first_note(self):
7788
if self.first_note != False:
7889
note, status = self.simplenote_instance.get_note(self.first_note)

0 commit comments

Comments
 (0)