Skip to content

Instantly share code, notes, and snippets.

@AgentO3
Created June 7, 2018 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AgentO3/0cd93a2cd0834ad70fd17800bc047701 to your computer and use it in GitHub Desktop.
Save AgentO3/0cd93a2cd0834ad70fd17800bc047701 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import urwid
import urwid.raw_display
import urwid.web_display
import sqlite3
import time
conn = sqlite3.connect('keg.db')
current_pour_id = 0
current_pour_user_id = 0
def get_or_create_user(user):
c = conn.cursor()
c.execute("insert into user(name) select '" + user + "' where not exists(select 1 from user where name = '" + user + "')")
conn.commit()
c.execute("select id from user where name='" + user + "'")
conn.commit()
return c.fetchone()[0]
def start_pour(user_id):
c = conn.cursor()
c.execute("insert into pour_time(user_id, ptime_start) values(?, ?)", [user_id, time.time()])
conn.commit()
return c.lastrowid
def stop_pour(pour_id):
c = conn.cursor()
c.execute("update pour_time set ptime_end=? where id=?", [time.time(), pour_id])
conn.commit()
def main():
text_intro = (u"Welcome to KegUI")
text_enter_name = (u"Please enter your name before you pour.")
text_pour_name = urwid.Text(u"", align='center')
name = urwid.Edit(u"Name:", u"")
blank = urwid.Divider()
list_content = [blank,
blank,
urwid.Text(text_intro, align='center'),
blank,
urwid.Text(text_enter_name, align='center'),
blank,
urwid.Padding(
urwid.AttrWrap(name,
'editbx', 'editfc'), left=10, right=10),
blank,
text_pour_name]
listbox = urwid.ListBox(urwid.SimpleListWalker(list_content))
frame = urwid.Frame(urwid.AttrWrap(listbox, 'body'))
palette = [
('body','black','light gray', 'standout'),
('reverse','light gray','black'),
('header','white','dark red', 'bold'),
('important','dark blue','light gray',('standout','underline')),
('editfc','white', 'dark blue', 'bold'),
('editbx','light gray', 'dark blue'),
('editcp','black','light gray', 'standout'),
('bright','dark gray','light gray', ('bold','standout')),
('buttn','black','dark cyan'),
('buttnf','white','dark blue','bold'),
]
if urwid.web_display.is_web_request():
screen = urwid.web_display.Screen()
else:
screen = urwid.raw_display.Screen()
def unhandled(key):
global current_pour_user_id
global current_pour_id
if key == 'enter':
user = name.edit_text
user_id = get_or_create_user(user)
# If user has changed
if current_pour_user_id != user_id:
text_pour_name.set_text(("Pouring as, %s" % user))
# Start a new pour for user
pour_id = start_pour(user_id)
# Make sure it's not the first run
if current_pour_user_id != 0:
#stop the pour for the previous user
stop_pour(current_pour_id)
# Reset the current id
current_pour_user_id = user_id
current_pour_id = pour_id
if key == 'f8':
raise urwid.ExitMainLoop()
urwid.MainLoop(frame, palette, screen,
unhandled_input=unhandled).run()
def setup():
urwid.web_display.set_preferences("Urwid Tour")
# try to handle short web requests quickly
if urwid.web_display.handle_short_request():
return
main()
if '__main__'==__name__ or urwid.web_display.is_web_request():
setup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment