Patchwork [3,of,4] hgweb: split graphdata() into jsdata() and nodes()

login
register
mail settings
Submitter Anton Shestakov
Date Dec. 12, 2017, 4:27 p.m.
Message ID <79649cbad159b574d859.1513096068@neuro>
Download mbox | patch
Permalink /patch/26235/
State Accepted
Headers show

Comments

Anton Shestakov - Dec. 12, 2017, 4:27 p.m.
# HG changeset patch
# User Anton Shestakov <av6@dwimlabs.net>
# Date 1512971278 -28800
#      Mon Dec 11 13:47:58 2017 +0800
# Node ID 79649cbad159b574d85902abc195089af01241cf
# Parent  2319d0216460c6f3b91f174a5027190aa80e07b7
hgweb: split graphdata() into jsdata() and nodes()

nodes keyword passed to the template can be any iterator, but jsdata needs to
be a list because it gets JSONified.

Patch

diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -1231,33 +1231,29 @@  def graph(web, req, tmpl):
         tree = list(item for item in graphmod.colored(dag, web.repo)
                     if item[1] == graphmod.CHANGESET)
 
-    def graphdata(usetuples):
-        data = []
+    def jsdata():
+        return [{'node': pycompat.bytestr(ctx),
+                 'vertex': vtx,
+                 'edges': edges}
+                for (id, type, ctx, vtx, edges) in tree]
 
-        row = 0
-        for (id, type, ctx, vtx, edges) in tree:
-            if usetuples:
-                node = pycompat.bytestr(ctx)
-                data.append({'node': node, 'vertex': vtx, 'edges': edges})
-            else:
-                entry = webutil.commonentry(web.repo, ctx)
-                edgedata = [{'col': edge[0], 'nextcol': edge[1],
-                             'color': (edge[2] - 1) % 6 + 1,
-                             'width': edge[3], 'bcolor': edge[4]}
-                            for edge in edges]
+    def nodes():
+        for row, (id, type, ctx, vtx, edges) in enumerate(tree):
+            entry = webutil.commonentry(web.repo, ctx)
+            edgedata = [{'col': edge[0],
+                         'nextcol': edge[1],
+                         'color': (edge[2] - 1) % 6 + 1,
+                         'width': edge[3],
+                         'bcolor': edge[4]}
+                        for edge in edges]
 
-                entry.update(
-                    {'col': vtx[0],
-                     'color': (vtx[1] - 1) % 6 + 1,
-                     'edges': edgedata,
-                     'row': row,
-                     'nextrow': row + 1})
+            entry.update({'col': vtx[0],
+                          'color': (vtx[1] - 1) % 6 + 1,
+                          'edges': edgedata,
+                          'row': row,
+                          'nextrow': row + 1})
 
-                data.append(entry)
-
-            row += 1
-
-        return data
+            yield entry
 
     rows = len(tree)
 
@@ -1267,8 +1263,8 @@  def graph(web, req, tmpl):
                 rows=rows,
                 bg_height=bg_height,
                 changesets=count,
-                jsdata=lambda **x: graphdata(True),
-                nodes=lambda **x: graphdata(False),
+                jsdata=lambda **x: jsdata(),
+                nodes=lambda **x: nodes(),
                 node=ctx.hex(), changenav=changenav)
 
 def _getdoc(e):