comparison hgsubversion/maps.py @ 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
comparison
equal deleted inserted replaced
1439:ab15749252b0 1440:4d3a51e82147
18 self._ui = meta.ui 18 self._ui = meta.ui
19 19
20 self._commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*') 20 self._commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*')
21 self.syntaxes = ('re', 'glob') 21 self.syntaxes = ('re', 'glob')
22 22
23 self._filepath = meta.__getattribute__(self.defaultfilenameattr())
24 self.load(self._filepath)
25
26 # Append mappings specified from the commandline. A little
27 # magic here: our name in the config mapping is the same as
28 # the class name lowercased.
29 clmap = util.configpath(self._ui, self.mapname())
30 if clmap:
31 self.load(clmap)
32
33 @classmethod
34 def mapname(cls):
35 return cls.__name__.lower()
36
37 @classmethod
38 def defaultfilenameattr(cls):
23 # trickery: all subclasses have the same name as their file and config 39 # trickery: all subclasses have the same name as their file and config
24 # names, e.g. AuthorMap is meta.authormap_file for the filename and 40 # names, e.g. AuthorMap is meta.authormap_file for the filename and
25 # 'authormap' for the config option 41 # 'authormap' for the config option
26 self.mapname = self.__class__.__name__.lower() 42 return cls.mapname() + '_file'
27 self.mapfilename = self.mapname + '_file'
28 self._filepath = meta.__getattribute__(self.mapfilename)
29 self.load(self._filepath)
30
31 # append mappings specified from the commandline
32 clmap = util.configpath(self._ui, self.mapname)
33 if clmap:
34 self.load(clmap)
35 43
36 def _findkey(self, key): 44 def _findkey(self, key):
37 '''Takes a string and finds the first corresponding key that matches 45 '''Takes a string and finds the first corresponding key that matches
38 via regex''' 46 via regex'''
39 if not key: 47 if not key:
109 writing = False 117 writing = False
110 mapfile = self._filepath 118 mapfile = self._filepath
111 if path != mapfile: 119 if path != mapfile:
112 writing = open(mapfile, 'a') 120 writing = open(mapfile, 'a')
113 121
114 self._ui.debug('reading %s from %s\n' % (self.mapname , path)) 122 self._ui.debug('reading %s from %s\n' % (self.mapname() , path))
115 f = open(path, 'r') 123 f = open(path, 'r')
116 syntax = '' 124 syntax = ''
117 for number, line in enumerate(f): 125 for number, line in enumerate(f):
118 126
119 if writing: 127 if writing:
145 # split on the first '=' 153 # split on the first '='
146 try: 154 try:
147 src, dst = line.split('=', 1) 155 src, dst = line.split('=', 1)
148 except (IndexError, ValueError): 156 except (IndexError, ValueError):
149 msg = 'ignoring line %i in %s %s: %s\n' 157 msg = 'ignoring line %i in %s %s: %s\n'
150 self._ui.status(msg % (number, self.mapname, path, 158 self._ui.status(msg % (number, self.mapname(), path,
151 line.rstrip())) 159 line.rstrip()))
152 continue 160 continue
153 161
154 src = src.strip() 162 src = src.strip()
155 dst = dst.strip() 163 dst = dst.strip()
159 if pat == 'glob': 167 if pat == 'glob':
160 src = src.replace('\\*', '.*') 168 src = src.replace('\\*', '.*')
161 src = re.compile(src) 169 src = re.compile(src)
162 170
163 if src not in self: 171 if src not in self:
164 self._ui.debug('adding %s to %s\n' % (src, self.mapname)) 172 self._ui.debug('adding %s to %s\n' % (src, self.mapname()))
165 elif dst != self[src]: 173 elif dst != self[src]:
166 msg = 'overriding %s: "%s" to "%s" (%s)\n' 174 msg = 'overriding %s: "%s" to "%s" (%s)\n'
167 self._ui.status(msg % (self.mapname, self[src], dst, src)) 175 self._ui.status(msg % (self.mapname(), self[src], dst, src))
168 self[src] = dst 176 self[src] = dst
169 177
170 f.close() 178 f.close()
171 if writing: 179 if writing:
172 writing.close() 180 writing.close()