diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 54304e921..b73a13376 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -552,7 +552,6 @@ jobs: # - linkcheck-docs # - metadata-validation - pre-commit - - pre-commit-pep257 - setup-check # - spellcheck-docs fail-fast: false @@ -574,7 +573,7 @@ jobs: - name: Grab the source from Git if: >- contains( - fromJSON('["pre-commit", "pre-commit-pep257", "spellcheck-docs"]'), + fromJSON('["pre-commit", "spellcheck-docs"]'), matrix.toxenv ) uses: actions/checkout@v3 @@ -583,7 +582,7 @@ jobs: - name: Retrieve the project source from an sdist inside the GHA artifact if: >- !contains( - fromJSON('["pre-commit", "pre-commit-pep257", "spellcheck-docs"]'), + fromJSON('["pre-commit", "spellcheck-docs"]'), matrix.toxenv ) uses: re-actors/checkout-python-sdist@release/v1 diff --git a/.pre-commit-config-pep257.yaml b/.pre-commit-config-pep257.yaml deleted file mode 100644 index 84eab227f..000000000 --- a/.pre-commit-config-pep257.yaml +++ /dev/null @@ -1,9 +0,0 @@ ---- - -repos: -- repo: https://github.com/PyCQA/pydocstyle.git - rev: 6.2.2 - hooks: - - id: pydocstyle - -... diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dc20b02e9..26ab04826 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -39,23 +39,6 @@ repos: rev: 6.3.0 hooks: - id: pydocstyle - exclude: | - (?x) - tests/dist| - cherrypy/( - _cp( - compat|modpy|logging|error|wsgi|dispatch|tools|reqbody|request - )| - ( - lib/( - auth|auth_basic|auth_digest|caching|covercp| - cpstats|cptools|encoding|gctools|httpauth|httputil| - jsontools|lockfile|locking|profiler|reprconf|sessions - )| - process/(plugins|servers|win32) - ).py| - test|tutorial - ) - repo: https://github.com/Lucas-C/pre-commit-hooks.git rev: v1.5.4 diff --git a/cherrypy/_cpcompat.py b/cherrypy/_cpcompat.py index a43f6d369..6e8053fc4 100644 --- a/cherrypy/_cpcompat.py +++ b/cherrypy/_cpcompat.py @@ -22,8 +22,9 @@ def ntob(n, encoding='ISO-8859-1'): - """Return the given native string as a byte string in the given - encoding. + """Convert a native :class:`str` to a :class:`bytes` instance. + + The encoding can be changed to non-ASCII optionally. """ assert_native(n) # In Python 3, the native string type is unicode @@ -31,8 +32,9 @@ def ntob(n, encoding='ISO-8859-1'): def ntou(n, encoding='ISO-8859-1'): - """Return the given native string as a unicode string with the given - encoding. + """Convert a native :class:`str` to a :class:`str` instance. + + This doesn't actually do anything. """ assert_native(n) # In Python 3, the native string type is unicode @@ -48,6 +50,7 @@ def tonative(n, encoding='ISO-8859-1'): def assert_native(n): + """Ensure that input is a native :class:`str`.""" if not isinstance(n, str): raise TypeError('n must be a native str (got %s)' % type(n).__name__) diff --git a/cherrypy/_cpdispatch.py b/cherrypy/_cpdispatch.py index 5a3a8ad60..782bb22c1 100644 --- a/cherrypy/_cpdispatch.py +++ b/cherrypy/_cpdispatch.py @@ -25,6 +25,7 @@ class PageHandler(object): """Callable which sets response.body.""" def __init__(self, callable, *args, **kwargs): + """Initialize the page handler.""" self.callable = callable self.args = args self.kwargs = kwargs @@ -36,6 +37,7 @@ def args(self): @args.setter def args(self, args): + """Set the request arguments in order.""" cherrypy.serving.request.args = args return cherrypy.serving.request.args @@ -46,10 +48,12 @@ def kwargs(self): @kwargs.setter def kwargs(self, kwargs): + """Set the named request keyword arguments as :class:`dict`.""" cherrypy.serving.request.kwargs = kwargs return cherrypy.serving.request.kwargs def __call__(self): + """Invoke an HTTP handler callable for :class:`PageHandler`.""" try: return self.callable(*self.args, **self.kwargs) except TypeError: @@ -203,15 +207,18 @@ def test_callable_spec(callable, callable_args, callable_kwargs): import inspect except ImportError: def test_callable_spec(callable, args, kwargs): # noqa: F811 + """Do nothing as a no-op.""" return None else: def getargspec(callable): + """Get argument specification using :mod:`inspect`.""" return inspect.getfullargspec(callable)[:4] class LateParamPageHandler(PageHandler): + """Page handler callable with delayed request parameters binding. - """When passing cherrypy.request.params to the page handler, we do not + When passing ``cherrypy.request.params`` to the page handler, we do not want to capture that dict too early; we want to give tools like the decoding tool a chance to modify the params dict in-between the lookup of the handler and the actual calling of the handler. This subclass @@ -221,7 +228,11 @@ class LateParamPageHandler(PageHandler): @property def kwargs(self): - """Page handler kwargs (with cherrypy.request.params copied in).""" + """Page handler keyword arguments. + + The returned value contains data merged in + from ``cherrypy.request.params``. + """ kwargs = cherrypy.serving.request.params.copy() if self._kwargs: kwargs.update(self._kwargs) @@ -229,6 +240,7 @@ def kwargs(self): @kwargs.setter def kwargs(self, kwargs): + """Set the named request keyword arguments as :class:`dict`.""" cherrypy.serving.request.kwargs = kwargs self._kwargs = kwargs @@ -238,6 +250,7 @@ def kwargs(self, kwargs): string.punctuation, '_' * len(string.punctuation)) def validate_translator(t): + """Ensure the translator is of the correct length and size.""" if not isinstance(t, str) or len(t) != 256: raise ValueError( 'The translate argument must be a str of len 256.') @@ -246,6 +259,7 @@ def validate_translator(t): string.punctuation, '_' * len(string.punctuation)) def validate_translator(t): + """Ensure the translator is of the correct length and size.""" if not isinstance(t, dict): raise ValueError('The translate argument must be a dict.') @@ -273,6 +287,7 @@ class Dispatcher(object): def __init__(self, dispatch_method_name=None, translate=punctuation_to_underscores): + """Initialize the HTTP request dispatcher.""" validate_translator(translate) self.translate = translate if dispatch_method_name: @@ -389,7 +404,9 @@ def find_handler(self, path): object_trail.append([name, node, nodeconf, segleft]) def set_conf(): - """Collapse all object_trail config into cherrypy.request.config. + """Collapse all ``object_trail`` conf into config. + + The config being ``cherrypy.request.config``. """ base = cherrypy.config.copy() # Note that we merge the config from each node @@ -505,10 +522,12 @@ def __init__(self, full_result=False, **mapper_options): self.mapper.controller_scan = self.controllers.keys def connect(self, name, route, controller, **kwargs): + """Mount an HTTP handler into the router.""" self.controllers[name] = controller self.mapper.connect(name, route, controller=name, **kwargs) def redirect(self, url): + """Perform an HTTP redirect to the given URL.""" raise cherrypy.HTTPRedirect(url) def __call__(self, path_info): @@ -602,6 +621,7 @@ def merge(nodeconf): def XMLRPCDispatcher(next_dispatcher=Dispatcher()): + """Chain an HTTP dispatcher variant implementing XML-RPC.""" from cherrypy.lib import xmlrpcutil def xmlrpc_dispatch(path_info): diff --git a/cherrypy/_cperror.py b/cherrypy/_cperror.py index 203fabf5c..34061a445 100644 --- a/cherrypy/_cperror.py +++ b/cherrypy/_cperror.py @@ -137,7 +137,6 @@ class Root: class CherryPyException(Exception): """A base class for CherryPy exceptions.""" - pass class InternalRedirect(CherryPyException): @@ -150,6 +149,7 @@ class InternalRedirect(CherryPyException): """ def __init__(self, path, query_string=''): + """Initialize the internal redirect exception.""" self.request = cherrypy.serving.request self.query_string = query_string @@ -202,6 +202,7 @@ class HTTPRedirect(CherryPyException): """The encoding when passed urls are not native strings.""" def __init__(self, urls, status=None, encoding=None): + """Initialize the HTTP redirect exception.""" self.urls = abs_urls = [ # Note that urljoin will "do the right thing" whether url is: # 1. a complete URL with host (e.g. "http://www.example.com/test") @@ -227,7 +228,9 @@ def __init__(self, urls, status=None, encoding=None): @classproperty def default_status(cls): - """The default redirect status for the request. + """Redirect status for the request. + + This is the default handler. RFC 2616 indicates a 301 response code fits our goal; however, browser support for 301 is quite messy. Use 302/303 instead. See @@ -242,8 +245,9 @@ def status(self): return status def set_response(self): - """Modify cherrypy.response status, headers, and body to represent - self. + """Modify ``cherrypy.response`` to represent ``self``. + + Modifies status, headers, and body. CherryPy uses this internally, but you can also use it to create an HTTPRedirect object and set its output without *raising* the @@ -366,6 +370,7 @@ class HTTPError(CherryPyException): """The HTTP Reason-Phrase string.""" def __init__(self, status=500, message=None): + """Initialize an HTTP error.""" self.status = status try: self.code, self.reason, defaultmsg = _httputil.valid_status(status) @@ -381,8 +386,9 @@ def __init__(self, status=500, message=None): CherryPyException.__init__(self, status, message) def set_response(self): - """Modify cherrypy.response status, headers, and body to represent - self. + """Modify ``cherrypy.response`` to represent ``self``. + + Modifies status, headers, and body. CherryPy uses this internally, but you can also use it to create an HTTPError object and set its output without *raising* the @@ -408,6 +414,7 @@ def set_response(self): _be_ie_unfriendly(self.code) def get_error_page(self, *args, **kwargs): + """Compose an HTML page with error information.""" return get_error_page(*args, **kwargs) def __call__(self): @@ -432,6 +439,7 @@ class NotFound(HTTPError): """ def __init__(self, path=None): + """Initialize an HTTP Not Found error.""" if path is None: request = cherrypy.serving.request path = request.script_name + request.path_info @@ -600,7 +608,6 @@ def bare_error(extrabody=None): is set in the body. If extrabody is a string, it will be appended as-is to the body. """ - # The whole point of this function is to be a last line-of-defense # in handling errors. That is, it must not raise any errors itself; # it cannot be allowed to fail. Therefore, don't add to it! diff --git a/cherrypy/_cplogging.py b/cherrypy/_cplogging.py index 754ac7bf9..dae72692b 100644 --- a/cherrypy/_cplogging.py +++ b/cherrypy/_cplogging.py @@ -1,4 +1,6 @@ """ +CherryPy logging module. + Simple config ============= @@ -126,12 +128,15 @@ class NullHandler(logging.Handler): """A no-op logging handler to silence the logging.lastResort handler.""" def handle(self, record): + """Handle a log record doing no-op.""" pass def emit(self, record): + """Emit a log record doing no-op.""" pass def createLock(self): + """Lock log write with no-op.""" self.lock = None @@ -167,6 +172,7 @@ class LogManager(object): """ def __init__(self, appid=None, logger_root='cherrypy'): + """Initialize a CherryPy log manager.""" self.logger_root = logger_root self.appid = appid if appid is None: @@ -217,11 +223,11 @@ def error(self, msg='', context='', severity=logging.INFO, ) def __call__(self, *args, **kwargs): - """An alias for ``error``.""" + """Record an error log entry.""" return self.error(*args, **kwargs) def access(self): - """Write to the access log (in Apache/NCSA Combined Log format). + r"""Write to the access log (in Apache/NCSA Combined Log format). See the `apache documentation @@ -414,7 +420,10 @@ def wsgi(self, newvalue): class WSGIErrorHandler(logging.Handler): - "A handler class which writes logging records to environ['wsgi.errors']." + """A handler class writing logs to WSGI env. + + Specifically, the target is ``environ['wsgi.errors']``. + """ def flush(self): """Flushes the stream.""" @@ -450,6 +459,8 @@ def emit(self, record): class LazyRfc3339UtcTime(object): + """A postponed timestamp string retrieval class.""" + def __str__(self): """Return datetime in RFC3339 UTC Format.""" iso_formatted_now = datetime.datetime.now( diff --git a/cherrypy/_cpmodpy.py b/cherrypy/_cpmodpy.py index 7c01233d8..9029b43d8 100644 --- a/cherrypy/_cpmodpy.py +++ b/cherrypy/_cpmodpy.py @@ -72,6 +72,7 @@ def setup_server(): def setup(req): + """Execute pre-initialization functions.""" from mod_python import apache # Run any setup functions defined by a "PythonOption cherrypy.setup" @@ -140,6 +141,7 @@ def __init__(self, req): def handler(req): + """Invoke the HTTP handler.""" from mod_python import apache try: global _isSetUp @@ -251,6 +253,7 @@ def handler(req): def send_response(req, status, headers, body, stream=False): + """Send the HTTP response to the client.""" # Set response status req.status = int(status[:3]) @@ -276,17 +279,20 @@ def send_response(req, status, headers, body, stream=False): import subprocess def popen(fullcmd): + """Invoke a subprocess via :mod:`subprocess`.""" p = subprocess.Popen(fullcmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True) return p.stdout except ImportError: def popen(fullcmd): + """Invoke a subprocess via :mod:`os`.""" pipein, pipeout = os.popen4(fullcmd) return pipeout def read_process(cmd, args=''): + """Return a subprocess standard output.""" fullcmd = '%s %s' % (cmd, args) pipeout = popen(fullcmd) try: @@ -305,6 +311,7 @@ def read_process(cmd, args=''): class ModPythonServer(object): + """A server wrapper for ``mod_python``.""" template = """ # Apache2 server configuration file for running CherryPy with mod_python. @@ -323,6 +330,7 @@ class ModPythonServer(object): def __init__(self, loc='/', port=80, opts=None, apache_path='apache', handler='cherrypy._cpmodpy::handler'): + """Initialize a ``mod_python`` server.""" self.loc = loc self.port = port self.opts = opts @@ -330,6 +338,7 @@ def __init__(self, loc='/', port=80, opts=None, apache_path='apache', self.handler = handler def start(self): + """Start an Apache2/httpd server.""" opts = ''.join([' PythonOption %s %s\n' % (k, v) for k, v in self.opts]) conf_data = self.template % {'port': self.port, @@ -347,5 +356,6 @@ def start(self): return response def stop(self): + """Stop an Apache2/httpd server.""" os.popen('apache -k stop') self.ready = False diff --git a/cherrypy/_cpreqbody.py b/cherrypy/_cpreqbody.py index 7e0d98bee..ffa6289f2 100644 --- a/cherrypy/_cpreqbody.py +++ b/cherrypy/_cpreqbody.py @@ -220,7 +220,9 @@ def process_multipart(entity): def process_multipart_form_data(entity): - """Read all multipart/form-data parts into entity.parts or entity.params. + """Read ``multipart/form-data`` parts. + + This function saves them into ``entity.parts`` or ``entity.params``. """ process_multipart(entity) @@ -248,7 +250,7 @@ def process_multipart_form_data(entity): def _old_process_multipart(entity): - """The behavior of 3.2 and lower. + """Behavior of 3.2 and lower. Deprecated and will be changed in 3.3. """ @@ -411,6 +413,7 @@ class Entity(object): """ def __init__(self, fp, headers, params=None, parts=None): + """Initialize an HTTP entity.""" # Make an instance-specific copy of the class processors # so Tools, etc. can replace them per-request. self.processors = self.processors.copy() @@ -479,24 +482,30 @@ def __init__(self, fp, headers, params=None, parts=None): self.filename = unquote(str(filename), encoding) def read(self, size=None, fp_out=None): + """Read bytes from the connection.""" return self.fp.read(size, fp_out) def readline(self, size=None): + """Read a line of bytes from the connection.""" return self.fp.readline(size) def readlines(self, sizehint=None): + """Read some byte lines from the connection.""" return self.fp.readlines(sizehint) def __iter__(self): + """Set up the iterator.""" return self def __next__(self): + """Return the next line of bytes.""" line = self.readline() if not line: raise StopIteration return line def next(self): + """Return the next line of bytes.""" # FIXME: python 2? return self.__next__() def read_into_file(self, fp_out=None): @@ -564,7 +573,9 @@ def process(self): proc(self) def default_proc(self): - """Called if a more-specific processor is not found for the + """Process unknown data as a fallback. + + Called if a more-specific processor is not found for the ``Content-Type``. """ # Leave the fp alone for someone else to read. This works fine @@ -614,6 +625,7 @@ class Part(Entity): """ def __init__(self, fp, headers, boundary): + """Initialize an entity part.""" Entity.__init__(self, fp, headers) self.boundary = boundary self.file = None @@ -621,11 +633,13 @@ def __init__(self, fp, headers, boundary): @classmethod def from_fp(cls, fp, boundary): + """Initialize an entity part from a file handle.""" headers = cls.read_headers(fp) return cls(fp, headers, boundary) @classmethod def read_headers(cls, fp): + """Read HTTP headers from a file handle.""" headers = httputil.HeaderMap() while True: line = fp.readline() @@ -713,7 +727,9 @@ def read_lines_to_boundary(self, fp_out=None): return fp_out def default_proc(self): - """Called if a more-specific processor is not found for the + """Process unknown data as a fallback. + + Called if a more-specific processor is not found for the ``Content-Type``. """ if self.filename: @@ -743,9 +759,11 @@ def read_into_file(self, fp_out=None): class SizedReader: + """A buffered/sized reader.""" def __init__(self, fp, length, maxbytes, bufsize=DEFAULT_BUFFER_SIZE, has_trailers=False): + """Initialize buffered file handle reader.""" # Wrap our fp in a buffer so peek() works self.fp = fp self.length = length @@ -773,7 +791,6 @@ def read(self, size=None, fp_out=None): object that supports the 'write' method; all bytes read will be written to the fp, and None is returned. """ - if self.length is None: if size is None: remaining = inf @@ -889,6 +906,7 @@ def readlines(self, sizehint=None): return lines def finish(self): + """Finalize reading the HTTP trailer headers.""" self.done = True if self.has_trailers and hasattr(self.fp, 'read_trailer_lines'): self.trailers = {} @@ -946,6 +964,7 @@ class RequestBody(Entity): """ def __init__(self, fp, headers, params=None, request_params=None): + """Initialize a request body entity.""" Entity.__init__(self, fp, headers, params) # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1 diff --git a/cherrypy/_cprequest.py b/cherrypy/_cprequest.py index a4ad298b0..14ad69311 100644 --- a/cherrypy/_cprequest.py +++ b/cherrypy/_cprequest.py @@ -42,6 +42,7 @@ class Hook(object): callable on each call.""" def __init__(self, callback, failsafe=None, priority=None, **kwargs): + """Initialize the hook instance.""" self.callback = callback if failsafe is None: @@ -55,7 +56,14 @@ def __init__(self, callback, failsafe=None, priority=None, **kwargs): self.kwargs = kwargs def __lt__(self, other): - """ + """Check if this hook's priority is lower than the other's. + + :param other: Another object to compare priority with. + :type other: Hook + + :returns: Whether the other Hook's priority is higher than this one's. + :rtype: bool + Hooks sort by priority, ascending, such that hooks of lower priority are run first. """ @@ -66,6 +74,7 @@ def __call__(self): return self.callback(**self.kwargs) def __repr__(self): + """Render a string representation of :class:`Hook` instance.""" cls = self.__class__ return ('%s.%s(callback=%r, failsafe=%r, priority=%r, %s)' % (cls.__module__, cls.__name__, self.callback, @@ -78,12 +87,14 @@ class HookMap(dict): """A map of call points to lists of callbacks (Hook objects).""" def __new__(cls, points=None): + """Construct a fresh hook map instance.""" d = dict.__new__(cls) for p in points or []: d[p] = [] return d def __init__(self, *a, **kw): + """Initialize a hook map instance post-construction.""" pass def attach(self, point, callback, failsafe=None, priority=None, **kwargs): @@ -124,6 +135,7 @@ def run_hooks(cls, hooks): raise def __copy__(self): + """Duplicate object per the copy protocol.""" newmap = self.__class__() # We can't just use 'update' because we want copies of the # mutable values (each is a list) as well. @@ -133,6 +145,7 @@ def __copy__(self): copy = __copy__ def __repr__(self): + """Render a string representation of :class:`HookMap`.""" cls = self.__class__ return '%s.%s(points=%r)' % ( cls.__module__, @@ -541,7 +554,9 @@ def close(self): self.stage = 'close' def run(self, method, path, query_string, req_protocol, headers, rfile): - r"""Process the Request. (Core) + r"""Process the request. + + *(core)* method, path, query_string, and req_protocol should be pulled directly from the Request-Line (e.g. "GET /path?key=val HTTP/1.0"). @@ -815,6 +830,7 @@ class ResponseBody(object): 'if you wish to return unicode.') def __get__(self, obj, objclass=None): + """Return a response body through the descriptor protocol.""" if obj is None: # When calling on the class instead of an instance... return self @@ -822,7 +838,10 @@ def __get__(self, obj, objclass=None): return obj._body def __set__(self, obj, value): - # Convert the given value to an iterable object. + """Set a response body through the descriptor protocol. + + Convert the given value to an iterable object. + """ if isinstance(value, str): raise ValueError(self.unicode_err) elif isinstance(value, list): @@ -874,6 +893,7 @@ class Response(object): """If False, buffer the response body.""" def __init__(self): + """Intialize the HTTP response instance.""" self.status = None self.header_list = None self._body = [] @@ -896,8 +916,14 @@ def collapse_body(self): return new_body def _flush_body(self): - """Discard self.body but consume any generator such that any - finalization can occur, such as is required by caching.tee_output().""" + """Exhaust the body iterator. + + :rtype: None + + Discard ``self.body`` but consume any generator such that any + finalization can occur, such as is required by + ``caching.tee_output()``. + """ consume(iter(self.body)) def finalize(self): @@ -951,6 +977,8 @@ def finalize(self): class LazyUUID4(object): + """A delayed UUID4 string maker.""" + def __str__(self): """Return UUID4 and keep it for future calls.""" return str(self.uuid4) diff --git a/cherrypy/_cptools.py b/cherrypy/_cptools.py index e47c046ea..883ba785c 100644 --- a/cherrypy/_cptools.py +++ b/cherrypy/_cptools.py @@ -57,6 +57,7 @@ class Tool(object): namespace = 'tools' def __init__(self, point, callable, name=None, priority=50): + """Initialize a CherryPy tool instance.""" self._point = point self.callable = callable self._name = name @@ -66,10 +67,12 @@ def __init__(self, point, callable, name=None, priority=50): @property def on(self): + """Flag whether the tool is enabled.""" raise AttributeError(_attr_error) @on.setter def on(self, value): + """Set a flag for whether the tool is enabled.""" raise AttributeError(_attr_error) def _setargs(self): @@ -133,7 +136,7 @@ def tool_decorator(f): return tool_decorator def _setup(self): - """Hook this tool into cherrypy.request. + """Wire this tool into ``cherrypy.request``. The standard CherryPy request object will automatically call this method when the tool is "turned on" in config. @@ -159,6 +162,7 @@ class HandlerTool(Tool): """ def __init__(self, callable, name=None): + """Initialize a handler tool.""" Tool.__init__(self, 'before_handler', callable, name) def handler(self, *args, **kwargs): @@ -183,7 +187,7 @@ def _wrapper(self, **kwargs): cherrypy.serving.request.handler = None def _setup(self): - """Hook this tool into cherrypy.request. + """Wire this tool into ``cherrypy.request``. The standard CherryPy request object will automatically call this method when the tool is "turned on" in config. @@ -217,12 +221,14 @@ def interpolator(next_handler, *args, **kwargs): def __init__(self, newhandler, point='before_handler', name=None, priority=50): + """Initialize a handler wrapper tool.""" self.newhandler = newhandler self._point = point self._name = name self._priority = priority def callable(self, *args, **kwargs): + """Decorate a request handler with a handler tool callable.""" innerfunc = cherrypy.serving.request.handler def wrap(*args, **kwargs): @@ -234,13 +240,14 @@ class ErrorTool(Tool): """Tool which is used to replace the default request.error_response.""" def __init__(self, callable, name=None): + """Initialize an error tool.""" Tool.__init__(self, None, callable, name) def _wrapper(self): self.callable(**self._merged_args()) def _setup(self): - """Hook this tool into cherrypy.request. + """Wire this tool into ``cherrypy.request``. The standard CherryPy request object will automatically call this method when the tool is "turned on" in config. @@ -270,6 +277,7 @@ class SessionTool(Tool): """ def __init__(self): + """Initialize a session tool.""" # _sessions.init must be bound after headers are read Tool.__init__(self, 'before_request_body', _sessions.init) @@ -277,7 +285,7 @@ def _lock_session(self): cherrypy.serving.session.acquire_lock() def _setup(self): - """Hook this tool into cherrypy.request. + """Wire this tool into ``cherrypy.request``. The standard CherryPy request object will automatically call this method when the tool is "turned on" in config. @@ -360,6 +368,7 @@ class XMLRPCController(object): @expose def default(self, *vpath, **params): + """Process the unhandled XML-RPC methods.""" rpcparams, rpcmethod = _xmlrpc.process_body() subhandler = self @@ -384,7 +393,7 @@ def default(self, *vpath, **params): class SessionAuthTool(HandlerTool): - pass + """An HTTP session authentication tool.""" class CachingTool(Tool): @@ -402,7 +411,7 @@ def _wrapper(self, **kwargs): _wrapper.priority = 90 def _setup(self): - """Hook caching into cherrypy.request.""" + """Wire caching into ``cherrypy.request``.""" conf = self._merged_args() p = conf.pop('priority', None) @@ -419,9 +428,11 @@ class Toolbox(object): """ def __init__(self, namespace): + """Initialize a toolbox instance.""" self.namespace = namespace def __setattr__(self, name, value): + """Set an attribute on this :class:`Toolbox` instance.""" # If the Tool._name is None, supply it from the attribute name. if isinstance(value, Tool): if value._name is None: @@ -449,7 +460,8 @@ def __exit__(self, exc_type, exc_val, exc_tb): tool._setup() def register(self, point, **kwargs): - """ + """Register a hook point handler in the toolbox. + Return a decorator which registers the function at the given hook point. """ diff --git a/cherrypy/_cpwsgi.py b/cherrypy/_cpwsgi.py index b2a6da52a..75635f504 100644 --- a/cherrypy/_cpwsgi.py +++ b/cherrypy/_cpwsgi.py @@ -18,8 +18,7 @@ def downgrade_wsgi_ux_to_1x(environ): - """Return a new environ dict for WSGI 1.x from the given WSGI u.x environ. - """ + """Return new environ dict for WSGI 1.x from provided WSGI u.x environ.""" env1x = {} url_encoding = environ[ntou('wsgi.url_encoding')] @@ -54,10 +53,11 @@ class VirtualHost(object): cherrypy.tree.graft(vhost) """ + default = None - """Required. + """The default WSGI application. - The default WSGI application. + Required. """ use_x_forwarded_host = True @@ -67,7 +67,6 @@ class VirtualHost(object): domains = {} """A dict of {host header value: application} pairs. - The incoming "Host" request header is looked up in this dict, and, if a match is found, the corresponding WSGI application will be called instead of the default. Note that you often need separate @@ -76,11 +75,30 @@ class VirtualHost(object): """ def __init__(self, default, domains=None, use_x_forwarded_host=True): + """ + Initialize a virtual host app. + + :param default: The default WSGI application + :type default: WSGI application + :param use_x_forwarded_host: If True (the default), any + "X-Forwarded-Host" request header will be used instead of the + "Host" header. This is commonly added by HTTP servers (such as + Apache) when proxying. + :type use_x_forwarded_host: Bool, optional + :param domains: A dict of {host header value: application} pairs. + The incoming "Host" request header is looked up in this dict, and, + if a match is found, the corresponding WSGI application will be + called instead of the default. Note that you often need separate + entries for "example.com" and "www.example.com". In addition, + "Host" headers may contain the port number. + :type domains: Dict, optional + """ self.default = default self.domains = domains or {} self.use_x_forwarded_host = use_x_forwarded_host def __call__(self, environ, start_response): + """Route WSGI requests based on host names.""" domain = environ.get('HTTP_HOST', '') if self.use_x_forwarded_host: domain = environ.get('HTTP_X_FORWARDED_HOST', domain) @@ -95,10 +113,12 @@ class InternalRedirector(object): """WSGI middleware that handles raised cherrypy.InternalRedirect.""" def __init__(self, nextapp, recursive=False): + """Initialize an internal redirector.""" self.nextapp = nextapp self.recursive = recursive def __call__(self, environ, start_response): + """Process internal WSGI request redirects.""" redirections = [] while True: environ = environ.copy() @@ -142,10 +162,12 @@ class ExceptionTrapper(object): """WSGI middleware that traps exceptions.""" def __init__(self, nextapp, throws=(KeyboardInterrupt, SystemExit)): + """Initialize exception trapper.""" self.nextapp = nextapp self.throws = throws def __call__(self, environ, start_response): + """Handle exceptions while processing a WSGI request.""" return _TrappedResponse( self.nextapp, environ, @@ -230,6 +252,7 @@ class AppResponse(object): """WSGI response iterable for CherryPy applications.""" def __init__(self, environ, start_response, cpapp): + """Initialize the WSGI app response.""" self.cpapp = cpapp try: self.environ = environ @@ -271,9 +294,11 @@ def __init__(self, environ, start_response, cpapp): raise def __iter__(self): + """Make an app response iterator.""" return self def __next__(self): + """Iterate over the app response.""" return next(self.iter_response) def close(self): @@ -346,6 +371,7 @@ def run(self): } def recode_path_qs(self, path, qs): + """Recode app response path query string.""" # This isn't perfect; if the given PATH_INFO is in the # wrong encoding, it may fail to match the appropriate config # section URI. But meh. @@ -416,6 +442,7 @@ class CPWSGIApp(object): """ def __init__(self, cpapp, pipeline=None): + """Initialize a framework WSGI app wrapper.""" self.cpapp = cpapp self.pipeline = self.pipeline[:] if pipeline: @@ -431,6 +458,7 @@ def tail(self, environ, start_response): return self.response_class(environ, start_response, self.cpapp) def __call__(self, environ, start_response): + """Process a WSGI request.""" head = self.head if head is None: # Create and nest the WSGI apps in our pipeline (in reverse order). diff --git a/cherrypy/lib/auth_basic.py b/cherrypy/lib/auth_basic.py index b938a5600..bf57d77e1 100644 --- a/cherrypy/lib/auth_basic.py +++ b/cherrypy/lib/auth_basic.py @@ -33,7 +33,9 @@ def checkpassword_dict(user_password_dict): - """Returns a checkpassword function which checks credentials + """Check credentials against a dictionary. + + Returns a checkpassword function which checks credentials against a dictionary of the form: {username : password}. If you want a simple dictionary-based authentication scheme, use @@ -48,7 +50,9 @@ def checkpassword(realm, user, password): def basic_auth(realm, checkpassword, debug=False, accept_charset='utf-8'): - """A CherryPy tool which hooks at before_handler to perform + """Perform basic auth. + + A CherryPy tool which hooks at before_handler to perform HTTP Basic Access Authentication, as specified in :rfc:`2617` and :rfc:`7617`. @@ -69,7 +73,6 @@ def basic_auth(realm, checkpassword, debug=False, accept_charset='utf-8'): returns True, else it returns False. """ - fallback_charset = 'ISO-8859-1' if '"' in realm: diff --git a/cherrypy/lib/auth_digest.py b/cherrypy/lib/auth_digest.py index 467492689..d94b5bab4 100644 --- a/cherrypy/lib/auth_digest.py +++ b/cherrypy/lib/auth_digest.py @@ -34,6 +34,7 @@ def md5_hex(s): + """Return hexdigest of md5sum.""" return md5(ntob(s, 'utf-8')).hexdigest() @@ -48,6 +49,7 @@ def md5_hex(s): def TRACE(msg): + """Log message in TOOLS.AUTH_DIGEST context.""" cherrypy.log(msg, context='TOOLS.AUTH_DIGEST') # Three helper functions for users of the tool, providing three variants @@ -55,8 +57,9 @@ def TRACE(msg): def get_ha1_dict_plain(user_password_dict): - """Return a get_ha1 function which obtains a plaintext password from a - dictionary of the form: {username : password}. + """Return a get_ha1 function which obtains a plaintext password. + + user_password_dict is a dictionary of the form: {username : password}. If you want a simple dictionary-based authentication scheme, with plaintext passwords, use get_ha1_dict_plain(my_userpass_dict) as the value for the @@ -72,8 +75,9 @@ def get_ha1(realm, username): def get_ha1_dict(user_ha1_dict): - """Return a get_ha1 function which obtains a HA1 password hash from a - dictionary of the form: {username : HA1}. + """Return a get_ha1 function which obtains a HA1 password hash. + + user_ha1_dict is a dictionary of the form: {username : HA1}. If you want a dictionary-based authentication scheme, but with pre-computed HA1 hashes instead of plain-text passwords, use @@ -87,7 +91,9 @@ def get_ha1(realm, username): def get_ha1_file_htdigest(filename): - """Return a get_ha1 function which obtains a HA1 password hash from a + """Return a get_ha1 function. + + The returned function obtains a HA1 password hash from a flat file with lines of the same format as that produced by the Apache htdigest utility. For example, for realm 'wonderland', username 'alice', and password '4x5istwelve', the htdigest line would be:: @@ -113,7 +119,9 @@ def get_ha1(realm, username): def synthesize_nonce(s, key, timestamp=None): - """Synthesize a nonce value which resists spoofing and can be checked + """Synthesize a nonce value. + + A nonce value resists spoofing and can be checked for staleness. Returns a string suitable as the value for 'nonce' in the www-authenticate header. @@ -135,7 +143,7 @@ def synthesize_nonce(s, key, timestamp=None): def H(s): - """The hash function H.""" + """Return an ``md5`` HEX hash.""" return md5_hex(s) @@ -152,7 +160,8 @@ def _try_decode_header(header, charset): class HttpDigestAuthorization(object): - """ + """Digest Authorization implementation. + Parses a Digest Authorization header and performs re-calculation of the digest. """ @@ -160,10 +169,12 @@ class HttpDigestAuthorization(object): scheme = 'digest' def errmsg(self, s): + """Make an error message for HTTP Digest Authorization.""" return 'Digest Authorization header: %s' % s @classmethod def matches(cls, header): + """Check if header scheme matches auth implementation.""" scheme, _, _ = header.partition(' ') return scheme.lower() == cls.scheme @@ -171,6 +182,7 @@ def __init__( self, auth_header, http_method, debug=False, accept_charset=DEFAULT_CHARSET[:], ): + """Initialize an HTTP Digest Authorization parser.""" self.http_method = http_method self.debug = debug @@ -229,10 +241,12 @@ def __init__( 'neither cnonce nor nc can be present')) def __str__(self): + """Render an HTTP Digest Auth header as a string.""" return 'authorization : %s' % self.auth_header def validate_nonce(self, s, key): """Validate the nonce. + Returns True if nonce was generated by synthesize_nonce() and the timestamp is not spoofed, else returns False. @@ -298,7 +312,7 @@ def HA2(self, entity_body=''): return H(a2) def request_digest(self, ha1, entity_body=''): - """Calculates the Request-Digest. See :rfc:`2617` section 3.2.2.1. + """Calculate the Request-Digest. See :rfc:`2617` section 3.2.2.1. ha1 The HA1 string obtained from the credentials store. @@ -351,7 +365,7 @@ def www_authenticate( realm, key, algorithm='MD5', nonce=None, qop=qop_auth, stale=False, accept_charset=DEFAULT_CHARSET[:], ): - """Constructs a WWW-Authenticate header for Digest authentication.""" + """Construct a WWW-Authenticate header for Digest authentication.""" if qop not in valid_qops: raise ValueError("Unsupported value for qop: '%s'" % qop) if algorithm not in valid_algorithms: @@ -374,7 +388,9 @@ def www_authenticate( def digest_auth(realm, get_ha1, key, debug=False, accept_charset='utf-8'): - """A CherryPy tool that hooks at before_handler to perform + """Perform HTTP Digest Access Authentication. + + A CherryPy tool that hooks at ``before_handler`` to perform HTTP Digest Access Authentication, as specified in :rfc:`2617`. If the request has an 'authorization' header with a 'Digest' scheme, diff --git a/cherrypy/lib/caching.py b/cherrypy/lib/caching.py index 89cb0442a..a2522f337 100644 --- a/cherrypy/lib/caching.py +++ b/cherrypy/lib/caching.py @@ -1,4 +1,6 @@ """ +CherryPy cache tooling. + CherryPy implements a simple caching system as a pluggable Tool. This tool tries to be an (in-process) HTTP/1.1-compliant cache. It's not quite there yet, but it's probably good enough for most sites. @@ -161,6 +163,7 @@ class MemoryCache(Cache): debug = False def __init__(self): + """Initialize in-memory cache store.""" self.clear() # Run self.expire_cache in a separate daemon thread. @@ -442,7 +445,6 @@ def expires(secs=0, force=False, debug=False): If any are already present, none of the above response headers are set. """ - response = cherrypy.serving.response headers = response.headers diff --git a/cherrypy/lib/covercp.py b/cherrypy/lib/covercp.py index f22cce742..a2621a2d4 100644 --- a/cherrypy/lib/covercp.py +++ b/cherrypy/lib/covercp.py @@ -38,6 +38,7 @@ the_coverage = coverage(data_file=localFile) def start(): + """Start collecting coverage.""" the_coverage.start() except ImportError: # Setting the_coverage to None will raise errors @@ -50,6 +51,7 @@ def start(): 'coverage.py could not be imported.') def start(): + """Start collecting coverage.""" pass start.priority = 20 @@ -284,8 +286,10 @@ def get_tree(base, exclude, coverage=the_coverage): class CoverStats(object): + """HTTP handler for the coverage stats.""" def __init__(self, coverage, root=None): + """Initialize the coverage stats application.""" self.coverage = coverage if root is None: # Guess initial depth. Files outside this path will not be @@ -295,12 +299,13 @@ def __init__(self, coverage, root=None): @cherrypy.expose def index(self): + """Render the coverage stats index page.""" return TEMPLATE_FRAMESET % self.root.lower() @cherrypy.expose def menu(self, base='/', pct='50', showpct='', exclude=r'python\d\.\d|test|tut\d|tutorial'): - + """Render HTML menu web page.""" # The coverage module uses all-lower-case names. base = base.lower().rstrip(os.sep) @@ -334,6 +339,7 @@ def menu(self, base='/', pct='50', showpct='', yield '' def annotated_file(self, filename, statements, excluded, missing): + """Annotate given file with coverage information.""" with open(filename, 'r') as source: lines = source.readlines() buffer = [] @@ -358,6 +364,7 @@ def annotated_file(self, filename, statements, excluded, missing): @cherrypy.expose def report(self, name): + """Render coverage stats as HTML.""" filename, statements, excluded, missing, _ = self.coverage.analysis2( name) pc = _percent(statements, missing) @@ -374,6 +381,7 @@ def report(self, name): def serve(path=localFile, port=8080, root=None): + """Serve the coverage app over HTTP.""" if coverage is None: raise ImportError('The coverage module could not be imported.') from coverage import coverage diff --git a/cherrypy/lib/cpstats.py b/cherrypy/lib/cpstats.py index 5dff319b0..5b9c85e47 100644 --- a/cherrypy/lib/cpstats.py +++ b/cherrypy/lib/cpstats.py @@ -249,6 +249,7 @@ def extrapolate_statistics(scope): def proc_time(s): + """Compute current HTTP request processing time.""" return time.time() - s['Start Time'] @@ -256,20 +257,24 @@ class ByteCountWrapper(object): """Wraps a file-like object, counting the number of bytes read.""" def __init__(self, rfile): + """Initialize a read byte counter.""" self.rfile = rfile self.bytes_read = 0 def read(self, size=-1): + """Read from file, counting bytes.""" data = self.rfile.read(size) self.bytes_read += len(data) return data def readline(self, size=-1): + """Read a line from file, counting bytes.""" data = self.rfile.readline(size) self.bytes_read += len(data) return data def readlines(self, sizehint=0): + """Read a list of lines from file, counting bytes.""" # Shamelessly stolen from StringIO total = 0 lines = [] @@ -283,22 +288,27 @@ def readlines(self, sizehint=0): return lines def close(self): + """Close the underlying file object.""" self.rfile.close() def __iter__(self): + """Make a file reader iterator.""" return self def next(self): + """Return next portion of bytes from the iterated file.""" data = self.rfile.next() self.bytes_read += len(data) return data def average_uriset_time(s): + """Compute average request processing time within a URI set.""" return s['Count'] and (s['Sum'] / s['Count']) or 0 def _get_threading_ident(): + """Discover the current thread identifier.""" if sys.version_info >= (3, 3): return threading.get_ident() return threading._get_ident() @@ -308,10 +318,11 @@ class StatsTool(cherrypy.Tool): """Record various information about the current request.""" def __init__(self): + """Initialize the statistics gathering tool.""" cherrypy.Tool.__init__(self, 'on_end_request', self.record_stop) def _setup(self): - """Hook this tool into cherrypy.request. + """Plug this tool into ``cherrypy.request``. The standard CherryPy request object will automatically call this method when the tool is "turned on" in config. @@ -404,14 +415,17 @@ def record_stop( def locale_date(v): + """Format given date per current locale.""" return time.strftime('%c', time.gmtime(v)) def iso_format(v): + """Format given date as ISO string.""" return time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(v)) def pause_resume(ns): + """Produce pause or resume HTML form maker.""" def _pause_resume(enabled): pause_disabled = '' resume_disabled = '' @@ -433,6 +447,7 @@ def _pause_resume(enabled): class StatsPage(object): + """An app rendering the gathered statistics.""" formatting = { 'CherryPy Applications': { @@ -474,6 +489,7 @@ class StatsPage(object): @cherrypy.expose def index(self): + """Render the app stats index page.""" # Transform the raw data into pretty output for HTML yield """ @@ -672,12 +688,14 @@ def get_list_collection(self, v, formatting): if json is not None: @cherrypy.expose def data(self): + """Render statistics as JSON.""" s = extrapolate_statistics(logging.statistics) cherrypy.response.headers['Content-Type'] = 'application/json' return json.dumps(s, sort_keys=True, indent=4).encode('utf-8') @cherrypy.expose def pause(self, namespace): + """Pause gathering the statistics.""" logging.statistics.get(namespace, {})['Enabled'] = False raise cherrypy.HTTPRedirect('./') pause.cp_config = {'tools.allow.on': True, @@ -685,6 +703,7 @@ def pause(self, namespace): @cherrypy.expose def resume(self, namespace): + """Resume gathering the statistics.""" logging.statistics.get(namespace, {})['Enabled'] = True raise cherrypy.HTTPRedirect('./') resume.cp_config = {'tools.allow.on': True, diff --git a/cherrypy/lib/cptools.py b/cherrypy/lib/cptools.py index 61d4d36b0..2fc8ea878 100644 --- a/cherrypy/lib/cptools.py +++ b/cherrypy/lib/cptools.py @@ -172,7 +172,6 @@ def proxy(base=None, local='X-Forwarded-Host', remote='X-Forwarded-For', default, 'remote' is set to 'X-Forwarded-For'. If you do not want to rewrite remote.ip, set the 'remote' arg to an empty string. """ - request = cherrypy.serving.request if scheme: @@ -288,23 +287,45 @@ class SessionAuth(object): debug = False def check_username_and_password(self, username, password): - pass + """Assert the login credentials. + + :param username: A user name sent from the login form. + :type username: str + :param password: A pass word sent from the login form. + :type password: str + + :returns: A non-empty error string if the authentication fails. + :rtype: str + """ def anonymous(self): """Provide a temporary user name for anonymous users.""" pass def on_login(self, username): - pass + """Process a successful login event. + + :param username: The logged in user name. + :type username: str + """ def on_logout(self, username): - pass + """Process a successful logout event. + + :param username: The logged out user name. + :type username: str + """ def on_check(self, username): - pass + """Process a successful check event. + + :param username: The checked user name. + :type username: str + """ def login_screen(self, from_page='..', username='', error_msg='', **kwargs): + """Render the login HTML page.""" return (str(""" Message: %(error_msg)s
@@ -385,6 +406,7 @@ def _debug_message(self, template, context={}): cherrypy.log(template % context, 'TOOLS.SESSAUTH') def run(self): + """Perform session authentication.""" request = cherrypy.serving.request response = cherrypy.serving.response @@ -592,19 +614,21 @@ def accept(media=None, debug=False): class MonitoredHeaderMap(_httputil.HeaderMap): + """An access-tracked HTTP header mapping.""" def transform_key(self, key): + """Normalize and track an HTTP header name.""" self.accessed_headers.add(key) return super(MonitoredHeaderMap, self).transform_key(key) def __init__(self): + """Initialize a monitored HTTP header mapping.""" self.accessed_headers = set() super(MonitoredHeaderMap, self).__init__() def autovary(ignore=None, debug=False): - """Auto-populate the Vary response header based on request.header access. - """ + """Populate ``Vary`` response header based on ``request.header`` access.""" request = cherrypy.serving.request req_h = request.headers diff --git a/cherrypy/lib/encoding.py b/cherrypy/lib/encoding.py index 068c88d25..42419bacd 100644 --- a/cherrypy/lib/encoding.py +++ b/cherrypy/lib/encoding.py @@ -1,3 +1,4 @@ +"""Encoding module.""" import struct import time import io @@ -41,32 +42,41 @@ def decode(encoding=None, default_encoding='utf-8'): class UTF8StreamEncoder: + """UTF8 Stream Encoder.""" + def __init__(self, iterator): + """Initialize a UTF-8 stream encoder instance.""" self._iterator = iterator def __iter__(self): + """Make a UTF-8-encoded stream iterator.""" return self def next(self): + """UTF-8-encode the next chunk of the stream.""" return self.__next__() def __next__(self): + """UTF-8-encode the next chunk of the stream.""" res = next(self._iterator) if isinstance(res, str): res = res.encode('utf-8') return res def close(self): + """Close the underlying byte stream.""" if is_closable_iterator(self._iterator): self._iterator.close() def __getattr__(self, attr): + """Return the underlying byte stream attribute value.""" if attr.startswith('__'): raise AttributeError(self, attr) return getattr(self._iterator, attr) class ResponseEncoder: + """An HTTP response payload encoder.""" default_encoding = 'utf-8' failmsg = 'Response body could not be encoded with %r.' @@ -77,6 +87,7 @@ class ResponseEncoder: debug = False def __init__(self, **kwargs): + """Initialize HTTP response payload encoder.""" for k, v in kwargs.items(): setattr(self, k, v) @@ -124,6 +135,7 @@ def encode_string(self, encoding): return True def find_acceptable_charset(self): + """Deduce acceptable charset for HTTP response.""" request = cherrypy.serving.request response = cherrypy.serving.response @@ -219,6 +231,7 @@ def find_acceptable_charset(self): raise cherrypy.HTTPError(406, msg) def __call__(self, *args, **kwargs): + """Set up encoding for the HTTP response.""" response = cherrypy.serving.response self.body = self.oldhandler(*args, **kwargs) @@ -330,6 +343,7 @@ def compress(body, compress_level): def decompress(body): + """Decompress a blob of bytes.""" import gzip zbuf = io.BytesIO() diff --git a/cherrypy/lib/gctools.py b/cherrypy/lib/gctools.py index 14d3ba7e7..0e0149ebd 100644 --- a/cherrypy/lib/gctools.py +++ b/cherrypy/lib/gctools.py @@ -1,3 +1,4 @@ +"""Garbage collection inspection tooling.""" import gc import inspect import sys @@ -19,6 +20,7 @@ class ReferrerTree(object): peek_length = 40 def __init__(self, ignore=None, maxdepth=2, maxparents=10): + """Initialize a referrer tree structure.""" self.ignore = ignore or [] self.ignore.append(inspect.currentframe().f_back) self.maxdepth = maxdepth @@ -99,18 +101,23 @@ def ascend(branch, depth=1): def get_instances(cls): + """Return GC instances.""" return [x for x in gc.get_objects() if isinstance(x, cls)] class RequestCounter(SimplePlugin): + """An HTTP request counter plugin.""" def start(self): + """Initialize the internal counter.""" self.count = 0 def before_request(self): + """Increment the counter before HTTP request.""" self.count += 1 def after_request(self): + """Decrement the counter after HTTP request.""" self.count -= 1 @@ -119,6 +126,7 @@ def after_request(self): def get_context(obj): + """Compute object's runtime context information.""" if isinstance(obj, _cprequest.Request): return 'path=%s;stage=%s' % (obj.path_info, obj.stage) elif isinstance(obj, _cprequest.Response): @@ -144,10 +152,12 @@ class GCRoot(object): @cherrypy.expose def index(self): + """Render the index page HTML content.""" return 'Hello, world!' @cherrypy.expose def stats(self): + """Render garbage collection statistics page HTML content.""" output = ['Statistics:'] for trial in range(10): diff --git a/cherrypy/lib/headers.py b/cherrypy/lib/headers.py index ddbe515be..30e4813c5 100644 --- a/cherrypy/lib/headers.py +++ b/cherrypy/lib/headers.py @@ -1,4 +1,4 @@ -"""headers.""" +"""HTTP header parsing helpers.""" def _parse_param(s): @@ -15,11 +15,11 @@ def _parse_param(s): def parse_header(line): - """Parse a Content-type like header. + """Parse a ``Content-Type`` like header. - Return the main content-type and a dictionary of options. + Return the main ``Content-Type`` and a dictionary of options. - Copied from removed stdlib cgi module. See + Copied from removed stdlib :mod:`cgi` module. See `cherrypy/cherrypy#2014 (comment) `_ for background. diff --git a/cherrypy/lib/httputil.py b/cherrypy/lib/httputil.py index 7f0a24252..204eb488e 100644 --- a/cherrypy/lib/httputil.py +++ b/cherrypy/lib/httputil.py @@ -78,7 +78,6 @@ def get_ranges(headervalue, content_length): If this function returns an empty list, you should return HTTP 416. """ - if not headervalue: return None @@ -130,25 +129,31 @@ class HeaderElement(object): """An element (with parameters) from an HTTP header's element list.""" def __init__(self, value, params=None): + """Initialize an HTTP header value representation.""" self.value = value if params is None: params = {} self.params = params def __cmp__(self, other): + """Compare current HTTP header to another by value only.""" return builtins.cmp(self.value, other.value) def __lt__(self, other): + """Check if this header value is less than the other.""" return self.value < other.value def __str__(self): + """Render the HTTP header value as a string.""" p = [';%s=%s' % (k, v) for k, v in self.params.items()] return str('%s%s' % (self.value, ''.join(p))) def __bytes__(self): + """Turn the HTTP header value string representation to bytes.""" return ntob(self.__str__()) def __unicode__(self): + """Render the HTTP header value as a string.""" return ntou(self.__str__()) @staticmethod @@ -180,6 +185,7 @@ class AcceptElement(HeaderElement): @classmethod def from_str(cls, elementstr): + """Make an :class:`AcceptElement` instance from a string.""" qvalue = None # The first "q" parameter (if any) separates the initial # media-range parameter(s) (if any) from the accept-params. @@ -197,7 +203,7 @@ def from_str(cls, elementstr): @property def qvalue(self): - 'The qvalue, or priority, of this value.' + """The qvalue, or priority, of this value.""" val = self.params.get('q', '1') if isinstance(val, HeaderElement): val = val.value @@ -215,12 +221,17 @@ def qvalue(self): ) from val_err def __cmp__(self, other): + """Compare current header to another by qvalues then strings.""" diff = builtins.cmp(self.qvalue, other.qvalue) if diff == 0: diff = builtins.cmp(str(self), str(other)) return diff def __lt__(self, other): + """Check if this header qvalue is less than the other. + + This method uses string comparison as the second factor. + """ if self.qvalue == other.qvalue: return str(self) < str(other) else: @@ -231,7 +242,9 @@ def __lt__(self, other): def header_elements(fieldname, fieldvalue): - """Return a sorted HeaderElement list from a comma-separated header string. + """Return a sorted :class:`HeaderElement` list. + + Constucted from a comma-separated header string. """ if not fieldvalue: return [] @@ -283,7 +296,6 @@ def valid_status(status): ... ) + BaseHTTPRequestHandler.responses[http.client.ACCEPTED] True """ - if not status: status = 200 @@ -322,7 +334,6 @@ def _parse_qs(qs, keep_blank_values=0, strict_parsing=0, encoding='utf-8'): """Parse a query given as a string argument. Arguments: - qs: URL-encoded query string to be parsed keep_blank_values: flag indicating whether blank values in @@ -391,6 +402,7 @@ class CaseInsensitiveDict(jaraco.collections.KeyTransformingDict): @staticmethod def transform_key(key): + """Title-case an HTTP header name.""" if key is None: # TODO(#1830): why? return 'None' @@ -444,7 +456,8 @@ def output(self): @classmethod def encode_header_items(cls, header_items): - """ + """Emit tuples of wire-ready HTTP headers. + Prepare the sequence of name, value tuples into a form suitable for transmitting on the wire for HTTP. """ @@ -456,6 +469,7 @@ def encode_header_items(cls, header_items): @classmethod def encode_header_item(cls, item): + """Encode an HTTP header for sending over the wire.""" if isinstance(item, str): item = cls.encode(item) @@ -501,6 +515,7 @@ class Host(object): name = 'unknown.tld' def __init__(self, ip, port, name=None): + """Initialize a TCP service representation.""" self.ip = ip self.port = port if name is None: @@ -508,11 +523,13 @@ def __init__(self, ip, port, name=None): self.name = name def __repr__(self): + """Render a :class:`Host` instance representation.""" return 'httputil.Host(%r, %r, %r)' % (self.ip, self.port, self.name) class SanitizedHost(str): - r""" + r"""A normalized host header value. + Wraps a raw host header received from the network in a sanitized version that elides dangerous characters. @@ -526,9 +543,11 @@ class SanitizedHost(str): >>> isinstance(SanitizedHost('foobar'), SanitizedHost) False """ + dangerous = re.compile(r'[\n\r]') def __new__(cls, raw): + """Construct a new :class:`SanitizedHost` instance.""" sanitized = cls._sanitize(raw) if sanitized == raw: return raw @@ -538,4 +557,5 @@ def __new__(cls, raw): @classmethod def _sanitize(cls, raw): + """Clean up the CR LF chars from input.""" return cls.dangerous.sub('', raw) diff --git a/cherrypy/lib/jsontools.py b/cherrypy/lib/jsontools.py index 9ca75a8f3..13685a9dc 100644 --- a/cherrypy/lib/jsontools.py +++ b/cherrypy/lib/jsontools.py @@ -1,3 +1,4 @@ +"""JSON tools.""" import cherrypy from cherrypy import _json as json from cherrypy._cpcompat import text_or_bytes, ntou @@ -15,7 +16,8 @@ def json_processor(entity): def json_in(content_type=[ntou('application/json'), ntou('text/javascript')], force=True, debug=False, processor=json_processor): - """Add a processor to parse JSON request entities: + """Add a processor to parse JSON request entities. + The default processor places the parsed data into request.json. Incoming request entities which match the given content_type(s) will @@ -56,6 +58,7 @@ def json_in(content_type=[ntou('application/json'), ntou('text/javascript')], def json_handler(*args, **kwargs): + """Convert decorated HTTP handler-returned object to JSON string.""" value = cherrypy.serving.request._json_inner_handler(*args, **kwargs) return json.encode(value) diff --git a/cherrypy/lib/locking.py b/cherrypy/lib/locking.py index ea76450a1..fb282f65d 100644 --- a/cherrypy/lib/locking.py +++ b/cherrypy/lib/locking.py @@ -1,15 +1,20 @@ +"""Lock acquisition helpers.""" import datetime class NeverExpires(object): + """A representation of a never expiring object.""" + def expired(self): + """Communicate that the object hasn't expired.""" return False class Timer(object): """A simple timer that will indicate when an expiration time has passed.""" + def __init__(self, expiration): - 'Create a timer that expires at `expiration` (UTC datetime)' + """Create a timer that expires at `expiration` (UTC datetime).""" self.expiration = expiration @classmethod @@ -20,18 +25,21 @@ def after(cls, elapsed): ) def expired(self): + """Check whether the timer has expired.""" return datetime.datetime.now( datetime.timezone.utc, ) >= self.expiration class LockTimeout(Exception): - 'An exception when a lock could not be acquired before a timeout period' + """Exception when a lock could not be acquired before a timeout period.""" class LockChecker(object): """Keep track of the time and detect if a timeout has expired.""" + def __init__(self, session_id, timeout): + """Initialize a lock acquisition tracker.""" self.session_id = session_id if timeout: self.timer = Timer.after(timeout) @@ -39,6 +47,7 @@ def __init__(self, session_id, timeout): self.timer = NeverExpires() def expired(self): + """Check whether the lock checker has expired.""" if self.timer.expired(): raise LockTimeout( 'Timeout acquiring lock for %(session_id)s' % vars(self)) diff --git a/cherrypy/lib/profiler.py b/cherrypy/lib/profiler.py index 9cda41d88..86383ed45 100644 --- a/cherrypy/lib/profiler.py +++ b/cherrypy/lib/profiler.py @@ -69,8 +69,10 @@ def new_func_strip_path(func_name): class Profiler(object): + """A profiling app.""" def __init__(self, path=None): + """Prepare the profiling app resources.""" if not path: path = os.path.join(os.path.dirname(__file__), 'profile') self.path = path @@ -88,13 +90,19 @@ def run(self, func, *args, **params): return result def statfiles(self): - """:rtype: list of available profiles. + """Compose a list of statistics file names. + + :returns: A list of available profiles. + :rtype: list[str] """ return [f for f in os.listdir(self.path) if f.startswith('cp_') and f.endswith('.prof')] def stats(self, filename, sortby='cumulative'): - """:rtype stats(index): output of print_stats() for the given profile. + """Generate statistics from given profile. + + :returns: The sorted stats index printout. + :rtype: str """ sio = io.StringIO() if sys.version_info >= (2, 5): @@ -120,6 +128,7 @@ def stats(self, filename, sortby='cumulative'): @cherrypy.expose def index(self): + """Render the profiling viewer index page.""" return """ CherryPy profile data @@ -131,6 +140,7 @@ def index(self): @cherrypy.expose def menu(self): + """Render the profiler menu page html layout.""" yield '

