changeset 1400:3e264851f879

maps: protect author map functions from regexes In an upcoming patch, we will add regexes so we protect the AuthorMap functions that assume a string being passed in.
author Sean Farley <sean.michael.farley@gmail.com>
date Mon, 24 Mar 2014 11:21:01 -0500
parents 3b96075bffa7
children 70cb6ba038fa
files hgsubversion/maps.py
diffstat 1 files changed, 16 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/maps.py
+++ b/hgsubversion/maps.py
@@ -173,21 +173,28 @@ class AuthorMap(BaseMap):
 
         super(AuthorMap, self).__init__(meta)
 
+    def _lowercase(self, key):
+        '''Determine whether or not to lowercase a str or regex using the
+        meta.caseignoreauthors.'''
+        k = key
+        if self.meta.caseignoreauthors:
+            if isinstance(key, str):
+                k = key.lower()
+            else:
+                k = re.compile(key.pattern.lower())
+        return k
+
     def __setitem__(self, key, value):
         '''Similar to dict.__setitem__, except we check caseignoreauthors to
         use lowercase string or not
         '''
-        if self.meta.caseignoreauthors:
-            key = key.lower()
-        super(AuthorMap, self).__setitem__(key, value)
+        super(AuthorMap, self).__setitem__(self._lowercase(key), value)
 
     def __contains__(self, key):
         '''Similar to dict.__contains__, except we check caseignoreauthors to
         use lowercase string or not
         '''
-        if self.meta.caseignoreauthors:
-            key = key.lower()
-        return super(AuthorMap, self).__contains__(key)
+        return super(AuthorMap, self).__contains__(self._lowercase(key))
 
     def __getitem__(self, author):
         ''' Similar to dict.__getitem__, except in case of an unknown author.
@@ -196,6 +203,9 @@ class AuthorMap(BaseMap):
         if author is None:
             author = '(no author)'
 
+        if not isinstance(author, str):
+            return super(AuthorMap, self).__getitem__(author)
+
         search_author = author
         if self.meta.caseignoreauthors:
             search_author = author.lower()