ui: fold readsections into readconfig
authorMatt Mackall <mpm@selenic.com>
Thu, 23 Apr 2009 15:40:10 -0500
changeset 8142 912bfef12ba6
parent 8141 e40b629bedd1
child 8143 507c49e297e1
ui: fold readsections into readconfig readconfig now reads only single files readconfig takes an optional list of sections readconfig trusts files we're looking for sections in
hgext/acl.py
hgext/bugzilla.py
hgext/notify.py
mercurial/ui.py
tests/test-acl.out
tests/test-notify
tests/test-trusted.py
tests/test-trusted.py.out
--- a/hgext/acl.py	Thu Apr 23 15:40:10 2009 -0500
+++ b/hgext/acl.py	Thu Apr 23 15:40:10 2009 -0500
@@ -74,7 +74,7 @@
     user = getpass.getuser()
     cfg = ui.config('acl', 'config')
     if cfg:
-        ui.readsections(cfg, 'acl.allow', 'acl.deny')
+        ui.readconfig(cfg, sections = ['acl.allow', 'acl.deny'])
     allow = buildmatch(ui, repo, user, 'acl.allow')
     deny = buildmatch(ui, repo, user, 'acl.deny')
 
--- a/hgext/bugzilla.py	Thu Apr 23 15:40:10 2009 -0500
+++ b/hgext/bugzilla.py	Thu Apr 23 15:40:10 2009 -0500
@@ -139,7 +139,7 @@
         timeout = int(self.ui.config('bugzilla', 'timeout', 5))
         usermap = self.ui.config('bugzilla', 'usermap')
         if usermap:
-            self.ui.readsections(usermap, 'usermap')
+            self.ui.readconfig(usermap, 'usermap')
         self.ui.note(_('connecting to %s:%s as %s, password %s\n') %
                      (host, db, user, '*' * len(passwd)))
         self.conn = MySQLdb.connect(host=host, user=user, passwd=passwd,
--- a/hgext/notify.py	Thu Apr 23 15:40:10 2009 -0500
+++ b/hgext/notify.py	Thu Apr 23 15:40:10 2009 -0500
@@ -99,7 +99,7 @@
         self.ui = ui
         cfg = self.ui.config('notify', 'config')
         if cfg:
-            self.ui.readsections(cfg, 'usersubs', 'reposubs')
+            self.ui.readconfig(cfg, sections=['usersubs', 'reposubs'])
         self.repo = repo
         self.stripcount = int(self.ui.config('notify', 'strip', 0))
         self.root = self.strip(self.repo.root)
--- a/mercurial/ui.py	Thu Apr 23 15:40:10 2009 -0500
+++ b/mercurial/ui.py	Thu Apr 23 15:40:10 2009 -0500
@@ -20,6 +20,8 @@
     for section in sections:
         if not dest.has_section(section):
             dest.add_section(section)
+        if not source.has_section(section):
+            continue
         for name, value in source.items(section, raw=True):
             dest.set(section, name, value)
 
@@ -39,7 +41,8 @@
             self.ucdata = util.configparser()
 
             # we always trust global config files
-            self.readconfig(util.rcpath(), assumetrusted=True)
+            for f in util.rcpath():
+                self.readconfig(f, assumetrusted=True)
         else:
             # parentui may point to an ui object which is already a child
             self.parentui = parentui.parentui or parentui
@@ -51,6 +54,7 @@
 
             # we want the overlay from the parent, not the root
             self.overlay = dupconfig(parentui.overlay)
+            self.fixconfig()
 
     def __getattr__(self, key):
         return getattr(self.parentui, key)
@@ -84,66 +88,36 @@
                         'user %s, group %s\n') % (f, user, group))
         return False
 
-    def readconfig(self, fn, root=None, assumetrusted=False):
-        cdata = util.configparser()
+    def readconfig(self, filename, root=None, assumetrusted=False,
+                   sections = None):
+        try:
+            fp = open(filename)
+        except IOError:
+            if not sections: # ignore unless we were looking for something
+                return
+            raise
 
-        if isinstance(fn, basestring):
-            fn = [fn]
-        for f in fn:
-            try:
-                fp = open(f)
-            except IOError:
-                continue
-
-            trusted = assumetrusted or self._is_trusted(fp, f)
+        cdata = util.configparser()
+        trusted = sections or assumetrusted or self._is_trusted(fp, filename)
 
