-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
executable file
·307 lines (275 loc) · 8.63 KB
/
tasks.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Tasks 1.0 - Organize your tasks.
#
# Copyright 2011 Dimos Poupos <dimospoupos@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
try:
import sqlite3
except ImportError:
exit("Please install sqlite3 module for python to run this program.")
from os.path import *
from os import getenv
from os import system
from os import mkdir
settings = {
'path' : '%s/.tasks/' % ( getenv('HOME') ),
'database' : 'tasks.db'
}
def sizeof_db(number):
"""Convert KiloBytes to other unit of measurement."""
int(number)
for x in ['bytes','KB','MB','GB','TB']:
if number < 1024.0:
return "%3.2f %s" % (number, x)
number = number / 1024.0
return '(Error: Not Found!)'
class Database:
"""Interaction with the database."""
def __init__(self, db_name):
self.connection = sqlite3.connect(db_name)
self.cursor = self.connection.cursor()
self.db_name = db_name
def create_table(self):
self.cursor.execute('CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY,\
task VARCHAR, status INTEGER)')
self.connection.commit()
def add_task(self):
text = raw_input("The task: ")
self.cursor.execute('INSERT INTO tasks VALUES (null, ?, ?)', (text, 0))
self.connection.commit()
def delete_task(self, task_id):
command = 'DELETE FROM tasks WHERE id=%d' % (task_id)
self.cursor.execute(command)
self.connection.commit()
def edit_task(self, task_id, field, value):
try:
int(value)
command = 'UPDATE tasks SET {0}={1} WHERE id={2}'.format(field, value, task_id)
except:
command = "UPDATE tasks SET {0}='{1}' WHERE id={2}".format(field, value, task_id)
self.cursor.execute(command)
self.connection.commit()
def all_completed(self):
self.cursor.execute('SELECT * FROM tasks WHERE status=1')
for row in self.cursor:
print "Task %d - %s" % (row[0], row[1])
return 0
def all_todo(self):
self.cursor.execute('SELECT * FROM tasks WHERE status=0')
for row in self.cursor:
print "Task %d - %s" % (row[0], row[1])
def all_tasks(self):
self.cursor.execute('SELECT * FROM tasks')
for row in self.cursor:
if row[2] == 1:
status = "completed"
else:
status = "uncompleted"
print "Task %d - %s (%s)" % (row[0], row[1], status )
def tasks_len(self):
i = 0
self.cursor.execute('SELECT * FROM tasks')
for i in self.cursor:
pass
try:
return i[0]
except TypeError:
return 0
def db_info(self, all_tasks_len):
size = sizeof_db(getsize(self.db_name))
print """All tasks: %d
Database's path: %s
Database file size: %s
""" % ( all_tasks_len,
self.db_name,
size
)
def close_connection(self):
self.connection.commit()
self.cursor.close()
self.connection.close()
def first_time():
if not exists(settings['path']) and not exists(settings['path'] + settings['database']):
mkdir(settings['path'])
return 1
else:
return 0
def check_id(db, task_id):
if task_id == 'q':
menu(db)
try:
task_id = int(task_id)
return 0
except:
print "Please insert a valid task id."
return 1
def menu(db):
system("clear")
actions = {
'0' : exit_program,
'1' : add_menu,
'2' : markc_menu,
'3' : edit_menu,
'4' : delete_menu,
'5' : info_menu,
'6' : all_menu,
}
print "--- Todo ---\n"
if db.tasks_len() == 0:
print "(There are no tasks!)"
else:
db.all_todo()
print "\n--- Completed ---\n"
if db.tasks_len() == 0:
print "(There are no tasks!)"
else:
db.all_completed()
print """\n\
----------------------------------------------------------
| Actions: 1) Add task 2) Mark as completed 3) Edit task |
| 4) Delete task 5) Database info 6) All tasks |
| 0) Exit |
----------------------------------------------------------
"""
choice = raw_input("> ")
while choice not in ['0', '1', '2', '3', '4', '5', '6']:
print "Please choose a valid action:"
choice = raw_input("('q' to exit) > ")
if choice == 'q':
exit_program(db)
actions[str(choice)](db)
def add_menu(db):
system("clear")
print "--- Add task ---"
db.add_task()
system("clear")
print "Task added."
print "1) Return to main menu 2) Exit program"
choice = int(raw_input("> "))
if choice == 1:
menu(db)
elif choice == 2:
exit_program(db)
def markc_menu(db):
system("clear")
print "--- Mark as completed ---"
print "Uncompleted tasks: \n"
if db.tasks_len() == 0:
print "There are no tasks!"
else:
db.all_todo()
print ("\nInsert the id of task you want:")
task_id = int(raw_input("> "))
while check_id(db, task_id) == 1:
task_id = raw_input("('q' to exit) > ")
if task_id != 'q':
int(task_id)
elif task_id == 'q':
exit_program(db)
db.edit_task(task_id, 'status', 1)
system("clear")
print "Task marked as completed."
print "1) Return to main menu 2) Exit program"
choice = int(raw_input("> "))
if choice == 1:
menu(db)
elif choice == 2:
exit_program(db)
def edit_menu(db):
system("clear")
print "--- Edit task ---"
print "All tasks: \n"
if db.tasks_len() == 0:
print "(There are no tasks!)"
else:
db.all_tasks()
print ("\nInsert the id of task you want to edit:")
task_id = int(raw_input("> "))
while check_id(db, task_id) == 1:
task_id = raw_input("('q' to exit) > ")
if task_id != 'q':
int(task_id)
elif task_id == 'q':
exit_program(db)
system("clear")
print "Give the text of new task:"
value = raw_input("> ")
db.edit_task(task_id, 'task', value)
system("clear")
print "Task edited."
print "1) Return to main menu 2) Exit program"
choice = int(raw_input("> "))
if choice == 1:
menu(db)
elif choice == 2:
exit_program(db)
def delete_menu(db):
system("clear")
print "--- Delete task ---"
print "All tasks: \n"
if db.tasks_len() == 0:
print "(There are no tasks!)"
else:
db.all_tasks()
print ("\nInsert the id of task you want to delete:")
task_id = int(raw_input("> "))
while check_id(db, task_id) == 1:
task_id = raw_input("('q' to exit) > ")
if task_id != 'q':
int(task_id)
elif task_id == 'q':
exit_program(db)
db.delete_task(task_id)
system("clear")
print "Task deleted."
print "1) Return to main menu 2) Exit program"
choice = int(raw_input("> "))
if choice == 1:
menu(db)
elif choice == 2:
exit_program(db)
def info_menu(db):
system("clear")
print "--- Database's informarions ---\n"
db.db_info(db.tasks_len())
raw_input("\nPress <Enter> to return to main menu...")
menu(db)
def all_menu(db):
system("clear")
print "--- All tasks ---\n"
if db.tasks_len() == 0:
print "(There are no tasks!)"
else:
db.all_tasks()
raw_input("\nPress <Enter> to return to main menu...")
menu(db)
def exit_program(db):
db.close_connection()
exit(0)
def main():
if first_time() == 1:
db = Database(settings['path'] + settings['database'])
db.create_table()
else:
db = Database(settings['path'] + settings['database'])
menu(db)
exit(0)
if __name__ == '__main__':
main()