Mercurial > cosocket
changeset 37:979b247cba5d
Serialization of conversation is now done in JSON format.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Mon, 27 Apr 2009 16:56:46 -0700 |
parents | d93c08dca64b |
children | 5fea1533e8ff |
files | .hgignore openwebchat.py |
diffstat | 2 files changed, 15 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgignore Mon Apr 27 16:48:46 2009 -0700 +++ b/.hgignore Mon Apr 27 16:56:46 2009 -0700 @@ -1,2 +1,3 @@ syntax: glob *.pyc +*.dat
--- a/openwebchat.py Mon Apr 27 16:48:46 2009 -0700 +++ b/openwebchat.py Mon Apr 27 16:56:46 2009 -0700 @@ -8,18 +8,23 @@ from cosocket import * import channels +try: + import json +except ImportError: + import simplejson as json + class Conversation(list): def __init__(self, fileobj): list.__init__(self) self.__file = fileobj items = [] for line in self.__file.readlines(): - items.append(eval(line)) + items.append(json.loads(line)) self[:] = items def append(self, item): list.append(self, item) - self.__file.write('%s\n' % repr(item)) + self.__file.write('%s\n' % json.dumps(item)) self.__file.flush() class OpenWebChatServer(object): @@ -109,14 +114,14 @@ i = 0 while 1: while i < len(self._conv): - msg = self._conv[i] + msg = json.dumps(self._conv[i]) i += 1 yield self._until_multipart_part_sent(self.BOUNDARY, msg) yield channels.until_message_received('new-message') elif req_parts[1] == '/send': length = int(headers.getheader('Content-Length', 0)) msg = yield until_received(bytes = length) - self._conv.append(msg) + self._conv.append(json.loads(msg)) yield channels.until_message_sent('new-message', None) yield self._until_http_response_sent('sent.') elif req_parts[1] in ['/', '/jquery.js', '/openwebchat.js', @@ -137,9 +142,11 @@ code = 404) if __name__ == '__main__': - if not os.path.exists('conversation.txt'): - open('conversation.txt', 'w').close() + CONVERSATION_FILE = 'conversation.dat' + + if not os.path.exists(CONVERSATION_FILE): + open(CONVERSATION_FILE, 'w').close() server = OpenWebChatServer(('127.0.0.1', 8071), - Conversation(open('conversation.txt', 'r+w'))) + Conversation(open(CONVERSATION_FILE, 'r+w'))) server.run()