Skip to content

Commit

Permalink
Notes commands Issue #5: Create note.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekmo committed Apr 20, 2018
1 parent dcebad8 commit 1119c89
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions google_keep_tasks/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ class LoginError(GKeepError):
body = 'Check credentials file. The syntax is: <username> <password>.'


class InvalidColor(GKeepError):
def __init__(self, invalid_color):
import gkeepapi
colors = [color.name for color in gkeepapi.node.ColorValue]
super(InvalidColor, self).__init__('Invalid color: {}. Available colors: {}'.format(
invalid_color, ', '.join(colors)
))


def catch(fn):
def wrap(*args, **kwargs):
try:
Expand Down
1 change: 1 addition & 0 deletions google_keep_tasks/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ def cli(ctx, debug, auth):


import google_keep_tasks.items
import google_keep_tasks.notes
46 changes: 46 additions & 0 deletions google_keep_tasks/notes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import click
import gkeepapi

from google_keep_tasks.exceptions import InvalidColor
from google_keep_tasks.management import cli


def add_color(note, color):
if not color:
return
color = color.title()
if color and not hasattr(gkeepapi.node.ColorValue, color):
raise InvalidColor(color)
note.color = getattr(gkeepapi.node.ColorValue, color)


def find_or_create_label(keep, label_name):
label = keep.findLabel(label_name)
if not label:
label = keep.createLabel(label_name)
return label


def add_labels(keep, note, labels):
if not labels:
return
for label in labels:
note.labels.add(find_or_create_label(keep, label))


def comma_separated(ctx, param, value):
return value.split(',') if value else []


@cli.command('add-note')
@click.option('--color', default='')
@click.option('--labels', default='', callback=comma_separated)
@click.argument('title')
@click.argument('text')
@click.pass_context
def add_note(ctx, color, labels, title, text):
keep = ctx.obj['keep']
gnote = keep.createNote(title, text)
add_color(gnote, color)
add_labels(keep, gnote, labels)
keep.sync()

0 comments on commit 1119c89

Please sign in to comment.