diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2018-08-17 12:30:22 -0400 |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2018-08-17 12:42:07 -0400 |
commit | 47cf4a969d7a439485014ca785054bcefebe8e87 (patch) | |
tree | c71d0d401ecb0370b0241ec41d824c35e7a216be | |
parent | 550a17ad9078dde8d4ecae1cfe222ac1cce09326 (diff) | |
download | cherrypy-git-47cf4a969d7a439485014ca785054bcefebe8e87.tar.gz |
Consolidate as_dict behavior.
-rw-r--r-- | cherrypy/_cpconfig.py | 2 | ||||
-rw-r--r-- | cherrypy/lib/reprconf.py | 32 |
2 files changed, 14 insertions, 20 deletions
diff --git a/cherrypy/_cpconfig.py b/cherrypy/_cpconfig.py index 072219f9..5afd6336 100644 --- a/cherrypy/_cpconfig.py +++ b/cherrypy/_cpconfig.py @@ -133,7 +133,7 @@ def merge(base, other): cherrypy.engine.autoreload.files.add(other) # Load other into base - for section, value_map in reprconf.as_dict(other).items(): + for section, value_map in reprconf.Parser.load(other).items(): if not isinstance(value_map, dict): raise ValueError( 'Application config must include section headers, but the ' diff --git a/cherrypy/lib/reprconf.py b/cherrypy/lib/reprconf.py index 208a7a4e..f7964d65 100644 --- a/cherrypy/lib/reprconf.py +++ b/cherrypy/lib/reprconf.py @@ -37,15 +37,6 @@ import sys from six.moves import builtins -def as_dict(config): - """Return a dict from 'config' whether it is a dict, file, or filename.""" - if isinstance(config, text_or_bytes): - config = Parser().dict_from_file(config) - elif hasattr(config, 'read'): - config = Parser().dict_from_file(config) - return config - - class NamespaceSet(dict): """A dict of config namespace names and handlers. @@ -146,16 +137,8 @@ class Config(dict): dict.update(self, self.defaults) def update(self, config): - """Update self from a dict, file or filename.""" - if isinstance(config, text_or_bytes): - # Filename - config = Parser().dict_from_file(config) - elif hasattr(config, 'read'): - # Open file object - config = Parser().dict_from_file(config) - else: - config = config.copy() - self._apply(config) + """Update self from a dict, file, or filename.""" + self._apply(Parser.load(config)) def _apply(self, config): """Update self from a dict.""" @@ -224,6 +207,17 @@ class Parser(ConfigParser): self.read(file) return self.as_dict() + @classmethod + def load(self, input): + """Resolve 'input' to dict from a dict, file, or filename.""" + is_file = ( + # Filename + isinstance(input, text_or_bytes) + # Open file object + or hasattr(input, 'read') + ) + return Parser().dict_from_file(input) if is_file else input.copy() + # public domain "unrepr" implementation, found on the web and then improved. |