-
Notifications
You must be signed in to change notification settings - Fork 0
/
pydo
executable file
·136 lines (104 loc) · 3.07 KB
/
pydo
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os.path
import sqlite3
import __main__ as main
import getpass
import messages
# Get user id
uid = getpass.getuser()
db_path = "/home/"+uid+"/pydo/"
# Create pydo directory for our DB to reside
if not os.path.exists(db_path):
os.makedirs(db_path)
# Our DB file
dbfile = db_path+'pydo.db'
# Global variables for DB
db = sqlite3.connect(dbfile)
db.text_factory = str
cur = db.cursor()
cur.execute('create table if not exists TODOLIST (id INTEGER PRIMARY KEY, ToDo TEXT)')
# Global variables for command line arguments
arg_len = len(sys.argv)
file_path = main.__file__
class PyDo(object):
"""docstring for to-do list"""
def __init__(self):
pass
def getLength(self):
length = cur.execute('SELECT count(*) FROM TODOLIST')
return length.fetchone()[0]
def addTask(self, id_no, task):
toDo=[(id_no, task),]
cur.executemany("insert into TODOLIST values (?, ?)", toDo)
db.commit()
print toDo[0][1]+ messages.success["added"]
def done(self):
if self.getLength()==0:
print messages.error["done"]
else:
remove_id = raw_input("Enter id no of done item: ")
done_task = cur.execute("select ToDo from TODOLIST where id = (?)", remove_id).fetchone()[0]
cur.execute("delete from TODOLIST where id = (?)", remove_id)
db.commit()
print done_task+ messages.success["done"]
def view(self):
if self.getLength()==0:
print messages.error["view"]
else:
cur.execute('select * from TODOLIST')
results = cur.fetchall()
print messages.info["view"]
for item in results:
print item
def throw(self):
if self.getLength()==0:
print messages.error["throw"]
else:
print messages.info["throw"]
cur.execute("drop table TODOLIST")
# Amen!
print "Done!"
def does_not_match(no):
try:
if (no == str(cur.execute("select * from TODOLIST where id = (?)",no).fetchone()[0]) ) is True:
return False
except Exception as e:
return True
def fetchTask(no):
task = cur.execute("select ToDo from TODOLIST where id = (?)",no)
task = task.fetchone()[0]
return task
def main(toDo):
if arg_len==1 and sys.argv[0] == file_path:
try:
id_no, task = raw_input().split(",",1)
if does_not_match(id_no) is True: # If there does not exists a task with same id number
if task.strip() != "": # Empty task... lazylord
toDo.addTask(id_no, task.strip())
else:
print messages.error["empty_task"]
else:
''' Task with given id already exists'''
print messages.error["task_exists1"]\
+fetchTask(id_no)+ messages.error["task_exists2"]
except Exception as e:
print messages.error["wrong"]
return # Parting apart!
elif arg_len==2:
''' Other arguments '''
if (sys.argv)[1]=="--v" or (sys.argv)[1]=="-view":
return toDo.view()
if (sys.argv)[1]=="--d" or (sys.argv)[1]=="-done":
return toDo.done()
if (sys.argv)[1]=="--t" or (sys.argv)[1]=="-throw":
return toDo.throw()
else:
print messages.error["args"]
else:
print messages.error["args"]
if __name__=="__main__":
# Make an object of our PyDo Class
toDo = PyDo()
main(toDo)