changeset 1398:75745298d99d

maps: add custom __setitem__ Similar to dict.__setitem__ except we compile the key into a regex, if need be.
author Sean Farley <sean.michael.farley@gmail.com>
date Mon, 24 Mar 2014 11:21:01 -0500
parents 304fdb9810a6
children 3b96075bffa7
files hgsubversion/maps.py
diffstat 1 files changed, 14 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/maps.py
+++ b/hgsubversion/maps.py
@@ -80,6 +80,20 @@ class BaseMap(dict):
 
         return val
 
+    def __setitem__(self, key, value):
+        '''Similar to dict.__setitem__, except we compile the string into a regex, if
+        need be.
+        '''
+        # try to find the regex already in the map
+        k = self._findkey(key)
+        # if we found one, then use it
+        if k:
+            key = k
+        # else make a new regex
+        if isinstance(key, str):
+            key = re.compile(re.escape(key))
+        super(BaseMap, self).__setitem__(key, value)
+
     def load(self, path):
         '''Load mappings from a file at the specified path.'''
         path = os.path.expandvars(path)