# HG changeset patch # User Sean Farley # Date 1395678061 18000 # Node ID 304fdb9810a6c0f2d1872905246315fdab7febd8 # Parent 77594c88d91f7b414aff01fc289f6ad86a820218 maps: add custom __getitem__ Similar to dict.__getitem__ except we use our own matching function, _findkey. diff --git a/hgsubversion/maps.py b/hgsubversion/maps.py --- a/hgsubversion/maps.py +++ b/hgsubversion/maps.py @@ -66,6 +66,20 @@ class BaseMap(dict): return self[key] return default + def __getitem__(self, key): + '''Similar to dict.get, except we use our own matcher, _findkey. If the key is + a string, then we can use our regex matching to map its value. + ''' + k = self._findkey(key) + val = super(BaseMap, self).__getitem__(k) + + # if key is a string then we can transform it using our regex, else we + # don't have enough information, so we just return the val + if isinstance(key, str): + val = k.sub(val, key) + + return val + def load(self, path): '''Load mappings from a file at the specified path.''' path = os.path.expandvars(path)