diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2009-12-03 17:43:40 +0100 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2009-12-03 17:43:40 +0100 |
commit | 7d00fa4f6cad398250ce868cef34357b45abaad3 (patch) | |
tree | fb9c7ab507618f9f55100ac90dd5b617850213cd | |
parent | c05ef0e7543c2845fd431420509476537fefe2b0 (diff) | |
download | gitpython-7d00fa4f6cad398250ce868cef34357b45abaad3.tar.gz |
config: implemented get_value method to have a safe way to make general queries to the git configuration, returning a value in the proper type. In a way its not supposed to be used as you should know the type of your configuration option or get an exception otherwise
-rw-r--r-- | lib/git/config.py | 33 | ||||
-rw-r--r-- | test/git/test_config.py | 2 |
2 files changed, 35 insertions, 0 deletions
diff --git a/lib/git/config.py b/lib/git/config.py index bf4c6469..2f567559 100644 --- a/lib/git/config.py +++ b/lib/git/config.py @@ -346,3 +346,36 @@ class GitConfigParser(cp.RawConfigParser, LockFile): True if this instance may change the configuration file """ return self._read_only + + def get_value(self, section, option): + """ + Returns + a properly typed value, either int, float or string + Raises TypeError in case the value could not be understood + """ + valuestr = self.get(section, option) + types = ( long, float ) + for numtype in types: + try: + val = numtype( valuestr ) + + # truncated value ? + if val != float( valuestr ): + continue + + return val + except (ValueError,TypeError): + continue + # END for each numeric type + + # try boolean values as git uses them + vl = valuestr.lower() + if vl == 'false': + return False + if vl == 'true': + return True + + if not isinstance( valuestr, basestring ): + raise TypeError( "Invalid value type: only int, long, float and str are allowed", valuestr ) + + return valuestr diff --git a/test/git/test_config.py b/test/git/test_config.py index c2909b8f..4b6c4f11 100644 --- a/test/git/test_config.py +++ b/test/git/test_config.py @@ -72,6 +72,8 @@ class TestBase(TestCase): for option in r_config.options(section): num_options += 1 val = r_config.get(section, option) + val_typed = r_config.get_value(section, option) + assert isinstance(val_typed, (bool, long, float, basestring)) assert val assert "\n" not in option assert "\n" not in val |