10
10
store_path = os .path .join (config_path , 'document.json' )
11
11
credentials_path = os .path .join (config_path , 'credentials.json' )
12
12
headers_path = os .path .join (config_path , 'headers.json' )
13
+ bookmarks_path = os .path .join (config_path , 'bookmarks.json' )
13
14
14
15
15
16
def coerce_key_types (doc , keys ):
@@ -266,6 +267,83 @@ def headers_remove(header):
266
267
click .echo (header )
267
268
268
269
270
+ # Headers
271
+
272
+ def get_bookmarks ():
273
+ if not os .path .isfile (bookmarks_path ):
274
+ return {}
275
+ bookmarks_file = open (bookmarks_path , 'rb' )
276
+ bookmarks = json .loads (bookmarks_file .read ())
277
+ bookmarks_file .close ()
278
+ return bookmarks
279
+
280
+
281
+ def set_bookmarks (bookmarks ):
282
+ bookmarks_file = open (bookmarks_path , 'wb' )
283
+ bookmarks_file .write (json .dumps (bookmarks ))
284
+ bookmarks_file .close ()
285
+
286
+
287
+ @click .group (help = "Add, remove and show bookmarks." )
288
+ def bookmarks ():
289
+ pass
290
+
291
+
292
+ @click .command (help = "List bookmarks." )
293
+ def bookmarks_show ():
294
+ bookmarks = get_bookmarks ()
295
+
296
+ if bookmarks :
297
+ width = max ([len (key ) for key in bookmarks .keys ()])
298
+ fmt = '{name:%d} <{title} {url}>' % width
299
+
300
+ click .echo (click .style ('Bookmarks' , bold = True ))
301
+ for key , value in sorted (bookmarks .items ()):
302
+ click .echo (fmt .format (name = key , title = value ['title' ] or 'Document' , url = value ['url' ]))
303
+
304
+
305
+ @click .command (help = "Add the current document to the bookmarks, with the given NAME." )
306
+ @click .argument ('name' , nargs = 1 )
307
+ def bookmarks_add (name ):
308
+ doc = read_from_store ()
309
+ if doc is None :
310
+ click .echo ('No current document.' )
311
+ return
312
+
313
+ bookmarks = get_bookmarks ()
314
+ bookmarks [name ] = {'url' : doc .url , 'title' : doc .title }
315
+ set_bookmarks (bookmarks )
316
+
317
+ click .echo (click .style ('Added bookmark' , bold = True ))
318
+ click .echo (name )
319
+
320
+
321
+ @click .command (help = "Remove a bookmark with the given NAME." )
322
+ @click .argument ('name' , nargs = 1 )
323
+ def bookmarks_remove (name ):
324
+ bookmarks = get_bookmarks ()
325
+ bookmarks .pop (name , None )
326
+ set_bookmarks (bookmarks )
327
+
328
+ click .echo (click .style ('Removed bookmark' , bold = True ))
329
+ click .echo (name )
330
+
331
+
332
+ @click .command (help = "Fetch the bookmarked document with the given NAME." )
333
+ @click .argument ('name' , nargs = 1 )
334
+ def bookmarks_get (name ):
335
+ bookmarks = get_bookmarks ()
336
+ bookmark = bookmarks .get (name )
337
+ if bookmark is None :
338
+ click .echo ('Bookmark "%s" does not exist.' % name )
339
+ return
340
+
341
+ session = get_session ()
342
+ doc = session .get (bookmark ['url' ])
343
+ click .echo (dump_to_console (doc ))
344
+ write_to_store (doc )
345
+
346
+
269
347
client .add_command (get )
270
348
client .add_command (show )
271
349
client .add_command (action )
@@ -281,6 +359,12 @@ def headers_remove(header):
281
359
headers .add_command (headers_remove , name = 'remove' )
282
360
headers .add_command (headers_show , name = 'show' )
283
361
362
+ client .add_command (bookmarks )
363
+ bookmarks .add_command (bookmarks_add , name = 'add' )
364
+ bookmarks .add_command (bookmarks_get , name = 'get' )
365
+ bookmarks .add_command (bookmarks_remove , name = 'remove' )
366
+ bookmarks .add_command (bookmarks_show , name = 'show' )
367
+
284
368
285
369
if __name__ == '__main__' :
286
370
client ()
0 commit comments