[RFC 2/3] models: Add series-related properties to models

Stephen Finucane stephen.finucane at intel.com
Sat Apr 2 03:14:45 AEDT 2016


The properties will be used to build series from patches in
a follow-on patch.

TODO add missing tests

Signed-off-by: Doug Anderson <dianders at chromium.org>
---
 patchwork/models.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)

diff --git a/patchwork/models.py b/patchwork/models.py
index 9ad2bcb..4ec2fe8 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -389,6 +389,100 @@ class Patch(Submission):
         str = fname_re.sub('-', self.name)
         return str.strip('-') + '.patch'
 
+    @staticmethod
+    def _raw_patch_tags(name):
+        """Return a list of tags for the patch.
+
+        Tags are all in the [] section at the start of the patch name.
+
+        >>> Patch._raw_patch_tags('[1/2] Grok the frobber')
+        ['1/2']
+        >>> Patch._raw_patch_tags('[1/2,v5,RFC] Frob the grokker')
+        ['1/2', 'v5', 'RFC']
+        >>> Patch._raw_patch_tags('[RFC,V5,1/2] Krof the grubber')
+        ['RFC', 'V5', '1/2']
+        >>> Patch._raw_patch_tags('[1/2,v5] Brof the krogger [RESEND]')
+        ['1/2', 'v5']
+        >>> Patch._raw_patch_tags('Grok everything')
+        []
+
+        @name: The patch name
+        @return: A list of tags with no processing done on them.
+        """
+        mo = re.match(r"\[([^\]]*)\]", name)
+        if mo:
+            return mo.group(1).split(',')
+        return []
+
+    @staticmethod
+    def _parse_patch_name(name):
+        """Parse tags out of a patch name.
+
+        TODO copy tests
+
+        Args:
+            name: The patch name
+        Returns:
+            A dictionary with the following keys
+                version: integer version of the patch
+                part_num: integer part number of the patch
+                num_parts: integer number of parts in the patch
+        """
+        version = 1
+        part_num = 1
+        num_parts = 1
+        series_tags = []
+
+        # Work on one tag at a time
+        for tag in Patch._raw_patch_tags(name):
+            mo = re.match(r'(\d*)/(\d*)', tag)
+            if mo:
+                part_num = int(mo.group(1))
+                num_parts = int(mo.group(2))
+                continue
+
+            mo = re.match(r"[vV](\d*)", tag)
+            if mo:
+                version = int(mo.group(1))
+
+            series_tags.append(tag)
+
+        # Add num_parts to the series tags
+        series_tags.append("%d parts" % num_parts)
+
+        return {
+            'version': version,
+            'part_num': part_num,
+            'num_parts': num_parts
+        }
+
+    @property
+    def version(self):
+        """Get the version of this patch
+
+        Returns:
+            An integral version number.
+        """
+        return self._parse_patch_name(self.name)['version']
+
+    @property
+    def num_parts(self):
+        """Get the number of parts in the series this patch belongs to.
+
+        Returns:
+            The number of parts in the series.
+        """
+        return self._parse_patch_name(self.name)['num_parts']
+
+    @property
+    def part_num(self):
+        """Get the part number of this patch in its series.
+
+        Returns:
+            The part number of this patch in its series.
+        """
+        return self._parse_patch_name(self.name)['part_num']
+
     @property
     def combined_check_state(self):
         """Return the combined state for all checks.
-- 
2.0.0



More information about the Patchwork mailing list