Force redraw after status thing
[public/pyceo-broken.git] / ceo / urwid / main.py
1 import os, grp, pwd, sys, random, urwid.curses_display
2 from ceo.urwid.widgets import *
3 from ceo.urwid.window import *
4 from ceo.urwid import newmember, renew, info, search, positions, groups, \
5     shell, library, databases
6
7 def program_name():
8     cwords = [ "CSC" ] * 20 + [ "Club" ] * 10 + [ "Campus" ] * 5 + \
9         [ "Communist", "Canadian", "Celestial", "Cryptographic", "Calum's",
10           "Canonical", "Capitalist", "Catastrophic", "Ceremonial", "Chaotic", "Civic",
11           "City", "County", "Caffeinated" ]
12     ewords = [ "Embellished", "Ergonomic", "Electric", "Eccentric", "European", "Economic",
13         "Evil", "Egotistical", "Elliptic", "Emasculating", "Embalming",
14         "Embryonic", "Emigrant", "Emissary's", "Emoting", "Employment", "Emulated",
15         "Enabling", "Enamoring", "Encapsulated", "Enchanted", "Encoded", "Encrypted",
16         "Encumbered", "Endemic", "Enhanced", "Enigmatic", "Enlightened", "Enormous",
17         "Enrollment", "Enshrouded", "Ephemeral", "Epidemic", "Episodic", "Epsilon",
18         "Equitable", "Equestrian", "Equilateral", "Erroneous", "Erratic",
19         "Espresso", "Essential", "Estate", "Esteemed", "Eternal", "Ethical", "Eucalyptus",
20         "Euphemistic", "Evangelist", "Evasive", "Everyday", "Evidence", "Eviction", "Evildoer's",
21         "Evolution", "Exacerbation", "Exalted", "Examiner's", "Excise", "Exciting", "Exclusion",
22         "Exec", "Executioner's", "Exile", "Existential", "Expedient", "Expert", "Expletive",
23         "Exploiter's", "Explosive", "Exponential", "Exposing", "Extortion", "Extraction",
24         "Extraneous", "Extravaganza", "Extreme", "Extraterrestrial", "Extremist", "Eerie" ]
25     owords = [ "Office" ] * 50 + [ "Outhouse", "Outpost" ]
26
27     cword = random.choice(cwords)
28     eword = random.choice(ewords)
29     oword = random.choice(owords)
30
31     return "%s %s %s" % (cword, eword, oword)
32
33 office_data = {
34     "name" : "Office Staff",
35     "group" : "office",
36     "groups" : [ "cdrom", "audio", "video", "www" ],
37 }
38
39 syscom_data = {
40     "name" : "Systems Committee",
41     "group" : "syscom",
42     "groups" : [ "office", "staff", "adm", "src" ],
43 }
44
45 def new_member(*args, **kwargs):
46     push_wizard("New Member", [
47         newmember.IntroPage,
48         newmember.InfoPage,
49         newmember.SignPage,
50         newmember.PassPage,
51         newmember.EndPage,
52     ], (60, 15))
53
54 def new_club(*args, **kwargs):
55     push_wizard("New Club Account", [
56         newmember.ClubIntroPage,
57         newmember.ClubInfoPage,
58         (newmember.EndPage, "club"),
59     ], (60, 15))
60
61 def new_club_user(*args, **kwargs):
62     push_wizard("New Club Rep Account", [
63         newmember.ClubUserIntroPage,
64         newmember.InfoPage,
65         newmember.SignPage,
66         newmember.PassPage,
67         (newmember.EndPage, "clubuser"),
68     ], (60, 15))
69
70 def manage_group(*args, **kwargs):
71     push_wizard("Manage Club or Group Members", [
72         groups.IntroPage,
73         groups.InfoPage,
74     ], (60, 15))
75
76 def renew_member(*args, **kwargs):
77     push_wizard("Renew Membership", [
78         renew.IntroPage,
79         renew.UserPage,
80         renew.EmailPage,
81         renew.EmailDonePage,
82         renew.TermPage,
83         renew.PayPage,
84         renew.EndPage,
85     ], (60, 15))
86
87 def renew_club_user(*args, **kwargs):
88     push_wizard("Renew Club Rep Account", [
89         renew.ClubUserIntroPage,
90         renew.UserPage,
91         renew.EmailPage,
92         renew.EmailDonePage,
93         (renew.TermPage, "clubuser"),
94         (renew.EndPage, "clubuser"),
95     ], (60, 15))
96
97 def display_member(data):
98     push_wizard("Display Member", [
99         renew.UserPage,
100         info.InfoPage,
101     ], (60, 15))
102
103 def search_members(data):
104     menu = make_menu([
105         ("Members by term", search_term, None),
106         ("Members by name", search_name, None),
107         ("Members by group", search_group, None),
108         ("Back", raise_back, None),
109     ])
110     push_window(menu, "Search Members")
111
112 def search_name(data):
113     push_wizard("By Name", [ search.NamePage ])
114
115 def search_term(data):
116     push_wizard("By Term", [ search.TermPage ])
117
118 def search_group(data):
119     push_wizard("By Group", [ search.GroupPage ])
120
121 def manage_positions(data):
122     push_wizard("Manage Positions", [
123         positions.IntroPage,
124         positions.InfoPage,
125         positions.EndPage,
126     ], (50, 15))
127
128 def change_shell(data):
129     push_wizard("Change Shell", [
130         shell.IntroPage,
131         shell.YouPage,
132         shell.ShellPage,
133         shell.EndPage
134     ], (50, 20))
135
136 def create_mysql_db(data):
137     push_wizard("Create MySQL database", [
138         databases.IntroPage,
139         databases.UserPage,
140         databases.EndPage,
141     ], (60, 15))
142
143 def check_group(group):
144     try:
145         me = pwd.getpwuid(os.getuid()).pw_name
146         return me in grp.getgrnam(group).gr_mem
147     except KeyError:
148         pass
149
150 def top_menu():
151     office_only = [
152         ("New Member", new_member, None),
153         ("New Club Rep", new_club_user, None),
154         ("Renew Membership", renew_member, None),
155         ("Renew Club Rep", renew_club_user, None),
156         ("New Club", new_club, None),
157         ("Library", library.library, None),
158     ]
159     syscom_only = [
160         ("Manage Club or Group Members", manage_group, None),
161         ("Manage Positions", manage_positions, None),
162         ("Manage Office Staff", groups.group_members, office_data),
163         ("Manage Systems Committee", groups.group_members, syscom_data),
164     ]
165     unrestricted = [
166         ("Display Member", display_member, None),
167         ("Search Members", search_members, None),
168         ("Change Shell", change_shell, None),
169         ("Create MySQL database", create_mysql_db, None),
170     ]
171     footer = [
172         ("Exit", raise_abort, None),
173     ]
174     menu = None
175
176     # reorder the menu for convenience
177     if not check_group('office') and not check_group('syscom'):
178         menu = labelled_menu([
179             ('Unrestricted', unrestricted),
180             ('Office Staff', office_only),
181             ('Systems Committee', syscom_only),
182             (None, footer)
183         ])
184     else:
185         menu = labelled_menu([
186             ('Office Staff', office_only),
187             ('Unrestricted', unrestricted),
188             ('Systems Committee', syscom_only),
189             (None, footer)
190         ])
191
192     return menu
193
194 def run():
195     push_window(top_menu(), program_name())
196     event_loop(ui)
197
198 def start():
199     ui.run_wrapper( run )
200
201 if __name__ == '__main__':
202     start()