-
Notifications
You must be signed in to change notification settings - Fork 7
/
pygmynote3.py
executable file
·314 lines (262 loc) · 8.45 KB
/
pygmynote3.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
308
309
310
311
312
313
314
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Pygmynote is a command-line tool for storing and managing heterogeneous bit of data, including notes, tasks, links, and file attachments.Pygmynote is written in Python and uses a SQLite database as its back end.
Thanks to Luis Cabrera Sauco for implementing SQLite and i18 support.
i18:
~~~~
pygettext -k_ -o pygmynote.po pygmynote.py
msgfmt pygmynote.po -o pygmynote.mo
mkdir -p es/LC_MESSAGES
mv pygmynote.mo es/LC_MESSAGES/pygmynote.mo
mv pygmynote.po es/LC_MESSAGES/pygmynote.po
test_i18:
~~~~~~~~~
$ LANGUAGE=es python pygmynote.py
"""
__author__ = "Dmitri Popov [dmpop@linux.com]"
__copyright__ = "Copyright 2011, 2012 Dmitri Popov"
__license__ = "GPLv3"
__version__ = "0.7.13"
__URL__ = "http://www.github.com/dmpop"
import sys
import datetime
import os
import time
import calendar
import gettext
import sqlite3 as sqlite
DEBUG = False
DOMAIN = "pygmynote"
try:
TRANSLATION = gettext.translation(DOMAIN, ".")
_ = TRANSLATION.ugettext
except IOError:
_ = gettext.gettext
DB = "pygmynote.db"
ENC = "utf-8"
if os.path.exists(DB):
if DEBUG == True:
print ("The database already exists.")
CREATE = False
else:
if DEBUG == False:
print ("Creating a new database.")
CREATE = True
try:
if DEBUG == False:
conn = sqlite.connect(DB)
else:
conn = sqlite.connect(DB, timeout=0.5, encoding=ENC)
cursor = conn.cursor()
except:
sys.exit("Connection to the SQLite database failed!")
today = time.strftime("%Y-%m-%d")
command = ""
counter = 0
print ("""\033[1;32m
( 0)>
(( *))
||
==========="=="=============
Pygmynote is ready. Pile up!
============================\033[1;m\n""")
def escapechar(sel):
sel=sel.replace("\'", "\''")
sel=sel.replace("\"", "\"""")
return sel
if CREATE == True:
CREATE_SQL = \
"CREATE TABLE notes (\
id INTEGER PRIMARY KEY UNIQUE NOT NULL,\
note VARCHAR(1024),\
file BLOB,\
due DATE,\
type VARCHAR(3),\
ext VARCHAR(3),\
tags VARCHAR(256));"
cursor.execute(CREATE_SQL)
conn.commit()
print ("Today\"s deadlines:")
cursor.execute ("SELECT due, id, note, tags FROM notes WHERE due = '" + today + "' AND type <> '0' ORDER BY id ASC")
for row in cursor:
print ("\n%s -- \033[1;32m%s\033[1;m %s \033[1;30m[%s]\033[1;m" % (row[0], row[1], row[2], row[3]))
while command != "q":
try:
command = input('\n>')
if command == 'help':
print ("""
===================
Pygmynote commands:
===================
i Insert new record
f Insert new record with an attachment
s Save attachment
u Update record
n Search records by note
t Search records by tag
a Show active records
ar Show archived records
tl Show tasks
at Show records with attachments
w Export records as CSV file
d Delete record by its ID
q Quit""")
elif command == "i":
# Insert new record
rtxt = escapechar(input('Note: '))
rtags = escapechar(input('Tags: '))
rdue = input('Due date (yyyy-mm-dd). Press Enter to skip: ')
rtype = '1'
sqlquery = \
"INSERT INTO notes (note, due, tags, type) VALUES ('%s', '%s', '%s', '%s')"\
% (rtxt, rdue, rtags, rtype)
cursor.execute(sqlquery)
conn.commit()
print ('\nRecord has been added.')
elif command == 'f':
# Insert new record with file
rtxt = escapechar(input('Note: '))
rtags = escapechar(input('Tags: '))
rfile = escapechar(input('Enter path to file: '))
rtype='1'
f=open(rfile.rstrip(), 'rb')
ablob = f.read()
f.close()
cursor.execute("INSERT INTO notes (note, tags, type, ext, file) VALUES('" + rtxt + "', '" + rtags + "', '" + rtype + "', '" + rfile[-3:] + "', ?)", [sqlite.Binary(ablob)])
conn.commit()
print ('\nRecord has been added.')
elif command == 's':
# Save file
recid = input('Record id: ')
outfile=input('Specify full path and file name (e.g., /home/user/loremipsum.odt): ')
f=open(outfile, 'wb')
cursor.execute ("SELECT file FROM notes WHERE id='" + recid + "'")
ablob = cursor.fetchone()
f.write(ablob[0])
f.close()
cursor.close()
conn.commit()
print ('\nFile has been saved.')
elif command == 'n':
# Search records by note
rtxt = input('Search notes for: ')
cursor.execute("SELECT id, note, tags FROM notes WHERE note LIKE '%"
+ rtxt + "%'ORDER BY id ASC")
print ("\n-----")
for row in cursor:
print ("\n\033[1;32m%s\033[1;m %s \033[1;30m[%s]\033[1;m" % (row[0], row[1], row[2]))
counter = counter + 1
print ("\n-----")
print ("\033[1;34mRecord count:\033[1;m %s") % counter
counter = 0
elif command == 't':
# Search records by tag
stag = input ('Search by tag: ')
cursor.execute("SELECT id, note, tags FROM notes WHERE tags LIKE '%"
+ stag + "%' AND type='1' ORDER BY id ASC")
print ('\n-----')
for row in cursor:
print ('\n\033[1;32m%s\033[1;m %s \033[1;30m[%s]\033[1;m' % (row[0], row[1], row[2]))
counter = counter + 1
print ('\n-----')
print ('\033[1;34mRecord count:\033[1;m %s' % counter)
counter = 0
elif command == 'a':
# Show active records
cursor.execute("SELECT id, note, tags FROM notes WHERE type='1' ORDER BY id ASC")
print ("\n-----")
for row in cursor:
print ("\n\033[1;32m%s\033[1;m %s \033[1;30m[%s]\033[1;m" % (row[0], row[1], row[2]))
counter = counter + 1
print ('\n-----')
print ('\033[1;34mRecord count:\033[1;m %s' % counter)
counter = 0
elif command == 'ar':
# Show archived records
cursor.execute("SELECT id, note, tags FROM notes WHERE type='0' ORDER BY id ASC")
print ('\n-----')
for row in cursor:
print ('\n\033[1;32m%s\033[1;m %s \033[1;30m[%s]\033[1;m' % (row[0], row[1], row[2]))
counter = counter + 1
print ('\n-----')
print ('\033[1;34mRecord count:\033[1;m %s' % counter)
counter = 0
elif command == 'u':
# Update record
recid = input('Record id: ')
rtype = input('Update note [0], tags [1], due date [2], or archive [3]: ')
if rtype == '0':
cursor.execute ("SELECT id, note FROM notes WHERE id='" + recid + "'")
row = cursor.fetchone()
print ('Current contents: %s' % row[1])
noteupd = input('Note: ')
sqlstr = escapechar(noteupd)
cursor.execute("UPDATE notes SET note='" + sqlstr
+ "' WHERE id='" + recid + "'")
elif rtype == '1':
tagupd = input('Tags: ')
sqlstr = escapechar(tagupd)
cursor.execute("UPDATE notes SET tags='" + sqlstr
+ "' WHERE id='" + recid + "'")
elif rtype == '2':
dueupd = input('Due date: ')
cursor.execute("UPDATE notes SET due='" + dueupd
+ "' WHERE id='" + recid + "'")
else:
rtype = '0'
cursor.execute("UPDATE notes SET type='" + rtype
+ "' WHERE id='" + recid + "'")
conn.commit()
print ('\nRecord has been updated.')
elif command == 'tl':
# Show tasks
cursor.execute ("SELECT due, id, note, tags FROM notes WHERE due <> '' AND tags NOT LIKE '%private%' AND type = '1' ORDER BY due ASC")
print ("\n-----")
now = datetime.datetime.now()
calendar.prmonth(now.year, now.month)
for row in cursor:
print ("\n%s -- \033[1;32m%s\033[1;m %s \033[1;30m[%s]\033[1;m" % (row[0], row[1], row[2], row[3]))
counter = counter + 1
print ('\n-----')
print ('\033[1;34mRecord count:\033[1;m %s' % counter)
counter = 0
elif command == 'at':
# Show records with attachments
cursor.execute("SELECT id, note, tags, ext FROM notes WHERE ext <> 'None' AND type='1' ORDER BY id ASC")
print ('\n-----')
for row in cursor:
print ('\n\033[1;32m%s\033[1;m %s \033[1;30m[%s]\033[1;m \033[1;43m%s\033[1;m' % (row[0], row[1], row[2], row[3]))
counter = counter + 1
print ('\n-----')
print ('\033[1;34mRecord count:\033[1;m %s' % counter)
counter = 0
elif command == 'd':
# Delete record by its ID
recid = input('Delete note ID: ')
cursor.execute("DELETE FROM notes WHERE ID='" + recid + "'")
print ('\nRecord has been deleted.')
conn.commit()
elif command == 'w':
# Save all records in pygmynote.txt
cursor.execute("SELECT id, note, tags, due FROM notes ORDER BY id ASC")
if os.path.exists('pygmynote.txt'):
os.remove('pygmynote.txt')
for row in cursor:
filename = 'pygmynote.txt'
file = open(filename, 'a')
file.write('%s\t%s\t[%s]\t%s\n' % (row[0], row[1], row[2], row[3]))
file.close()
print ('\nRecords have been saved in the pygmynote.txt file.')
except:
print ('\n\n\033[1;31mSomething went wrong. Try again.\033[1;m')
continue
print ("""\033[1;33m
( 0)>
(( *))
||
========"=="=========
Bye! Have a nice day.
=====================\033[1;m\n""")
cursor.close()
conn.close()