comparison hgsubversion/maps.py @ 1395:53184be1b1fd

maps: add _findkey method This private method will help us find a key that matches a regex. Priority is given to old-style strings.
author Sean Farley <sean.michael.farley@gmail.com>
date Mon, 24 Mar 2014 11:21:00 -0500
parents c4055968f030
children 77594c88d91f
comparison
equal deleted inserted replaced
1394:c4055968f030 1395:53184be1b1fd
28 28
29 # append mappings specified from the commandline 29 # append mappings specified from the commandline
30 clmap = util.configpath(self.meta.ui, self.mapname) 30 clmap = util.configpath(self.meta.ui, self.mapname)
31 if clmap: 31 if clmap:
32 self.load(clmap) 32 self.load(clmap)
33
34 def _findkey(self, key):
35 '''Takes a string and finds the first corresponding key that matches
36 via regex'''
37 if not key:
38 return None
39
40 # compile a new regex key if we're given a string; can't use
41 # hgutil.compilere since we need regex.sub
42 k = key
43 if isinstance(key, str):
44 k = re.compile(re.escape(key))
45
46 # preference goes to matching the exact pattern, i.e. 'foo' should
47 # first match 'foo' before trying regexes
48 for regex in self:
49 if regex.pattern == k.pattern:
50 return regex
51
52 # if key isn't a string, then we are done; nothing matches
53 if not isinstance(key, str):
54 return None
55
56 # now we test the regex; the above loop will be faster and is
57 # equivalent to not having regexes (i.e. just doing string compares)
58 for regex in self:
59 if regex.search(key):
60 return regex
61 return None
33 62
34 def load(self, path): 63 def load(self, path):
35 '''Load mappings from a file at the specified path.''' 64 '''Load mappings from a file at the specified path.'''
36 path = os.path.expandvars(path) 65 path = os.path.expandvars(path)
37 if not os.path.exists(path): 66 if not os.path.exists(path):