-            try:
-                cdata.readfp(fp, f)
-            except ConfigParser.ParsingError, inst:
-                msg = _("Failed to parse %s\n%s") % (f, inst)
-                if trusted:
-                    raise util.Abort(msg)
-                self.warn(_("Ignored: %s\n") % msg)
+        try:
+            cdata.readfp(fp, filename)
+        except ConfigParser.ParsingError, inst:
+            msg = _("Failed to parse %s\n%s") % (filename, inst)
+            if trusted:
+                raise util.Abort(msg)
+            self.warn(_("Ignored: %s\n") % msg)
 
-            if trusted:
-                updateconfig(cdata, self.cdata)
-                updateconfig(self.overlay, self.cdata)
-            updateconfig(cdata, self.ucdata)
-            updateconfig(self.overlay, self.ucdata)
+        if trusted:
+            updateconfig(cdata, self.cdata, sections)
+            updateconfig(self.overlay, self.cdata, sections)
+        updateconfig(cdata, self.ucdata, sections)
+        updateconfig(self.overlay, self.ucdata, sections)
 
         if root is None:
             root = os.path.expanduser('~')
         self.fixconfig(root=root)
 
-    def readsections(self, filename, *sections):
-        """Read filename and add only the specified sections to the config data
-
-        The settings are added to the trusted config data.
-        """
-        if not sections:
-            return
-
-        cdata = util.configparser()
-        try:
-            try:
-                fp = open(filename)
-            except IOError, inst:
-                raise util.Abort(_("unable to open %s: %s") %
-                                 (filename, getattr(inst, "strerror", inst)))
-            try:
-                cdata.readfp(fp, filename)
-            finally:
-                fp.close()
-        except ConfigParser.ParsingError, inst:
-            raise util.Abort(_("failed to parse %s\n%s") % (filename, inst))
-
-        for section in sections:
-            if not cdata.has_section(section):
-                cdata.add_section(section)
-
-        updateconfig(cdata, self.cdata, sections)
-        updateconfig(cdata, self.ucdata, sections)
-
     def fixconfig(self, section=None, name=None, value=None, root=None):
         # translate paths relative to root (or home) into absolute paths
         if section is None or section == 'paths':
--- a/tests/test-acl.out	Thu Apr 23 15:40:10 2009 -0500
+++ b/tests/test-acl.out	Thu Apr 23 15:40:10 2009 -0500
@@ -473,10 +473,10 @@
 adding quux/file.py revisions
 added 3 changesets with 3 changes to 3 files
 calling hook pretxnchangegroup.acl: hgext.acl.hook
-error: pretxnchangegroup.acl hook failed: unable to open ../acl.config: No such file or directory
+error: pretxnchangegroup.acl hook raised an exception: [Errno 2] No such file or directory: '../acl.config'
 transaction abort!
 rollback completed
-abort: unable to open ../acl.config: No such file or directory
+abort: No such file or directory: ../acl.config
 no rollback information available
 0:6675d58eff77
 
--- a/tests/test-notify	Thu Apr 23 15:40:10 2009 -0500
+++ b/tests/test-notify	Thu Apr 23 15:40:10 2009 -0500
@@ -60,7 +60,7 @@
 
 echo % fail for config file is missing
 hg --cwd b rollback
-hg --cwd b pull ../a 2>&1 | grep 'unable to open.*\.notify\.conf' > /dev/null && echo pull failed
+hg --cwd b pull ../a 2>&1 | grep 'error.*\.notify\.conf' > /dev/null && echo pull failed
 
 touch "$HGTMP/.notify.conf"
 
--- a/tests/test-trusted.py	Thu Apr 23 15:40:10 2009 -0500
+++ b/tests/test-trusted.py	Thu Apr 23 15:40:10 2009 -0500
@@ -133,13 +133,13 @@
 print "# prints debug warnings"
 u = testui(user='abc', group='def', cuser='foo', debug=True)
 
-print "# ui.readsections"
+print "# ui.readconfig sections"
 filename = 'foobar'
 f = open(filename, 'w')
 f.write('[foobar]\n')
 f.write('baz = quux\n')
 f.close()
-u.readsections(filename, 'foobar')
+u.readconfig(filename, sections = ['foobar'])
 print u.config('foobar', 'baz')
 
 print
--- a/tests/test-trusted.py.out	Thu Apr 23 15:40:10 2009 -0500
+++ b/tests/test-trusted.py.out	Thu Apr 23 15:40:10 2009 -0500
@@ -165,7 +165,7 @@
 .Ignoring untrusted configuration option paths.local = /another/path
  . local = /another/path
 
-# ui.readsections
+# ui.readconfig sections
 quux
 
 # read trusted, untrusted, new ui, trusted