Add password prompt
[mspang/pyceo.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" : [ "office", "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         ("Renew Membership", renew_member, None),
59         ("Create Club Account", new_club, None),
60         ("Display Member", display_member, None),
61         ("Change Shell", change_shell, None),
62         ("Search", search_members, None),
63         ("Manage Club or Group Members", manage_group, None),
64         ("Manage Positions", manage_positions, None),
65         ("Manage Office Staff", groups.group_members, office_data),
66         ("Manage Systems Committee", groups.group_members, syscom_data),
67         ("Exit", raise_abort, None),
68     ]
69
70     listbox = urwid.ListBox( menu_items( menu ) )
71     return listbox
72
73 def new_member(*args, **kwargs):
74     push_wizard("New Member", [
75         newmember.IntroPage,
76         newmember.InfoPage,
77         newmember.SignPage,
78         newmember.PassPage,
79         newmember.EndPage,
80     ])
81
82 def new_club(*args, **kwargs):
83     push_wizard("New Club Account", [
84         newmember.ClubIntroPage,
85         newmember.ClubInfoPage,
86         (newmember.EndPage, "club"),
87     ], (60, 15))
88
89 def manage_group(*args, **kwargs):
90     push_wizard("Manage Club or Group Members", [
91         groups.IntroPage,
92         groups.InfoPage,
93     ], (60, 15))
94
95 def renew_member(*args, **kwargs):
96     push_wizard("Renew Membership", [
97         renew.IntroPage,
98         renew.UserPage,
99         renew.TermPage,
100         renew.PayPage,
101         renew.EndPage,
102     ])
103
104 def display_member(data):
105     push_wizard("Display Member", [
106         renew.UserPage,
107         info.InfoPage,
108     ], (60, 15))
109
110 def search_members(data):
111     menu = [
112         ("Members by term", search_term, None),
113         ("Members by name", search_name, None),
114         ("Members by group", search_group, None),
115         ("Back", raise_back, None),
116     ]
117
118     listbox = urwid.ListBox( menu_items( menu ) )
119     push_window(listbox, "Search")
120
121 def search_name(data):
122     push_wizard("By Name", [ search.NamePage ])
123
124 def search_term(data):
125     push_wizard("By Term", [ search.TermPage ])
126
127 def search_group(data):
128     push_wizard("By Group", [ search.GroupPage ])
129
130 def manage_positions(data):
131     push_wizard("Manage Positions", [
132         positions.IntroPage,
133         positions.InfoPage,
134         positions.EndPage,
135     ], (50, 15))
136
137 def change_shell(data):
138     push_wizard("Change Shell", [
139         shell.IntroPage,
140         shell.YouPage,
141         shell.ShellPage,
142         shell.EndPage
143     ], (50, 20))
144
145 def run():
146     push_window( main_menu(), program_name() )
147     event_loop( ui )
148
149 def start():
150     try:
151         print "Connecting...\n",
152         sys.stdout.flush()
153         members.connect(AuthCallback())
154         print "done."
155
156         ui.run_wrapper( run )
157     except ldap.LOCAL_ERROR, e:
158         print ldapi.format_ldaperror(e)
159         print "Hint: You may need to run 'kinit'"
160     except ldap.INSUFFICIENT_ACCESS, e:
161         print ldapi.format_ldaperror(e)
162         print "You probably aren't permitted to do whatever you just tried."
163         print "Admittedly, ceo probably shouldn't have crashed either."
164
165 class AuthCallback:
166     def callback(self, error):
167         try:
168             return getpass.getpass("Password: ")
169         except KeyboardInterrupt:
170             print ""
171             sys.exit(1)
172
173 if __name__ == '__main__':
174     start()