[RFC PATCH 02/19] templatetags: Trivial cleanup

Stephen Finucane stephen at that.guru
Thu Aug 12 07:36:48 AEST 2021


Make this a little nicer to read.

Signed-off-by: Stephen Finucane <stephen at that.guru>
---
 patchwork/templatetags/listurl.py | 22 ++++++++-----
 patchwork/templatetags/patch.py   | 24 ++++++++------
 patchwork/templatetags/person.py  |  9 +++--
 patchwork/templatetags/project.py | 10 ++++--
 patchwork/templatetags/syntax.py  | 55 ++++++++++++++++++-------------
 5 files changed, 74 insertions(+), 46 deletions(-)

diff --git patchwork/templatetags/listurl.py patchwork/templatetags/listurl.py
index 3d5388d2..ad920de2 100644
--- patchwork/templatetags/listurl.py
+++ patchwork/templatetags/listurl.py
@@ -5,8 +5,8 @@
 
 from django.conf import settings
 from django import template
-from django.urls import reverse
 from django.urls import NoReverseMatch
+from django.urls import reverse
 from django.utils.encoding import smart_str
 from django.utils.html import escape
 
@@ -20,7 +20,6 @@ list_params = [c.param for c in FILTERS] + ['order', 'page']
 
 
 class ListURLNode(template.defaulttags.URLNode):
-
     def __init__(self, kwargs):
         super(ListURLNode, self).__init__(None, [], {}, False)
         self.params = {}
@@ -38,8 +37,9 @@ class ListURLNode(template.defaulttags.URLNode):
         except NoReverseMatch:
             try:
                 project_name = settings.SETTINGS_MODULE.split('.')[0]
-                path = reverse(project_name + '.' + view_name,
-                               args=[], kwargs=kwargs)
+                path = reverse(
+                    project_name + '.' + view_name, args=[], kwargs=kwargs
+                )
             except NoReverseMatch:
                 raise
 
@@ -59,8 +59,12 @@ class ListURLNode(template.defaulttags.URLNode):
         if not params:
             return path
 
-        return path + '?' + '&'.join(
-            ['%s=%s' % (k, escape(v)) for (k, v) in list(params.items())])
+        return '?'.join(
+            [
+                path,
+                '&'.join('%s=%s' % (k, escape(v)) for k, v in params.items()),
+            ]
+        )
 
 
 @register.tag
@@ -68,7 +72,8 @@ def listurl(parser, token):
     bits = token.contents.split(' ', 1)
     if not bits:
         raise template.TemplateSyntaxError(
-            "'%s' takes at least one argument (path to a view)" % bits[0])
+            "'%s' takes at least one argument (path to a view)" % bits[0]
+        )
     kwargs = {}
     if len(bits) > 1:
         for arg in bits[1].split(','):
@@ -78,5 +83,6 @@ def listurl(parser, token):
                 kwargs[k] = parser.compile_filter(v)
             else:
                 raise template.TemplateSyntaxError(
-                    "'%s' requires name=value params" % bits[0])
+                    "'%s' requires name=value params" % bits[0]
+                )
     return ListURLNode(kwargs)
diff --git patchwork/templatetags/patch.py patchwork/templatetags/patch.py
index 3837798d..cf22f251 100644
--- patchwork/templatetags/patch.py
+++ patchwork/templatetags/patch.py
@@ -18,6 +18,7 @@ register = template.Library()
 def patch_tags(patch):
     counts = []
     titles = []
+
     for tag in [t for t in patch.project.tags if t.show_column]:
         count = getattr(patch, tag.attr_name)
         titles.append('%d %s' % (count, tag.name))
@@ -25,9 +26,10 @@ def patch_tags(patch):
             counts.append("-")
         else:
             counts.append(str(count))
-    return mark_safe('<span title="%s">%s</span>' % (
-        ' / '.join(titles),
-        ' '.join(counts)))
+
+    return mark_safe(
+        '<span title="%s">%s</span>' % (' / '.join(titles), ' '.join(counts))
+    )
 
 
 @register.filter(name='patch_checks')
@@ -51,14 +53,15 @@ def patch_checks(patch):
             count = '-'
 
         check_elements.append(
-            '<span class="patchlistchecks {}">{}</span>'.format(
-                color, count))
+            f'<span class="patchlistchecks {color}">{count}</span>'
+        )
 
     check_elements.reverse()
 
-    return mark_safe('<span title="%s">%s</span>' % (
-        ' / '.join(titles),
-        ''.join(check_elements)))
+    return mark_safe(
+        '<span title="%s">%s</span>'
+        % (' / '.join(titles), ''.join(check_elements))
+    )
 
 
 @register.filter(name='patch_commit_display')
@@ -69,5 +72,6 @@ def patch_commit_display(patch):
     if not fmt:
         return escape(commit)
 
