diff mbox

[03/15] parsemail: Add a function to parse series markers eg. "1/12"

Message ID 1444387202-25735-4-git-send-email-damien.lespiau@intel.com
State Superseded
Headers show

Commit Message

Damien Lespiau Oct. 9, 2015, 10:39 a.m. UTC
This can be used to identify cover letters, patches part of series,
length of series, ...

Signed-off-by: Damien Lespiau <damien.lespiau@intel.com>
---
 patchwork/bin/parsemail.py          | 12 ++++++++++++
 patchwork/tests/test_patchparser.py |  9 ++++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)

Comments

Stephen Finucane Oct. 9, 2015, 11:21 p.m. UTC | #1
> This can be used to identify cover letters, patches part of series,

> length of series, ...

> 

> Signed-off-by: Damien Lespiau <damien.lespiau@intel.com>


Two low priority comments below. If you can fix them for the PR or a v3 then please do. Other than that:

Acked-by: Stephen Finucane <stephen.finucane@intel.com>


> +def parse_series_marker(subject_prefixes):

> +    """If this patch is part a of multi-patches series, ie has x/n in its

> +       subject, return (x, n). Otherwise, return (None, None)."""


Can you format docstrings with a summary and body? It makes editing in an IDE and the use of tools like PyDoc much easier (they extract the summary and use it on hover). Example:

    """Parse the series markers, if present.

    If this patch is part a of multi-patches series, ie has x/n in its
    subject, return (x, n). Otherwise, return (None, None).
    """

>  from patchwork.bin.parsemail import find_content, find_author,

> find_project, \

> -                                    parse_mail, split_prefixes,

> clean_subject

> +                                    parse_mail, split_prefixes,

> clean_subject, \

> +                                    parse_series_marker


Could you replace the escape characters with brackets?

	from patchwork.bin.parsemail import (
	    find_content, find_author, find_project, parse_mail, split_prefixes,
	    clean_subject, parse_series_marker)

Or similar.
diff mbox

Patch

diff --git a/patchwork/bin/parsemail.py b/patchwork/bin/parsemail.py
index e61f1f4..a98066f 100755
--- a/patchwork/bin/parsemail.py
+++ b/patchwork/bin/parsemail.py
@@ -160,6 +160,18 @@  class MailContent:
         self.patch = None
         self.comment = None
 
+def parse_series_marker(subject_prefixes):
+    """If this patch is part a of multi-patches series, ie has x/n in its
+       subject, return (x, n). Otherwise, return (None, None)."""
+
+    regex = re.compile('^([0-9]+)/([0-9]+)$')
+    for prefix in subject_prefixes:
+        m = regex.match(prefix)
+        if not m:
+            continue
+        return (int(m.group(1)), int(m.group(2)))
+    return (None, None)
+
 def find_content(project, mail):
     patchbuf = None
     commentbuf = ''
diff --git a/patchwork/tests/test_patchparser.py b/patchwork/tests/test_patchparser.py
index a70def6..fe1fd2a 100644
--- a/patchwork/tests/test_patchparser.py
+++ b/patchwork/tests/test_patchparser.py
@@ -34,7 +34,8 @@  class PatchTest(TestCase):
     project = defaults.project
 
 from patchwork.bin.parsemail import find_content, find_author, find_project, \
-                                    parse_mail, split_prefixes, clean_subject
+                                    parse_mail, split_prefixes, clean_subject, \
+                                    parse_series_marker
 
 class InlinePatchTest(PatchTest):
     patch_filename = '0001-add-line.patch'
@@ -619,6 +620,12 @@  class PrefixTest(TestCase):
         self.assertEquals(split_prefixes('PATCH,RFC'), ['PATCH', 'RFC'])
         self.assertEquals(split_prefixes('PATCH 1/2'), ['PATCH', '1/2'])
 
+    def testSeriesMarkers(self):
+        self.assertEqual(parse_series_marker([]), (None, None))
+        self.assertEqual(parse_series_marker(['bar']), (None, None))
+        self.assertEqual(parse_series_marker(['bar', '1/2']), (1, 2))
+        self.assertEqual(parse_series_marker(['bar', '0/12']), (0, 12))
+
 class SubjectTest(TestCase):
 
     def testCleanSubject(self):