Mercurial > hg-stable
annotate mercurial/templater.py @ 3641:54d27caf6a78
templater: speed up changeset writes and stringify
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Mon, 13 Nov 2006 13:26:57 -0600 |
parents | 1b2cd5dceca2 |
children | b2c47652e8e3 |
rev | line source |
---|---|
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
1 # templater.py - template expansion for output |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
2 # |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
4 # |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
5 # This software may be used and distributed according to the terms |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
6 # of the GNU General Public License, incorporated herein by reference. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
7 |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
8 from demandload import demandload |
1901
c64bef3d7043
use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1900
diff
changeset
|
9 from i18n import gettext as _ |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
10 from node import * |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
11 demandload(globals(), "cStringIO cgi re sys os time urllib util textwrap") |
1901
c64bef3d7043
use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1900
diff
changeset
|
12 |
1902
1cc5f25653a3
make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1901
diff
changeset
|
13 def parsestring(s, quoted=True): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
14 '''parse a string using simple c-like syntax. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
15 string must be in quotes if quoted is True.''' |
1902
1cc5f25653a3
make parsestring work with strings that do not have quotes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1901
diff
changeset
|
16 if quoted: |
3639
5c9a36210662
templater: simplify parsestring
Matt Mackall <mpm@selenic.com>
parents:
3638
diff
changeset
|
17 if len(s) < 2 or s[0] != s[-1]: |
5c9a36210662
templater: simplify parsestring
Matt Mackall <mpm@selenic.com>
parents:
3638
diff
changeset
|
18 raise SyntaxError(_('unmatched quotes')) |
5c9a36210662
templater: simplify parsestring
Matt Mackall <mpm@selenic.com>
parents:
3638
diff
changeset
|
19 return s[1:-1].decode('string_escape') |
3632
231393b7316f
templater: use str.decode in parse_string
Matt Mackall <mpm@selenic.com>
parents:
3462
diff
changeset
|
20 |
231393b7316f
templater: use str.decode in parse_string
Matt Mackall <mpm@selenic.com>
parents:
3462
diff
changeset
|
21 return s.decode('string_escape') |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
22 |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
23 class templater(object): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
24 '''template expansion engine. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
25 |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
26 template expansion works like this. a map file contains key=value |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
27 pairs. if value is quoted, it is treated as string. otherwise, it |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
28 is treated as name of template file. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
29 |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
30 templater is asked to expand a key in map. it looks up key, and |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
31 looks for atrings like this: {foo}. it expands {foo} by looking up |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
32 foo in map, and substituting it. expansion is recursive: it stops |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
33 when there is no more {foo} to replace. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
34 |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
35 expansion also allows formatting and filtering. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
36 |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
37 format uses key to expand each item in list. syntax is |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
38 {key%format}. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
39 |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
40 filter uses function to transform value. syntax is |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
41 {key|filter1|filter2|...}.''' |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
42 |
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
43 template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))" |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
44 r"(\w+)(?:(?:%(\w+))|((?:\|\w+)*))[#}]") |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
45 |
1964
778281d46bb2
fix template bug that made hgweb break.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1955
diff
changeset
|
46 def __init__(self, mapfile, filters={}, defaults={}, cache={}): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
47 '''set up template engine. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
48 mapfile is name of file to read map definitions from. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
49 filters is dict of functions. each transforms a value into another. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
50 defaults is dict of default map definitions.''' |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
51 self.mapfile = mapfile or 'template' |
1975
6e1a8ea5d717
Duplicate cache when creating templater.
Shun-ichi Goto <shunichi.goto@gmail.com>
parents:
1964
diff
changeset
|
52 self.cache = cache.copy() |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
53 self.map = {} |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
54 self.base = (mapfile and os.path.dirname(mapfile)) or '' |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
55 self.filters = filters |
1964
778281d46bb2
fix template bug that made hgweb break.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1955
diff
changeset
|
56 self.defaults = defaults |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
57 |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
58 if not mapfile: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
59 return |
1901
c64bef3d7043
use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1900
diff
changeset
|
60 i = 0 |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
61 for l in file(mapfile): |
1914
a5bf0030df5f
make --style=compact look for map-cmdline.compact.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1913
diff
changeset
|
62 l = l.strip() |
1901
c64bef3d7043
use safer string parser for template engine.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1900
diff
changeset
|
63 i += 1 |
1914
a5bf0030df5f
make --style=compact look for map-cmdline.compact.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1913
diff
changeset
|
64 if not l or l[0] in '#;': continue |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
65 m = re.match(r'([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*(.+)$', l) |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
66 if m: |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
67 key, val = m.groups() |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
68 if val[0] in "'\"": |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
69 try: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
70 self.cache[key] = parsestring(val) |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
71 except SyntaxError, inst: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
72 raise SyntaxError('%s:%s: %s' % |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
73 (mapfile, i, inst.args[0])) |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
74 else: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
75 self.map[key] = os.path.join(self.base, val) |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
76 else: |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
77 raise SyntaxError(_("%s:%s: parse error") % (mapfile, i)) |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
78 |
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
79 def __contains__(self, key): |
3637
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3636
diff
changeset
|
80 return key in self.cache or key in self.map |
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
81 |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
82 def __call__(self, t, **map): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
83 '''perform expansion. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
84 t is name of map element to expand. |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
85 map is added elements to use during expansion.''' |
3637
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3636
diff
changeset
|
86 if not self.cache.has_key(t): |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
87 try: |
3637
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3636
diff
changeset
|
88 self.cache[t] = file(self.map[t]).read() |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
89 except IOError, inst: |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
90 raise IOError(inst.args[0], _('template file %s: %s') % |
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
91 (self.map[t], inst.args[1])) |
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
92 tmpl = self.cache[t] |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
93 |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
94 while tmpl: |
1900
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
95 m = self.template_re.search(tmpl) |
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
96 if not m: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
97 yield tmpl |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
98 break |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
99 |
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
100 start, end = m.span(0) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
101 key, format, fl = m.groups() |
3636
dfbbb33f3fa3
templater: simplify template function
Matt Mackall <mpm@selenic.com>
parents:
3635
diff
changeset
|
102 |
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
103 if start: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
104 yield tmpl[:start] |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
105 tmpl = tmpl[end:] |
3636
dfbbb33f3fa3
templater: simplify template function
Matt Mackall <mpm@selenic.com>
parents:
3635
diff
changeset
|
106 |
3638
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
107 if key in map: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
108 v = map[key] |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
109 else: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
110 v = self.defaults.get(key, "") |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
111 if callable(v): |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
112 v = v(**map) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
113 if format: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
114 if not hasattr(v, '__iter__'): |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
115 raise SyntaxError(_("Error expanding '%s%s'") |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
116 % (key, format)) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
117 lm = map.copy() |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
118 for i in v: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
119 lm.update(i) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
120 yield self(format, **lm) |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
121 else: |
7b064d8bac5e
template: fold template() into __call__, minor optimizations
Matt Mackall <mpm@selenic.com>
parents:
3637
diff
changeset
|
122 if fl: |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
123 for f in fl.split("|")[1:]: |
3637
e7639888bb2f
templater: simplify cache and remove filter argument in __call__
Matt Mackall <mpm@selenic.com>
parents:
3636
diff
changeset
|
124 v = self.filters[f](v) |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
125 yield v |
1900
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
126 |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
127 agescales = [("second", 1), |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
128 ("minute", 60), |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
129 ("hour", 3600), |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
130 ("day", 3600 * 24), |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
131 ("week", 3600 * 24 * 7), |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
132 ("month", 3600 * 24 * 30), |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
133 ("year", 3600 * 24 * 365)] |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
134 |
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
135 agescales.reverse() |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
136 |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
137 def age(date): |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
138 '''turn a (timestamp, tzoff) tuple into an age string.''' |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
139 |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
140 def plural(t, c): |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
141 if c == 1: |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
142 return t |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
143 return t + "s" |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
144 def fmt(t, c): |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
145 return "%d %s" % (c, plural(t, c)) |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
146 |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
147 now = time.time() |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
148 then = date[0] |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
149 delta = max(1, int(now - then)) |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
150 |
1900
f2815605186e
move repeated work out of inner loops.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1899
diff
changeset
|
151 for t, s in agescales: |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
152 n = delta / s |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
153 if n >= 2 or s == 1: |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
154 return fmt(t, n) |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
155 |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
156 def stringify(thing): |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
157 '''turn nested template iterator into string.''' |
3641
54d27caf6a78
templater: speed up changeset writes and stringify
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
158 if hasattr(thing, '__iter__'): |
54d27caf6a78
templater: speed up changeset writes and stringify
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
159 return "".join([stringify(t) for t in thing]) |
54d27caf6a78
templater: speed up changeset writes and stringify
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
160 return str(thing) |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
161 |
2470
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2202
diff
changeset
|
162 para_re = None |
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2202
diff
changeset
|
163 space_re = None |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
164 |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
165 def fill(text, width): |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
166 '''fill many paragraphs.''' |
2470
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2202
diff
changeset
|
167 global para_re, space_re |
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2202
diff
changeset
|
168 if para_re is None: |
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2202
diff
changeset
|
169 para_re = re.compile('(\n\n|\n\\s*[-*]\\s*)', re.M) |
fe1689273f84
use demandload more.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2202
diff
changeset
|
170 space_re = re.compile(r' +') |
2535
b8ccf6386db7
Arrange for old copies of CGI scripts to still work.
Eric Hopper <hopper@omnifarious.org>
parents:
2470
diff
changeset
|
171 |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
172 def findparas(): |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
173 start = 0 |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
174 while True: |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
175 m = para_re.search(text, start) |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
176 if not m: |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
177 w = len(text) |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
178 while w > start and text[w-1].isspace(): w -= 1 |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
179 yield text[start:w], text[w:] |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
180 break |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
181 yield text[start:m.start(0)], m.group(1) |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
182 start = m.end(1) |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
183 |
3633
bf3dec184c78
templater: remove cStringIO for fill
Matt Mackall <mpm@selenic.com>
parents:
3632
diff
changeset
|
184 return "".join([space_re.sub(' ', textwrap.fill(para, width)) + rest |
bf3dec184c78
templater: remove cStringIO for fill
Matt Mackall <mpm@selenic.com>
parents:
3632
diff
changeset
|
185 for para, rest in findparas()]) |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
186 |
2559
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
187 def firstline(text): |
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
188 '''return the first line of text''' |
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
189 try: |
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
190 return text.splitlines(1)[0].rstrip('\r\n') |
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
191 except IndexError: |
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
192 return '' |
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
193 |
1906
9dec2479622d
add iso date template filter.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1905
diff
changeset
|
194 def isodate(date): |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
195 '''turn a (timestamp, tzoff) tuple into an iso 8631 date and time.''' |
1906
9dec2479622d
add iso date template filter.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1905
diff
changeset
|
196 return util.datestr(date, format='%Y-%m-%d %H:%M') |
9dec2479622d
add iso date template filter.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1905
diff
changeset
|
197 |
2519
deb466fa6957
add a new template function 'hgdate'
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2470
diff
changeset
|
198 def hgdate(date): |
deb466fa6957
add a new template function 'hgdate'
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2470
diff
changeset
|
199 '''turn a (timestamp, tzoff) tuple into an hg cset timestamp.''' |
deb466fa6957
add a new template function 'hgdate'
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2470
diff
changeset
|
200 return "%d %d" % date |
deb466fa6957
add a new template function 'hgdate'
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2470
diff
changeset
|
201 |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
202 def nl2br(text): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
203 '''replace raw newlines with xhtml line breaks.''' |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
204 return text.replace('\n', '<br/>\n') |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
205 |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
206 def obfuscate(text): |
2649
3b53fa20dda8
Preserve multibyte UTF-8 characters when obfuscating.
Brendan Cully <brendan@kublai.com>
parents:
2559
diff
changeset
|
207 text = unicode(text, 'utf-8', 'replace') |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
208 return ''.join(['&#%d;' % ord(c) for c in text]) |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
209 |
1904 | 210 def domain(author): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
211 '''get domain of author, or empty string if none.''' |
1904 | 212 f = author.find('@') |
213 if f == -1: return '' | |
214 author = author[f+1:] | |
215 f = author.find('>') | |
216 if f >= 0: author = author[:f] | |
217 return author | |
218 | |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
219 def email(author): |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
220 '''get email of author.''' |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
221 r = author.find('>') |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
222 if r == -1: r = None |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
223 return author[author.find('<')+1:r] |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
224 |
1904 | 225 def person(author): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
226 '''get name of author, or else username.''' |
1904 | 227 f = author.find('<') |
228 if f == -1: return util.shortuser(author) | |
229 return author[:f].rstrip() | |
230 | |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
231 def shortdate(date): |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
232 '''turn (timestamp, tzoff) tuple into iso 8631 date.''' |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
233 return util.datestr(date, format='%Y-%m-%d', timezone=False) |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
234 |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
235 def indent(text, prefix): |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
236 '''indent each non-empty line of text after first with prefix.''' |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
237 lines = text.splitlines() |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
238 num_lines = len(lines) |
3635
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3634
diff
changeset
|
239 def indenter(): |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3634
diff
changeset
|
240 for i in xrange(num_lines): |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3634
diff
changeset
|
241 l = lines[i] |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3634
diff
changeset
|
242 if i and l.strip(): |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3634
diff
changeset
|
243 yield prefix |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3634
diff
changeset
|
244 yield l |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3634
diff
changeset
|
245 if i < num_lines - 1 or text.endswith('\n'): |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3634
diff
changeset
|
246 yield '\n' |
7af1f54c044c
templater: take cStringIO out of indent
Matt Mackall <mpm@selenic.com>
parents:
3634
diff
changeset
|
247 return "".join(indenter()) |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
248 |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
249 common_filters = { |
1904 | 250 "addbreaks": nl2br, |
2191
c2e43535d4d1
make templater bit more flexible and efficient for external users.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2189
diff
changeset
|
251 "basename": os.path.basename, |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
252 "age": age, |
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
253 "date": lambda x: util.datestr(x), |
1905
0c760737b996
improve template errors when something is wrong.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1904
diff
changeset
|
254 "domain": domain, |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
255 "email": email, |
1904 | 256 "escape": lambda x: cgi.escape(x, True), |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
257 "fill68": lambda x: fill(x, width=68), |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
258 "fill76": lambda x: fill(x, width=76), |
2559
bf67d0f6531c
templater.firstline should handle empty strings
Brendan Cully <brendan@kublai.com>
parents:
2539
diff
changeset
|
259 "firstline": firstline, |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
260 "tabindent": lambda x: indent(x, '\t'), |
2519
deb466fa6957
add a new template function 'hgdate'
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
2470
diff
changeset
|
261 "hgdate": hgdate, |
1906
9dec2479622d
add iso date template filter.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1905
diff
changeset
|
262 "isodate": isodate, |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
263 "obfuscate": obfuscate, |
1912
b288b4bb8448
hide some functions behind lambdas, so demandload is useful.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1909
diff
changeset
|
264 "permissions": lambda x: x and "-rwxr-xr-x" or "-rw-r--r--", |
1904 | 265 "person": person, |
266 "rfc822date": lambda x: util.datestr(x, "%a, %d %b %Y %H:%M:%S"), | |
1912
b288b4bb8448
hide some functions behind lambdas, so demandload is useful.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1909
diff
changeset
|
267 "short": lambda x: x[:12], |
1987
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
268 "shortdate": shortdate, |
04c17fc39c84
add changelog style to command line template.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1975
diff
changeset
|
269 "stringify": stringify, |
1904 | 270 "strip": lambda x: x.strip(), |
1912
b288b4bb8448
hide some functions behind lambdas, so demandload is useful.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1909
diff
changeset
|
271 "urlescape": lambda x: urllib.quote(x), |
b288b4bb8448
hide some functions behind lambdas, so demandload is useful.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1909
diff
changeset
|
272 "user": lambda x: util.shortuser(x), |
3462
fab28100ea88
Added extra changeset info to templater and map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3461
diff
changeset
|
273 "stringescape": lambda x: x.encode('string_escape'), |
1896
f8f818a04f5b
move hgweb template code out to templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
274 } |
1897
58b6784cf9f1
move hgweb.templatepath into templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1896
diff
changeset
|
275 |
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
276 def templatepath(name=None): |
1909
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
277 '''return location of template file or directory (if no name). |
37b9f80a5fbb
add doc comments to template code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1906
diff
changeset
|
278 returns None if not found.''' |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
279 |
2001
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
280 # executable version (py2exe) doesn't support __file__ |
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
281 if hasattr(sys, 'frozen'): |
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
282 module = sys.executable |
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
283 else: |
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
284 module = __file__ |
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
285 for f in 'templates', '../templates': |
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
286 fl = f.split('/') |
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
287 if name: fl.append(name) |
2001
a439b7b51530
Windows py2exe version didn't handle names given to templatepath() correctly
Lee Cantey <lcantey@gmail.com>
parents:
1987
diff
changeset
|
288 p = os.path.join(os.path.dirname(module), *fl) |
1899
888d298ddb91
many small changes to templater.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1897
diff
changeset
|
289 if (name and os.path.exists(p)) or os.path.isdir(p): |
1897
58b6784cf9f1
move hgweb.templatepath into templater
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1896
diff
changeset
|
290 return os.path.normpath(p) |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
291 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
292 class changeset_templater(object): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
293 '''format changeset information.''' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
294 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
295 def __init__(self, ui, repo, mapfile, dest=None): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
296 self.t = templater(mapfile, common_filters, |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
297 cache={'parent': '{rev}:{node|short} ', |
3197 | 298 'manifest': '{rev}:{node|short}', |
299 'filecopy': '{name} ({source})'}) | |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
300 self.ui = ui |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
301 self.dest = dest |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
302 self.repo = repo |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
303 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
304 def use_template(self, t): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
305 '''set template string to use''' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
306 self.t.cache['changeset'] = t |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
307 |
3640
1b2cd5dceca2
changeset_templater: remove changes arg
Matt Mackall <mpm@selenic.com>
parents:
3639
diff
changeset
|
308 def show(self, rev=0, changenode=None, brinfo=None, copies=[], **props): |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
309 '''show a single changeset or file revision''' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
310 log = self.repo.changelog |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
311 if changenode is None: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
312 changenode = log.node(rev) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
313 elif not rev: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
314 rev = log.rev(changenode) |
3640
1b2cd5dceca2
changeset_templater: remove changes arg
Matt Mackall <mpm@selenic.com>
parents:
3639
diff
changeset
|
315 |
1b2cd5dceca2
changeset_templater: remove changes arg
Matt Mackall <mpm@selenic.com>
parents:
3639
diff
changeset
|
316 changes = log.read(changenode) |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
317 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
318 def showlist(name, values, plural=None, **args): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
319 '''expand set of values. |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
320 name is name of key in template map. |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
321 values is list of strings or dicts. |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
322 plural is plural of name, if not simply name + 's'. |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
323 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
324 expansion works like this, given name 'foo'. |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
325 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
326 if values is empty, expand 'no_foos'. |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
327 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
328 if 'foo' not in template map, return values as a string, |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
329 joined by space. |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
330 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
331 expand 'start_foos'. |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
332 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
333 for each value, expand 'foo'. if 'last_foo' in template |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
334 map, expand it instead of 'foo' for last key. |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
335 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
336 expand 'end_foos'. |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
337 ''' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
338 if plural: names = plural |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
339 else: names = name + 's' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
340 if not values: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
341 noname = 'no_' + names |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
342 if noname in self.t: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
343 yield self.t(noname, **args) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
344 return |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
345 if name not in self.t: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
346 if isinstance(values[0], str): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
347 yield ' '.join(values) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
348 else: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
349 for v in values: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
350 yield dict(v, **args) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
351 return |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
352 startname = 'start_' + names |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
353 if startname in self.t: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
354 yield self.t(startname, **args) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
355 vargs = args.copy() |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
356 def one(v, tag=name): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
357 try: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
358 vargs.update(v) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
359 except (AttributeError, ValueError): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
360 try: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
361 for a, b in v: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
362 vargs[a] = b |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
363 except ValueError: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
364 vargs[name] = v |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
365 return self.t(tag, **vargs) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
366 lastname = 'last_' + name |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
367 if lastname in self.t: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
368 last = values.pop() |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
369 else: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
370 last = None |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
371 for v in values: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
372 yield one(v) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
373 if last is not None: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
374 yield one(last, tag=lastname) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
375 endname = 'end_' + names |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
376 if endname in self.t: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
377 yield self.t(endname, **args) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
378 |
3461
451ec905625b
Add new branch info to templater and use it in map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3361
diff
changeset
|
379 def showbranches(**args): |
451ec905625b
Add new branch info to templater and use it in map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3361
diff
changeset
|
380 branch = changes[5].get("branch") |
451ec905625b
Add new branch info to templater and use it in map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3361
diff
changeset
|
381 if branch: |
451ec905625b
Add new branch info to templater and use it in map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3361
diff
changeset
|
382 yield showlist('branch', [branch], plural='branches', **args) |
451ec905625b
Add new branch info to templater and use it in map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3361
diff
changeset
|
383 # add old style branches if requested |
451ec905625b
Add new branch info to templater and use it in map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3361
diff
changeset
|
384 if brinfo and changenode in brinfo: |
451ec905625b
Add new branch info to templater and use it in map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3361
diff
changeset
|
385 for x in showlist('branch', brinfo[changenode], |
451ec905625b
Add new branch info to templater and use it in map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3361
diff
changeset
|
386 plural='branches', **args): |
451ec905625b
Add new branch info to templater and use it in map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3361
diff
changeset
|
387 yield x |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
388 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
389 if self.ui.debugflag: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
390 def showmanifest(**args): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
391 args = args.copy() |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
392 args.update(dict(rev=self.repo.manifest.rev(changes[0]), |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
393 node=hex(changes[0]))) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
394 yield self.t('manifest', **args) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
395 else: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
396 showmanifest = '' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
397 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
398 def showparents(**args): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
399 parents = [[('rev', log.rev(p)), ('node', hex(p))] |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
400 for p in log.parents(changenode) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
401 if self.ui.debugflag or p != nullid] |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
402 if (not self.ui.debugflag and len(parents) == 1 and |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
403 parents[0][0][1] == rev - 1): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
404 return |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
405 for x in showlist('parent', parents, **args): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
406 yield x |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
407 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
408 def showtags(**args): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
409 for x in showlist('tag', self.repo.nodetags(changenode), **args): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
410 yield x |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
411 |
3462
fab28100ea88
Added extra changeset info to templater and map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3461
diff
changeset
|
412 def showextras(**args): |
fab28100ea88
Added extra changeset info to templater and map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3461
diff
changeset
|
413 extras = changes[5].items() |
fab28100ea88
Added extra changeset info to templater and map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3461
diff
changeset
|
414 extras.sort() |
fab28100ea88
Added extra changeset info to templater and map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3461
diff
changeset
|
415 for key, value in extras: |
fab28100ea88
Added extra changeset info to templater and map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3461
diff
changeset
|
416 args = args.copy() |
fab28100ea88
Added extra changeset info to templater and map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3461
diff
changeset
|
417 args.update(dict(key=key, value=value)) |
fab28100ea88
Added extra changeset info to templater and map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3461
diff
changeset
|
418 yield self.t('extra', **args) |
fab28100ea88
Added extra changeset info to templater and map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3461
diff
changeset
|
419 |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
420 if self.ui.debugflag: |
2875
3d6efcbbd1c9
remove localrepository.changes.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2649
diff
changeset
|
421 files = self.repo.status(log.parents(changenode)[0], changenode)[:3] |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
422 def showfiles(**args): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
423 for x in showlist('file', files[0], **args): yield x |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
424 def showadds(**args): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
425 for x in showlist('file_add', files[1], **args): yield x |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
426 def showdels(**args): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
427 for x in showlist('file_del', files[2], **args): yield x |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
428 else: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
429 def showfiles(**args): |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
430 for x in showlist('file', changes[3], **args): yield x |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
431 showadds = '' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
432 showdels = '' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
433 |
3197 | 434 copies = [{'name': x[0], 'source': x[1]} |
435 for x in copies] | |
436 def showcopies(**args): | |
437 for x in showlist('file_copy', copies, plural='file_copies', | |
438 **args): | |
439 yield x | |
440 | |
2191
c2e43535d4d1
make templater bit more flexible and efficient for external users.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2189
diff
changeset
|
441 defprops = { |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
442 'author': changes[1], |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
443 'branches': showbranches, |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
444 'date': changes[2], |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
445 'desc': changes[4], |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
446 'file_adds': showadds, |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
447 'file_dels': showdels, |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
448 'files': showfiles, |
3197 | 449 'file_copies': showcopies, |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
450 'manifest': showmanifest, |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
451 'node': hex(changenode), |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
452 'parents': showparents, |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
453 'rev': rev, |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
454 'tags': showtags, |
3462
fab28100ea88
Added extra changeset info to templater and map-cmdline.default.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3461
diff
changeset
|
455 'extras': showextras, |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
456 } |
2191
c2e43535d4d1
make templater bit more flexible and efficient for external users.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2189
diff
changeset
|
457 props = props.copy() |
c2e43535d4d1
make templater bit more flexible and efficient for external users.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2189
diff
changeset
|
458 props.update(defprops) |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
459 |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
460 try: |
3641
54d27caf6a78
templater: speed up changeset writes and stringify
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
461 dest = self.dest or self.ui |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
462 if self.ui.debugflag and 'header_debug' in self.t: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
463 key = 'header_debug' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
464 elif self.ui.quiet and 'header_quiet' in self.t: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
465 key = 'header_quiet' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
466 elif self.ui.verbose and 'header_verbose' in self.t: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
467 key = 'header_verbose' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
468 elif 'header' in self.t: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
469 key = 'header' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
470 else: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
471 key = '' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
472 if key: |
3641
54d27caf6a78
templater: speed up changeset writes and stringify
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
473 dest.write_header(stringify(self.t(key, **props))) |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
474 if self.ui.debugflag and 'changeset_debug' in self.t: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
475 key = 'changeset_debug' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
476 elif self.ui.quiet and 'changeset_quiet' in self.t: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
477 key = 'changeset_quiet' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
478 elif self.ui.verbose and 'changeset_verbose' in self.t: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
479 key = 'changeset_verbose' |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
480 else: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
481 key = 'changeset' |
3641
54d27caf6a78
templater: speed up changeset writes and stringify
Matt Mackall <mpm@selenic.com>
parents:
3640
diff
changeset
|
482 dest.write(stringify(self.t(key, **props))) |
2189
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
483 except KeyError, inst: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
484 raise util.Abort(_("%s: no key named '%s'") % (self.t.mapfile, |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
485 inst.args[0])) |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
486 except SyntaxError, inst: |
e3eba577a0ae
move changeset_templater into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2001
diff
changeset
|
487 raise util.Abort(_('%s: %s') % (self.t.mapfile, inst.args[0])) |
2199
f1986a61ccff
move stringio class from bugzilla extension into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2191
diff
changeset
|
488 |
f1986a61ccff
move stringio class from bugzilla extension into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2191
diff
changeset
|
489 class stringio(object): |
f1986a61ccff
move stringio class from bugzilla extension into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2191
diff
changeset
|
490 '''wrap cStringIO for use by changeset_templater.''' |
f1986a61ccff
move stringio class from bugzilla extension into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2191
diff
changeset
|
491 def __init__(self): |
f1986a61ccff
move stringio class from bugzilla extension into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2191
diff
changeset
|
492 self.fp = cStringIO.StringIO() |
f1986a61ccff
move stringio class from bugzilla extension into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2191
diff
changeset
|
493 |
f1986a61ccff
move stringio class from bugzilla extension into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2191
diff
changeset
|
494 def write(self, *args): |
f1986a61ccff
move stringio class from bugzilla extension into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2191
diff
changeset
|
495 for a in args: |
f1986a61ccff
move stringio class from bugzilla extension into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2191
diff
changeset
|
496 self.fp.write(a) |
f1986a61ccff
move stringio class from bugzilla extension into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2191
diff
changeset
|
497 |
f1986a61ccff
move stringio class from bugzilla extension into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2191
diff
changeset
|
498 write_header = write |
f1986a61ccff
move stringio class from bugzilla extension into templater module.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2191
diff
changeset
|
499 |
2202
bc35cd725c37
make templater.stringio a tiny bit more use.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2201
diff
changeset
|
500 def __getattr__(self, key): |
bc35cd725c37
make templater.stringio a tiny bit more use.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2201
diff
changeset
|
501 return getattr(self.fp, key) |