diff options
Diffstat (limited to 'cherrypy/_cpchecker.py')
| -rw-r--r-- | cherrypy/_cpchecker.py | 191 |
1 files changed, 92 insertions, 99 deletions
diff --git a/cherrypy/_cpchecker.py b/cherrypy/_cpchecker.py index 4ef82597..0066cd5a 100644 --- a/cherrypy/_cpchecker.py +++ b/cherrypy/_cpchecker.py @@ -1,12 +1,14 @@ +"""Checker for CherryPy sites and mounted apps.""" import os import warnings +import six +from six.moves import builtins + import cherrypy -from cherrypy._cpcompat import iteritems, copykeys, builtins class Checker(object): - """A checker for CherryPy sites and their mounted applications. When this object is called at engine startup, it executes each @@ -24,6 +26,7 @@ class Checker(object): """If True (the default), run all checks; if False, turn off all checks.""" def __init__(self): + """Initialize Checker instance.""" self._populate_known_types() def __call__(self): @@ -33,7 +36,7 @@ class Checker(object): warnings.formatwarning = self.formatwarning try: for name in dir(self): - if name.startswith("check_"): + if name.startswith('check_'): method = getattr(self, name) if method and hasattr(method, '__call__'): method() @@ -42,14 +45,13 @@ class Checker(object): def formatwarning(self, message, category, filename, lineno, line=None): """Function to format a warning.""" - return "CherryPy Checker:\n%s\n\n" % message + return 'CherryPy Checker:\n%s\n\n' % message # This value should be set inside _cpconfig. global_config_contained_paths = False def check_app_config_entries_dont_start_with_script_name(self): - """Check for Application config with sections that repeat script_name. - """ + """Check for App config with sections that repeat script_name.""" for sn, app in cherrypy.tree.apps.items(): if not isinstance(app, cherrypy.Application): continue @@ -57,36 +59,36 @@ class Checker(object): continue if sn == '': continue - sn_atoms = sn.strip("/").split("/") + sn_atoms = sn.strip('/').split('/') for key in app.config.keys(): - key_atoms = key.strip("/").split("/") + key_atoms = key.strip('/').split('/') if key_atoms[:len(sn_atoms)] == sn_atoms: warnings.warn( - "The application mounted at %r has config " - "entries that start with its script name: %r" % (sn, + 'The application mounted at %r has config ' + 'entries that start with its script name: %r' % (sn, key)) def check_site_config_entries_in_app_config(self): """Check for mounted Applications that have site-scoped config.""" - for sn, app in iteritems(cherrypy.tree.apps): + for sn, app in six.iteritems(cherrypy.tree.apps): if not isinstance(app, cherrypy.Application): continue msg = [] - for section, entries in iteritems(app.config): + for section, entries in six.iteritems(app.config): if section.startswith('/'): - for key, value in iteritems(entries): - for n in ("engine.", "server.", "tree.", "checker."): + for key, value in six.iteritems(entries): + for n in ('engine.', 'server.', 'tree.', 'checker.'): if key.startswith(n): - msg.append("[%s] %s = %s" % + msg.append('[%s] %s = %s' % (section, key, value)) if msg: msg.insert(0, - "The application mounted at %r contains the " - "following config entries, which are only allowed " - "in site-wide config. Move them to a [global] " - "section and pass them to cherrypy.config.update() " - "instead of tree.mount()." % sn) + 'The application mounted at %r contains the ' + 'following config entries, which are only allowed ' + 'in site-wide config. Move them to a [global] ' + 'section and pass them to cherrypy.config.update() ' + 'instead of tree.mount().' % sn) warnings.warn(os.linesep.join(msg)) def check_skipped_app_config(self): @@ -95,32 +97,30 @@ class Checker(object): if not isinstance(app, cherrypy.Application): continue if not app.config: - msg = "The Application mounted at %r has an empty config." % sn + msg = 'The Application mounted at %r has an empty config.' % sn if self.global_config_contained_paths: - msg += (" It looks like the config you passed to " - "cherrypy.config.update() contains application-" - "specific sections. You must explicitly pass " - "application config via " - "cherrypy.tree.mount(..., config=app_config)") + msg += (' It looks like the config you passed to ' + 'cherrypy.config.update() contains application-' + 'specific sections. You must explicitly pass ' + 'application config via ' + 'cherrypy.tree.mount(..., config=app_config)') warnings.warn(msg) return def check_app_config_brackets(self): - """Check for Application config with extraneous brackets in section - names. - """ + """Check for App config with extraneous brackets in section names.""" for sn, app in cherrypy.tree.apps.items(): if not isinstance(app, cherrypy.Application): continue if not app.config: continue for key in app.config.keys(): - if key.startswith("[") or key.endswith("]"): + if key.startswith('[') or key.endswith(']'): warnings.warn( - "The application mounted at %r has config " - "section names with extraneous brackets: %r. " - "Config *files* need brackets; config *dicts* " - "(e.g. passed to tree.mount) do not." % (sn, key)) + 'The application mounted at %r has config ' + 'section names with extraneous brackets: %r. ' + 'Config *files* need brackets; config *dicts* ' + '(e.g. passed to tree.mount) do not.' % (sn, key)) def check_static_paths(self): """Check Application config for incorrect static paths.""" @@ -132,47 +132,47 @@ class Checker(object): request.app = app for section in app.config: # get_resource will populate request.config - request.get_resource(section + "/dummy.html") + request.get_resource(section + '/dummy.html') conf = request.config.get - if conf("tools.staticdir.on", False): - msg = "" - root = conf("tools.staticdir.root") - dir = conf("tools.staticdir.dir") + if conf('tools.staticdir.on', False): + msg = '' + root = conf('tools.staticdir.root') + dir = conf('tools.staticdir.dir') if dir is None: - msg = "tools.staticdir.dir is not set." + msg = 'tools.staticdir.dir is not set.' else: - fulldir = "" + fulldir = '' if os.path.isabs(dir): fulldir = dir if root: - msg = ("dir is an absolute path, even " - "though a root is provided.") + msg = ('dir is an absolute path, even ' + 'though a root is provided.') testdir = os.path.join(root, dir[1:]) if os.path.exists(testdir): msg += ( - "\nIf you meant to serve the " - "filesystem folder at %r, remove the " - "leading slash from dir." % (testdir,)) + '\nIf you meant to serve the ' + 'filesystem folder at %r, remove the ' + 'leading slash from dir.' % (testdir,)) else: if not root: msg = ( - "dir is a relative path and " - "no root provided.") + 'dir is a relative path and ' + 'no root provided.') else: fulldir = os.path.join(root, dir) if not os.path.isabs(fulldir): - msg = ("%r is not an absolute path." % ( + msg = ('%r is not an absolute path.' % ( fulldir,)) if fulldir and not os.path.exists(fulldir): if msg: - msg += "\n" - msg += ("%r (root + dir) is not an existing " - "filesystem path." % fulldir) + msg += '\n' + msg += ('%r (root + dir) is not an existing ' + 'filesystem path.' % fulldir) if msg: - warnings.warn("%s\nsection: [%s]\nroot: %r\ndir: %r" + warnings.warn('%s\nsection: [%s]\nroot: %r\ndir: %r' % (msg, section, root, dir)) # -------------------------- Compatibility -------------------------- # @@ -196,21 +196,21 @@ class Checker(object): """Process config and warn on each obsolete or deprecated entry.""" for section, conf in config.items(): if isinstance(conf, dict): - for k, v in conf.items(): + for k in conf: if k in self.obsolete: - warnings.warn("%r is obsolete. Use %r instead.\n" - "section: [%s]" % + warnings.warn('%r is obsolete. Use %r instead.\n' + 'section: [%s]' % (k, self.obsolete[k], section)) elif k in self.deprecated: - warnings.warn("%r is deprecated. Use %r instead.\n" - "section: [%s]" % + warnings.warn('%r is deprecated. Use %r instead.\n' + 'section: [%s]' % (k, self.deprecated[k], section)) else: if section in self.obsolete: - warnings.warn("%r is obsolete. Use %r instead." + warnings.warn('%r is obsolete. Use %r instead.' % (section, self.obsolete[section])) elif section in self.deprecated: - warnings.warn("%r is deprecated. Use %r instead." + warnings.warn('%r is deprecated. Use %r instead.' % (section, self.deprecated[section])) def check_compatibility(self): @@ -225,40 +225,40 @@ class Checker(object): extra_config_namespaces = [] def _known_ns(self, app): - ns = ["wsgi"] - ns.extend(copykeys(app.toolboxes)) - ns.extend(copykeys(app.namespaces)) - ns.extend(copykeys(app.request_class.namespaces)) - ns.extend(copykeys(cherrypy.config.namespaces)) + ns = ['wsgi'] + ns.extend(app.toolboxes) + ns.extend(app.namespaces) + ns.extend(app.request_class.namespaces) + ns.extend(cherrypy.config.namespaces) ns += self.extra_config_namespaces for section, conf in app.config.items(): - is_path_section = section.startswith("/") + is_path_section = section.startswith('/') if is_path_section and isinstance(conf, dict): - for k, v in conf.items(): - atoms = k.split(".") + for k in conf: + atoms = k.split('.') if len(atoms) > 1: if atoms[0] not in ns: # Spit out a special warning if a known # namespace is preceded by "cherrypy." - if atoms[0] == "cherrypy" and atoms[1] in ns: + if atoms[0] == 'cherrypy' and atoms[1] in ns: msg = ( - "The config entry %r is invalid; " - "try %r instead.\nsection: [%s]" - % (k, ".".join(atoms[1:]), section)) + 'The config entry %r is invalid; ' + 'try %r instead.\nsection: [%s]' + % (k, '.'.join(atoms[1:]), section)) else: msg = ( - "The config entry %r is invalid, " - "because the %r config namespace " - "is unknown.\n" - "section: [%s]" % (k, atoms[0], section)) + 'The config entry %r is invalid, ' + 'because the %r config namespace ' + 'is unknown.\n' + 'section: [%s]' % (k, atoms[0], section)) warnings.warn(msg) - elif atoms[0] == "tools": + elif atoms[0] == 'tools': if atoms[1] not in dir(cherrypy.tools): msg = ( - "The config entry %r may be invalid, " - "because the %r tool was not found.\n" - "section: [%s]" % (k, atoms[1], section)) + 'The config entry %r may be invalid, ' + 'because the %r tool was not found.\n' + 'section: [%s]' % (k, atoms[1], section)) warnings.warn(msg) def check_config_namespaces(self): @@ -282,29 +282,22 @@ class Checker(object): continue vtype = type(getattr(obj, name, None)) if vtype in b: - self.known_config_types[namespace + "." + name] = vtype + self.known_config_types[namespace + '.' + name] = vtype - traverse(cherrypy.request, "request") - traverse(cherrypy.response, "response") - traverse(cherrypy.server, "server") - traverse(cherrypy.engine, "engine") - traverse(cherrypy.log, "log") + traverse(cherrypy.request, 'request') + traverse(cherrypy.response, 'response') + traverse(cherrypy.server, 'server') + traverse(cherrypy.engine, 'engine') + traverse(cherrypy.log, 'log') def _known_types(self, config): - msg = ("The config entry %r in section %r is of type %r, " - "which does not match the expected type %r.") + msg = ('The config entry %r in section %r is of type %r, ' + 'which does not match the expected type %r.') for section, conf in config.items(): - if isinstance(conf, dict): - for k, v in conf.items(): - if v is not None: - expected_type = self.known_config_types.get(k, None) - vtype = type(v) - if expected_type and vtype != expected_type: - warnings.warn(msg % (k, section, vtype.__name__, - expected_type.__name__)) - else: - k, v = section, conf + if not isinstance(conf, dict): + conf = {section: conf} + for k, v in conf.items(): if v is not None: expected_type = self.known_config_types.get(k, None) vtype = type(v) @@ -326,7 +319,7 @@ class Checker(object): for k, v in cherrypy.config.items(): if k == 'server.socket_host' and v == 'localhost': warnings.warn("The use of 'localhost' as a socket host can " - "cause problems on newer systems, since " + 'cause problems on newer systems, since ' "'localhost' can map to either an IPv4 or an " "IPv6 address. You should use '127.0.0.1' " "or '[::1]' instead.") |
