changeset 1440:4d3a51e82147

BaseMap: extract filename attribute magic into a classmethod I'm not really sure I like this magic anymore, but we can at least contain it to make the next patch cleaner.
author Augie Fackler <raf@durin42.com>
date Mon, 06 Jun 2016 00:42:07 -0400
parents ab15749252b0
children e79ff1a85938
files hgsubversion/maps.py
diffstat 1 files changed, 20 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/maps.py
+++ b/hgsubversion/maps.py
@@ -20,19 +20,27 @@ class BaseMap(dict):
         self._commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*')
         self.syntaxes = ('re', 'glob')
 
-        # trickery: all subclasses have the same name as their file and config
-        # names, e.g. AuthorMap is meta.authormap_file for the filename and
-        # 'authormap' for the config option
-        self.mapname = self.__class__.__name__.lower()
-        self.mapfilename = self.mapname + '_file'
-        self._filepath = meta.__getattribute__(self.mapfilename)
+        self._filepath = meta.__getattribute__(self.defaultfilenameattr())
         self.load(self._filepath)
 
-        # append mappings specified from the commandline
-        clmap = util.configpath(self._ui, self.mapname)
+        # Append mappings specified from the commandline. A little
+        # magic here: our name in the config mapping is the same as
+        # the class name lowercased.
+        clmap = util.configpath(self._ui, self.mapname())
         if clmap:
             self.load(clmap)
 
+    @classmethod
+    def mapname(cls):
+        return cls.__name__.lower()
+
+    @classmethod
+    def defaultfilenameattr(cls):
+        # trickery: all subclasses have the same name as their file and config
+        # names, e.g. AuthorMap is meta.authormap_file for the filename and
+        # 'authormap' for the config option
+        return cls.mapname() + '_file'
+
     def _findkey(self, key):
         '''Takes a string and finds the first corresponding key that matches
         via regex'''
@@ -111,7 +119,7 @@ class BaseMap(dict):
         if path != mapfile:
             writing = open(mapfile, 'a')
 
-        self._ui.debug('reading %s from %s\n' % (self.mapname , path))
+        self._ui.debug('reading %s from %s\n' % (self.mapname() , path))
         f = open(path, 'r')
         syntax = ''
         for number, line in enumerate(f):
@@ -147,7 +155,7 @@ class BaseMap(dict):
                 src, dst = line.split('=', 1)
             except (IndexError, ValueError):
                 msg = 'ignoring line %i in %s %s: %s\n'
-                self._ui.status(msg % (number, self.mapname, path,
+                self._ui.status(msg % (number, self.mapname(), path,
                                            line.rstrip()))
                 continue
 
@@ -161,10 +169,10 @@ class BaseMap(dict):
             src = re.compile(src)
 
             if src not in self:
-                self._ui.debug('adding %s to %s\n' % (src, self.mapname))
+                self._ui.debug('adding %s to %s\n' % (src, self.mapname()))
             elif dst != self[src]:
                 msg = 'overriding %s: "%s" to "%s" (%s)\n'
-                self._ui.status(msg % (self.mapname, self[src], dst, src))
+                self._ui.status(msg % (self.mapname(), self[src], dst, src))
             self[src] = dst
 
         f.close()