forked from ctb/meep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meeplib.py
180 lines (135 loc) · 4.1 KB
/
meeplib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
meeplib - A simple message board back-end implementation.
Functions and classes:
* u = User(username, password) - creates & saves a User object. u.id
is a guaranteed unique integer reference.
* m = Message(title, post, author) - creates & saves a Message object.
'author' must be a User object. 'm.id' guaranteed unique integer.
* get_all_messages() - returns a list of all Message objects.
* get_all_users() - returns a list of all User objects.
* delete_message(m) - deletes Message object 'm' from internal lists.
* delete_user(u) - deletes User object 'u' from internal lists.
* get_user(username) - retrieves User object for user 'username'.
* get_message(msg_id) - retrieves Message object for message with id msg_id.
"""
import cPickle
from Cookie import SimpleCookie
__all__ = ['Message', 'get_all_messages', 'get_message', 'delete_message',
'User', 'get_user', 'get_all_users', 'delete_user']
###
# internal data structures & functions; please don't access these
# directly from outside the module. Note, I'm not responsible for
# what happens to you if you do access them directly. CTB
# a string, stores the current user that is logged on
_curr_user = []
# a dictionary, storing all messages by a (unique, int) ID -> Message object.
_messages = {}
def _get_next_message_id():
if _messages:
return max(_messages.keys()) + 1
return len(_messages)
# a dictionary, storing all users by a (unique, int) ID -> User object.
_user_ids = {}
# a dictionary, storing all users by username
_users = {}
def _get_next_user_id():
if _users:
return max(_user_ids.keys()) + 1
return 0
def _reset():
"""
Clean out all persistent data structures, for testing purposes.
"""
global _messages, _users, _user_ids, _curr_user
_messages = {}
_users = {}
_user_ids = {}
_curr_user = []
def save_state():
filename = "save.pickle"
fp = open(filename, 'w')
objects = (_messages, _user_ids, _users)
cPickle.dump(objects, fp)
print "saving"
print _messages
fp.close()
def load_state():
try:
filename = "save.pickle"
fp = open(filename, 'r')
objects = cPickle.load(fp)
(_messages, _user_ids, _users) = objects
print "successfully loaded"
print _messages, _user_ids, _users
return _messages, _user_ids, _users
except IOError:
print "meeplib.load_state() IOError"
return {}, {}, {}
###
#modify to include parent and children ids
class Message(object):
"""
Simple "Message" object, containing title/post/author.
'author' must be an object of type 'User'.
'child' must be an object of type 'array'
"""
def __init__(self, title, post, author, parent, child):
self.title = title
self.post = post
self.parent = parent
self.child = child
assert isinstance(author, User)
self.author = author
self._save_message()
def _save_message(self):
self.id = _get_next_message_id()
# register this new message with the messages list:
_messages[self.id] = self
def update_children(parent_msg, child_id):
parent_msg.child.append(child_id)
def get_all_messages(sort_by='id'):
return _messages.values()
def get_message(id):
return _messages[id]
def delete_message(msg):
assert isinstance(msg, Message)
del _messages[msg.id]
###
class User(object):
def __init__(self, username, password):
self.username = username
self.password = password
self._save_user()
def _save_user(self):
self.id = _get_next_user_id()
# register new user ID with the users list:
_user_ids[self.id] = self
_users[self.username] = self
def set_curr_user(username):
_curr_user.insert(0, username)
def get_curr_user():
return _curr_user[0]
def delete_curr_user(username):
_curr_user.remove(_curr_user.index(0))
def get_user(username):
return _users.get(username) # return None if no such user
def get_all_users():
return _users.values()
def delete_user(user):
del _users[user.username]
del _user_ids[user.id]
def check_user(username, password):
try:
aUser = get_user(username)
except NameError:
aUser = None
try:
password
except NameError:
password = None
if aUser is not None:
if aUser.password is not None:
if aUser.password == password:
return True
else:
return False