Add experimental urwid-based GUI
[public/pyceo-broken.git] / pylib / csc / apps / urwid / main.py
1 import random, time
2 import urwid, urwid.curses_display
3
4 from csc.apps.urwid.widgets import *
5 from csc.apps.urwid.window import *
6 import csc.apps.urwid.newmember as newmember
7 import csc.apps.urwid.renew as renew
8 import csc.apps.urwid.info as info
9 import csc.apps.urwid.search as search
10
11 from csc.adm import accounts, members, terms
12 from csc.common.excep import InvalidArgument
13
14 ui = urwid.curses_display.Screen()
15
16 ui.register_palette([
17     # name, foreground, background, mono
18     ('banner', 'light gray', 'default', None),
19     ('menu', 'light gray', 'default', 'bold'),
20     ('selected', 'black', 'light gray', 'bold'),
21 ])
22
23
24 def program_name():
25     cwords = [ "CSC" ] * 20 + [ "Club" ] * 10 + [ "Campus" ] * 5 + \
26         [ "Communist", "Canadian", "Celestial", "Cryptographic", "Calum's",
27           "Canonical", "Capitalist", "Catastrophic", "Ceremonial", "Chaotic", "Civic",
28           "City", "County", "Caffeinated" ]
29     ewords = [ "Embellished", "Ergonomic", "Electric", "Eccentric", "European", "Economic",
30         "Evil", "Egotistical", "Elliptic", "Emasculating", "Embalming",
31         "Embryonic", "Emigrant", "Emissary's", "Emoting", "Employment", "Emulated",
32         "Enabling", "Enamoring", "Encapsulated", "Enchanted", "Encoded", "Encrypted",
33         "Encumbered", "Endemic", "Enhanced", "Enigmatic", "Enlightened", "Enormous",
34         "Enrollment", "Enshrouded", "Ephermal", "Epidemic", "Episodic", "Epsilon",
35         "Equitable", "Equestrian", "Equilateral", "Erroneous", "Erratic",
36         "Espresso", "Essential", "Estate", "Esteemed", "Eternal", "Ethical", "Eucalyptus",
37         "Euphemistic", "Envangelist", "Evasive", "Everyday", "Evidence", "Eviction", "Evildoer's",
38         "Evolution", "Exacerbation", "Exalted", "Examiner's", "Excise", "Exciting", "Exclusion",
39         "Exec", "Executioner's", "Exile", "Existential", "Expedient", "Expert", "Expletive",
40         "Exploiter's", "Explosive", "Exponential", "Exposing", "Extortion", "Extraction",
41         "Extraneous", "Extravaganza", "Extreme", "Extraterrestrial", "Extremist", "Eerie" ]
42     owords = [ "Office" ] * 50 + [ "Outhouse", "Outpost" ]
43
44     cword = random.choice(cwords)
45     eword = random.choice(ewords)
46     oword = random.choice(owords)
47
48     return "%s %s %s" % (cword, eword, oword)
49
50 def menu_items(items):
51     return [ urwid.AttrWrap( ButtonText( cb, txt ), 'menu', 'selected') for (txt, cb) in items ]
52
53 def main_menu():
54     menu = [
55         ("New Member", new_member),
56         ("Renew Membership", renew_member),
57         ("Display Member", display_member),
58         ("Search", search_members),
59         ("Exit", raise_abort),
60     ]
61
62     listbox = urwid.ListBox( menu_items( menu ) )
63     return listbox
64
65 def push_wizard(name, pages, dimensions=(50, 10)):
66     state = {}
67     wiz = Wizard()
68     for page in pages:
69         wiz.add_panel( page(state) )
70     push_window( urwid.Filler( urwid.Padding(
71         urwid.LineBox(wiz), 'center', dimensions[0]),
72         'middle', dimensions[1] ), name )
73     
74 def new_member(*args, **kwargs):
75     push_wizard("New Member", [
76         newmember.IntroPage,
77         newmember.InfoPage,
78         newmember.SignPage,
79         newmember.PassPage,
80         newmember.EndPage,
81     ])
82
83 def renew_member(*args, **kwargs):
84     push_wizard("Renew Membership", [
85         renew.IntroPage,
86         renew.UserPage,
87         renew.TermPage,
88         renew.PayPage,
89         renew.EndPage,
90     ])
91
92 def display_member(a):
93     push_wizard("Display Member", [
94         renew.UserPage,
95         info.InfoPage,
96     ], (60, 15))
97
98 def search_members(a):
99     menu = [
100         ("Members by term", search_term),
101         ("Members by name", search_name),
102         ("Back", raise_back),
103     ]
104
105     listbox = urwid.ListBox( menu_items( menu ) )
106     push_window(listbox, "Search")
107
108 def search_name(a):
109     push_wizard("By Name", [ search.NamePage ])
110
111 def search_term(a):
112     push_wizard("By Term", [ search.TermPage ])
113
114 def run():
115     push_window( main_menu(), program_name() )
116     event_loop( ui )
117
118 def start():
119     ui.run_wrapper( run )
120
121 if __name__ == '__main__':
122     start()