diff options
author | Bryan Bishop <kanzure@gmail.com> | 2013-09-04 20:38:11 -0700 |
---|---|---|
committer | Bryan Bishop <kanzure@gmail.com> | 2013-09-04 20:38:11 -0700 |
commit | ee05e2fe1d03e0e68c64cea09ec41ab70e12bc3a (patch) | |
tree | db3b846aa6c92acd4cbf6f4bc0f7e5fb566ade27 /pokemontools/config.py | |
parent | c2712bb90f09083f0bfa786750be2a9b34105fa9 (diff) | |
parent | 37441a35b13f3421ba0c0f234e2ee4bbc5db4b63 (diff) |
Merge pull request #10 from kanzure/config
Configuration for paths
Diffstat (limited to 'pokemontools/config.py')
-rw-r--r-- | pokemontools/config.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/pokemontools/config.py b/pokemontools/config.py new file mode 100644 index 0000000..656fab0 --- /dev/null +++ b/pokemontools/config.py @@ -0,0 +1,46 @@ +""" +Configuration +""" + +import os + +import exceptions + +class Config(object): + """ + The Config class handles all configuration for pokemontools. Other classes + and functions use a Config object to determine where expected files can be + located. + """ + + def __init__(self, **kwargs): + """ + Store all parameters. + """ + self._config = {} + + for (key, value) in kwargs.items(): + if key not in self.__dict__: + self._config[key] = value + else: + raise exceptions.ConfigException( + "Can't store \"{0}\" in configuration because the key conflicts with an existing property." + .format(key) + ) + + if "path" not in self._config: + self._config["path"] = os.getcwd() + + def __getattr__(self, key): + """ + Grab the value from the class properties, then check the configuration, + and raise an exception if nothing works. + """ + if key in self.__dict__: + return self.__dict__[key] + elif key in self._config: + return self._config[key] + else: + raise exceptions.ConfigException( + "no config found for \"{0}\"".format(key) + ) |