changeset 98:f5441510cbb0

Regroup all ConfigParser dependent code before refactoring
author Patrick Mezard <pmezard@gmail.com>
date Sun, 04 Oct 2009 16:21:00 +0200
parents 4b2c5c4ffeff
children 9e722e8d001d
files forest.py
diffstat 1 files changed, 19 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/forest.py	Thu Sep 24 13:50:07 2009 +0200
+++ b/forest.py	Sun Oct 04 16:21:00 2009 +0200
@@ -105,11 +105,21 @@
             """Mercurial <= 0.9.3 doesn't have this feature."""
             return url, (revs or None)
 
-
 # For backwards compatibility, find the HTTP protocol.
 if not hasattr(hgweb, 'protocol'):
     hgweb.protocol = hgweb.hgweb_mod.hgweb
 
+ConfigError = ConfigParser.Error
+
+class SnapshotError(ConfigParser.NoSectionError):
+    pass
+
+def readconfig(path):
+    cfg = ConfigParser.RawConfigParser()
+    if not cfg.read([path]):
+        return None
+    return cfg
+
 def cmd_options(ui, cmd, remove=None, table=commands.table):
     aliases, spec = findcmd(ui, cmd, table)
     res = list(spec[1])
@@ -413,10 +423,7 @@
     This data structure describes the Forest contained within the
     current repository.  It contains a list of Trees that describe
     each sub-repository.
-    """
-
-    class SnapshotError(ConfigParser.NoSectionError):
-        pass
+    """    
 
     class Tree(object):
         """Describe a local sub-repository within a forest."""
@@ -678,8 +685,8 @@
         """
         if not toppath:
             toppath = "."
-        cfg = ConfigParser.RawConfigParser()
-        if not cfg.read([snapfile]):
+        cfg = readconfig(snapfile)
+        if not cfg:
             raise util.Abort("%s: %s" % (snapfile, os.strerror(errno.ENOENT)))
         seen_root = False
         sections = {}
@@ -715,8 +722,8 @@
                                                     revs=[rev],
                                                     paths=paths)
         if not seen_root:
-            raise Forest.SnapshotError("Could not find 'root = .' in '%s'" %
-                                       snapfile)
+            raise SnapshotError("Could not find 'root = .' in '%s'" %
+                                snapfile)
         self.trees = sections.values()
         self.trees.sort(key=(lambda tree: tree.root))
 
@@ -1322,16 +1329,12 @@
 
     snapfile = None
     if revision:
-        cp = ConfigParser.RawConfigParser()
         try:
-            if cp.read([revision]):
+            if readconfig(revision):
                 # Compatibility with old 'hg fupdate SNAPFILE' syntax
                 snapfile = revision
-        except Exception, err:
-            if isinstance(err, ConfigParser.Error):
-                ui.warn(_("warning: %s\n") % err)
-            else:
-                raise err
+        except ConfigError, err:
+            ui.warn(_("warning: %s\n") % err)
     if snapfile is None:
         snapfile = opts['snapfile']
         opts['rev'] = revision