diff options
author | Alfredo Deza <alfredodeza@gmail.com> | 2013-01-22 22:39:15 -0500 |
---|---|---|
committer | Alfredo Deza <alfredodeza@gmail.com> | 2013-01-22 22:39:15 -0500 |
commit | 82653e36c5e2a2c49e703527b8cdb8fbe3de137a (patch) | |
tree | b5092409b7f011fe3691d76aba23175312981ee2 /pecan/configuration.py | |
parent | b91e4fad59387051b8a71900412302adeb8acbb5 (diff) | |
download | pecan-82653e36c5e2a2c49e703527b8cdb8fbe3de137a.tar.gz |
get env support with a helper function, deferr to it
Diffstat (limited to 'pecan/configuration.py')
-rw-r--r-- | pecan/configuration.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/pecan/configuration.py b/pecan/configuration.py index 94dd579..897bf52 100644 --- a/pecan/configuration.py +++ b/pecan/configuration.py @@ -147,6 +147,8 @@ def conf_from_file(filepath): abspath = os.path.abspath(os.path.expanduser(filepath)) conf_dict = {} + if not os.path.isfile(abspath): + raise RuntimeError('`%s` is not a file.' % abspath) execfile(abspath, globals(), conf_dict) conf_dict['__file__'] = abspath @@ -154,6 +156,25 @@ def conf_from_file(filepath): return conf_from_dict(conf_dict) +def conf_from_env(): + ''' + If the ``PECAN_CONFIG`` environment variable exists and it points to + a valid path it will return that, otherwise it will raise + a ``RuntimeError``. + ''' + config_path = os.environ.get('PECAN_CONFIG') + error = None + if not config_path: + error = "PECAN_CONFIG is not set and " \ + "no config file was passed as an argument." + elif not os.path.isfile(config_path): + error = "PECAN_CONFIG was set to an invalid path: %s" % config_path + + if error: + raise RuntimeError(error) + return config_path + + def conf_from_dict(conf_dict): ''' Creates a configuration dictionary from a dictionary. @@ -191,6 +212,8 @@ def set_config(config, overwrite=False): if overwrite is True: _runtime_conf.empty() + config = config or conf_from_env() + if isinstance(config, basestring): config = conf_from_file(config) _runtime_conf.update(config) |