Profiling runs

' yield '

Click on one of the runs below to see profiling data.

' runs = self.statfiles() @@ -141,19 +151,23 @@ def menu(self): @cherrypy.expose def report(self, filename): + """Render a statistics report.""" cherrypy.response.headers['Content-Type'] = 'text/plain' return self.stats(filename) class ProfileAggregator(Profiler): + """A profiling aggregator app.""" def __init__(self, path=None): + """Prepare the profiling aggregator app resources.""" Profiler.__init__(self, path) global _count self.count = _count = _count + 1 self.profiler = profile.Profile() def run(self, func, *args, **params): + """Dump aggeregated profile data into ``self.path``.""" path = os.path.join(self.path, 'cp_%04d.prof' % self.count) result = self.profiler.runcall(func, *args, **params) self.profiler.dump_stats(path) @@ -161,6 +175,7 @@ def run(self, func, *args, **params): class make_app: + """Profiling WSGI middleware wrapper.""" def __init__(self, nextapp, path=None, aggregate=False): """Make a WSGI middleware app which wraps 'nextapp' with profiling. @@ -194,6 +209,7 @@ def __init__(self, nextapp, path=None, aggregate=False): self.profiler = Profiler(path) def __call__(self, environ, start_response): + """Process a WSGI request.""" def gather(): result = [] for line in self.nextapp(environ, start_response): @@ -203,6 +219,7 @@ def gather(): def serve(path=None, port=8080): + """Serve the web app with profiler activated.""" if profile is None or pstats is None: msg = ('Your installation of Python does not have a profile module. ' "If you're on Debian, try " diff --git a/cherrypy/lib/reprconf.py b/cherrypy/lib/reprconf.py index 532610949..2f9b8e488 100644 --- a/cherrypy/lib/reprconf.py +++ b/cherrypy/lib/reprconf.py @@ -92,10 +92,12 @@ def __call__(self, config): handler(k, v) def __repr__(self): + """Render representation of a :class:`NamespaceSet` instance.""" return '%s.%s(%s)' % (self.__module__, self.__class__.__name__, dict.__repr__(self)) def __copy__(self): + """Make a copy of this instance.""" newobj = self.__class__() newobj.update(self) return newobj @@ -113,6 +115,7 @@ class Config(dict): namespaces = NamespaceSet() def __init__(self, file=None, **kwargs): + """Initialize a CherryPy :class:`Config`.""" self.reset() if file is not None: self.update(file) @@ -141,20 +144,24 @@ def _apply(self, config): self.namespaces(config) def __setitem__(self, k, v): + """Assign a config setting.""" dict.__setitem__(self, k, v) self.namespaces({k: v}) class Parser(configparser.ConfigParser): + """A parser for the INI-style config file. - """Sub-class of ConfigParser that keeps the case of options and that + Sub-class of ConfigParser that keeps the case of options and that raises an exception if the file cannot be read. """ def optionxform(self, optionstr): + """Keep the option names unedited.""" return optionstr def read(self, filenames): + """Read the config from files on disk.""" if isinstance(filenames, text_or_bytes): filenames = [filenames] for filename in filenames: @@ -186,6 +193,7 @@ def as_dict(self, raw=False, vars=None): return result def dict_from_file(self, file): + """Generate a dict from a file.""" if hasattr(file, 'read'): self.read_file(file) else: @@ -235,10 +243,9 @@ def build_Index(self, o): return self.build(o.value) def _build_call35(self, o): - """ - Workaround for python 3.5 _ast.Call signature, docs found here - https://greentreesnakes.readthedocs.org/en/latest/nodes.html - """ + """Emulate ``build_Call`` under Python 3.5.""" + # Workaround for python 3.5. _ast.Call signature, docs found at + # https://greentreesnakes.readthedocs.org/en/latest/nodes.html import ast callee = self.build(o.func) args = [] @@ -375,7 +382,6 @@ def modules(modulePath): def attributes(full_attribute_name): """Load a module and retrieve an attribute of that module.""" - # Parse out the path, module, and attribute last_dot = full_attribute_name.rfind('.') attr_name = full_attribute_name[last_dot + 1:] diff --git a/cherrypy/lib/sessions.py b/cherrypy/lib/sessions.py index 68badd93b..f0d2678b4 100644 --- a/cherrypy/lib/sessions.py +++ b/cherrypy/lib/sessions.py @@ -177,6 +177,7 @@ def id(self, value): # --------------------- Session management methods --------------------- # def __init__(self, id=None, **kwargs): + """Initialize the session tool.""" self.id_observers = [] self._data = {} @@ -321,16 +322,19 @@ def delete(self): # -------------------- Application accessor methods -------------------- # def __getitem__(self, key): + """Retrieve a session-stored object.""" if not self.loaded: self.load() return self._data[key] def __setitem__(self, key, value): + """Store an object in the session.""" if not self.loaded: self.load() self._data[key] = value def __delitem__(self, key): + """Delete object stored in the session.""" if not self.loaded: self.load() del self._data[key] @@ -349,13 +353,15 @@ def pop(self, key, default=missing): return self._data.pop(key, default) def __contains__(self, key): + """Check if the session has an object by key.""" if not self.loaded: self.load() return key in self._data def get(self, key, default=None): - """D.get(k[,d]) -> D[k] if k in D, else d. + """Retrieve a session-stored object. + D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """ if not self.loaded: @@ -363,8 +369,9 @@ def get(self, key, default=None): return self._data.get(key, default) def update(self, d): - """D.update(E) -> None. + """Update multiple session-stored objects in one go. + D.update(E) -> None. Update D from E: for k in E: D[k] = E[k]. """ if not self.loaded: @@ -372,14 +379,18 @@ def update(self, d): self._data.update(d) def setdefault(self, key, default=None): - """D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D.""" + """Set a default session key value. + + D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D. + """ if not self.loaded: self.load() return self._data.setdefault(key, default) def clear(self): - """D.clear() -> None. + """Clean up the session-stored data. + D.clear() -> None. Remove all items from D. """ if not self.loaded: @@ -387,25 +398,35 @@ def clear(self): self._data.clear() def keys(self): - """D.keys() -> list of D's keys.""" + """Return an iterable of session keys. + + D.keys() -> list of D's keys. + """ if not self.loaded: self.load() return self._data.keys() def items(self): - """D.items() -> list of D's (key, value) pairs, as 2-tuples.""" + """Return an iterable of items as tuples. + + D.items() -> list of D's (key, value) pairs, as 2-tuples. + """ if not self.loaded: self.load() return self._data.items() def values(self): - """D.values() -> list of D's values.""" + """Return an iterable of session objects. + + D.values() -> list of D's values. + """ if not self.loaded: self.load() return self._data.values() class RamSession(Session): + """A memory-baked session store implementation.""" # Class-level objects. Don't rebind these! cache = {} @@ -413,7 +434,6 @@ class RamSession(Session): def clean_up(self): """Clean up expired sessions.""" - now = self.now() for _id, (data, expiration_time) in self.cache.copy().items(): if expiration_time <= now: @@ -466,8 +486,7 @@ def __len__(self): class FileSession(Session): - - """Implementation of the File backend for sessions + """Implementation of the file backend for sessions. storage_path The folder where session data will be saved. Each session @@ -485,6 +504,7 @@ class FileSession(Session): pickle_protocol = pickle.HIGHEST_PROTOCOL def __init__(self, id=None, **kwargs): + """Prepare the file session store.""" # The 'storage_path' arg is required for file-based sessions. kwargs['storage_path'] = os.path.abspath(kwargs['storage_path']) kwargs.setdefault('lock_timeout', None) @@ -614,6 +634,7 @@ def __len__(self): class MemcachedSession(Session): + """A Memcached-baked session store.""" # The most popular memcached client for Python isn't thread-safe. # Wrap all .get and .set operations in a single lock. @@ -687,7 +708,6 @@ def __len__(self): def save(): """Save any changed session data.""" - if not hasattr(cherrypy.serving, 'session'): return request = cherrypy.serving.request @@ -783,7 +803,6 @@ def init(storage_type=None, path=None, path_header=None, name='session_id', and may be specific to the storage type. See the subclass of Session you're using for more information. """ - # Py27 compat storage_class = kwargs.pop('storage_class', RamSession) @@ -898,7 +917,8 @@ def set_response_cookie(path=None, path_header=None, name='session_id', def _add_MSIE_max_age_workaround(cookie, timeout): - """ + """Inject a Microsoft Internet Explorer ``max-age`` workaround. + We'd like to use the "max-age" param as indicated in http://www.faqs.org/rfcs/rfc2109.html but IE doesn't save it to disk and the session is lost if people close diff --git a/cherrypy/process/plugins.py b/cherrypy/process/plugins.py index 20bf1c665..53be2faa6 100644 --- a/cherrypy/process/plugins.py +++ b/cherrypy/process/plugins.py @@ -38,6 +38,7 @@ class SimplePlugin(object): """ def __init__(self, bus): + """Initialize a simple plugin.""" self.bus = bus def subscribe(self): @@ -93,6 +94,7 @@ class SignalHandler(object): del k, v def __init__(self, bus): + """Initialize a signal handler plugin.""" self.bus = bus # Set default handlers self.handlers = {'SIGTERM': self.bus.exit, @@ -117,8 +119,7 @@ def _jython_SIGINT_handler(self, signum=None, frame=None): self.bus.exit() def _is_daemonized(self): - """Return boolean indicating if the current process is - running as a daemon. + """Check if current process is running as a daemon. The criteria to determine the `daemon` condition is to verify if the current pid is not the same as the one that got used on @@ -223,6 +224,7 @@ class DropPrivileges(SimplePlugin): """ def __init__(self, bus, umask=None, uid=None, gid=None): + """Initialize the privilege dropping plugin.""" SimplePlugin.__init__(self, bus) self.finalized = False self.uid = uid @@ -288,6 +290,7 @@ def umask(self, val): self._umask = val def start(self): + """Drop the process privileges.""" # uid/gid def current_ids(): """Return the current (uid, gid) if available.""" @@ -353,6 +356,7 @@ class Daemonizer(SimplePlugin): def __init__(self, bus, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): + """Initialize the daemonizer plugin.""" SimplePlugin.__init__(self, bus) self.stdin = stdin self.stdout = stdout @@ -360,6 +364,7 @@ def __init__(self, bus, stdin='/dev/null', stdout='/dev/null', self.finalized = False def start(self): + """Attempt to daemonize the process.""" if self.finalized: self.bus.log('Already deamonized.') @@ -382,6 +387,7 @@ def start(self): def daemonize( stdin='/dev/null', stdout='/dev/null', stderr='/dev/null', logger=lambda msg: None): + """Daemonize the process.""" # See http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 # (or http://www.faqs.org/faqs/unix-faq/programmer/faq/ section 1.7) # and http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012 @@ -428,11 +434,13 @@ class PIDFile(SimplePlugin): """Maintain a PID file via a WSPBus.""" def __init__(self, bus, pidfile): + """Initialize the PID file plugin.""" SimplePlugin.__init__(self, bus) self.pidfile = pidfile self.finalized = False def start(self): + """Write a PID file to disk.""" pid = os.getpid() if self.finalized: self.bus.log('PID %r already written to %r.' % (pid, self.pidfile)) @@ -444,6 +452,7 @@ def start(self): start.priority = 70 def exit(self): + """Delete the PID file from disk.""" try: os.remove(self.pidfile) self.bus.log('PID file removed: %r.' % self.pidfile) @@ -462,11 +471,12 @@ class PerpetualTimer(threading.Timer): """ def __init__(self, *args, **kwargs): - "Override parent constructor to allow 'bus' to be provided." + """Override parent constructor to allow 'bus' to be provided.""" self.bus = kwargs.pop('bus', None) super(PerpetualTimer, self).__init__(*args, **kwargs) def run(self): + """Run an infinitely repeated callable.""" while True: self.finished.wait(self.interval) if self.finished.isSet(): @@ -494,6 +504,7 @@ class BackgroundTask(threading.Thread): """ def __init__(self, interval, function, args=[], kwargs={}, bus=None): + """Initialize a background task parameters.""" super(BackgroundTask, self).__init__() self.interval = interval self.function = function @@ -506,9 +517,11 @@ def __init__(self, interval, function, args=[], kwargs={}, bus=None): self.daemon = True def cancel(self): + """Set a task cancellation flag.""" self.running = False def run(self): + """Start running the repeated background task in the loop.""" self.running = True while self.running: time.sleep(self.interval) @@ -539,6 +552,7 @@ class Monitor(SimplePlugin): """ def __init__(self, bus, callback, frequency=60, name=None): + """Initialize the monitor plugin.""" SimplePlugin.__init__(self, bus) self.callback = callback self.frequency = frequency @@ -611,6 +625,7 @@ class Autoreloader(Monitor): """A regular expression by which to match filenames.""" def __init__(self, bus, frequency=1, match='.*'): + """Initialize the auto-reloader monitor plugin.""" self.mtimes = {} self.files = set() self.match = match @@ -717,6 +732,7 @@ class ThreadManager(SimplePlugin): """A map of {thread ident: index number} pairs.""" def __init__(self, bus): + """Initialize the thread manager plugin.""" self.threads = {} SimplePlugin.__init__(self, bus) self.bus.listeners.setdefault('acquire_thread', set()) diff --git a/cherrypy/process/servers.py b/cherrypy/process/servers.py index fdd4bb681..66444c46b 100644 --- a/cherrypy/process/servers.py +++ b/cherrypy/process/servers.py @@ -1,4 +1,6 @@ r""" +Server interfaces. + Starting in CherryPy 3.1, cherrypy.server is implemented as an :ref:`Engine Plugin`. It's an instance of :class:`cherrypy._cpserver.Server`, which is a subclass of @@ -127,6 +129,8 @@ def index(self): class Timeouts: + """Timeout constants.""" + occupied = 5 free = 1 @@ -146,6 +150,7 @@ class ServerAdapter(object): """ def __init__(self, bus, httpserver=None, bind_addr=None): + """Initialize the HTTP server plugin.""" self.bus = bus self.httpserver = httpserver self.bind_addr = bind_addr @@ -153,10 +158,12 @@ def __init__(self, bus, httpserver=None, bind_addr=None): self.running = False def subscribe(self): + """Subscribe control methods to the bus lifecycle events.""" self.bus.subscribe('start', self.start) self.bus.subscribe('stop', self.stop) def unsubscribe(self): + """Unsubcribe control methods to the bus lifecycle events.""" self.bus.unsubscribe('start', self.start) self.bus.unsubscribe('stop', self.stop) @@ -212,7 +219,9 @@ def _get_base(self): return '%s://%s' % (scheme, host) def _start_http_thread(self): - """HTTP servers MUST be running in new threads, so that the + """Start the HTTP server thread. + + HTTP servers MUST be running in new threads, so that the main thread persists to receive KeyboardInterrupt's. If an exception is raised in the httpserver's thread then it's trapped here, and the bus (and therefore our httpserver) @@ -258,9 +267,10 @@ def wait(self): @property def bound_addr(self): - """ - The bind address, or if it's an ephemeral port and the - socket has been bound, return the actual port bound. + """The bind address. + + If it's an ephemeral port and the socket has been bound, + return the actual port bound. """ host, port = self.bind_addr if port == 0 and self.httpserver.socket: @@ -292,6 +302,7 @@ class FlupCGIServer(object): """Adapter for a flup.server.cgi.WSGIServer.""" def __init__(self, *args, **kwargs): + """Initialize the flup CGI Server plugin.""" self.args = args self.kwargs = kwargs self.ready = False @@ -315,6 +326,7 @@ class FlupFCGIServer(object): """Adapter for a flup.server.fcgi.WSGIServer.""" def __init__(self, *args, **kwargs): + """Initialize the FCGI server parameters.""" if kwargs.get('bindAddress', None) is None: import socket if not hasattr(socket, 'fromfd'): @@ -360,6 +372,7 @@ class FlupSCGIServer(object): """Adapter for a flup.server.scgi.WSGIServer.""" def __init__(self, *args, **kwargs): + """Initialize the SCGI server parameters.""" self.args = args self.kwargs = kwargs self.ready = False @@ -395,7 +408,8 @@ def stop(self): @contextlib.contextmanager def _safe_wait(host, port): - """ + """Warn when bind interface is ambiguous. + On systems where a loopback interface is not available and the server is bound to all interfaces, it's difficult to determine whether the server is in fact occupying the port. In this case, diff --git a/cherrypy/process/win32.py b/cherrypy/process/win32.py index 305596bac..c089bbaa0 100644 --- a/cherrypy/process/win32.py +++ b/cherrypy/process/win32.py @@ -17,10 +17,12 @@ class ConsoleCtrlHandler(plugins.SimplePlugin): """A WSPBus plugin for handling Win32 console events (like Ctrl-C).""" def __init__(self, bus): + """Initialize the console control handler.""" self.is_set = False plugins.SimplePlugin.__init__(self, bus) def start(self): + """Register handling of the console control events.""" if self.is_set: self.bus.log('Handler for console events already set.', level=20) return @@ -34,6 +36,7 @@ def start(self): self.is_set = True def stop(self): + """Unregister the console control handlers.""" if not self.is_set: self.bus.log('Handler for console events already off.', level=20) return @@ -78,6 +81,7 @@ class Win32Bus(wspbus.Bus): """ def __init__(self): + """Initialize a Win32 bus implementation.""" self.events = {} wspbus.Bus.__init__(self) @@ -94,10 +98,12 @@ def _get_state_event(self, state): @property def state(self): + """The bus state.""" return self._state @state.setter def state(self, value): + """Set the bus state.""" self._state = value event = self._get_state_event(value) win32event.PulseEvent(event) @@ -144,6 +150,7 @@ def key_for(self, obj): def signal_child(service, command): + """Send a control command to a service.""" if command == 'stop': win32serviceutil.StopService(service) elif command == 'restart': @@ -165,16 +172,19 @@ class PyWebService(win32serviceutil.ServiceFramework): _svc_description_ = 'Python Web Service' def SvcDoRun(self): + """Start the service.""" from cherrypy import process process.bus.start() process.bus.block() def SvcStop(self): + """Stop the service.""" from cherrypy import process self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) process.bus.exit() def SvcOther(self, control): + """Send a command to the service.""" from cherrypy import process process.bus.publish(control_codes.key_for(control)) diff --git a/cherrypy/test/__init__.py b/cherrypy/test/__init__.py index 72c4c2618..05bb17ad7 100644 --- a/cherrypy/test/__init__.py +++ b/cherrypy/test/__init__.py @@ -5,10 +5,12 @@ def newexit(): + """Exit the process with return code of 1.""" os._exit(1) def setup(): + """Monkey-patch ``sys.exit()``.""" # We want to monkey patch sys.exit so that we can get some # information about where exit is being called. newexit._old = sys.exit @@ -16,6 +18,7 @@ def setup(): def teardown(): + """Recover the original ``sys.exit()``.""" try: sys.exit = sys.exit._old except AttributeError: diff --git a/cherrypy/test/_test_decorators.py b/cherrypy/test/_test_decorators.py index 02effbee4..9e261d383 100644 --- a/cherrypy/test/_test_decorators.py +++ b/cherrypy/test/_test_decorators.py @@ -5,29 +5,36 @@ class ExposeExamples(object): + """Exposed routes test app.""" @expose def no_call(self): + """Return a string on ``/no_call``.""" return 'Mr E. R. Bradshaw' @expose() def call_empty(self): + """Return a string on ``/call_empty``.""" return 'Mrs. B.J. Smegma' @expose('call_alias') def nesbitt(self): + """Return "Mr Nesbitt" on ``/call_alias``.""" return 'Mr Nesbitt' @expose(['alias1', 'alias2']) def andrews(self): + """Return a string on ``/andrews``, ``/alias1``, ``/alias2``.""" return 'Mr Ken Andrews' @expose(alias='alias3') def watson(self): + """Return "Mr. and Mrs. Watson" on ``/watson``, ``/alias3``.""" return 'Mr. and Mrs. Watson' class ToolExamples(object): + """A web app with tools.""" @expose # This is here to demonstrate that using the config decorator @@ -36,4 +43,5 @@ class ToolExamples(object): @cherrypy.config(**{'response.stream': True}) @tools.response_headers(headers=[('Content-Type', 'application/data')]) def blah(self): + """Emit "Blah" on ``/blah``.""" yield b'blah' diff --git a/cherrypy/test/_test_states_demo.py b/cherrypy/test/_test_states_demo.py index a49407baf..7ccbc1772 100644 --- a/cherrypy/test/_test_states_demo.py +++ b/cherrypy/test/_test_states_demo.py @@ -8,25 +8,31 @@ class Root: + """A test web app.""" @cherrypy.expose def index(self): + """Produce HTTP response body of test app index URI.""" return 'Hello World' @cherrypy.expose def mtimes(self): + """Respond with timestamps.""" return repr(cherrypy.engine.publish('Autoreloader', 'mtimes')) @cherrypy.expose def pid(self): + """Respond with the current process ID.""" return str(os.getpid()) @cherrypy.expose def start(self): + """Respond with the start time.""" return repr(starttime) @cherrypy.expose def exit(self): + """Stop the server.""" # This handler might be called before the engine is STARTED if an # HTTP worker thread handles it before the HTTP server returns # control to engine.start. We avoid that race condition here @@ -37,6 +43,7 @@ def exit(self): @cherrypy.engine.subscribe('start', priority=100) def unsub_sig(): + """Unsubscribe the default signal handler.""" cherrypy.log('unsubsig: %s' % cherrypy.config.get('unsubsig', False)) if cherrypy.config.get('unsubsig', False): cherrypy.log('Unsubscribing the default cherrypy signal handler') @@ -55,12 +62,14 @@ def old_term_handler(signum=None, frame=None): @cherrypy.engine.subscribe('start', priority=6) def starterror(): + """Error out on start.""" if cherrypy.config.get('starterror', False): 1 / 0 @cherrypy.engine.subscribe('start', priority=6) def log_test_case_name(): + """Log test case name.""" if cherrypy.config.get('test_case_name', False): cherrypy.log('STARTED FROM: %s' % cherrypy.config.get('test_case_name')) diff --git a/cherrypy/test/benchmark.py b/cherrypy/test/benchmark.py index 9c65e17b1..2de46457c 100644 --- a/cherrypy/test/benchmark.py +++ b/cherrypy/test/benchmark.py @@ -47,9 +47,11 @@ class Root: + """Test web app.""" @cherrypy.expose def index(self): + """Produce HTTP response body of test app index URI.""" return """ CherryPy Benchmark @@ -67,10 +69,12 @@ def index(self): @cherrypy.expose def hello(self): + """Render a "Hello world!" message on ``/hello`` URI.""" return 'Hello, world\r\n' @cherrypy.expose def sizer(self, size): + """Send a cached sized response.""" resp = size_cache.get(size, None) if resp is None: size_cache[size] = resp = 'X' * int(size) @@ -78,7 +82,7 @@ def sizer(self, size): def init(): - + """Configure the test server.""" cherrypy.config.update({ 'log.error.file': '', 'environment': 'production', @@ -109,12 +113,13 @@ class NullRequest: """A null HTTP request class, returning 200 and an empty body.""" def __init__(self, local, remote, scheme='http'): - pass + """Initialize a null request instance.""" def close(self): - pass + """Close the null request.""" def run(self, method, path, query_string, protocol, headers, rfile): + """Construct an HTTP response.""" cherrypy.response.status = '200 OK' cherrypy.response.header_list = [('Content-Type', 'text/html'), ('Server', 'Null CherryPy'), @@ -126,7 +131,7 @@ def run(self, method, path, query_string, protocol, headers, rfile): class NullResponse: - pass + """A null HTTP response.""" class ABSession: @@ -206,11 +211,13 @@ class ABSession: def __init__(self, path=SCRIPT_NAME + '/hello', requests=1000, concurrency=10): + """Initialize an Apache Benchmark session.""" self.path = path self.requests = requests self.concurrency = concurrency def args(self): + """Compute the Apache Benchmark CLI arguments.""" port = cherrypy.server.socket_port assert self.concurrency > 0 assert self.requests > 0 @@ -221,6 +228,7 @@ def args(self): (self.requests, self.concurrency, port, self.path)) def run(self): + """Run an Apache Benchmark test.""" # Parse output of ab, setting attributes on self try: self.output = _cpmodpy.read_process(AB_PATH or 'ab', self.args()) @@ -244,6 +252,7 @@ def run(self): def thread_report(path=SCRIPT_NAME + '/hello', concurrency=safe_threads): + """Report Apache Benchmark against a multi-threaded server.""" sess = ABSession(path) attrs, names, patterns = list(zip(*sess.parse_patterns)) avg = dict.fromkeys(attrs, 0.0) @@ -271,6 +280,7 @@ def thread_report(path=SCRIPT_NAME + '/hello', concurrency=safe_threads): def size_report(sizes=(10, 100, 1000, 10000, 100000, 100000000), concurrency=50): + """Report Apache Benchmark against different payload sizes.""" sess = ABSession(concurrency=concurrency) attrs, names, patterns = list(zip(*sess.parse_patterns)) yield ('bytes',) + names @@ -281,6 +291,7 @@ def size_report(sizes=(10, 100, 1000, 10000, 100000, 100000000), def print_report(rows): + """Print rows to standard out.""" for row in rows: print('') for val in row: @@ -289,6 +300,7 @@ def print_report(rows): def run_standard_benchmarks(): + """Run Standard Benchmarks.""" print('') print('Client Thread Report (1000 requests, 14 byte response body, ' '%s server threads):' % cherrypy.server.thread_pool) @@ -308,8 +320,7 @@ def run_standard_benchmarks(): # modpython and other WSGI # def startup_modpython(req=None): - """Start the CherryPy app server in 'serverless' mode (for modpython/WSGI). - """ + """Start CherryPy app server in 'serverless' mode (for modpython/WSGI).""" if cherrypy.engine.state == cherrypy._cpengine.STOPPED: if req: if 'nullreq' in req.get_options(): diff --git a/cherrypy/test/checkerdemo.py b/cherrypy/test/checkerdemo.py index 02553ab00..327a75b66 100644 --- a/cherrypy/test/checkerdemo.py +++ b/cherrypy/test/checkerdemo.py @@ -11,7 +11,7 @@ class Root: - pass + """Demo web app.""" if __name__ == '__main__': diff --git a/cherrypy/test/helper.py b/cherrypy/test/helper.py index 5cf93d0ca..25b25668d 100644 --- a/cherrypy/test/helper.py +++ b/cherrypy/test/helper.py @@ -31,6 +31,7 @@ class Supervisor(object): """Base class for modeling and controlling servers during testing.""" def __init__(self, **kwargs): + """Initialize a supervisor.""" for k, v in kwargs.items(): if k == 'port': setattr(self, k, int(v)) @@ -38,12 +39,12 @@ def __init__(self, **kwargs): def log_to_stderr(msg, level): + """Log to Standard Error.""" return sys.stderr.write(msg + os.linesep) class LocalSupervisor(Supervisor): - """Base class for modeling/controlling servers which run in the same - process. + """Base for modeling/controlling servers which run in the same process. When the server side runs in a different process, start/stop can dump all state between each test module easily. When the server side @@ -55,6 +56,7 @@ class LocalSupervisor(Supervisor): using_wsgi = False def __init__(self, **kwargs): + """Initialize the local supervisor.""" for k, v in kwargs.items(): setattr(self, k, v) @@ -85,6 +87,7 @@ def sync_apps(self): pass def stop(self): + """Stop the HTTP server.""" td = getattr(self, 'teardown', None) if td: td() @@ -104,6 +107,7 @@ class NativeServerSupervisor(LocalSupervisor): using_wsgi = False def __str__(self): + """Render a :class:`NativeServerSupervisor` instance as a string.""" return 'Builtin HTTP Server on %s:%s' % (self.host, self.port) @@ -115,10 +119,11 @@ class LocalWSGISupervisor(LocalSupervisor): using_wsgi = True def __str__(self): + """Render a :class:`LocalWSGISupervisor` instance as a string.""" return 'Builtin WSGI Server on %s:%s' % (self.host, self.port) def sync_apps(self): - """Hook a new WSGI app into the origin server.""" + """Connect a new WSGI app into the origin server.""" cherrypy.server.httpserver.wsgi_app = self.get_app() def get_app(self, app=None): @@ -140,6 +145,7 @@ def get_app(self, app=None): def get_cpmodpy_supervisor(**options): + """Load a CherryPy ``mod_python`` supervisor.""" from cherrypy.test import modpy sup = modpy.ModPythonSupervisor(**options) sup.template = modpy.conf_cpmodpy @@ -147,6 +153,7 @@ def get_cpmodpy_supervisor(**options): def get_modpygw_supervisor(**options): + """Load a CherryPy ``mod_python`` gateway supervisor.""" from cherrypy.test import modpy sup = modpy.ModPythonSupervisor(**options) sup.template = modpy.conf_modpython_gateway @@ -155,26 +162,31 @@ def get_modpygw_supervisor(**options): def get_modwsgi_supervisor(**options): + """Load a CherryPy ``mod_wsgi`` supervisor.""" from cherrypy.test import modwsgi return modwsgi.ModWSGISupervisor(**options) def get_modfcgid_supervisor(**options): + """Load a CherryPy ``mod_fcgi`` supervisor.""" from cherrypy.test import modfcgid return modfcgid.ModFCGISupervisor(**options) def get_modfastcgi_supervisor(**options): + """Load a CherryPy ``mod_fastcgi`` supervisor.""" from cherrypy.test import modfastcgi return modfastcgi.ModFCGISupervisor(**options) def get_wsgi_u_supervisor(**options): + """Load a CherryPy WSGI supervisor.""" cherrypy.server.wsgi_version = ('u', 0) return LocalWSGISupervisor(**options) class CPWebCase(webtest.WebCase): + """CherryPy web test case base.""" script_name = '' scheme = 'http' @@ -236,8 +248,7 @@ def _setup_server(cls, supervisor, conf): @classmethod def setup_class(cls): - '' - # Creates a server + """Invoke a test server.""" conf = { 'scheme': 'http', 'protocol': 'HTTP/1.1', @@ -274,13 +285,14 @@ def setup_class(cls): @classmethod def teardown_class(cls): - '' + """Tear down the test server.""" if hasattr(cls, 'setup_server'): cls.supervisor.stop() do_gc_test = False def test_gc(self): + """Perform the garbage collection testing.""" if not self.do_gc_test: return @@ -291,9 +303,11 @@ def test_gc(self): 'Failures occur intermittently. See #1420' def prefix(self): + """Get an HTTP handler prefix.""" return self.script_name.rstrip('/') def base(self): + """Construct the base server URL.""" if ((self.scheme == 'http' and self.PORT == 80) or (self.scheme == 'https' and self.PORT == 443)): port = '' @@ -304,6 +318,7 @@ def base(self): self.script_name.rstrip('/')) def exit(self): + """Terminate the program.""" sys.exit() def getPage(self, url, *args, **kwargs): @@ -313,6 +328,7 @@ def getPage(self, url, *args, **kwargs): return webtest.WebCase.getPage(self, url, *args, **kwargs) def skip(self, msg='skipped '): + """Skip the currently running test.""" pytest.skip(msg) def assertErrorPage(self, status, message=None, pattern=''): @@ -321,7 +337,6 @@ def assertErrorPage(self, status, message=None, pattern=''): The function will optionally look for the regexp pattern, within the exception embedded in the error page. """ - # This will never contain a traceback page = cherrypy._cperror.get_error_page(status, message=message) @@ -394,6 +409,7 @@ def setup_client(): class CPProcess(object): + """CherryPy Process Spawning Helper.""" pid_file = os.path.join(thisdir, 'test.pid') config_file = os.path.join(thisdir, 'test.conf') @@ -412,6 +428,7 @@ class CPProcess(object): def __init__(self, wait=False, daemonize=False, ssl=False, socket_host=None, socket_port=None): + """Initialize a server process runner.""" self.wait = wait self.daemonize = daemonize self.ssl = ssl @@ -419,6 +436,7 @@ def __init__(self, wait=False, daemonize=False, ssl=False, self.port = socket_port or cherrypy.server.socket_port def write_conf(self, extra=''): + """Write the server config to disk.""" if self.ssl: serverpem = os.path.join(thisdir, 'test.pem') ssl = """ @@ -498,6 +516,7 @@ def start(self, imports=None): time.sleep(1) def get_pid(self): + """Get the server process ID.""" if self.daemonize: with open(self.pid_file, 'rb') as f: return int(f.read()) diff --git a/cherrypy/test/logtest.py b/cherrypy/test/logtest.py index acdc95876..433d4e5c9 100644 --- a/cherrypy/test/logtest.py +++ b/cherrypy/test/logtest.py @@ -14,6 +14,7 @@ import msvcrt def getchar(): + """Get a key press from the Windows terminal.""" return msvcrt.getch() except ImportError: # Unix getchr @@ -21,6 +22,7 @@ def getchar(): import termios def getchar(): + """Get a key press from the UNIX terminal.""" fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: @@ -92,6 +94,7 @@ def _handleLogError(self, msg, data, marker, pattern): sys.stdout.write(p + ' ') def exit(self): + """Terminate the program.""" sys.exit() def emptyLog(self): diff --git a/cherrypy/test/modfastcgi.py b/cherrypy/test/modfastcgi.py index 16927d3cc..8c20605fb 100644 --- a/cherrypy/test/modfastcgi.py +++ b/cherrypy/test/modfastcgi.py @@ -44,6 +44,7 @@ def read_process(cmd, args=''): + """Return subprocess' console output.""" pipein, pipeout = os.popen4('%s %s' % (cmd, args)) try: firstline = pipeout.readline() @@ -81,11 +82,13 @@ def read_process(cmd, args=''): def erase_script_name(environ, start_response): + """Erase script name from the WSGI environment.""" environ['SCRIPT_NAME'] = '' return cherrypy.tree(environ, start_response) class ModFCGISupervisor(helper.LocalWSGISupervisor): + """Server Controller for ModFastCGI and CherryPy.""" httpserver_class = 'cherrypy.process.servers.FlupFCGIServer' using_apache = True @@ -93,9 +96,11 @@ class ModFCGISupervisor(helper.LocalWSGISupervisor): template = conf_fastcgi def __str__(self): + """Render a :class:`ModFastCGISupervisor` instance as a string.""" return 'FCGI Server on %s:%s' % (self.host, self.port) def start(self, modulename): + """Spawn an Apache ``mod_fastcgi`` supervisor process.""" cherrypy.server.httpserver = servers.FlupFCGIServer( application=erase_script_name, bindAddress=('127.0.0.1', 4000)) cherrypy.server.httpserver.bind_addr = ('127.0.0.1', 4000) @@ -107,6 +112,7 @@ def start(self, modulename): self.sync_apps() def start_apache(self): + """Start Apache instance.""" fcgiconf = CONF_PATH if not os.path.isabs(fcgiconf): fcgiconf = os.path.join(curdir, fcgiconf) @@ -129,5 +135,6 @@ def stop(self): helper.LocalWSGISupervisor.stop(self) def sync_apps(self): + """Set up the FastCGI request handler.""" cherrypy.server.httpserver.fcgiserver.application = self.get_app( erase_script_name) diff --git a/cherrypy/test/modfcgid.py b/cherrypy/test/modfcgid.py index 6dd48a943..5a4a3a8f1 100644 --- a/cherrypy/test/modfcgid.py +++ b/cherrypy/test/modfcgid.py @@ -45,6 +45,7 @@ def read_process(cmd, args=''): + """Return subprocess' console output.""" pipein, pipeout = os.popen4('%s %s' % (cmd, args)) try: firstline = pipeout.readline() @@ -78,15 +79,18 @@ def read_process(cmd, args=''): class ModFCGISupervisor(helper.LocalSupervisor): + """Server Controller for ModFCGI and CherryPy.""" using_apache = True using_wsgi = True template = conf_fcgid def __str__(self): + """Render a :class:`ModFCGISupervisor` instance as a string.""" return 'FCGI Server on %s:%s' % (self.host, self.port) def start(self, modulename): + """Spawn an Apache ``mod_fcgid`` supervisor process.""" cherrypy.server.httpserver = servers.FlupFCGIServer( application=cherrypy.tree, bindAddress=('127.0.0.1', 4000)) cherrypy.server.httpserver.bind_addr = ('127.0.0.1', 4000) @@ -96,6 +100,7 @@ def start(self, modulename): helper.LocalServer.start(self, modulename) def start_apache(self): + """Start Apache instance.""" fcgiconf = CONF_PATH if not os.path.isabs(fcgiconf): fcgiconf = os.path.join(curdir, fcgiconf) @@ -118,4 +123,5 @@ def stop(self): helper.LocalServer.stop(self) def sync_apps(self): + """Set up the FCGID request handler.""" cherrypy.server.httpserver.fcgiserver.application = self.get_app() diff --git a/cherrypy/test/modpy.py b/cherrypy/test/modpy.py index f4f685232..c9b3062b0 100644 --- a/cherrypy/test/modpy.py +++ b/cherrypy/test/modpy.py @@ -44,6 +44,7 @@ def read_process(cmd, args=''): + """Return subprocess' console output.""" pipein, pipeout = os.popen4('%s %s' % (cmd, args)) try: firstline = pipeout.readline() @@ -94,15 +95,18 @@ def read_process(cmd, args=''): class ModPythonSupervisor(helper.Supervisor): + """Server Controller for ModPython and CherryPy.""" using_apache = True using_wsgi = False template = None def __str__(self): + """Render a :class:`ModPythonSupervisor` instance as a string.""" return 'ModPython Server on %s:%s' % (self.host, self.port) def start(self, modulename): + """Spawn an Apache ``mod_python`` supervisor process.""" mpconf = CONF_PATH if not os.path.isabs(mpconf): mpconf = os.path.join(curdir, mpconf) @@ -125,6 +129,7 @@ def stop(self): def wsgisetup(req): + """Start a WGSI server.""" global loaded if not loaded: loaded = True @@ -147,6 +152,7 @@ def wsgisetup(req): def cpmodpysetup(req): + """Start a CherryPy server with ``mod_python``.""" global loaded if not loaded: loaded = True diff --git a/cherrypy/test/modwsgi.py b/cherrypy/test/modwsgi.py index 911d48191..1e074e63b 100644 --- a/cherrypy/test/modwsgi.py +++ b/cherrypy/test/modwsgi.py @@ -48,6 +48,7 @@ def read_process(cmd, args=''): + """Return subprocess' console output.""" pipein, pipeout = os.popen4('%s %s' % (cmd, args)) try: firstline = pipeout.readline() @@ -101,9 +102,11 @@ class ModWSGISupervisor(helper.Supervisor): template = conf_modwsgi def __str__(self): + """Render a :class:`ModWSGISupervisor` instance as a string.""" return 'ModWSGI Server on %s:%s' % (self.host, self.port) def start(self, modulename): + """Spawn an Apache ``mod_wsgi`` supervisor process.""" mpconf = CONF_PATH if not os.path.isabs(mpconf): mpconf = os.path.join(curdir, mpconf) @@ -133,6 +136,7 @@ def stop(self): def application(environ, start_response): + """Respond to a WSGI-interfaced HTTP request via test module.""" global loaded if not loaded: loaded = True diff --git a/cherrypy/test/sessiondemo.py b/cherrypy/test/sessiondemo.py index 0eb13cf72..989e23a56 100755 --- a/cherrypy/test/sessiondemo.py +++ b/cherrypy/test/sessiondemo.py @@ -98,8 +98,10 @@ class Root(object): + """Session demo app.""" def page(self): + """Render HTTP request-related details.""" changemsg = [] if cherrypy.session.id != cherrypy.session.originalid: if cherrypy.session.originalid is None: @@ -137,17 +139,20 @@ def page(self): @cherrypy.expose def index(self): + """Save green color in session at app index URI.""" # Must modify data or the session will not be saved. cherrypy.session['color'] = 'green' return self.page() @cherrypy.expose def expire(self): + """Expire the existing session.""" sessions.expire() return self.page() @cherrypy.expose def regen(self): + """Regenerate the session, storing yellow color in it.""" cherrypy.session.regenerate() # Must modify data or the session will not be saved. cherrypy.session['color'] = 'yellow' diff --git a/cherrypy/test/webtest.py b/cherrypy/test/webtest.py index 9fb6ce62b..f579bc4cd 100644 --- a/cherrypy/test/webtest.py +++ b/cherrypy/test/webtest.py @@ -1,3 +1,4 @@ +"""A deprecated web app testing helper interface.""" # for compatibility, expose cheroot webtest here import warnings diff --git a/cherrypy/tutorial/__init__.py b/cherrypy/tutorial/__init__.py index 08c142c5f..d6b560567 100644 --- a/cherrypy/tutorial/__init__.py +++ b/cherrypy/tutorial/__init__.py @@ -1,3 +1,3 @@ - +"""A package with standalone tutorial modules.""" # This is used in test_config to test unrepr of "from A import B" thing2 = object() diff --git a/cherrypy/tutorial/tut01_helloworld.py b/cherrypy/tutorial/tut01_helloworld.py index e575a9e29..5cdde927c 100644 --- a/cherrypy/tutorial/tut01_helloworld.py +++ b/cherrypy/tutorial/tut01_helloworld.py @@ -1,5 +1,5 @@ """ -Tutorial - Hello World +Tutorial - Hello World. The most basic (working) CherryPy application possible. """ @@ -17,6 +17,7 @@ class HelloWorld: # publish methods that don't have the exposed attribute set to True. @cherrypy.expose def index(self): + """Produce HTTP response body of hello world app index URI.""" # CherryPy will call this method for the root URI ("/") and send # its return value to the client. Because this is tutorial # lesson number 01, we'll just send something really simple. diff --git a/cherrypy/tutorial/tut02_expose_methods.py b/cherrypy/tutorial/tut02_expose_methods.py index 8afbf7d8b..52823dfb5 100644 --- a/cherrypy/tutorial/tut02_expose_methods.py +++ b/cherrypy/tutorial/tut02_expose_methods.py @@ -1,5 +1,5 @@ """ -Tutorial - Multiple methods +Tutorial - Multiple methods. This tutorial shows you how to link to other methods of your request handler. @@ -11,15 +11,17 @@ class HelloWorld: + """Hello world app.""" @cherrypy.expose def index(self): + """Produce HTTP response body of hello world app index URI.""" # Let's link to another method here. return 'We have an important message for you!' @cherrypy.expose def show_msg(self): - # Here's the important message! + """Render a "Hello world!" message on ``/show_msg`` URI.""" return 'Hello world!' diff --git a/cherrypy/tutorial/tut03_get_and_post.py b/cherrypy/tutorial/tut03_get_and_post.py index 0b3d46131..3a50a5847 100644 --- a/cherrypy/tutorial/tut03_get_and_post.py +++ b/cherrypy/tutorial/tut03_get_and_post.py @@ -1,5 +1,5 @@ """ -Tutorial - Passing variables +Tutorial - Passing variables. This tutorial shows you how to pass GET/POST variables to methods. """ @@ -10,9 +10,11 @@ class WelcomePage: + """Welcome page app.""" @cherrypy.expose def index(self): + """Produce HTTP response body of welcome app index URI.""" # Ask for the user's name. return ''' @@ -23,6 +25,7 @@ def index(self): @cherrypy.expose def greetUser(self, name=None): + """Render a greeting or form on ``/greetUser`` URI.""" # CherryPy passes all GET and POST variables as method parameters. # It doesn't make a difference where the variables come from, how # large their contents are, and so on. @@ -30,7 +33,6 @@ def greetUser(self, name=None): # You can define default parameter values as usual. In this # example, the "name" parameter defaults to None so we can check # if a name was actually specified. - if name: # Greet the user! return "Hey %s, what's up?" % name diff --git a/cherrypy/tutorial/tut04_complex_site.py b/cherrypy/tutorial/tut04_complex_site.py index fe04054f1..ddba6d9ad 100644 --- a/cherrypy/tutorial/tut04_complex_site.py +++ b/cherrypy/tutorial/tut04_complex_site.py @@ -1,5 +1,5 @@ """ -Tutorial - Multiple objects +Tutorial - Multiple objects. This tutorial shows you how to create a site structure through multiple possibly nested request handler objects. @@ -11,9 +11,11 @@ class HomePage: + """Home page app.""" @cherrypy.expose def index(self): + """Produce HTTP response body of home page app index URI.""" return '''

