diff options
author | Jonathan LaCour <jonathan@cleverdevil.org> | 2011-03-06 14:36:56 -0500 |
---|---|---|
committer | Jonathan LaCour <jonathan@cleverdevil.org> | 2011-03-06 14:36:56 -0500 |
commit | 52cdec95ba96c0e8928da5417344f21f4568eea0 (patch) | |
tree | e28a3984c69f954850aade22184b93c890156264 /pecan/configuration.py | |
parent | 615813486fd3d14d0a748a76b2223297b5033f08 (diff) | |
download | pecan-52cdec95ba96c0e8928da5417344f21f4568eea0.tar.gz |
Docstrings for the pecan.configuration module.
Diffstat (limited to 'pecan/configuration.py')
-rw-r--r-- | pecan/configuration.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/pecan/configuration.py b/pecan/configuration.py index 20510eb..ce14bc1 100644 --- a/pecan/configuration.py +++ b/pecan/configuration.py @@ -10,12 +10,30 @@ class ConfigDict(dict): pass class Config(object): + ''' + Base class for Pecan configurations. + ''' + def __init__(self, conf_dict={}, filename=''): + ''' + Create a Pecan configuration object from a dictionary or a + filename. + + :param conf_dict: A python dictionary to use for the configuration. + :param filename: A filename to use for the configuration. + ''' + self.__values__ = {} self.__file__ = filename self.update(conf_dict) def update(self, conf_dict): + ''' + Updates this configuration with a dictionary. + + :param conf_dict: A python dictionary to update this configuration with. + ''' + if isinstance(conf_dict, dict): iterator = conf_dict.iteritems() else: @@ -33,6 +51,12 @@ class Config(object): self[k] = conf_dict[k] def update_with_module(self, module): + ''' + Updates this configuration with a module. + + :param module: The module to update this configuration with. Either a string or the module itself. + ''' + self.update(conf_from_module(module)) def __getattr__(self, name): @@ -67,6 +91,12 @@ class Config(object): return 'Config(%s)' % str(self.__values__) def conf_from_module(module): + ''' + Creates a configuration dictionary from a module. + + :param module: The module, either as a string or the module itself. + ''' + if isinstance(module, str): module = import_module(module) @@ -76,6 +106,12 @@ def conf_from_module(module): def conf_from_file(filepath): + ''' + Creates a configuration dictionary from a file. + + :param filepath: The path to the file. + ''' + abspath = os.path.abspath(os.path.expanduser(filepath)) conf_dict = {} @@ -86,6 +122,12 @@ def conf_from_file(filepath): def conf_from_dict(conf_dict): + ''' + Creates a configuration dictionary from a dictionary. + + :param conf_dict: The configuration dictionary. + ''' + conf = Config(filename=conf_dict.get('__file__', '')) for k,v in conf_dict.iteritems(): @@ -99,6 +141,12 @@ def conf_from_dict(conf_dict): def import_module(conf): + ''' + Imports the a configuration as a module. + + :param conf: The string to the configuration. Automatically strips off ".py" file extensions. + ''' + if conf.endswith('.py'): conf = conf[:-3] @@ -119,12 +167,23 @@ def import_module(conf): def initconf(): + ''' + Initializes the default configuration and exposes it at ``pecan.configuration.conf``, + which is also exposed at ``pecan.conf``. + ''' + import default_config conf = conf_from_module(default_config) return conf def set_config(name): + ''' + Updates the global configuration from a path or filename. + + :param name: Path or filename, as a string. + ''' + if '/' in name: _runtime_conf.update(conf_from_file(name)) else: |