From: Damien Lespiau Date: Sat, 24 May 2014 17:49:39 +0000 (+0100) Subject: parsemail: Parse series markers e.g. "1/12" X-Git-Tag: v2.0.0-rc1~363 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4b0a835bce3c7b653a51912eecc46a6d7ae4f215;p=thirdparty%2Fpatchwork.git parsemail: Parse series markers e.g. "1/12" This can be used to identify cover letters, patches part of series, length of series, etc. Signed-off-by: Damien Lespiau Signed-off-by: Stephen Finucane --- diff --git a/patchwork/bin/parsemail.py b/patchwork/bin/parsemail.py index f405f6bb..3ade49e5 100755 --- a/patchwork/bin/parsemail.py +++ b/patchwork/bin/parsemail.py @@ -193,6 +193,29 @@ def try_decode(payload, charset): return payload +def parse_series_marker(subject_prefixes): + """Extract series markers from subject. + + Extract the markers of multi-patches series, i.e. 'x/n', from the + provided subject series. + + Args: + subject_prefixes: List of subject prefixes to extract markers + from + + Returns: + (x, n) if markers found, else (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 06a2598b..2bc54cb7 100644 --- a/patchwork/tests/test_patchparser.py +++ b/patchwork/tests/test_patchparser.py @@ -25,7 +25,8 @@ from django.test import TestCase from patchwork.bin.parsemail import (find_content, find_author, find_project_by_header, parse_mail, - split_prefixes, clean_subject) + split_prefixes, clean_subject, + parse_series_marker) from patchwork.models import (Project, Person, Patch, Comment, State, get_default_initial_patch_state) from patchwork.tests.utils import (read_patch, read_mail, create_email, @@ -644,6 +645,12 @@ class PrefixTest(TestCase): self.assertEqual(split_prefixes('PATCH,RFC'), ['PATCH', 'RFC']) self.assertEqual(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):