diff options
author | Bryan Bishop <kanzure@gmail.com> | 2013-09-02 00:50:57 -0500 |
---|---|---|
committer | Bryan Bishop <kanzure@gmail.com> | 2013-09-02 12:33:03 -0500 |
commit | 6f9ed6a829aa3e0be2c7f8e8d871bdeb593d5698 (patch) | |
tree | c33deb2cd5591137a2e4e510c5b3d4c06fac23c0 | |
parent | 672d527f9e69a59cb204463a2255bb32e814a676 (diff) |
basic config support
-rw-r--r-- | pokemontools/__init__.py | 1 | ||||
-rw-r--r-- | pokemontools/config.py | 46 | ||||
-rw-r--r-- | pokemontools/exceptions.py | 5 |
3 files changed, 52 insertions, 0 deletions
diff --git a/pokemontools/__init__.py b/pokemontools/__init__.py index 8fb8b19..09331af 100644 --- a/pokemontools/__init__.py +++ b/pokemontools/__init__.py @@ -1,2 +1,3 @@ +import config import crystal import preprocessor 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) + ) diff --git a/pokemontools/exceptions.py b/pokemontools/exceptions.py index 71d0da2..2ccc58a 100644 --- a/pokemontools/exceptions.py +++ b/pokemontools/exceptions.py @@ -11,3 +11,8 @@ class TextScriptException(Exception): """ TextScript encountered an inconsistency or problem. """ + +class ConfigException(Exception): + """ + Configuration error. Maybe a missing config variable. + """ |