forked from coleifer/peewee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiary.py
executable file
·89 lines (74 loc) · 2.42 KB
/
diary.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
#!/usr/bin/env python
from collections import OrderedDict
import datetime
from getpass import getpass
import sys
from peewee import *
from playhouse.sqlcipher_ext import SqlCipherDatabase
# Defer initialization of the database until the script is executed from the
# command-line.
db = SqlCipherDatabase(None)
class Entry(Model):
content = TextField()
timestamp = DateTimeField(default=datetime.datetime.now)
class Meta:
database = db
def initialize(passphrase):
db.init('diary.db', passphrase=passphrase, kdf_iter=64000)
db.create_tables([Entry])
def menu_loop():
choice = None
while choice != 'q':
for key, value in menu.items():
print('%s) %s' % (key, value.__doc__))
choice = raw_input('Action: ').lower().strip()
if choice in menu:
menu[choice]()
def add_entry():
"""Add entry"""
print('Enter your entry. Press ctrl+d when finished.')
data = sys.stdin.read().strip()
if data and raw_input('Save entry? [Yn] ') != 'n':
Entry.create(content=data)
print('Saved successfully.')
def view_entries(search_query=None):
"""View previous entries"""
query = Entry.select().order_by(Entry.timestamp.desc())
if search_query:
query = query.where(Entry.content.contains(search_query))
for entry in query:
timestamp = entry.timestamp.strftime('%A %B %d, %Y %I:%M%p')
print(timestamp)
print('=' * len(timestamp))
print(entry.content)
print('n) next entry')
print('d) delete entry')
print('q) return to main menu')
action = raw_input('Choice? (Ndq) ').lower().strip()
if action == 'q':
break
elif action == 'd':
entry.delete_instance()
break
def search_entries():
"""Search entries"""
view_entries(raw_input('Search query: '))
menu = OrderedDict([
('a', add_entry),
('v', view_entries),
('s', search_entries),
])
if __name__ == '__main__':
# Collect the passphrase using a secure method.
passphrase = getpass('Enter password: ')
if not passphrase:
sys.stderr.write('Passphrase required to access diary.\n')
sys.stderr.flush()
sys.exit(1)
elif len(passphrase) < 8:
sys.stderr.write('Passphrase must be at least 8 characters.\n')
sys.stderr.flush()
sys.exit(1)
# Initialize the database.
initialize(passphrase)
menu_loop()