Skip to content

Commit

Permalink
Updates to satsify pep257 violations
Browse files Browse the repository at this point in the history
  • Loading branch information
radez committed Apr 19, 2024
1 parent 1073610 commit 257f105
Show file tree
Hide file tree
Showing 50 changed files with 593 additions and 157 deletions.
9 changes: 3 additions & 6 deletions cherrypy/_cpcompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@


def ntob(n, encoding='ISO-8859-1'):
"""Return the given native string as a byte string in the given
encoding.
"""
"""Return given native string as a byte str in the given encoding."""
assert_native(n)
# In Python 3, the native string type is unicode
return n.encode(encoding)


def ntou(n, encoding='ISO-8859-1'):
"""Return the given native string as a unicode string with the given
encoding.
"""
"""Return given native string as a unicode str in the given encoding."""
assert_native(n)
# In Python 3, the native string type is unicode
return n
Expand All @@ -48,6 +44,7 @@ def tonative(n, encoding='ISO-8859-1'):


def assert_native(n):
"""Assert if native string."""
if not isinstance(n, str):
raise TypeError('n must be a native str (got %s)' % type(n).__name__)

Expand Down
21 changes: 17 additions & 4 deletions cherrypy/_cpdispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class PageHandler(object):
"""Callable which sets response.body."""

def __init__(self, callable, *args, **kwargs):
"""Initialize PageHandler."""
self.callable = callable
self.args = args
self.kwargs = kwargs
Expand All @@ -36,6 +37,7 @@ def args(self):

@args.setter
def args(self, args):
"""Setter for ordered args."""
cherrypy.serving.request.args = args
return cherrypy.serving.request.args

Expand All @@ -46,10 +48,12 @@ def kwargs(self):

@kwargs.setter
def kwargs(self, kwargs):
"""Setter for named kwargs."""
cherrypy.serving.request.kwargs = kwargs
return cherrypy.serving.request.kwargs

def __call__(self):
"""Call handler for PageHandler."""
try:
return self.callable(*self.args, **self.kwargs)
except TypeError:
Expand Down Expand Up @@ -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
"""Test callable spec."""
return None
else:
def getargspec(callable):
"""Get arg spec."""
return inspect.getfullargspec(callable)[:4]


class LateParamPageHandler(PageHandler):
"""LateParamPageHandler PageHandler class.
"""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
Expand All @@ -221,14 +228,15 @@ class LateParamPageHandler(PageHandler):

@property
def kwargs(self):
"""Page handler kwargs (with cherrypy.request.params copied in)."""
"""Page handler kwargs with cherrypy.request.params copied in."""
kwargs = cherrypy.serving.request.params.copy()
if self._kwargs:
kwargs.update(self._kwargs)
return kwargs

@kwargs.setter
def kwargs(self, kwargs):
"""Setter for kwargs."""
cherrypy.serving.request.kwargs = kwargs
self._kwargs = kwargs

Expand All @@ -238,6 +246,7 @@ def kwargs(self, kwargs):
string.punctuation, '_' * len(string.punctuation))

def validate_translator(t):
"""Validate translator."""
if not isinstance(t, str) or len(t) != 256:
raise ValueError(
'The translate argument must be a str of len 256.')
Expand All @@ -246,6 +255,7 @@ def validate_translator(t):
string.punctuation, '_' * len(string.punctuation))

def validate_translator(t):
"""Validate translator."""
if not isinstance(t, dict):
raise ValueError('The translate argument must be a dict.')

Expand Down Expand Up @@ -273,6 +283,7 @@ class Dispatcher(object):

def __init__(self, dispatch_method_name=None,
translate=punctuation_to_underscores):
"""Initialize Dispatcher."""
validate_translator(translate)
self.translate = translate
if dispatch_method_name:
Expand Down Expand Up @@ -389,8 +400,7 @@ 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 cherrypy.request.config."""
base = cherrypy.config.copy()
# Note that we merge the config from each node
# even if that node was None.
Expand Down Expand Up @@ -505,10 +515,12 @@ def __init__(self, full_result=False, **mapper_options):
self.mapper.controller_scan = self.controllers.keys

def connect(self, name, route, controller, **kwargs):
"""Connect."""
self.controllers[name] = controller
self.mapper.connect(name, route, controller=name, **kwargs)

def redirect(self, url):
"""Redirect."""
raise cherrypy.HTTPRedirect(url)

def __call__(self, path_info):
Expand Down Expand Up @@ -602,6 +614,7 @@ def merge(nodeconf):


def XMLRPCDispatcher(next_dispatcher=Dispatcher()):
"""Xml RPC Dispatcher class."""
from cherrypy.lib import xmlrpcutil

def xmlrpc_dispatch(path_info):
Expand Down
19 changes: 13 additions & 6 deletions cherrypy/_cperror.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class Root:

class CherryPyException(Exception):
"""A base class for CherryPy exceptions."""

pass


Expand All @@ -150,6 +151,7 @@ class InternalRedirect(CherryPyException):
"""

def __init__(self, path, query_string=''):
"""Initialize InternalRedirect."""
self.request = cherrypy.serving.request

