[Patchew-devel] [PATCH 1/2] tags: use regex to match tags

Paolo Bonzini posted 2 patches 6 years, 9 months ago
There is a newer version of this series
[Patchew-devel] [PATCH 1/2] tags: use regex to match tags
Posted by Paolo Bonzini 6 years, 9 months ago
This is simpler, since we can use case insensitive matches, and
will also be easier to reuse in the next patch.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 mods/tags.py | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/mods/tags.py b/mods/tags.py
index 5ec5bc2..aa29c60 100644
--- a/mods/tags.py
+++ b/mods/tags.py
@@ -13,6 +13,8 @@ from mbox import parse_address
 from event import register_handler, emit_event, declare_event
 from api.models import Message
 from api.rest import PluginMethodField
+from patchew.tags import lines_iter
+import re
 
 REV_BY_PREFIX = "Reviewed-by:"
 BASED_ON_PREFIX = "Based-on:"
@@ -55,6 +57,11 @@ series cover letter, patch mail body and their replies.
         tagsconfig = self.get_config("default", "tags", default="")
         return set([x.strip() for x in tagsconfig.split(",") if x.strip()] + BUILT_IN_TAGS)
 
+    def get_tag_regex(self):
+        tags = self.get_tag_prefixes()
+        tags_re = '|'.join(map(re.escape, tags))
+        return re.compile('^(?i:%s):' % tags_re)
+
     def update_tags(self, s):
         old = s.get_property("tags", [])
         new = self.look_for_tags(s)
@@ -105,10 +112,10 @@ series cover letter, patch mail body and their replies.
 
     def parse_message_tags(self, m):
         r = []
+        regex = self.get_tag_regex()
         for l in m.get_body().splitlines():
-            for p in self.get_tag_prefixes():
-                if l.lower().startswith(p.lower()):
-                    r.append(l)
+            if regex.match(l):
+                r.append(l)
         return r
 
     def look_for_tags(self, m):
-- 
2.17.1


_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel
Re: [Patchew-devel] [PATCH 1/2] tags: use regex to match tags
Posted by Fam Zheng 6 years, 9 months ago
On Mon, 07/30 12:44, Paolo Bonzini wrote:
> This is simpler, since we can use case insensitive matches, and
> will also be easier to reuse in the next patch.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  mods/tags.py | 13 ++++++++++---
>  1 file changed, 10 insertions(+), 3 deletions(-)
> 
> diff --git a/mods/tags.py b/mods/tags.py
> index 5ec5bc2..aa29c60 100644
> --- a/mods/tags.py
> +++ b/mods/tags.py
> @@ -13,6 +13,8 @@ from mbox import parse_address
>  from event import register_handler, emit_event, declare_event
>  from api.models import Message
>  from api.rest import PluginMethodField
> +from patchew.tags import lines_iter

lines_iter not used? Otherwise looks good.

Fam

> +import re
>  
>  REV_BY_PREFIX = "Reviewed-by:"
>  BASED_ON_PREFIX = "Based-on:"
> @@ -55,6 +57,11 @@ series cover letter, patch mail body and their replies.
>          tagsconfig = self.get_config("default", "tags", default="")
>          return set([x.strip() for x in tagsconfig.split(",") if x.strip()] + BUILT_IN_TAGS)
>  
> +    def get_tag_regex(self):
> +        tags = self.get_tag_prefixes()
> +        tags_re = '|'.join(map(re.escape, tags))
> +        return re.compile('^(?i:%s):' % tags_re)
> +
>      def update_tags(self, s):
>          old = s.get_property("tags", [])
>          new = self.look_for_tags(s)
> @@ -105,10 +112,10 @@ series cover letter, patch mail body and their replies.
>  
>      def parse_message_tags(self, m):
>          r = []
> +        regex = self.get_tag_regex()
>          for l in m.get_body().splitlines():
> -            for p in self.get_tag_prefixes():
> -                if l.lower().startswith(p.lower()):
> -                    r.append(l)
> +            if regex.match(l):
> +                r.append(l)
>          return r
>  
>      def look_for_tags(self, m):
> -- 
> 2.17.1
> 
> 
> _______________________________________________
> Patchew-devel mailing list
> Patchew-devel@redhat.com
> https://www.redhat.com/mailman/listinfo/patchew-devel

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel