changeset 4069:3fef134832d8

allow values that aren't strings in util.configparser
author Alexis S. L. Carvalho <alexis@cecm.usp.br>
date Fri, 09 Feb 2007 03:48:26 -0200
parents 5b1f663ef86d
children 961ccb615cf7
files mercurial/util.py tests/test-ui-config tests/test-ui-config.out
diffstat 3 files changed, 34 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/util.py	Tue Feb 06 16:12:22 2007 -0600
+++ b/mercurial/util.py	Fri Feb 09 03:48:26 2007 -0200
@@ -116,11 +116,23 @@
 class SignalInterrupt(Exception):
     """Exception raised on SIGTERM and SIGHUP."""
 
-# like SafeConfigParser but with case-sensitive keys
+# differences from SafeConfigParser:
+# - case-sensitive keys
+# - allows values that are not strings (this means that you may not
+#   be able to save the configuration to a file)
 class configparser(ConfigParser.SafeConfigParser):
     def optionxform(self, optionstr):
         return optionstr
 
+    def set(self, section, option, value):
+        return ConfigParser.ConfigParser.set(self, section, option, value)
+
+    def _interpolate(self, section, option, rawval, vars):
+        if not isinstance(rawval, basestring):
+            return rawval
+        return ConfigParser.SafeConfigParser._interpolate(self, section,
+                                                          option, rawval, vars)
+
 def cachefunc(func):
     '''cache the result of function calls'''
     # XXX doesn't handle keywords args
--- a/tests/test-ui-config	Tue Feb 06 16:12:22 2007 -0600
+++ b/tests/test-ui-config	Fri Feb 09 03:48:26 2007 -0200
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 
+import ConfigParser
 from mercurial import ui, util, commands
 
 testui = ui.ui()
@@ -70,3 +71,21 @@
 except util.Abort, inst:
     print inst
 print "---"
+
+cp = util.configparser()
+cp.add_section('foo')
+cp.set('foo', 'bar', 'baz')
+try:
+    # should fail - keys are case-sensitive
+    cp.get('foo', 'Bar')
+except ConfigParser.NoOptionError, inst:
+    print inst
+
+def function():
+    pass
+
+cp.add_section('hook')
+# values that aren't strings should work
+cp.set('hook', 'commit', function)
+f = cp.get('hook', 'commit')
+print "f %s= function" % (f == function and '=' or '!')
--- a/tests/test-ui-config.out	Tue Feb 06 16:12:22 2007 -0600
+++ b/tests/test-ui-config.out	Fri Feb 09 03:48:26 2007 -0200
@@ -43,3 +43,5 @@
 Error in configuration section [interpolation] parameter 'value5':
 '%' must be followed by '%' or '(', found: '%bad2'
 ---
+No option 'Bar' in section: 'foo'
+f == function