-    return mark_safe('<a href="%s">%s</a>' % (escape(fmt.format(commit)),
-                                              escape(commit)))
+    return mark_safe(
+        '<a href="%s">%s</a>' % (escape(fmt.format(commit)), escape(commit))
+    )
diff --git patchwork/templatetags/person.py patchwork/templatetags/person.py
index 61937d94..f49444ae 100644
--- patchwork/templatetags/person.py
+++ patchwork/templatetags/person.py
@@ -22,9 +22,12 @@ def personify(person, project):
     else:
         linktext = escape(person.email)
 
-    url = reverse('patch-list',
-                  kwargs={'project_id': project.linkname})
+    url = reverse('patch-list', kwargs={'project_id': project.linkname})
     out = '<a href="%s?%s=%s">%s</a>' % (
-        url, SubmitterFilter.param, escape(person.id), linktext)
+        url,
+        SubmitterFilter.param,
+        escape(person.id),
+        linktext,
+    )
 
     return mark_safe(out)
diff --git patchwork/templatetags/project.py patchwork/templatetags/project.py
index 0fad8eb2..93f82abb 100644
--- patchwork/templatetags/project.py
+++ patchwork/templatetags/project.py
@@ -13,6 +13,10 @@ register = template.Library()
 @register.simple_tag(takes_context=True)
 def project_tags(context):
     tags = [t for t in context['project'].tags if t.show_column]
-    return mark_safe('<span title="%s">%s</span>' % (
-        ' / '.join([tag.name for tag in tags]),
-        '/'.join([tag.abbrev for tag in tags])))
+
+    return mark_safe(
+        '<span title="%s">%s</span>' % (
+            ' / '.join([tag.name for tag in tags]),
+            '/'.join([tag.abbrev for tag in tags]),
+        )
+    )
diff --git patchwork/templatetags/syntax.py patchwork/templatetags/syntax.py
index 69830866..0132d17b 100644
--- patchwork/templatetags/syntax.py
+++ patchwork/templatetags/syntax.py
@@ -18,25 +18,32 @@ def _compile(value):
     return re.compile(regex, re.M | re.I), cls
 
 
-_patch_span_res = [_compile(x) for x in [
-    (r'^(Index:?|diff|\-\-\-|\+\+\+|\*\*\*) .*$', 'p_header'),
-    (r'^\+.*$', 'p_add'),
-    (r'^-.*$', 'p_del'),
-    (r'^!.*$', 'p_mod'),
-]]
+_patch_span_res = [
+    _compile(x)
+    for x in [
+        (r'^(Index:?|diff|\-\-\-|\+\+\+|\*\*\*) .*$', 'p_header'),
+        (r'^\+.*$', 'p_add'),
+        (r'^-.*$', 'p_del'),
+        (r'^!.*$', 'p_mod'),
+    ]
+]
 
 _patch_chunk_re = re.compile(
-    r'^(@@ \-\d+(?:,\d+)? \+\d+(?:,\d+)? @@)(.*)$', re.M | re.I)
-
-_comment_span_res = [_compile(x) for x in [
-    (r'^\s*Signed-off-by: .*$', 'signed-off-by'),
-    (r'^\s*Acked-by: .*$', 'acked-by'),
-    (r'^\s*Nacked-by: .*$', 'nacked-by'),
-    (r'^\s*Tested-by: .*$', 'tested-by'),
-    (r'^\s*Reviewed-by: .*$', 'reviewed-by'),
-    (r'^\s*From: .*$', 'from'),
-    (r'^\s*>.*$', 'quote'),
-]]
+    r'^(@@ \-\d+(?:,\d+)? \+\d+(?:,\d+)? @@)(.*)$', re.M | re.I
+)
+
+_comment_span_res = [
+    _compile(x)
+    for x in [
+        (r'^\s*Signed-off-by: .*$', 'signed-off-by'),
+        (r'^\s*Acked-by: .*$', 'acked-by'),
+        (r'^\s*Nacked-by: .*$', 'nacked-by'),
+        (r'^\s*Tested-by: .*$', 'tested-by'),
+        (r'^\s*Reviewed-by: .*$', 'reviewed-by'),
+        (r'^\s*From: .*$', 'from'),
+        (r'^\s*>.*$', 'quote'),
+    ]
+]
 
 _span = '<span class="%s">%s</span>'
 
@@ -45,14 +52,18 @@ _span = '<span class="%s">%s</span>'
 def patchsyntax(patch):
     diff = escape(patch.diff).replace('\r\n', '\n')
 
-    for (regex, cls) in _patch_span_res:
+    for regex, cls in _patch_span_res:
         diff = regex.sub(lambda x: _span % (cls, x.group(0)), diff)
 
     diff = _patch_chunk_re.sub(
-        lambda x:
-        _span % ('p_chunk', x.group(1)) + ' ' +
-        _span % ('p_context', x.group(2)),
-        diff)
+        lambda x: ' '.join(
+            [
+                _span % ('p_chunk', x.group(1)),
+                _span % ('p_context', x.group(2)),
+            ],
+        ),
+        diff,
+    )
 
     return mark_safe(diff)
 
-- 
2.31.1



More information about the Patchwork mailing list