self.query_string = query_string
Expand Down Expand Up @@ -202,6 +204,7 @@ class HTTPRedirect(CherryPyException):
"""The encoding when passed urls are not native strings."""

def __init__(self, urls, status=None, encoding=None):
"""Initialize HTTPRedirect."""
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")
Expand All @@ -227,7 +230,7 @@ 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
Expand All @@ -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
Expand Down Expand Up @@ -366,6 +370,7 @@ class HTTPError(CherryPyException):
"""The HTTP Reason-Phrase string."""

def __init__(self, status=500, message=None):
"""Initialize HTTPError."""
self.status = status
try:
self.code, self.reason, defaultmsg = _httputil.valid_status(status)
Expand All @@ -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
Expand All @@ -408,6 +414,7 @@ def set_response(self):
_be_ie_unfriendly(self.code)

def get_error_page(self, *args, **kwargs):
"""Get error page."""
return get_error_page(*args, **kwargs)

def __call__(self):
Expand All @@ -432,6 +439,7 @@ class NotFound(HTTPError):
"""

def __init__(self, path=None):
"""Initialize NotFound HTTPError."""
if path is None:
request = cherrypy.serving.request
path = request.script_name + request.path_info
Expand Down Expand Up @@ -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!
Expand Down
14 changes: 11 additions & 3 deletions cherrypy/_cplogging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
Cherrpy Logging module.
Simple config
=============
Expand Down Expand Up @@ -126,12 +128,15 @@ class NullHandler(logging.Handler):
"""A no-op logging handler to silence the logging.lastResort handler."""

def handle(self, record):
"""Handle stub."""
pass

def emit(self, record):
"""Emit stub."""
pass

def createLock(self):
"""Create Lock stub."""
self.lock = None


Expand Down Expand Up @@ -167,6 +172,7 @@ class LogManager(object):
"""

def __init__(self, appid=None, logger_root='cherrypy'):
"""Initialize LogManager."""
self.logger_root = logger_root
self.appid = appid
if appid is None:
Expand Down Expand Up @@ -217,11 +223,11 @@ def error(self, msg='', context='', severity=logging.INFO,
)

def __call__(self, *args, **kwargs):
"""An alias for ``error``."""
"""Call handler, An alias for ``error``."""
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
Expand Down Expand Up @@ -414,7 +420,7 @@ def wsgi(self, newvalue):


class WSGIErrorHandler(logging.Handler):
"A handler class which writes logging records to environ['wsgi.errors']."
"""Handler class that writes logging records to environ['wsgi.errors']."""

def flush(self):
"""Flushes the stream."""
Expand Down Expand Up @@ -450,6 +456,8 @@ def emit(self, record):


class LazyRfc3339UtcTime(object):
"""LazyRfc3339UtcTime class."""

def __str__(self):
"""Return datetime in RFC3339 UTC Format."""
iso_formatted_now = datetime.datetime.now(
Expand Down
10 changes: 10 additions & 0 deletions cherrypy/_cpmodpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def setup_server():


def setup(req):
"""Handle setup."""
from mod_python import apache

# Run any setup functions defined by a "PythonOption cherrypy.setup"
Expand Down Expand Up @@ -140,6 +141,7 @@ def __init__(self, req):


def handler(req):
"""Run handler."""
from mod_python import apache
try:
global _isSetUp
Expand Down Expand Up @@ -251,6 +253,7 @@ def handler(req):


def send_response(req, status, headers, body, stream=False):
"""Handle sending response."""
# Set response status
req.status = int(status[:3])

Expand All @@ -276,17 +279,20 @@ def send_response(req, status, headers, body, stream=False):
import subprocess

def popen(fullcmd):
"""Open process."""
p = subprocess.Popen(fullcmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
close_fds=True)
return p.stdout
except ImportError:
def popen(fullcmd):
"""Open process."""
pipein, pipeout = os.popen4(fullcmd)
return pipeout


def read_process(cmd, args=''):
"""Handle read process."""
fullcmd = '%s %s' % (cmd, args)
pipeout = popen(fullcmd)
try:
Expand All @@ -305,6 +311,7 @@ def read_process(cmd, args=''):


class ModPythonServer(object):
"""Mod Python Server class."""

template = """
# Apache2 server configuration file for running CherryPy with mod_python.
Expand All @@ -323,13 +330,15 @@ class ModPythonServer(object):

def __init__(self, loc='/', port=80, opts=None, apache_path='apache',
handler='cherrypy._cpmodpy::handler'):
"""Initialize ModPythonServer."""
self.loc = loc
self.port = port
self.opts = opts
self.apache_path = apache_path
self.handler = handler

def start(self):
"""Handle start."""
opts = ''.join([' PythonOption %s %s\n' % (k, v)
for k, v in self.opts])
conf_data = self.template % {'port': self.port,
Expand All @@ -347,5 +356,6 @@ def start(self):
return response

def stop(self):
"""Handle stop."""
os.popen('apache -k stop')
self.ready = False

0 comments on commit 257f105

Please sign in to comment.