Package cherrypy :: Package lib
[hide private]
[frames] | no frames]

Source Code for Package cherrypy.lib

 1  """CherryPy Library""" 
 2   
 3  # Deprecated in CherryPy 3.2 -- remove in CherryPy 3.3 
 4  from cherrypy.lib.reprconf import unrepr, modules, attributes 
 5   
6 -def is_iterator(obj):
7 '''Returns a boolean indicating if the object provided implements 8 the iterator protocol (i.e. like a generator). This will return 9 false for objects which iterable, but not iterators themselves.''' 10 from types import GeneratorType 11 if isinstance(obj, GeneratorType): 12 return True 13 elif not hasattr(obj, '__iter__'): 14 return False 15 else: 16 # Types which implement the protocol must return themselves when 17 # invoking 'iter' upon them. 18 return iter(obj) is obj
19
20 -class file_generator(object):
21 22 """Yield the given input (a file object) in chunks (default 64k). (Core)""" 23
24 - def __init__(self, input, chunkSize=65536):
25 self.input = input 26 self.chunkSize = chunkSize
27
28 - def __iter__(self):
29 return self
30
31 - def __next__(self):
32 chunk = self.input.read(self.chunkSize) 33 if chunk: 34 return chunk 35 else: 36 if hasattr(self.input, 'close'): 37 self.input.close() 38 raise StopIteration()
39 next = __next__
40 41
42 -def file_generator_limited(fileobj, count, chunk_size=65536):
43 """Yield the given file object in chunks, stopping after `count` 44 bytes has been emitted. Default chunk size is 64kB. (Core) 45 """ 46 remaining = count 47 while remaining > 0: 48 chunk = fileobj.read(min(chunk_size, remaining)) 49 chunklen = len(chunk) 50 if chunklen == 0: 51 return 52 remaining -= chunklen 53 yield chunk
54 55
56 -def set_vary_header(response, header_name):
57 "Add a Vary header to a response" 58 varies = response.headers.get("Vary", "") 59 varies = [x.strip() for x in varies.split(",") if x.strip()] 60 if header_name not in varies: 61 varies.append(header_name) 62 response.headers['Vary'] = ", ".join(varies)
63