Fix group modification code
[public/pyceo-broken.git] / ceo / urwid / main.py
1 import sys, random, ldap, urwid.curses_display, getpass
2 from ceo import members, ldapi
3 from ceo.urwid.widgets import *
4 from ceo.urwid.window import *
5 from ceo.urwid import newmember, renew, info, search, positions, groups, shell
6
7 ui = urwid.curses_display.Screen()
8
9 ui.register_palette([
10     # name, foreground, background, mono
11     ('banner', 'light gray', 'default', None),
12     ('menu', 'light gray', 'default', 'bold'),
13     ('selected', 'black', 'light gray', 'bold'),
14 ])
15
16
17 def program_name():
18     cwords = [ "CSC" ] * 20 + [ "Club" ] * 10 + [ "Campus" ] * 5 + \
19         [ "Communist", "Canadian", "Celestial", "Cryptographic", "Calum's",
20           "Canonical", "Capitalist", "Catastrophic", "Ceremonial", "Chaotic", "Civic",
21           "City", "County", "Caffeinated" ]
22     ewords = [ "Embellished", "Ergonomic", "Electric", "Eccentric", "European", "Economic",
23         "Evil", "Egotistical", "Elliptic", "Emasculating", "Embalming",
24         "Embryonic", "Emigrant", "Emissary's", "Emoting", "Employment", "Emulated",
25         "Enabling", "Enamoring", "Encapsulated", "Enchanted", "Encoded", "Encrypted",
26         "Encumbered", "Endemic", "Enhanced", "Enigmatic", "Enlightened", "Enormous",
27         "Enrollment", "Enshrouded", "Ephemeral", "Epidemic", "Episodic", "Epsilon",
28         "Equitable", "Equestrian", "Equilateral", "Erroneous", "Erratic",
29         "Espresso", "Essential", "Estate", "Esteemed", "Eternal", "Ethical", "Eucalyptus",
30         "Euphemistic", "Evangelist", "Evasive", "Everyday", "Evidence", "Eviction", "Evildoer's",
31         "Evolution", "Exacerbation", "Exalted", "Examiner's", "Excise", "Exciting", "Exclusion",
32         "Exec", "Executioner's", "Exile", "Existential", "Expedient", "Expert", "Expletive",
33         "Exploiter's", "Explosive", "Exponential", "Exposing", "Extortion", "Extraction",
34         "Extraneous", "Extravaganza", "Extreme", "Extraterrestrial", "Extremist", "Eerie" ]
35     owords = [ "Office" ] * 50 + [ "Outhouse", "Outpost" ]
36
37     cword = random.choice(cwords)
38     eword = random.choice(ewords)
39     oword = random.choice(owords)
40
41     return "%s %s %s" % (cword, eword, oword)
42
43 office_data = {
44     "name" : "Office Staff",
45     "group" : "office",
46     "groups" : [ "cdrom", "audio", "video", "www" ],
47 }
48
49 syscom_data = {
50     "name" : "Systems Committee",
51     "group" : "syscom",
52     "groups" : [ "office", "staff", "adm", "src" ],
53 }
54
55 def main_menu():
56     menu = [
57         ("New Member", new_member, None),
58         ("New Club Rep", new_club_user, None),
59         ("Renew Membership", renew_member, None),
60         ("Renew Club Rep", renew_club_user, None),
61         ("New Club", new_club, None),
62         ("Display Member", display_member, None),
63         ("Change Shell", change_shell, None),
64         ("Search", search_members, None),
65         ("Manage Club or Group Members", manage_group, None),
66         ("Manage Positions", manage_positions, None),
67         ("Manage Office Staff", groups.group_members, office_data),
68         ("Manage Systems Committee", groups.group_members, syscom_data),
69         ("Exit", raise_abort, None),
70     ]
71
72     listbox = urwid.ListBox( menu_items( menu ) )
73     return listbox
74
75 def new_member(*args, **kwargs):
76     push_wizard("New Member", [
77         newmember.IntroPage,
78         newmember.InfoPage,
79         newmember.SignPage,
80         newmember.PassPage,
81         newmember.EndPage,
82     ])
83
84 def new_club(*args, **kwargs):
85     push_wizard("New Club Account", [
86         newmember.ClubIntroPage,
87         newmember.ClubInfoPage,
88         (newmember.EndPage, "club"),
89     ], (60, 15))
90
91 def new_club_user(*args, **kwargs):
92     push_wizard("New Club Rep Account", [
93         newmember.ClubUserIntroPage,
94         newmember.InfoPage,
95         newmember.SignPage,
96         newmember.PassPage,
97         (newmember.EndPage, "clubuser"),
98     ], (60, 15))
99
100 def manage_group(*args, **kwargs):
101     push_wizard("Manage Club or Group Members", [
102         groups.IntroPage,
103         groups.InfoPage,
104     ], (60, 15))
105
106 def renew_member(*args, **kwargs):
107     push_wizard("Renew Membership", [
108         renew.IntroPage,
109         renew.UserPage,
110         renew.TermPage,
111         renew.PayPage,
112         renew.EndPage,
113     ])
114
115 def renew_club_user(*args, **kwargs):
116     push_wizard("Renew Club Rep Account", [
117         renew.ClubUserIntroPage,
118         renew.UserPage,
119         (renew.TermPage, "clubuser"),
120         (renew.EndPage, "clubuser"),
121     ], (60, 15))
122
123 def display_member(data):
124     push_wizard("Display Member", [
125         renew.UserPage,
126         info.InfoPage,
127     ], (60, 15))
128
129 def search_members(data):
130     menu = [
131         ("Members by term", search_term, None),
132         ("Members by name", search_name, None),
133         ("Members by group", search_group, None),
134         ("Back", raise_back, None),
135     ]
136
137     listbox = urwid.ListBox( menu_items( menu ) )
138     push_window(listbox, "Search")
139
140 def search_name(data):
141     push_wizard("By Name", [ search.NamePage ])
142
143 def search_term(data):
144     push_wizard("By Term", [ search.TermPage ])
145
146 def search_group(data):
147     push_wizard("By Group", [ search.GroupPage ])
148
149 def manage_positions(data):
150     push_wizard("Manage Positions", [
151         positions.IntroPage,
152         positions.InfoPage,
153         positions.EndPage,
154     ], (50, 15))
155
156 def change_shell(data):
157     push_wizard("Change Shell", [
158         shell.IntroPage,
159         shell.YouPage,
160         shell.ShellPage,
161         shell.EndPage
162     ], (50, 20))
163
164 def run():
165     push_window( main_menu(), program_name() )
166     event_loop( ui )
167
168 def start():
169     try:
170         print "Connecting...\n",
171         sys.stdout.flush()
172         members.connect(AuthCallback())
173         print "done."
174
175         ui.run_wrapper( run )
176     except ldap.LOCAL_ERROR, e:
177         print ldapi.format_ldaperror(e)
178     except ldap.INSUFFICIENT_ACCESS, e:
179         print ldapi.format_ldaperror(e)
180         print "You probably aren't permitted to do whatever you just tried."
181         print "Admittedly, ceo probably shouldn't have crashed either."
182
183 class AuthCallback:
184     def callback(self, error):
185         try:
186             return getpass.getpass("Password: ")
187         except KeyboardInterrupt:
188             print ""
189             sys.exit(1)
190
191 if __name__ == '__main__':
192     start()