[PATCH 07 of 23 Series-D] hgweb: move revnavgen into an object
Pierre-Yves David
pierre-yves.david at ens-lyon.org
Thu Jan 10 18:23:52 CST 2013
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at logilab.fr>
# Date 1357838057 -3600
# Node ID 171cc6997b2a2fc3e0b47e7320c4f118757814a9
# Parent 07aa6cd3e20a89210cda4496d888fdec2cf23901
hgweb: move revnavgen into an object
To simplify it and make it resistant to filtering we need to move the logic in
an object.
diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py
+++ b/mercurial/hgweb/webcommands.py
@@ -243,11 +243,11 @@ def changelog(web, req, tmpl, shortlog=F
start = max(0, pos - revcount + 1)
end = min(count, start + revcount)
pos = end - 1
parity = paritygen(web.stripecount, offset=start - end)
- changenav = webutil.revnavgen(pos, revcount, count, web.repo.changectx)
+ changenav = webutil.revnav().gen(pos, revcount, count, web.repo.changectx)
return tmpl(shortlog and 'shortlog' or 'changelog', changenav=changenav,
node=ctx.hex(), rev=pos, changesets=count,
entries=lambda **x: changelist(**x),
latestentry=lambda **x: changelist(lastest=True,**x),
@@ -774,11 +774,11 @@ def filelog(web, req, tmpl):
"branches": webutil.nodebranchdict(repo, iterfctx)})
for e in reversed(l):
yield e
nodefunc = lambda x: fctx.filectx(fileid=x)
- nav = webutil.revnavgen(end - 1, revcount, count, nodefunc)
+ nav = webutil.revnav().gen(end - 1, revcount, count, nodefunc)
return tmpl("filelog", file=f, node=fctx.hex(), nav=nav,
entries=lambda **x: entries(**x),
latestentry=lambda **x: entries(lastest=True, **x),
revcount=revcount, morevars=morevars, lessvars=lessvars)
@@ -854,11 +854,11 @@ def graph(web, req, tmpl):
end = min(count, start + revcount)
pos = end - 1
uprev = min(max(0, count - 1), rev + revcount)
downrev = max(0, rev - revcount)
- changenav = webutil.revnavgen(pos, revcount, count, web.repo.changectx)
+ changenav = webutil.revnav().gen(pos, revcount, count, web.repo.changectx)
tree = []
if start < end:
revs = list(web.repo.changelog.revs(end - 1, start))
dag = graphmod.dagwalker(web.repo, revs)
diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py
+++ b/mercurial/hgweb/webutil.py
@@ -22,63 +22,65 @@ def up(p):
up = os.path.dirname(p)
if up == "/":
return "/"
return up + "/"
-def revnavgen(pos, pagelen, limit, nodefunc):
- """computes label and revision id for navigation link
+class revnav(object):
- :pos: is the revision relative to which we generate navigation.
- :pagelen: the since of each navigation pages
- :limit: how far shall we link
- :nodefun: factory for a changectx from a revision
+ def gen(self, pos, pagelen, limit, nodefunc):
+ """computes label and revision id for navigation link
- The return is:
- - a single element tuple
- - containing a dictionary with a `before` and `after` key
- - values are generator function taking arbitrary number of kwargs
- - yield items are dictionnary with `label` and `node` key
- """
- def seq(factor, limit=None):
- if limit:
- yield limit
- if limit >= 20 and limit <= 40:
- yield 50
- else:
- yield 1 * factor
- yield 3 * factor
- for f in seq(factor * 10):
- yield f
+ :pos: is the revision relative to which we generate navigation.
+ :pagelen: the since of each navigation pages
+ :limit: how far shall we link
+ :nodefun: factory for a changectx from a revision
- navbefore = []
- navafter = []
+ The return is:
+ - a single element tuple
+ - containing a dictionary with a `before` and `after` key
+ - values are generator function taking arbitrary number of kwargs
+ - yield items are dictionnary with `label` and `node` key
+ """
+ def seq(factor, limit=None):
+ if limit:
+ yield limit
+ if limit >= 20 and limit <= 40:
+ yield 50
+ else:
+ yield 1 * factor
+ yield 3 * factor
+ for f in seq(factor * 10):
+ yield f
- last = 0
- for f in seq(1, pagelen):
- if f < pagelen or f <= last:
- continue
- if f > limit:
- break
- last = f
- if pos + f < limit:
- navafter.append(("+%d" % f, hex(nodefunc(pos + f).node())))
- if pos - f >= 0:
- navbefore.insert(0, ("-%d" % f, hex(nodefunc(pos - f).node())))
+ navbefore = []
+ navafter = []
- navafter.append(("tip", "tip"))
- try:
- navbefore.insert(0, ("(0)", hex(nodefunc('0').node())))
- except error.RepoError:
- pass
+ last = 0
+ for f in seq(1, pagelen):
+ if f < pagelen or f <= last:
+ continue
+ if f > limit:
+ break
+ last = f
+ if pos + f < limit:
+ navafter.append(("+%d" % f, hex(nodefunc(pos + f).node())))
+ if pos - f >= 0:
+ navbefore.insert(0, ("-%d" % f, hex(nodefunc(pos - f).node())))
- def gen(l):
- def f(**map):
- for label, node in l:
- yield {"label": label, "node": node}
- return f
+ navafter.append(("tip", "tip"))
+ try:
+ navbefore.insert(0, ("(0)", hex(nodefunc('0').node())))
+ except error.RepoError:
+ pass
- return (dict(before=gen(navbefore), after=gen(navafter)),)
+ def gen(l):
+ def f(**map):
+ for label, node in l:
+ yield {"label": label, "node": node}
+ return f
+
+ return (dict(before=gen(navbefore), after=gen(navafter)),)
def _siblings(siblings=[], hiderev=None):
siblings = [s for s in siblings if s.node() != nullid]
if len(siblings) == 1 and siblings[0].rev() == hiderev:
return
More information about the Mercurial-devel
mailing list