4949from collections import deque
5050
5151
52- class async_chat (asyncore .dispatcher ):
52+ class async_chat (asyncore .dispatcher ):
5353 """This is an abstract class. You must derive from this class, and add
5454 the two methods collect_incoming_data() and found_terminator()"""
5555
5656 # these are overridable defaults
5757
58- ac_in_buffer_size = 65536
59- ac_out_buffer_size = 65536
58+ ac_in_buffer_size = 65536
59+ ac_out_buffer_size = 65536
6060
6161 # we don't want to enable the use of encoding by default, because that is a
6262 # sign of an application bug that we don't want to pass silently
6363
64- use_encoding = 0
65- encoding = 'latin-1'
64+ use_encoding = 0
65+ encoding = 'latin-1'
6666
67- def __init__ (self , sock = None , map = None ):
67+ def __init__ (self , sock = None , map = None ):
6868 # for string terminator matching
6969 self .ac_in_buffer = b''
7070
@@ -76,7 +76,7 @@ def __init__ (self, sock=None, map=None):
7676 # we toss the use of the "simple producer" and replace it with
7777 # a pure deque, which the original fifo was a wrapping of
7878 self .producer_fifo = deque ()
79- asyncore .dispatcher .__init__ (self , sock , map )
79+ asyncore .dispatcher .__init__ (self , sock , map )
8080
8181 def collect_incoming_data (self , data ):
8282 raise NotImplementedError ("must be implemented in subclass" )
@@ -92,24 +92,27 @@ def _get_data(self):
9292 def found_terminator (self ):
9393 raise NotImplementedError ("must be implemented in subclass" )
9494
95- def set_terminator (self , term ):
96- "Set the input delimiter. Can be a fixed string of any length, an integer, or None"
95+ def set_terminator (self , term ):
96+ """Set the input delimiter.
97+
98+ Can be a fixed string of any length, an integer, or None.
99+ """
97100 if isinstance (term , str ) and self .use_encoding :
98101 term = bytes (term , self .encoding )
99102 self .terminator = term
100103
101- def get_terminator (self ):
104+ def get_terminator (self ):
102105 return self .terminator
103106
104107 # grab some more data from the socket,
105108 # throw it to the collector method,
106109 # check for the terminator,
107110 # if found, transition to the next state.
108111
109- def handle_read (self ):
112+ def handle_read (self ):
110113
111114 try :
112- data = self .recv (self .ac_in_buffer_size )
115+ data = self .recv (self .ac_in_buffer_size )
113116 except OSError as why :
114117 self .handle_error ()
115118 return
@@ -128,17 +131,17 @@ def handle_read (self):
128131 terminator = self .get_terminator ()
129132 if not terminator :
130133 # no terminator, collect it all
131- self .collect_incoming_data (self .ac_in_buffer )
134+ self .collect_incoming_data (self .ac_in_buffer )
132135 self .ac_in_buffer = b''
133136 elif isinstance (terminator , int ):
134137 # numeric terminator
135138 n = terminator
136139 if lb < n :
137- self .collect_incoming_data (self .ac_in_buffer )
140+ self .collect_incoming_data (self .ac_in_buffer )
138141 self .ac_in_buffer = b''
139142 self .terminator = self .terminator - lb
140143 else :
141- self .collect_incoming_data (self .ac_in_buffer [:n ])
144+ self .collect_incoming_data (self .ac_in_buffer [:n ])
142145 self .ac_in_buffer = self .ac_in_buffer [n :]
143146 self .terminator = 0
144147 self .found_terminator ()
@@ -155,32 +158,34 @@ def handle_read (self):
155158 if index != - 1 :
156159 # we found the terminator
157160 if index > 0 :
158- # don't bother reporting the empty string (source of subtle bugs)
159- self .collect_incoming_data (self .ac_in_buffer [:index ])
161+ # don't bother reporting the empty string
162+ # (source of subtle bugs)
163+ self .collect_incoming_data (self .ac_in_buffer [:index ])
160164 self .ac_in_buffer = self .ac_in_buffer [index + terminator_len :]
161- # This does the Right Thing if the terminator is changed here.
165+ # This does the Right Thing if the terminator
166+ # is changed here.
162167 self .found_terminator ()
163168 else :
164169 # check for a prefix of the terminator
165- index = find_prefix_at_end (self .ac_in_buffer , terminator )
170+ index = find_prefix_at_end (self .ac_in_buffer , terminator )
166171 if index :
167172 if index != lb :
168173 # we found a prefix, collect up to the prefix
169- self .collect_incoming_data (self .ac_in_buffer [:- index ])
174+ self .collect_incoming_data (self .ac_in_buffer [:- index ])
170175 self .ac_in_buffer = self .ac_in_buffer [- index :]
171176 break
172177 else :
173178 # no prefix, collect it all
174- self .collect_incoming_data (self .ac_in_buffer )
179+ self .collect_incoming_data (self .ac_in_buffer )
175180 self .ac_in_buffer = b''
176181
177- def handle_write (self ):
182+ def handle_write (self ):
178183 self .initiate_send ()
179184
180- def handle_close (self ):
185+ def handle_close (self ):
181186 self .close ()
182187
183- def push (self , data ):
188+ def push (self , data ):
184189 if not isinstance (data , (bytes , bytearray , memoryview )):
185190 raise TypeError ('data argument must be byte-ish (%r)' ,
186191 type (data ))
@@ -192,23 +197,23 @@ def push (self, data):
192197 self .producer_fifo .append (data )
193198 self .initiate_send ()
194199
195- def push_with_producer (self , producer ):
200+ def push_with_producer (self , producer ):
196201 self .producer_fifo .append (producer )
197202 self .initiate_send ()
198203
199- def readable (self ):
204+ def readable (self ):
200205 "predicate for inclusion in the readable for select()"
201206 # cannot use the old predicate, it violates the claim of the
202207 # set_terminator method.
203208
204209 # return (len(self.ac_in_buffer) <= self.ac_in_buffer_size)
205210 return 1
206211
207- def writable (self ):
212+ def writable (self ):
208213 "predicate for inclusion in the writable for select()"
209214 return self .producer_fifo or (not self .connected )
210215
211- def close_when_done (self ):
216+ def close_when_done (self ):
212217 "automatically close this channel once the outgoing queue is empty"
213218 self .producer_fifo .append (None )
214219
@@ -219,10 +224,8 @@ def initiate_send(self):
219224 if not first :
220225 del self .producer_fifo [0 ]
221226 if first is None :
222- ## print("first is None")
223227 self .handle_close ()
224228 return
225- ## print("first is not None")
226229
227230 # handle classic producer behavior
228231 obs = self .ac_out_buffer_size
@@ -254,20 +257,21 @@ def initiate_send(self):
254257 # we tried to send some actual data
255258 return
256259
257- def discard_buffers (self ):
260+ def discard_buffers (self ):
258261 # Emergencies only!
259262 self .ac_in_buffer = b''
260263 del self .incoming [:]
261264 self .producer_fifo .clear ()
262265
266+
263267class simple_producer :
264268
265- def __init__ (self , data , buffer_size = 512 ):
269+ def __init__ (self , data , buffer_size = 512 ):
266270 self .data = data
267271 self .buffer_size = buffer_size
268272
269- def more (self ):
270- if len (self .data ) > self .buffer_size :
273+ def more (self ):
274+ if len (self .data ) > self .buffer_size :
271275 result = self .data [:self .buffer_size ]
272276 self .data = self .data [self .buffer_size :]
273277 return result
@@ -276,8 +280,9 @@ def more (self):
276280 self .data = b''
277281 return result
278282
283+
279284class fifo :
280- def __init__ (self , list = None ):
285+ def __init__ (self , list = None ):
281286 import warnings
282287 warnings .warn ('fifo class will be removed in Python 3.6' ,
283288 DeprecationWarning , stacklevel = 2 )
@@ -286,31 +291,32 @@ def __init__ (self, list=None):
286291 else :
287292 self .list = deque (list )
288293
289- def __len__ (self ):
294+ def __len__ (self ):
290295 return len (self .list )
291296
292- def is_empty (self ):
297+ def is_empty (self ):
293298 return not self .list
294299
295- def first (self ):
300+ def first (self ):
296301 return self .list [0 ]
297302
298- def push (self , data ):
303+ def push (self , data ):
299304 self .list .append (data )
300305
301- def pop (self ):
306+ def pop (self ):
302307 if self .list :
303308 return (1 , self .list .popleft ())
304309 else :
305310 return (0 , None )
306311
312+
307313# Given 'haystack', see if any prefix of 'needle' is at its end. This
308314# assumes an exact match has already been checked. Return the number of
309315# characters matched.
310316# for example:
311- # f_p_a_e ("qwerty\r", "\r\n") => 1
312- # f_p_a_e ("qwertydkjf", "\r\n") => 0
313- # f_p_a_e ("qwerty\r\n", "\r\n") => <undefined>
317+ # f_p_a_e("qwerty\r", "\r\n") => 1
318+ # f_p_a_e("qwertydkjf", "\r\n") => 0
319+ # f_p_a_e("qwerty\r\n", "\r\n") => <undefined>
314320
315321# this could maybe be made faster with a computed regex?
316322# [answer: no; circa Python-2.0, Jan 2001]
@@ -319,7 +325,7 @@ def pop (self):
319325# re: 12820/s
320326# regex: 14035/s
321327
322- def find_prefix_at_end (haystack , needle ):
328+ def find_prefix_at_end (haystack , needle ):
323329 l = len (needle ) - 1
324330 while l and not haystack .endswith (needle [:l ]):
325331 l -= 1
0 commit comments