Reorganize namespace
[mspang/pyceo.git] / ceo / urwid / main.py
1 import random, ldap, urwid.curses_display
2 from ceo import members, terms
3 from ceo.excep import InvalidArgument
4 from ceo.urwid.widgets import *
5 from ceo.urwid.window import *
6 from ceo.urwid import newmember, renew, info, search, positions, groups
7
8 ui = urwid.curses_display.Screen()
9
10 ui.register_palette([
11     # name, foreground, background, mono
12     ('banner', 'light gray', 'default', None),
13     ('menu', 'light gray', 'default', 'bold'),
14     ('selected', 'black', 'light gray', 'bold'),
15 ])
16
17
18 def program_name():
19     cwords = [ "CSC" ] * 20 + [ "Club" ] * 10 + [ "Campus" ] * 5 + \
20         [ "Communist", "Canadian", "Celestial", "Cryptographic", "Calum's",
21           "Canonical", "Capitalist", "Catastrophic", "Ceremonial", "Chaotic", "Civic",
22           "City", "County", "Caffeinated" ]
23     ewords = [ "Embellished", "Ergonomic", "Electric", "Eccentric", "European", "Economic",
24         "Evil", "Egotistical", "Elliptic", "Emasculating", "Embalming",
25         "Embryonic", "Emigrant", "Emissary's", "Emoting", "Employment", "Emulated",
26         "Enabling", "Enamoring", "Encapsulated", "Enchanted", "Encoded", "Encrypted",
27         "Encumbered", "Endemic", "Enhanced", "Enigmatic", "Enlightened", "Enormous",
28         "Enrollment", "Enshrouded", "Ephemeral", "Epidemic", "Episodic", "Epsilon",
29         "Equitable", "Equestrian", "Equilateral", "Erroneous", "Erratic",
30         "Espresso", "Essential", "Estate", "Esteemed", "Eternal", "Ethical", "Eucalyptus",
31         "Euphemistic", "Evangelist", "Evasive", "Everyday", "Evidence", "Eviction", "Evildoer's",
32         "Evolution", "Exacerbation", "Exalted", "Examiner's", "Excise", "Exciting", "Exclusion",
33         "Exec", "Executioner's", "Exile", "Existential", "Expedient", "Expert", "Expletive",
34         "Exploiter's", "Explosive", "Exponential", "Exposing", "Extortion", "Extraction",
35         "Extraneous", "Extravaganza", "Extreme", "Extraterrestrial", "Extremist", "Eerie" ]
36     owords = [ "Office" ] * 50 + [ "Outhouse", "Outpost" ]
37
38     cword = random.choice(cwords)
39     eword = random.choice(ewords)
40     oword = random.choice(owords)
41
42     return "%s %s %s" % (cword, eword, oword)
43
44 office_data = {
45     "name" : "Office Staff",
46     "group" : "office",
47     "groups" : [ "office", "cdrom", "audio", "video", "www" ],
48 }
49
50 syscom_data = {
51     "name" : "Systems Committee",
52     "group" : "syscom",
53     "groups" : [ "office", "staff", "adm", "src" ],
54 }
55
56 def menu_items(items):
57     return [ urwid.AttrWrap( ButtonText( cb, data, txt ), 'menu', 'selected') for (txt, cb, data) in items ]
58
59 def main_menu():
60     menu = [
61         ("New Member", new_member, None),
62         ("Renew Membership", renew_member, None),
63         ("Create Club Account", new_club, None),
64         ("Display Member", display_member, None),
65         ("Search", search_members, None),
66         ("Manage Club or Group Members", manage_group, None),
67         ("Manage Positions", manage_positions, None),
68         ("Manage Office Staff", groups.group_members, office_data),
69         ("Manage Systems Committee", groups.group_members, syscom_data),
70         ("Exit", raise_abort, None),
71     ]
72
73     listbox = urwid.ListBox( menu_items( menu ) )
74     return listbox
75
76 def new_member(*args, **kwargs):
77     push_wizard("New Member", [
78         newmember.IntroPage,
79         newmember.InfoPage,
80         newmember.SignPage,
81         newmember.PassPage,
82         newmember.EndPage,
83     ])
84
85 def new_club(*args, **kwargs):
86     push_wizard("New Club Account", [
87         newmember.ClubIntroPage,
88         newmember.ClubInfoPage,
89         (newmember.EndPage, "club"),
90     ], (60, 15))
91
92 def manage_group(*args, **kwargs):
93     push_wizard("Manage Club or Group Members", [
94         groups.IntroPage,
95         groups.InfoPage,
96     ], (60, 15))
97
98 def renew_member(*args, **kwargs):
99     push_wizard("Renew Membership", [
100         renew.IntroPage,
101         renew.UserPage,
102         renew.TermPage,
103         renew.PayPage,
104         renew.EndPage,
105     ])
106
107 def display_member(data):
108     push_wizard("Display Member", [
109         renew.UserPage,
110         info.InfoPage,
111     ], (60, 15))
112
113 def search_members(data):
114     menu = [
115         ("Members by term", search_term, None),
116         ("Members by name", search_name, None),
117         ("Members by group", search_group, None),
118         ("Back", raise_back, None),
119     ]
120
121     listbox = urwid.ListBox( menu_items( menu ) )
122     push_window(listbox, "Search")
123
124 def search_name(data):
125     push_wizard("By Name", [ search.NamePage ])
126
127 def search_term(data):
128     push_wizard("By Term", [ search.TermPage ])
129
130 def search_group(data):
131     push_wizard("By Group", [ search.GroupPage ])
132
133 def manage_positions(data):
134     push_wizard("Manage Positions", [
135         positions.IntroPage,
136         positions.InfoPage,
137         positions.EndPage,
138     ], (50, 15))
139
140 def run():
141     members.connect()
142
143     push_window( main_menu(), program_name() )
144     event_loop( ui )
145
146 def start():
147     try:
148         ui.run_wrapper( run )
149     except ldap.LOCAL_ERROR, e:
150         print e[0]['info']
151         print "Hint: You may need to run 'kinit'"
152
153 if __name__ == '__main__':
154     import os
155     start()