Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit cdb563f

Browse files
committed
Added bookmarks to command line client
1 parent 85a6a62 commit cdb563f

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

coreapi/commandline.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
store_path = os.path.join(config_path, 'document.json')
1111
credentials_path = os.path.join(config_path, 'credentials.json')
1212
headers_path = os.path.join(config_path, 'headers.json')
13+
bookmarks_path = os.path.join(config_path, 'bookmarks.json')
1314

1415

1516
def coerce_key_types(doc, keys):
@@ -266,6 +267,83 @@ def headers_remove(header):
266267
click.echo(header)
267268

268269

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+
269347
client.add_command(get)
270348
client.add_command(show)
271349
client.add_command(action)
@@ -281,6 +359,12 @@ def headers_remove(header):
281359
headers.add_command(headers_remove, name='remove')
282360
headers.add_command(headers_show, name='show')
283361

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+
284368

285369
if __name__ == '__main__':
286370
client()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy