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 /lib/git/config.py | |
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
Diffstat (limited to 'lib/git/config.py')
-rw-r--r-- | lib/git/config.py | 33 |
1 files changed, 33 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 |