Hi, this is the home page! Check out the other fun stuff on this site:

@@ -25,9 +27,11 @@ def index(self): class JokePage: + """Joke app.""" @cherrypy.expose def index(self): + """Produce HTTP response body of joke page app index URI.""" return '''

"In Python, how do you create a string of random characters?" -- "Read a Perl file!"

@@ -35,8 +39,10 @@ def index(self): class LinksPage: + """Links page app.""" def __init__(self): + """Mount extra links page into the links page app.""" # Request handler objects can create their own nested request # handler objects. Simply create them inside their __init__ # methods! @@ -44,6 +50,7 @@ def __init__(self): @cherrypy.expose def index(self): + """Produce HTTP response body of links page app index URI.""" # Note the way we link to the extra links page (and back). # As you can see, this object doesn't really care about its # absolute position in the site tree, since we use relative @@ -68,9 +75,11 @@ def index(self): class ExtraLinksPage: + """Extra links app.""" @cherrypy.expose def index(self): + """Render extra useful links.""" # Note the relative link back to the Links page! return '''

Here are some extra useful links:

diff --git a/cherrypy/tutorial/tut05_derived_objects.py b/cherrypy/tutorial/tut05_derived_objects.py index f626e03f0..7c9e8d288 100644 --- a/cherrypy/tutorial/tut05_derived_objects.py +++ b/cherrypy/tutorial/tut05_derived_objects.py @@ -1,5 +1,5 @@ """ -Tutorial - Object inheritance +Tutorial - Object inheritance. You are free to derive your request handler classes from any base class you wish. In most real-world applications, you will probably @@ -13,10 +13,13 @@ class you wish. In most real-world applications, you will probably class Page: + """Web page base class.""" + # Store the page title in a class attribute title = 'Untitled Page' def header(self): + """Render HTML layout header.""" return ''' @@ -27,6 +30,7 @@ def header(self): ''' % (self.title, self.title) def footer(self): + """Render HTML layout footer.""" return ''' @@ -40,15 +44,19 @@ def footer(self): class HomePage(Page): + """Home page app.""" + # Different title for this page title = 'Tutorial 5' def __init__(self): + """Mount another page into the home page app.""" # create a subpage self.another = AnotherPage() @cherrypy.expose def index(self): + """Produce HTTP response body of home page app index URI.""" # Note that we call the header and footer methods inherited # from the Page class! return self.header() + ''' @@ -60,10 +68,13 @@ def index(self): class AnotherPage(Page): + """Another page app.""" + title = 'Another Page' @cherrypy.expose def index(self): + """Produce HTTP response body of another page app index URI.""" return self.header() + '''

And this is the amazing second page! diff --git a/cherrypy/tutorial/tut06_default_method.py b/cherrypy/tutorial/tut06_default_method.py index 0ce4cabe8..8a07a90e8 100644 --- a/cherrypy/tutorial/tut06_default_method.py +++ b/cherrypy/tutorial/tut06_default_method.py @@ -1,5 +1,5 @@ """ -Tutorial - The default method +Tutorial - The default method. Request handler objects can implement a method called "default" that is called when no other suitable method/object could be found. @@ -22,9 +22,11 @@ class UsersPage: + """The users app.""" @cherrypy.expose def index(self): + """Produce HTTP response body of the users app index URI.""" # Since this is just a stupid little example, we'll simply # display a list of links to random, made-up users. In a real # application, this could be generated from a database result set. @@ -36,6 +38,7 @@ def index(self): @cherrypy.expose def default(self, user): + """Produce HTTP response body of the users app fallback URI.""" # Here we react depending on the virtualPath -- the part of the # path that could not be mapped to an object method. In a real # application, we would probably do some database lookups here diff --git a/cherrypy/tutorial/tut07_sessions.py b/cherrypy/tutorial/tut07_sessions.py index 204322b58..210439904 100644 --- a/cherrypy/tutorial/tut07_sessions.py +++ b/cherrypy/tutorial/tut07_sessions.py @@ -1,5 +1,5 @@ """ -Tutorial - Sessions +Tutorial - Sessions. Storing session data in CherryPy applications is very easy: cherrypy provides a dictionary called "session" that represents the session @@ -14,11 +14,13 @@ class HitCounter: + """Hit counter app.""" _cp_config = {'tools.sessions.on': True} @cherrypy.expose def index(self): + """Produce HTTP response body of hit counter app index URI.""" # Increase the silly hit counter count = cherrypy.session.get('count', 0) + 1 diff --git a/cherrypy/tutorial/tut08_generators_and_yield.py b/cherrypy/tutorial/tut08_generators_and_yield.py index 18f42f934..4e5500916 100644 --- a/cherrypy/tutorial/tut08_generators_and_yield.py +++ b/cherrypy/tutorial/tut08_generators_and_yield.py @@ -1,5 +1,5 @@ """ -Bonus Tutorial: Using generators to return result bodies +Bonus Tutorial: Using generators to return result bodies. Instead of returning a complete result string, you can use the yield statement to return one result part after another. This may be convenient @@ -13,15 +13,19 @@ class GeneratorDemo: + """HTTP response streaming app.""" def header(self): + """Render HTML layout header.""" return '

