Skip to content

Commit

Permalink
Adding example diary app.
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Nov 2, 2014
1 parent 59e9d36 commit c7b1414
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions examples/diary.py
@@ -0,0 +1,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)
Entry.create_table(True)

def main():
while menu_loop():
pass

def menu_loop():
while True:
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('q) return to main menu')
if raw_input('Choice? (Nq) ') == 'q':
break

def search_entries():
"""Search entries"""
view_entries(raw_input('Search query: '))

def quit():
"""Quit"""
sys.exit(0)

menu = OrderedDict([
('a', add_entry),
('v', view_entries),
('s', search_entries),
('q', quit),
])

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)

# Initialize the database.
initialize(passphrase)
main()

0 comments on commit c7b1414

Please sign in to comment.