changeset 1384:2d1d05e6e46c

maps: add a load method to base map Since all three map classes (authors, tags, branches) have the exact same logic for their load method, we add a generic one to base map so that each one can use it.
author Sean Farley <sean.michael.farley@gmail.com>
date Mon, 24 Mar 2014 11:20:58 -0500
parents 73c76f99ca08
children 9139d9295a36
files hgsubversion/maps.py
diffstat 1 files changed, 44 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/maps.py
+++ b/hgsubversion/maps.py
@@ -28,6 +28,50 @@ class BaseMap(dict):
         if clmap:
             self.load(clmap)
 
+    def load(self, path):
+        '''Load mappings from a file at the specified path.'''
+        path = os.path.expandvars(path)
+        if not os.path.exists(path):
+            return
+
+        writing = False
+        mapfile = self.meta.__getattribute__(self.mapfilename)
+        if path != mapfile:
+            writing = open(mapfile, 'a')
+
+        self.meta.ui.debug('reading %s from %s\n' % (self.mapname , path))
+        f = open(path, 'r')
+        for number, line in enumerate(f):
+
+            if writing:
+                writing.write(line)
+
+            line = line.split('#')[0]
+            if not line.strip():
+                continue
+
+            try:
+                src, dst = line.split('=', 1)
+            except (IndexError, ValueError):
+                msg = 'ignoring line %i in %s %s: %s\n'
+                self.meta.ui.status(msg % (number, self.mapname, path,
+                                           line.rstrip()))
+                continue
+
+            src = src.strip()
+            dst = dst.strip()
+
+            if src not in self:
+                self.meta.ui.debug('adding %s to %s\n' % (src, self.mapname))
+            elif dst != self[src]:
+                msg = 'overriding %s: "%s" to "%s" (%s)\n'
+                self.meta.ui.status(msg % (self.mapname, self[src], dst, src))
+            self[src] = dst
+
+        f.close()
+        if writing:
+            writing.close()
+
 class AuthorMap(dict):
     '''A mapping from Subversion-style authors to Mercurial-style
     authors, and back. The data is stored persistently on disk.