1-
2- import sys , time , os , random
1+ import os
2+ import random
3+ import sys
4+ import time
35
46import transaction
7+ from BTrees import OOBTree
58from persistent import Persistent
6-
79from ZEO import ClientStorage
10+
811import ZODB
912from ZODB .POSException import ConflictError
10- from BTrees import OOBTree
13+
1114
1215class ChatSession (Persistent ):
1316
@@ -32,7 +35,6 @@ def __init__(self, name):
3235 # Internal attribute: _messages holds all the chat messages.
3336 self ._messages = OOBTree .OOBTree ()
3437
35-
3638 def new_messages (self ):
3739 "Return new messages."
3840
@@ -46,7 +48,7 @@ def new_messages(self):
4648
4749 for T2 , message in self ._messages .items ():
4850 if T2 > T :
49- new .append ( message )
51+ new .append (message )
5052 self ._v_last_time = T2
5153
5254 return new
@@ -59,7 +61,7 @@ def add_message(self, message):
5961 while 1 :
6062 try :
6163 now = time .time ()
62- self ._messages [ now ] = message
64+ self ._messages [now ] = message
6365 transaction .commit ()
6466 except ConflictError :
6567 # Conflict occurred; this process should abort,
@@ -72,6 +74,7 @@ def add_message(self, message):
7274 break
7375 # end while
7476
77+
7578def get_chat_session (conn , channelname ):
7679 """Return the chat session for a given channel, creating the session
7780 if required."""
@@ -81,20 +84,20 @@ def get_chat_session(conn, channelname):
8184 # the key 'chat_sessions'.
8285 root = conn .root ()
8386 if not root .has_key ('chat_sessions' ):
84- print 'Creating chat_sessions B-tree'
87+ print 'Creating chat_sessions B-tree' # NOQA: E999 print statement
8588 root ['chat_sessions' ] = OOBTree .OOBTree ()
8689 transaction .commit ()
8790
8891 sessions = root ['chat_sessions' ]
8992
9093 # Get a session object corresponding to the channel name, creating
9194 # it if necessary.
92- if not sessions .has_key ( channelname ):
95+ if not sessions .has_key (channelname ):
9396 print 'Creating new session:' , channelname
94- sessions [ channelname ] = ChatSession (channelname )
97+ sessions [channelname ] = ChatSession (channelname )
9598 transaction .commit ()
9699
97- session = sessions [ channelname ]
100+ session = sessions [channelname ]
98101 return session
99102
100103
@@ -103,8 +106,8 @@ def get_chat_session(conn, channelname):
103106 print 'Usage: %s <channelname>' % sys .argv [0 ]
104107 sys .exit (0 )
105108
106- storage = ClientStorage .ClientStorage ( ('localhost' , 9672 ) )
107- db = ZODB .DB ( storage )
109+ storage = ClientStorage .ClientStorage (('localhost' , 9672 ))
110+ db = ZODB .DB (storage )
108111 conn = db .open ()
109112
110113 s = session = get_chat_session (conn , sys .argv [1 ])
@@ -114,12 +117,12 @@ def get_chat_session(conn, channelname):
114117 while 1 :
115118 # Send a random message
116119 msg = random .choice (messages )
117- session .add_message ( '%s: pid %i' % (msg ,os .getpid () ))
120+ session .add_message ('%s: pid %i' % (msg , os .getpid ()))
118121
119122 # Display new messages
120123 for msg in session .new_messages ():
121124 print msg
122125
123126 # Wait for a few seconds
124- pause = random .randint ( 1 , 4 )
125- time .sleep ( pause )
127+ pause = random .randint (1 , 4 )
128+ time .sleep (pause )
0 commit comments