ba877f4fbde3cb464be9e32caac5a6bb4481dd39
[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 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 new_member(*args, **kwargs):
56     push_wizard("New Member", [
57         newmember.IntroPage,
58         newmember.InfoPage,
59         newmember.SignPage,
60         newmember.PassPage,
61         newmember.EndPage,
62     ], (60, 15))
63
64 def new_club(*args, **kwargs):
65     push_wizard("New Club Account", [
66         newmember.ClubIntroPage,
67         newmember.ClubInfoPage,
68         (newmember.EndPage, "club"),
69     ], (60, 15))
70
71 def new_club_user(*args, **kwargs):
72     push_wizard("New Club Rep Account", [
73         newmember.ClubUserIntroPage,
74         newmember.InfoPage,
75         newmember.SignPage,
76         newmember.PassPage,
77         (newmember.EndPage, "clubuser"),
78     ], (60, 15))
79
80 def manage_group(*args, **kwargs):
81     push_wizard("Manage Club or Group Members", [
82         groups.IntroPage,
83         groups.InfoPage,
84     ], (60, 15))
85
86 def renew_member(*args, **kwargs):
87     push_wizard("Renew Membership", [
88         renew.IntroPage,
89         renew.UserPage,
90         renew.EmailPage,
91         renew.EmailDonePage,
92         renew.TermPage,
93         renew.PayPage,
94         renew.EndPage,
95     ], (60, 15))
96
97 def renew_club_user(*args, **kwargs):
98     push_wizard("Renew Club Rep Account", [
99         renew.ClubUserIntroPage,
100         renew.UserPage,
101         renew.EmailPage,
102         renew.EmailDonePage,
103         (renew.TermPage, "clubuser"),
104         (renew.EndPage, "clubuser"),
105     ], (60, 15))
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 = make_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     push_window(menu, "Search")
121
122 def search_name(data):
123     push_wizard("By Name", [ search.NamePage ])
124
125 def search_term(data):
126     push_wizard("By Term", [ search.TermPage ])
127
128 def search_group(data):
129     push_wizard("By Group", [ search.GroupPage ])
130
131 def manage_positions(data):
132     push_wizard("Manage Positions", [
133         positions.IntroPage,
134         positions.InfoPage,
135         positions.EndPage,
136     ], (50, 15))
137
138 def change_shell(data):
139     push_wizard("Change Shell", [
140         shell.IntroPage,
141         shell.YouPage,
142         shell.ShellPage,
143         shell.EndPage
144     ], (50, 20))
145
146 def create_mysql_db(data):
147     push_wizard("Create MySQL database", [
148         databases.IntroPage,
149         databases.UserPage,
150         databases.EndPage,
151     ], (60, 15))
152
153 def check_group(group):
154     try:
155         me = pwd.getpwuid(os.getuid()).pw_name
156         return me in grp.getgrnam(group).gr_mem
157     except KeyError:
158         pass
159
160 def top_menu():
161     office_only = [
162         ("New Member", new_member, None),
163         ("New Club Rep", new_club_user, None),
164         ("Renew Membership", renew_member, None),
165         ("Renew Club Rep", renew_club_user, None),
166         ("New Club", new_club, None),
167         ("Library", library.library, None),
168     ]
169     syscom_only = [
170         ("Manage Club or Group Members", manage_group, None),
171         ("Manage Positions", manage_positions, None),
172         ("Manage Office Staff", groups.group_members, office_data),
173         ("Manage Systems Committee", groups.group_members, syscom_data),
174     ]
175     unrestricted = [
176         ("Display Member", display_member, None),
177         ("Search", search_members, None),
178         ("Change Shell", change_shell, None),
179         ("Create MySQL database", create_mysql_db, None),
180     ]
181     footer = [
182         ("Exit", raise_abort, None),
183     ]
184     menu = None
185
186     # reorder the menu for convenience
187     if not check_group('office') and not check_group('syscom'):
188         menu = labelled_menu([
189             ('Unrestricted', unrestricted),
190             ('Office Staff', office_only),
191             ('Systems Committee', syscom_only),
192             (None, footer)
193         ])
194     else:
195         menu = labelled_menu([
196             ('Office Staff', office_only),
197             ('Unrestricted', unrestricted),
198             ('Systems Committee', syscom_only),
199             (None, footer)
200         ])
201
202     return menu
203
204 def run():
205     push_window(top_menu(), program_name())
206     event_loop(ui)
207
208 def start():
209     ui.run_wrapper( run )
210
211 if __name__ == '__main__':
212     start()