forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
141 lines (100 loc) · 3.69 KB
/
tasks.py
File metadata and controls
141 lines (100 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Copyright 2016, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import datetime
# [START build_service]
from gcloud import datastore
def create_client(project_id):
return datastore.Client(project_id)
# [END build_service]
# [START add_entity]
def add_task(client, description):
key = client.key('Task')
task = datastore.Entity(
key, exclude_from_indexes=['description'])
task.update({
'created': datetime.datetime.utcnow(),
'description': description,
'done': False
})
client.put(task)
return task.key
# [END add_entity]
# [START update_entity]
def mark_done(client, task_id):
with client.transaction():
key = client.key('Task', task_id)
task = client.get(key)
if not task:
raise ValueError(
'Task {} does not exist.'.format(task_id))
task['done'] = True
client.put(task)
# [END update_entity]
# [START retrieve_entities]
def list_tasks(client):
query = client.query(kind='Task')
query.order = ['created']
return list(query.fetch())
# [END retrieve_entities]
# [START delete_entity]
def delete_task(client, task_id):
key = client.key('Task', task_id)
client.delete(key)
# [END delete_entity]
# [START format_results]
def format_tasks(tasks):
lines = []
for task in tasks:
if task['done']:
status = 'done'
else:
status = 'created {}'.format(task['created'])
lines.append('{}: {} ({})'.format(
task.key.id, task['description'], status))
return '\n'.join(lines)
# [END format_results]
def new_command(client, args):
"""Adds a task with description <description>."""
task_key = add_task(client, args.description)
print('Task {} added.'.format(task_key.id))
def done_command(client, args):
"""Marks a task as done."""
mark_done(client, args.task_id)
print('Task {} marked done.'.format(args.task_id))
def list_command(client, args):
"""Lists all tasks by creation time."""
print(format_tasks(list_tasks(client)))
def delete_command(client, args):
"""Deletes a task."""
delete_task(client, args.task_id)
print('Task {} deleted.'.format(args.task_id))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
parser.add_argument('--project-id', help='Your cloud project ID.')
new_parser = subparsers.add_parser('new', help=new_command.__doc__)
new_parser.set_defaults(func=new_command)
new_parser.add_argument('description', help='New task description.')
done_parser = subparsers.add_parser('done', help=done_command.__doc__)
done_parser.set_defaults(func=done_command)
done_parser.add_argument('task_id', help='Task ID.', type=int)
list_parser = subparsers.add_parser('list', help=list_command.__doc__)
list_parser.set_defaults(func=list_command)
delete_parser = subparsers.add_parser(
'delete', help=delete_command.__doc__)
delete_parser.set_defaults(func=delete_command)
delete_parser.add_argument('task_id', help='Task ID.', type=int)
args = parser.parse_args()
client = create_client(args.project_id)
args.func(client, args)