add ui.readsections
Given a file name and a set of sections, this function reads the file
and adds only the specified sections to the configuration data.
--- a/mercurial/ui.py Tue Oct 17 11:01:10 2006 -0700
+++ b/mercurial/ui.py Tue Oct 17 16:59:24 2006 -0300
@@ -15,8 +15,10 @@
updateconfig(orig, new)
return new
-def updateconfig(source, dest):
- for section in source.sections():
+def updateconfig(source, dest, sections=None):
+ if not sections:
+ sections = source.sections()
+ for section in sections:
if not dest.has_section(section):
dest.add_section(section)
for name, value in source.items(section, raw=True):
@@ -100,6 +102,23 @@
def addreadhook(self, hook):
self.readhooks.append(hook)
+ def readsections(self, filename, *sections):
+ "read filename and add only the specified sections to the config data"
+ if not sections:
+ return
+
+ cdata = util.configparser()
+ try:
+ cdata.read(filename)
+ except ConfigParser.ParsingError, inst:
+ raise util.Abort(_("failed to parse %s\n%s") % (f, inst))
+
+ for section in sections:
+ if not cdata.has_section(section):
+ cdata.add_section(section)
+
+ updateconfig(cdata, self.cdata, 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':