b9694cc493a03796ccc43fb267a0460c739dc484
[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 def new_member(*args, **kwargs):
34     push_wizard("New Member", [
35         newmember.IntroPage,
36         newmember.InfoPage,
37         newmember.NumberOfTermsPage,
38         newmember.SignPage,
39         newmember.PassPage,
40         newmember.EndPage,
41     ], (60, 15))
42
43 def new_club(*args, **kwargs):
44     push_wizard("New Club Account", [
45         newmember.ClubIntroPage,
46         newmember.ClubInfoPage,
47         (newmember.EndPage, "club"),
48     ], (60, 15))
49
50 def new_club_user(*args, **kwargs):
51     push_wizard("New Club Rep Account", [
52         newmember.ClubUserIntroPage,
53         newmember.InfoPage,
54         newmember.NumberOfTermsPage,
55         newmember.SignPage,
56         newmember.PassPage,
57         (newmember.EndPage, "clubuser"),
58     ], (60, 15))
59
60 def manage_group(*args, **kwargs):
61     push_wizard("Manage Club or Group Members", [
62         groups.IntroPage,
63         groups.InfoPage,
64     ], (60, 15))
65
66 def renew_member(*args, **kwargs):
67     push_wizard("Renew Membership", [
68         renew.IntroPage,
69         renew.UserPage,
70         renew.EmailPage,
71         renew.EmailDonePage,
72         renew.TermPage,
73         renew.PayPage,
74         renew.EndPage,
75     ], (60, 15))
76
77 def renew_club_user(*args, **kwargs):
78     push_wizard("Renew Club Rep Account", [
79         renew.ClubUserIntroPage,
80         renew.UserPage,
81         renew.EmailPage,
82         renew.EmailDonePage,
83         (renew.TermPage, "clubuser"),
84         (renew.EndPage, "clubuser"),
85     ], (60, 15))
86
87 def display_member(data):
88     push_wizard("Display Member", [
89         renew.UserPage,
90         info.InfoPage,
91     ], (60, 15))
92
93 def search_members(data):
94     menu = make_menu([
95         ("Members by term", search_term, None),
96         ("Members by name", search_name, None),
97         ("Members by group", search_group, None),
98         ("Back", raise_back, None),
99     ])
100     push_window(menu, "Search Members")
101
102 def search_name(data):
103     push_wizard("By Name", [ search.NamePage ])
104
105 def search_term(data):
106     push_wizard("By Term", [ search.TermPage ])
107
108 def search_group(data):
109     push_wizard("By Group", [ search.GroupPage ])
110
111 def manage_positions(data):
112     push_wizard("Manage Positions", [
113         positions.IntroPage,
114         positions.InfoPage,
115         positions.EndPage,
116     ], (50, 15))
117
118 def change_shell(data):
119     push_wizard("Change Shell", [
120         shell.IntroPage,
121         shell.YouPage,
122         shell.ShellPage,
123         shell.EndPage
124     ], (50, 20))
125
126 def create_mysql_db(data):
127     push_wizard("Create MySQL database", [
128         databases.IntroPage,
129         databases.UserPage,
130         databases.EndPage,
131     ], (60, 15))
132
133 def check_group(group):
134     try:
135         me = pwd.getpwuid(os.getuid()).pw_name
136         return me in grp.getgrnam(group).gr_mem
137     except KeyError:
138         pass
139
140 def top_menu():
141     office_only = [
142         ("New Member", new_member, None),
143         ("New Club Rep", new_club_user, None),
144         ("Renew Membership", renew_member, None),
145         ("Renew Club Rep", renew_club_user, None),
146         ("New Club", new_club, None),
147         ("Library", library.library, None),
148     ]
149     syscom_only = [
150         ("Manage Club or Group Members", manage_group, None),
151         ("Manage Positions", manage_positions, None),
152     ]
153     unrestricted = [
154         ("Display Member", display_member, None),
155         ("Search Members", search_members, None),
156         ("Change Shell", change_shell, None),
157         ("Create MySQL database", create_mysql_db, None),
158     ]
159     footer = [
160         ("Exit", raise_abort, None),
161     ]
162     menu = None
163
164     # reorder the menu for convenience
165     if not check_group('office') and not check_group('syscom'):
166         menu = labelled_menu([
167             ('Unrestricted', unrestricted),
168             ('Office Staff', office_only),
169             ('Systems Committee', syscom_only),
170             (None, footer)
171         ])
172     else:
173         menu = labelled_menu([
174             ('Office Staff', office_only),
175             ('Unrestricted', unrestricted),
176             ('Systems Committee', syscom_only),
177             (None, footer)
178         ])
179
180     return menu
181
182 def run():
183     push_window(top_menu(), program_name())
184     event_loop(ui)
185
186 def start():
187     ui.run_wrapper( run )
188
189 if __name__ == '__main__':
190     start()