Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 7946060a33
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 338 lines (282 sloc) 9.484 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 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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
#!/usr/bin/env python
# -*- 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

DEBUG = False

DOMAIN = 'pygmynote'

try:
TRANSLATION = gettext.translation(DOMAIN, '.')
_ = TRANSLATION.ugettext
except IOError:
_ = gettext.gettext

try:
import sqlite3 as sqlite
if DEBUG == True:
print 'Use sqlite3, with python %s' % sys.version
except ImportError:
from pysqlite2 import dbapi2 as sqlite
if DEBUG == True:
print 'Use pysqlite2, with python %s' % sys.version

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 <> 'H' 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 = raw_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
p Show records with the \"private" tag
h Show hidden 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

notetext = escapechar(raw_input('Note: '))
notetags = escapechar(raw_input('Tags: '))
notedue = raw_input('Due date (yyyy-mm-dd). Press Enter to skip: ')
notetype = "A"
sqlquery = \
"INSERT INTO notes (note, due, tags, type) VALUES ('%s', '%s', '%s', '%s')"\
% (notetext, notedue, notetags, notetype)
cursor.execute(sqlquery)
conn.commit()
print '\nRecord has been added.'
elif command == 'f':

# Insert new record with file

notetext = escapechar(raw_input('Note: '))
notetags = escapechar(raw_input('Tags: '))
notefile = escapechar(raw_input('Enter path to file: '))
notetype="A"
f=open(notefile.rstrip(), 'rb')
ablob = f.read()
f.close()
cursor.execute("INSERT INTO notes (note, tags, type, ext, file) VALUES('" + notetext + "', '" + notetags + "', '" + notetype + "', '" + notefile[-3:] + "', ?)", [sqlite.Binary(ablob)])
conn.commit()
print '\nRecord has been added.'
elif command == 's':

# Save file

recordid = raw_input('Record id: ')
outputfile=raw_input('Specify full path and file name (e.g., /home/user/loremipsum.odt): ')
f=open(outputfile, 'wb')
cursor.execute ("SELECT file FROM notes WHERE id='" + recordid + "'")
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

notetext = raw_input('Search notes for: ')
cursor.execute("SELECT id, note, tags FROM notes WHERE note LIKE '%"
+ notetext + "%'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

searchtag = raw_input ('Search by tag: ')
cursor.execute("SELECT id, note, tags FROM notes WHERE tags LIKE '%"
+ searchtag + "%' AND type='A' 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 == 'p':

# Show private records

searchtag = 'private'
cursor.execute("SELECT id, note, tags FROM notes WHERE tags LIKE '%"
+ searchtag + "%' AND type='A' 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='A' 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 == 'h':

# Show hidden records

cursor.execute("SELECT id, note, tags FROM notes WHERE type='H' 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 note

recordid = raw_input('Record id: ')
notetype = raw_input('Update note (n), tags (t), due date (d), or mark as hidden (h): ')
if notetype == 'n':
cursor.execute ("SELECT id, note FROM notes WHERE id='" + recordid + "'")
row = cursor.fetchone()
print 'Current contents: %s' % row[1]
noteupdate = raw_input('Note: ')
sqlstr = escapechar(noteupdate)
cursor.execute("UPDATE notes SET note='" + sqlstr
+ "' WHERE id='" + recordid + "'")
elif notetype == 't':
tagupdate = raw_input('Tags: ')
sqlstr = escapechar(tagupdate)
cursor.execute("UPDATE notes SET tags='" + sqlstr
+ "' WHERE id='" + recordid + "'")
elif notetype == 'd':
dueupdate = raw_input('Due date: ')
cursor.execute("UPDATE notes SET due='" + dueupdate
+ "' WHERE id='" + recordid + "'")
else:
notetype = 'H'
cursor.execute("UPDATE notes SET type='" + notetype
+ "' WHERE id='" + recordid + "'")
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 = 'A' 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='A' 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 note by its ID

recordid = raw_input('Delete note ID: ')
cursor.execute("DELETE FROM notes WHERE ID='" + recordid + "'")
print '\nRecord has been deleted.'
conn.commit()
elif command == 'w':

# Save all notes as 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()
Something went wrong with that request. Please try again.