# HG changeset patch
# User Sean Farley <sean.michael.farley@gmail.com>
# Date 1395678061 18000
# Node ID 75745298d99d689df40e1f1842e9caa20d1414f1
# Parent  304fdb9810a6c0f2d1872905246315fdab7febd8
maps: add custom __setitem__

Similar to dict.__setitem__ except we compile the key into a regex, if need be.

diff --git a/hgsubversion/maps.py b/hgsubversion/maps.py
--- 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)