Generators rule!

' def footer(self): + """Render HTML layout footer.""" return '' @cherrypy.expose def index(self): + """Stream HTTP response body of generator app index URI.""" # Let's make up a list of users for presentation purposes users = ['Remi', 'Carlos', 'Hendrik', 'Lorenzo Lamas'] diff --git a/cherrypy/tutorial/tut09_files.py b/cherrypy/tutorial/tut09_files.py index 48585cbe1..23bca6a59 100644 --- a/cherrypy/tutorial/tut09_files.py +++ b/cherrypy/tutorial/tut09_files.py @@ -1,6 +1,5 @@ """ - -Tutorial: File upload and download +Tutorial: File upload and download. Uploads ------- @@ -51,9 +50,11 @@ class FileDemo(object): + """File upload app.""" @cherrypy.expose def index(self): + """Produce HTTP response body of file upload app index URI.""" return """

Upload a file

@@ -68,6 +69,7 @@ def index(self): @cherrypy.expose def upload(self, myFile): + """Receive a file upload at ``/upload`` URI.""" out = """ myFile length: %s
@@ -91,6 +93,7 @@ def upload(self, myFile): @cherrypy.expose def download(self): + """Send file to the HTTP client accessing ``/download`` URI.""" path = os.path.join(absDir, 'pdf_file.pdf') return static.serve_file(path, 'application/x-download', 'attachment', os.path.basename(path)) diff --git a/cherrypy/tutorial/tut10_http_errors.py b/cherrypy/tutorial/tut10_http_errors.py index 18f02fd0e..f982df627 100644 --- a/cherrypy/tutorial/tut10_http_errors.py +++ b/cherrypy/tutorial/tut10_http_errors.py @@ -1,11 +1,9 @@ """ - -Tutorial: HTTP errors +Tutorial: HTTP errors. HTTPError is used to return an error response to the client. CherryPy has lots of options regarding how such errors are logged, displayed, and formatted. - """ import os @@ -18,6 +16,7 @@ class HTTPErrorDemo(object): + """HTTP error representation app.""" # Set a custom response for 403 errors. _cp_config = {'error_page.403': @@ -25,6 +24,7 @@ class HTTPErrorDemo(object): @cherrypy.expose def index(self): + """Produce HTTP response body of error display app index URI.""" # display some links that will result in errors tracebacks = cherrypy.request.show_tracebacks if tracebacks: @@ -55,6 +55,7 @@ def index(self): @cherrypy.expose def toggleTracebacks(self): + """Switch tracebacks setting on ``/toggleTracebacks`` URI.""" # simple function to toggle tracebacks on and off tracebacks = cherrypy.request.show_tracebacks cherrypy.config.update({'request.show_tracebacks': not tracebacks}) @@ -64,11 +65,13 @@ def toggleTracebacks(self): @cherrypy.expose def error(self, code): + """Respond with a given HTTP error.""" # raise an error based on the get query raise cherrypy.HTTPError(status=code) @cherrypy.expose def messageArg(self): + """Respond with an HTTP 500 and a custom message.""" message = ("If you construct an HTTPError with a 'message' " 'argument, it wil be placed on the error page ' '(underneath the status line by default).') diff --git a/tox.ini b/tox.ini index 487448a5c..f1765a2c6 100644 --- a/tox.ini +++ b/tox.ini @@ -50,11 +50,6 @@ commands = 'cmd = "{envpython} -m pre_commit install"; scr_width = len(cmd) + 10; sep = "=" * scr_width; cmd_str = " $ " + cmd; '\ 'print("\n" + sep + "\nTo install pre-commit hooks into the Git repo, run:\n\n" + cmd_str + "\n\n" + sep + "\n")' -[testenv:pre-commit-pep257] -deps = pre-commit -commands = - pre-commit run --config .pre-commit-config-pep257.yaml --all-files {posargs} - [testenv:dist-check] # ensure that package artifacts are installed as expected usedevelop = False