summaryrefslogtreecommitdiff
path: root/cherrypy/lib/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'cherrypy/lib/__init__.py')
-rw-r--r--cherrypy/lib/__init__.py47
1 files changed, 29 insertions, 18 deletions
diff --git a/cherrypy/lib/__init__.py b/cherrypy/lib/__init__.py
index a75a53da..f815f76a 100644
--- a/cherrypy/lib/__init__.py
+++ b/cherrypy/lib/__init__.py
@@ -1,12 +1,14 @@
-"""CherryPy Library"""
+"""CherryPy Library."""
-# Deprecated in CherryPy 3.2 -- remove in CherryPy 3.3
-from cherrypy.lib.reprconf import unrepr, modules, attributes
def is_iterator(obj):
- '''Returns a boolean indicating if the object provided implements
- the iterator protocol (i.e. like a generator). This will return
- false for objects which iterable, but not iterators themselves.'''
+ """Detect if the object provided implements the iterator protocol.
+
+ (i.e. like a generator).
+
+ This will return False for objects which are iterable,
+ but not iterators themselves.
+ """
from types import GeneratorType
if isinstance(obj, GeneratorType):
return True
@@ -16,22 +18,23 @@ def is_iterator(obj):
# Types which implement the protocol must return themselves when
# invoking 'iter' upon them.
return iter(obj) is obj
-
+
+
def is_closable_iterator(obj):
-
+ """Detect if the given object is both closable and iterator."""
# Not an iterator.
if not is_iterator(obj):
return False
-
+
# A generator - the easiest thing to deal with.
import inspect
if inspect.isgenerator(obj):
return True
-
+
# A custom iterator. Look for a close method...
if not (hasattr(obj, 'close') and callable(obj.close)):
return False
-
+
# ... which doesn't require any arguments.
try:
inspect.getcallargs(obj.close)
@@ -40,18 +43,24 @@ def is_closable_iterator(obj):
else:
return True
+
class file_generator(object):
+ """Yield the given input (a file object) in chunks (default 64k).
- """Yield the given input (a file object) in chunks (default 64k). (Core)"""
+ (Core)
+ """
def __init__(self, input, chunkSize=65536):
+ """Initialize file_generator with file ``input`` for chunked access."""
self.input = input
self.chunkSize = chunkSize
def __iter__(self):
+ """Return iterator."""
return self
def __next__(self):
+ """Return next chunk of file."""
chunk = self.input.read(self.chunkSize)
if chunk:
return chunk
@@ -63,8 +72,10 @@ class file_generator(object):
def file_generator_limited(fileobj, count, chunk_size=65536):
- """Yield the given file object in chunks, stopping after `count`
- bytes has been emitted. Default chunk size is 64kB. (Core)
+ """Yield the given file object in chunks.
+
+ Stopps after `count` bytes has been emitted.
+ Default chunk size is 64kB. (Core)
"""
remaining = count
while remaining > 0:
@@ -77,9 +88,9 @@ def file_generator_limited(fileobj, count, chunk_size=65536):
def set_vary_header(response, header_name):
- "Add a Vary header to a response"
- varies = response.headers.get("Vary", "")
- varies = [x.strip() for x in varies.split(",") if x.strip()]
+ """Add a Vary header to a response."""
+ varies = response.headers.get('Vary', '')
+ varies = [x.strip() for x in varies.split(',') if x.strip()]
if header_name not in varies:
varies.append(header_name)
- response.headers['Vary'] = ", ".join(varies)
+ response.headers['Vary'] = ', '.join(varies)