Mercurial > hg
annotate mercurial/formatter.py @ 47838:a99ffbcdb6a9 stable
pyoxidized: add a copy of the `defaultrc` file in the right location
This get the hacky setup closer to how an actual install is setup.
Differential Revision: https://phab.mercurial-scm.org/D11290
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Wed, 18 Aug 2021 01:18:34 +0200 |
parents | d4ba4d51f85f |
children | df56e6bd37f6 |
rev | line source |
---|---|
16134 | 1 # formatter.py - generic output formatting for mercurial |
2 # | |
46819
d4ba4d51f85f
contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents:
46769
diff
changeset
|
3 # Copyright 2012 Olivia Mackall <olivia@selenic.com> |
16134 | 4 # |
5 # This software may be used and distributed according to the terms of the | |
6 # GNU General Public License version 2 or any later version. | |
7 | |
30560
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
8 """Generic output formatting for Mercurial |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
9 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
10 The formatter provides API to show data in various ways. The following |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
11 functions should be used in place of ui.write(): |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
12 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
13 - fm.write() for unconditional output |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
14 - fm.condwrite() to show some extra data conditionally in plain output |
31172
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31170
diff
changeset
|
15 - fm.context() to provide changectx to template output |
30560
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
16 - fm.data() to provide extra data to JSON or template output |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
17 - fm.plain() to show raw text that isn't provided to JSON or template output |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
18 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
19 To show structured data (e.g. date tuples, dicts, lists), apply fm.format*() |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
20 beforehand so the data is converted to the appropriate data type. Use |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
21 fm.isplain() if you need to convert or format data conditionally which isn't |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
22 supported by the formatter API. |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
23 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
24 To build nested structure (i.e. a list of dicts), use fm.nested(). |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
25 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
26 See also https://www.mercurial-scm.org/wiki/GenericTemplatingPlan |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
27 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
28 fm.condwrite() vs 'if cond:': |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
29 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
30 In most cases, use fm.condwrite() so users can selectively show the data |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
31 in template output. If it's costly to build data, use plain 'if cond:' with |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
32 fm.write(). |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
33 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
34 fm.nested() vs fm.formatdict() (or fm.formatlist()): |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
35 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
36 fm.nested() should be used to form a tree structure (a list of dicts of |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
37 lists of dicts...) which can be accessed through template keywords, e.g. |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
38 "{foo % "{bar % {...}} {baz % {...}}"}". On the other hand, fm.formatdict() |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
39 exports a dict-type object to template, which can be accessed by e.g. |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
40 "{get(foo, key)}" function. |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
41 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
42 Doctest helper: |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
43 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
44 >>> def show(fn, verbose=False, **opts): |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
45 ... import sys |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
46 ... from . import ui as uimod |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
47 ... ui = uimod.ui() |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
48 ... ui.verbose = verbose |
34255
d6af8da4a3b8
py3: rewrite stdout hack of doctest by using ui.pushbuffer()
Yuya Nishihara <yuya@tcha.org>
parents:
34131
diff
changeset
|
49 ... ui.pushbuffer() |
d6af8da4a3b8
py3: rewrite stdout hack of doctest by using ui.pushbuffer()
Yuya Nishihara <yuya@tcha.org>
parents:
34131
diff
changeset
|
50 ... try: |
34256
ebe3d0095c69
py3: convert system strings to bytes in doctest of formatter.py
Yuya Nishihara <yuya@tcha.org>
parents:
34255
diff
changeset
|
51 ... return fn(ui, ui.formatter(pycompat.sysbytes(fn.__name__), |
ebe3d0095c69
py3: convert system strings to bytes in doctest of formatter.py
Yuya Nishihara <yuya@tcha.org>
parents:
34255
diff
changeset
|
52 ... pycompat.byteskwargs(opts))) |
34255
d6af8da4a3b8
py3: rewrite stdout hack of doctest by using ui.pushbuffer()
Yuya Nishihara <yuya@tcha.org>
parents:
34131
diff
changeset
|
53 ... finally: |
d6af8da4a3b8
py3: rewrite stdout hack of doctest by using ui.pushbuffer()
Yuya Nishihara <yuya@tcha.org>
parents:
34131
diff
changeset
|
54 ... print(pycompat.sysstr(ui.popbuffer()), end='') |
30560
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
55 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
56 Basic example: |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
57 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
58 >>> def files(ui, fm): |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33090
diff
changeset
|
59 ... files = [(b'foo', 123, (0, 0)), (b'bar', 456, (1, 0))] |
30560
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
60 ... for f in files: |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
61 ... fm.startitem() |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33090
diff
changeset
|
62 ... fm.write(b'path', b'%s', f[0]) |
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33090
diff
changeset
|
63 ... fm.condwrite(ui.verbose, b'date', b' %s', |
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33090
diff
changeset
|
64 ... fm.formatdate(f[2], b'%Y-%m-%d %H:%M:%S')) |
30560
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
65 ... fm.data(size=f[1]) |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33090
diff
changeset
|
66 ... fm.plain(b'\\n') |
30560
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
67 ... fm.end() |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
68 >>> show(files) |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
69 foo |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
70 bar |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
71 >>> show(files, verbose=True) |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
72 foo 1970-01-01 00:00:00 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
73 bar 1970-01-01 00:00:01 |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33090
diff
changeset
|
74 >>> show(files, template=b'json') |
30560
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
75 [ |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
76 { |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
77 "date": [0, 0], |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
78 "path": "foo", |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
79 "size": 123 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
80 }, |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
81 { |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
82 "date": [1, 0], |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
83 "path": "bar", |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
84 "size": 456 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
85 } |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
86 ] |
34131
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33090
diff
changeset
|
87 >>> show(files, template=b'path: {path}\\ndate: {date|rfc3339date}\\n') |
30560
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
88 path: foo |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
89 date: 1970-01-01T00:00:00+00:00 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
90 path: bar |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
91 date: 1970-01-01T00:00:01+00:00 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
92 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
93 Nested example: |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
94 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
95 >>> def subrepos(ui, fm): |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
96 ... fm.startitem() |
35470
a33be093ec62
templater: look up symbols/resources as if they were separated (issue5699)
Yuya Nishihara <yuya@tcha.org>
parents:
35469
diff
changeset
|
97 ... fm.write(b'reponame', b'[%s]\\n', b'baz') |
37500
8bb3899a0f47
formatter: make nested items somewhat readable in template output
Yuya Nishihara <yuya@tcha.org>
parents:
37105
diff
changeset
|
98 ... files(ui, fm.nested(b'files', tmpl=b'{reponame}')) |
30560
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
99 ... fm.end() |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
100 >>> show(subrepos) |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
101 [baz] |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
102 foo |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
103 bar |
35470
a33be093ec62
templater: look up symbols/resources as if they were separated (issue5699)
Yuya Nishihara <yuya@tcha.org>
parents:
35469
diff
changeset
|
104 >>> show(subrepos, template=b'{reponame}: {join(files % "{path}", ", ")}\\n') |
30560
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
105 baz: foo, bar |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
106 """ |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29949
diff
changeset
|
107 |
34255
d6af8da4a3b8
py3: rewrite stdout hack of doctest by using ui.pushbuffer()
Yuya Nishihara <yuya@tcha.org>
parents:
34131
diff
changeset
|
108 from __future__ import absolute_import, print_function |
25950
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
109 |
32580
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
110 import contextlib |
31807
e6eb86b154c5
templater: provide loop counter as "index" keyword
Yuya Nishihara <yuya@tcha.org>
parents:
31805
diff
changeset
|
111 import itertools |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
112 import os |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
113 |
25950
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
114 from .i18n import _ |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
115 from .node import ( |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
116 hex, |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
117 short, |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
118 ) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
119 from .thirdparty import attr |
25950
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
120 |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
121 from . import ( |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26373
diff
changeset
|
122 error, |
32159
0fd15522a848
py3: use pycompat.byteskwargs to converts kwargs to bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31925
diff
changeset
|
123 pycompat, |
31782
654e9a1c8a6c
formatter: use templatefilters.json()
Yuya Nishihara <yuya@tcha.org>
parents:
31396
diff
changeset
|
124 templatefilters, |
29676
c3a9cd78b151
formatter: add function to convert list to appropriate format (issue5217)
Yuya Nishihara <yuya@tcha.org>
parents:
29324
diff
changeset
|
125 templatekw, |
25950
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
126 templater, |
36920
6ff6e1d6b5b8
templater: move stringify() to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36634
diff
changeset
|
127 templateutil, |
29324
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28957
diff
changeset
|
128 util, |
25950
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
129 ) |
40274
a8b9174517c7
formatter: use stringutil.pprint() in debug output to drop b''
Yuya Nishihara <yuya@tcha.org>
parents:
40140
diff
changeset
|
130 from .utils import ( |
41996
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
131 cborutil, |
40274
a8b9174517c7
formatter: use stringutil.pprint() in debug output to drop b''
Yuya Nishihara <yuya@tcha.org>
parents:
40140
diff
changeset
|
132 dateutil, |
a8b9174517c7
formatter: use stringutil.pprint() in debug output to drop b''
Yuya Nishihara <yuya@tcha.org>
parents:
40140
diff
changeset
|
133 stringutil, |
a8b9174517c7
formatter: use stringutil.pprint() in debug output to drop b''
Yuya Nishihara <yuya@tcha.org>
parents:
40140
diff
changeset
|
134 ) |
25950
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
135 |
29324
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28957
diff
changeset
|
136 pickle = util.pickle |
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28957
diff
changeset
|
137 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
138 |
43335
242ad45b60b3
config: fix -Tjson to not crash due to unsupported defaultvalue types
Yuya Nishihara <yuya@tcha.org>
parents:
43106
diff
changeset
|
139 def isprintable(obj): |
242ad45b60b3
config: fix -Tjson to not crash due to unsupported defaultvalue types
Yuya Nishihara <yuya@tcha.org>
parents:
43106
diff
changeset
|
140 """Check if the given object can be directly passed in to formatter's |
242ad45b60b3
config: fix -Tjson to not crash due to unsupported defaultvalue types
Yuya Nishihara <yuya@tcha.org>
parents:
43106
diff
changeset
|
141 write() and data() functions |
242ad45b60b3
config: fix -Tjson to not crash due to unsupported defaultvalue types
Yuya Nishihara <yuya@tcha.org>
parents:
43106
diff
changeset
|
142 |
242ad45b60b3
config: fix -Tjson to not crash due to unsupported defaultvalue types
Yuya Nishihara <yuya@tcha.org>
parents:
43106
diff
changeset
|
143 Returns False if the object is unsupported or must be pre-processed by |
242ad45b60b3
config: fix -Tjson to not crash due to unsupported defaultvalue types
Yuya Nishihara <yuya@tcha.org>
parents:
43106
diff
changeset
|
144 formatdate(), formatdict(), or formatlist(). |
242ad45b60b3
config: fix -Tjson to not crash due to unsupported defaultvalue types
Yuya Nishihara <yuya@tcha.org>
parents:
43106
diff
changeset
|
145 """ |
242ad45b60b3
config: fix -Tjson to not crash due to unsupported defaultvalue types
Yuya Nishihara <yuya@tcha.org>
parents:
43106
diff
changeset
|
146 return isinstance(obj, (type(None), bool, int, pycompat.long, float, bytes)) |
242ad45b60b3
config: fix -Tjson to not crash due to unsupported defaultvalue types
Yuya Nishihara <yuya@tcha.org>
parents:
43106
diff
changeset
|
147 |
242ad45b60b3
config: fix -Tjson to not crash due to unsupported defaultvalue types
Yuya Nishihara <yuya@tcha.org>
parents:
43106
diff
changeset
|
148 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
149 class _nullconverter(object): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
150 '''convert non-primitive data types to be processed by formatter''' |
33090
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32952
diff
changeset
|
151 |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32952
diff
changeset
|
152 # set to True if context object should be stored as item |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32952
diff
changeset
|
153 storecontext = False |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32952
diff
changeset
|
154 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
155 @staticmethod |
37500
8bb3899a0f47
formatter: make nested items somewhat readable in template output
Yuya Nishihara <yuya@tcha.org>
parents:
37105
diff
changeset
|
156 def wrapnested(data, tmpl, sep): |
8bb3899a0f47
formatter: make nested items somewhat readable in template output
Yuya Nishihara <yuya@tcha.org>
parents:
37105
diff
changeset
|
157 '''wrap nested data by appropriate type''' |
8bb3899a0f47
formatter: make nested items somewhat readable in template output
Yuya Nishihara <yuya@tcha.org>
parents:
37105
diff
changeset
|
158 return data |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
159 |
37500
8bb3899a0f47
formatter: make nested items somewhat readable in template output
Yuya Nishihara <yuya@tcha.org>
parents:
37105
diff
changeset
|
160 @staticmethod |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
161 def formatdate(date, fmt): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
162 '''convert date tuple to appropriate format''' |
37770
31750413f8d7
formatter: convert timestamp to int
Yuya Nishihara <yuya@tcha.org>
parents:
37597
diff
changeset
|
163 # timestamp can be float, but the canonical form should be int |
31750413f8d7
formatter: convert timestamp to int
Yuya Nishihara <yuya@tcha.org>
parents:
37597
diff
changeset
|
164 ts, tz = date |
31750413f8d7
formatter: convert timestamp to int
Yuya Nishihara <yuya@tcha.org>
parents:
37597
diff
changeset
|
165 return (int(ts), tz) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
166 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
167 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
168 def formatdict(data, key, value, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
169 '''convert dict or key-value pairs to appropriate dict format''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
170 # use plain dict instead of util.sortdict so that data can be |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
171 # serialized as a builtin dict in pickle output |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
172 return dict(data) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
173 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
174 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
175 def formatlist(data, name, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
176 '''convert iterable to appropriate list format''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
177 return list(data) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
178 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
179 |
16134 | 180 class baseformatter(object): |
46769
67a2ecea8bd9
debugdiscovery: also integrate the discovery output in the json one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45320
diff
changeset
|
181 |
67a2ecea8bd9
debugdiscovery: also integrate the discovery output in the json one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45320
diff
changeset
|
182 # set to True if the formater output a strict format that does not support |
67a2ecea8bd9
debugdiscovery: also integrate the discovery output in the json one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45320
diff
changeset
|
183 # arbitrary output in the stream. |
67a2ecea8bd9
debugdiscovery: also integrate the discovery output in the json one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45320
diff
changeset
|
184 strict_format = False |
67a2ecea8bd9
debugdiscovery: also integrate the discovery output in the json one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45320
diff
changeset
|
185 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
186 def __init__(self, ui, topic, opts, converter): |
16134 | 187 self._ui = ui |
188 self._topic = topic | |
37597
d110167610db
formatter: carry opts to file-based formatters by basefm
Yuya Nishihara <yuya@tcha.org>
parents:
37596
diff
changeset
|
189 self._opts = opts |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
190 self._converter = converter |
16134 | 191 self._item = None |
22701
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
192 # function to convert node to string suitable for this output |
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
193 self.hexfunc = hex |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
194 |
29882
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29837
diff
changeset
|
195 def __enter__(self): |
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29837
diff
changeset
|
196 return self |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
197 |
29882
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29837
diff
changeset
|
198 def __exit__(self, exctype, excvalue, traceback): |
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29837
diff
changeset
|
199 if exctype is None: |
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29837
diff
changeset
|
200 self.end() |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
201 |
16134 | 202 def _showitem(self): |
203 '''show a formatted item once all data is collected''' | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
204 |
16134 | 205 def startitem(self): |
206 '''begin an item in the format list''' | |
207 if self._item is not None: | |
208 self._showitem() | |
209 self._item = {} | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
210 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
211 def formatdate(self, date, fmt=b'%a %b %d %H:%M:%S %Y %1%2'): |
29678
2f3f18ad55a2
formatter: add function to convert date tuple to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29676
diff
changeset
|
212 '''convert date tuple to appropriate format''' |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
213 return self._converter.formatdate(date, fmt) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
214 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
215 def formatdict(self, data, key=b'key', value=b'value', fmt=None, sep=b' '): |
29794
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29678
diff
changeset
|
216 '''convert dict or key-value pairs to appropriate dict format''' |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
217 return self._converter.formatdict(data, key, value, fmt, sep) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
218 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
219 def formatlist(self, data, name, fmt=None, sep=b' '): |
29676
c3a9cd78b151
formatter: add function to convert list to appropriate format (issue5217)
Yuya Nishihara <yuya@tcha.org>
parents:
29324
diff
changeset
|
220 '''convert iterable to appropriate list format''' |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
221 # name is mandatory argument for now, but it could be optional if |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
222 # we have default template keyword, e.g. {item} |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
223 return self._converter.formatlist(data, name, fmt, sep) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
224 |
31172
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31170
diff
changeset
|
225 def context(self, **ctxs): |
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31170
diff
changeset
|
226 '''insert context objects to be used to render template keywords''' |
33090
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32952
diff
changeset
|
227 ctxs = pycompat.byteskwargs(ctxs) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
228 assert all(k in {b'repo', b'ctx', b'fctx'} for k in ctxs) |
33090
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32952
diff
changeset
|
229 if self._converter.storecontext: |
39583
ee1e74ee037c
formatter: fill missing resources by formatter, not by resource mapper
Yuya Nishihara <yuya@tcha.org>
parents:
39582
diff
changeset
|
230 # populate missing resources in fctx -> ctx -> repo order |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
231 if b'fctx' in ctxs and b'ctx' not in ctxs: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
232 ctxs[b'ctx'] = ctxs[b'fctx'].changectx() |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
233 if b'ctx' in ctxs and b'repo' not in ctxs: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
234 ctxs[b'repo'] = ctxs[b'ctx'].repo() |
33090
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32952
diff
changeset
|
235 self._item.update(ctxs) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
236 |
38356
8221df643176
formatter: provide hint of referenced field names
Yuya Nishihara <yuya@tcha.org>
parents:
38285
diff
changeset
|
237 def datahint(self): |
8221df643176
formatter: provide hint of referenced field names
Yuya Nishihara <yuya@tcha.org>
parents:
38285
diff
changeset
|
238 '''set of field names to be referenced''' |
8221df643176
formatter: provide hint of referenced field names
Yuya Nishihara <yuya@tcha.org>
parents:
38285
diff
changeset
|
239 return set() |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
240 |
16134 | 241 def data(self, **data): |
242 '''insert data into item that's not shown in default output''' | |
32159
0fd15522a848
py3: use pycompat.byteskwargs to converts kwargs to bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31925
diff
changeset
|
243 data = pycompat.byteskwargs(data) |
17630
ff5ed1ecd43a
formatter: improve implementation of data method
David M. Carr <david@carrclan.us>
parents:
17597
diff
changeset
|
244 self._item.update(data) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
245 |
16134 | 246 def write(self, fields, deftext, *fielddata, **opts): |
247 '''do default text output while assigning data to item''' | |
26372
55de800937e0
formatter: verify number of arguments passed to write functions
Yuya Nishihara <yuya@tcha.org>
parents:
25950
diff
changeset
|
248 fieldkeys = fields.split() |
40140
46f9b1d2daf0
formatter: more details on assertion failure
Boris Feld <boris.feld@octobus.net>
parents:
39624
diff
changeset
|
249 assert len(fieldkeys) == len(fielddata), (fieldkeys, fielddata) |
26373
aa610ffad4e8
formatter: use dict.update() to set arguments passed to write functions
Yuya Nishihara <yuya@tcha.org>
parents:
26372
diff
changeset
|
250 self._item.update(zip(fieldkeys, fielddata)) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
251 |
17909
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
252 def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
253 '''do conditional write (primarily for plain formatter)''' |
26372
55de800937e0
formatter: verify number of arguments passed to write functions
Yuya Nishihara <yuya@tcha.org>
parents:
25950
diff
changeset
|
254 fieldkeys = fields.split() |
55de800937e0
formatter: verify number of arguments passed to write functions
Yuya Nishihara <yuya@tcha.org>
parents:
25950
diff
changeset
|
255 assert len(fieldkeys) == len(fielddata) |
26373
aa610ffad4e8
formatter: use dict.update() to set arguments passed to write functions
Yuya Nishihara <yuya@tcha.org>
parents:
26372
diff
changeset
|
256 self._item.update(zip(fieldkeys, fielddata)) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
257 |
16134 | 258 def plain(self, text, **opts): |
259 '''show raw text for non-templated mode''' | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
260 |
29949
e7cacb45c4be
formatter: introduce isplain() to replace (the inverse of) __nonzero__() (API)
Mathias De Maré <mathias.demare@gmail.com>
parents:
29882
diff
changeset
|
261 def isplain(self): |
e7cacb45c4be
formatter: introduce isplain() to replace (the inverse of) __nonzero__() (API)
Mathias De Maré <mathias.demare@gmail.com>
parents:
29882
diff
changeset
|
262 '''check for plain formatter usage''' |
e7cacb45c4be
formatter: introduce isplain() to replace (the inverse of) __nonzero__() (API)
Mathias De Maré <mathias.demare@gmail.com>
parents:
29882
diff
changeset
|
263 return False |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
264 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
265 def nested(self, field, tmpl=None, sep=b''): |
29837
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29836
diff
changeset
|
266 '''sub formatter to store nested data in the specified field''' |
37500
8bb3899a0f47
formatter: make nested items somewhat readable in template output
Yuya Nishihara <yuya@tcha.org>
parents:
37105
diff
changeset
|
267 data = [] |
8bb3899a0f47
formatter: make nested items somewhat readable in template output
Yuya Nishihara <yuya@tcha.org>
parents:
37105
diff
changeset
|
268 self._item[field] = self._converter.wrapnested(data, tmpl, sep) |
29837
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29836
diff
changeset
|
269 return _nestedformatter(self._ui, self._converter, data) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
270 |
16134 | 271 def end(self): |
272 '''end output for the formatter''' | |
273 if self._item is not None: | |
274 self._showitem() | |
275 | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
276 |
37597
d110167610db
formatter: carry opts to file-based formatters by basefm
Yuya Nishihara <yuya@tcha.org>
parents:
37596
diff
changeset
|
277 def nullformatter(ui, topic, opts): |
32581
e9bf3e132ea9
formatter: add nullformatter
Yuya Nishihara <yuya@tcha.org>
parents:
32580
diff
changeset
|
278 '''formatter that prints nothing''' |
37597
d110167610db
formatter: carry opts to file-based formatters by basefm
Yuya Nishihara <yuya@tcha.org>
parents:
37596
diff
changeset
|
279 return baseformatter(ui, topic, opts, converter=_nullconverter) |
32581
e9bf3e132ea9
formatter: add nullformatter
Yuya Nishihara <yuya@tcha.org>
parents:
32580
diff
changeset
|
280 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
281 |
29837
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29836
diff
changeset
|
282 class _nestedformatter(baseformatter): |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29836
diff
changeset
|
283 '''build sub items and store them in the parent formatter''' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
284 |
29837
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29836
diff
changeset
|
285 def __init__(self, ui, converter, data): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
286 baseformatter.__init__( |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
287 self, ui, topic=b'', opts={}, converter=converter |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
288 ) |
29837
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29836
diff
changeset
|
289 self._data = data |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
290 |
29837
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29836
diff
changeset
|
291 def _showitem(self): |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29836
diff
changeset
|
292 self._data.append(self._item) |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29836
diff
changeset
|
293 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
294 |
29794
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29678
diff
changeset
|
295 def _iteritems(data): |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29678
diff
changeset
|
296 '''iterate key-value pairs in stable order''' |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29678
diff
changeset
|
297 if isinstance(data, dict): |
43106
d783f945a701
py3: finish porting iteritems() to pycompat and remove source transformer
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43101
diff
changeset
|
298 return sorted(pycompat.iteritems(data)) |
29794
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29678
diff
changeset
|
299 return data |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29678
diff
changeset
|
300 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
301 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
302 class _plainconverter(object): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
303 '''convert non-primitive data types to text''' |
33090
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32952
diff
changeset
|
304 |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32952
diff
changeset
|
305 storecontext = False |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32952
diff
changeset
|
306 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
307 @staticmethod |
37500
8bb3899a0f47
formatter: make nested items somewhat readable in template output
Yuya Nishihara <yuya@tcha.org>
parents:
37105
diff
changeset
|
308 def wrapnested(data, tmpl, sep): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
309 raise error.ProgrammingError(b'plainformatter should never be nested') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
310 |
37500
8bb3899a0f47
formatter: make nested items somewhat readable in template output
Yuya Nishihara <yuya@tcha.org>
parents:
37105
diff
changeset
|
311 @staticmethod |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
312 def formatdate(date, fmt): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
313 '''stringify date tuple in the given format''' |
36607
c6061cadb400
util: extract all date-related utils in utils/dateutil module
Boris Feld <boris.feld@octobus.net>
parents:
36445
diff
changeset
|
314 return dateutil.datestr(date, fmt) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
315 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
316 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
317 def formatdict(data, key, value, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
318 '''stringify key-value pairs separated by sep''' |
36634
cafd0586876b
templater: byte-stringify dict/list values before passing to default format
Yuya Nishihara <yuya@tcha.org>
parents:
36633
diff
changeset
|
319 prefmt = pycompat.identity |
36633
034a07e60e98
templater: allow dynamically switching the default dict/list formatting
Yuya Nishihara <yuya@tcha.org>
parents:
36607
diff
changeset
|
320 if fmt is None: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
321 fmt = b'%s=%s' |
36634
cafd0586876b
templater: byte-stringify dict/list values before passing to default format
Yuya Nishihara <yuya@tcha.org>
parents:
36633
diff
changeset
|
322 prefmt = pycompat.bytestr |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
323 return sep.join( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
324 fmt % (prefmt(k), prefmt(v)) for k, v in _iteritems(data) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
325 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
326 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
327 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
328 def formatlist(data, name, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
329 '''stringify iterable separated by sep''' |
36634
cafd0586876b
templater: byte-stringify dict/list values before passing to default format
Yuya Nishihara <yuya@tcha.org>
parents:
36633
diff
changeset
|
330 prefmt = pycompat.identity |
36633
034a07e60e98
templater: allow dynamically switching the default dict/list formatting
Yuya Nishihara <yuya@tcha.org>
parents:
36607
diff
changeset
|
331 if fmt is None: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
332 fmt = b'%s' |
36634
cafd0586876b
templater: byte-stringify dict/list values before passing to default format
Yuya Nishihara <yuya@tcha.org>
parents:
36633
diff
changeset
|
333 prefmt = pycompat.bytestr |
cafd0586876b
templater: byte-stringify dict/list values before passing to default format
Yuya Nishihara <yuya@tcha.org>
parents:
36633
diff
changeset
|
334 return sep.join(fmt % prefmt(e) for e in data) |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
335 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
336 |
16134 | 337 class plainformatter(baseformatter): |
338 '''the default text output scheme''' | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
339 |
32579
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
340 def __init__(self, ui, out, topic, opts): |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
341 baseformatter.__init__(self, ui, topic, opts, _plainconverter) |
22701
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
342 if ui.debugflag: |
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
343 self.hexfunc = hex |
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
344 else: |
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
345 self.hexfunc = short |
32579
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
346 if ui is out: |
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
347 self._write = ui.write |
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
348 else: |
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
349 self._write = lambda s, **opts: out.write(s) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
350 |
16134 | 351 def startitem(self): |
352 pass | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
353 |
16134 | 354 def data(self, **data): |
355 pass | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
356 |
16134 | 357 def write(self, fields, deftext, *fielddata, **opts): |
32579
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
358 self._write(deftext % fielddata, **opts) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
359 |
17909
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
360 def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
361 '''do conditional write''' |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
362 if cond: |
32579
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
363 self._write(deftext % fielddata, **opts) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
364 |
16134 | 365 def plain(self, text, **opts): |
32579
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
366 self._write(text, **opts) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
367 |
29949
e7cacb45c4be
formatter: introduce isplain() to replace (the inverse of) __nonzero__() (API)
Mathias De Maré <mathias.demare@gmail.com>
parents:
29882
diff
changeset
|
368 def isplain(self): |
e7cacb45c4be
formatter: introduce isplain() to replace (the inverse of) __nonzero__() (API)
Mathias De Maré <mathias.demare@gmail.com>
parents:
29882
diff
changeset
|
369 return True |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
370 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
371 def nested(self, field, tmpl=None, sep=b''): |
29837
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29836
diff
changeset
|
372 # nested data will be directly written to ui |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29836
diff
changeset
|
373 return self |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
374 |
16134 | 375 def end(self): |
376 pass | |
377 | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
378 |
16134 | 379 class debugformatter(baseformatter): |
31182
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31172
diff
changeset
|
380 def __init__(self, ui, out, topic, opts): |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
381 baseformatter.__init__(self, ui, topic, opts, _nullconverter) |
31182
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31172
diff
changeset
|
382 self._out = out |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
383 self._out.write(b"%s = [\n" % self._topic) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
384 |
16134 | 385 def _showitem(self): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
386 self._out.write( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
387 b' %s,\n' % stringutil.pprint(self._item, indent=4, level=1) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
388 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
389 |
16134 | 390 def end(self): |
391 baseformatter.end(self) | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
392 self._out.write(b"]\n") |
16134 | 393 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
394 |
22430
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
395 class pickleformatter(baseformatter): |
31182
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31172
diff
changeset
|
396 def __init__(self, ui, out, topic, opts): |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
397 baseformatter.__init__(self, ui, topic, opts, _nullconverter) |
31182
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31172
diff
changeset
|
398 self._out = out |
22430
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
399 self._data = [] |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
400 |
22430
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
401 def _showitem(self): |
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
402 self._data.append(self._item) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
403 |
22430
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
404 def end(self): |
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
405 baseformatter.end(self) |
31182
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31172
diff
changeset
|
406 self._out.write(pickle.dumps(self._data)) |
22430
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
407 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
408 |
41996
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
409 class cborformatter(baseformatter): |
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
410 '''serialize items as an indefinite-length CBOR array''' |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
411 |
41996
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
412 def __init__(self, ui, out, topic, opts): |
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
413 baseformatter.__init__(self, ui, topic, opts, _nullconverter) |
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
414 self._out = out |
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
415 self._out.write(cborutil.BEGIN_INDEFINITE_ARRAY) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
416 |
41996
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
417 def _showitem(self): |
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
418 self._out.write(b''.join(cborutil.streamencode(self._item))) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
419 |
41996
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
420 def end(self): |
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
421 baseformatter.end(self) |
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
422 self._out.write(cborutil.BREAK) |
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
423 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
424 |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
425 class jsonformatter(baseformatter): |
46769
67a2ecea8bd9
debugdiscovery: also integrate the discovery output in the json one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45320
diff
changeset
|
426 |
67a2ecea8bd9
debugdiscovery: also integrate the discovery output in the json one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45320
diff
changeset
|
427 strict_format = True |
67a2ecea8bd9
debugdiscovery: also integrate the discovery output in the json one
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
45320
diff
changeset
|
428 |
31182
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31172
diff
changeset
|
429 def __init__(self, ui, out, topic, opts): |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
430 baseformatter.__init__(self, ui, topic, opts, _nullconverter) |
31182
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31172
diff
changeset
|
431 self._out = out |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
432 self._out.write(b"[") |
31298
59d09565ac77
formatter: set _first on formatter, not ui
Martin von Zweigbergk <martinvonz@google.com>
parents:
31182
diff
changeset
|
433 self._first = True |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
434 |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
435 def _showitem(self): |
31298
59d09565ac77
formatter: set _first on formatter, not ui
Martin von Zweigbergk <martinvonz@google.com>
parents:
31182
diff
changeset
|
436 if self._first: |
59d09565ac77
formatter: set _first on formatter, not ui
Martin von Zweigbergk <martinvonz@google.com>
parents:
31182
diff
changeset
|
437 self._first = False |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
438 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
439 self._out.write(b",") |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
440 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
441 self._out.write(b"\n {\n") |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
442 first = True |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
443 for k, v in sorted(self._item.items()): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
444 if first: |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
445 first = False |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
446 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
447 self._out.write(b",\n") |
31782
654e9a1c8a6c
formatter: use templatefilters.json()
Yuya Nishihara <yuya@tcha.org>
parents:
31396
diff
changeset
|
448 u = templatefilters.json(v, paranoid=False) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
449 self._out.write(b' "%s": %s' % (k, u)) |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
450 self._out.write(b"\n }") |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
451 |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
452 def end(self): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
453 baseformatter.end(self) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
454 self._out.write(b"\n]\n") |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
455 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
456 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
457 class _templateconverter(object): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
458 '''convert non-primitive data types to be processed by templater''' |
33090
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32952
diff
changeset
|
459 |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32952
diff
changeset
|
460 storecontext = True |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32952
diff
changeset
|
461 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
462 @staticmethod |
37500
8bb3899a0f47
formatter: make nested items somewhat readable in template output
Yuya Nishihara <yuya@tcha.org>
parents:
37105
diff
changeset
|
463 def wrapnested(data, tmpl, sep): |
8bb3899a0f47
formatter: make nested items somewhat readable in template output
Yuya Nishihara <yuya@tcha.org>
parents:
37105
diff
changeset
|
464 '''wrap nested data by templatable type''' |
8bb3899a0f47
formatter: make nested items somewhat readable in template output
Yuya Nishihara <yuya@tcha.org>
parents:
37105
diff
changeset
|
465 return templateutil.mappinglist(data, tmpl=tmpl, sep=sep) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
466 |
37500
8bb3899a0f47
formatter: make nested items somewhat readable in template output
Yuya Nishihara <yuya@tcha.org>
parents:
37105
diff
changeset
|
467 @staticmethod |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
468 def formatdate(date, fmt): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
469 '''return date tuple''' |
38285
8d6109b49b31
templater: introduce a wrapper for date tuple (BC)
Yuya Nishihara <yuya@tcha.org>
parents:
37839
diff
changeset
|
470 return templateutil.date(date) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
471 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
472 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
473 def formatdict(data, key, value, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
474 '''build object that can be evaluated as either plain string or dict''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
475 data = util.sortdict(_iteritems(data)) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
476 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
477 def f(): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
478 yield _plainconverter.formatdict(data, key, value, fmt, sep) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
479 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
480 return templateutil.hybriddict( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
481 data, key=key, value=value, fmt=fmt, gen=f |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
482 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
483 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
484 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
485 def formatlist(data, name, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
486 '''build object that can be evaluated as either plain string or list''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
487 data = list(data) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
488 |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
489 def f(): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
490 yield _plainconverter.formatlist(data, name, fmt, sep) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
491 |
36921
32f9b7e3f056
templater: move hybrid class and functions to templateutil module
Yuya Nishihara <yuya@tcha.org>
parents:
36920
diff
changeset
|
492 return templateutil.hybridlist(data, name=name, fmt=fmt, gen=f) |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
493 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
494 |
25513
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
495 class templateformatter(baseformatter): |
43101
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
496 def __init__(self, ui, out, topic, opts, spec, overridetemplates=None): |
29836
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29794
diff
changeset
|
497 baseformatter.__init__(self, ui, topic, opts, _templateconverter) |
31182
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31172
diff
changeset
|
498 self._out = out |
32841
883adaea9e80
formatter: render template specified by templatespec tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32840
diff
changeset
|
499 self._tref = spec.ref |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
500 self._t = loadtemplater( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
501 ui, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
502 spec, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
503 defaults=templatekw.keywords, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
504 resources=templateresources(ui), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
505 cache=templatekw.defaulttempl, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
506 ) |
43101
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
507 if overridetemplates: |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
508 self._t.cache.update(overridetemplates) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
509 self._parts = templatepartsmap( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
510 spec, self._t, [b'docheader', b'docfooter', b'separator'] |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
511 ) |
31807
e6eb86b154c5
templater: provide loop counter as "index" keyword
Yuya Nishihara <yuya@tcha.org>
parents:
31805
diff
changeset
|
512 self._counter = itertools.count() |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
513 self._renderitem(b'docheader', {}) |
32949
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
514 |
25513
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
515 def _showitem(self): |
32948
12a0794fa2e3
formatter: extract helper function to render template
Yuya Nishihara <yuya@tcha.org>
parents:
32897
diff
changeset
|
516 item = self._item.copy() |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
517 item[b'index'] = index = next(self._counter) |
32950
5100ce217dfa
formatter: add support for separator template
Yuya Nishihara <yuya@tcha.org>
parents:
32949
diff
changeset
|
518 if index > 0: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
519 self._renderitem(b'separator', {}) |
32948
12a0794fa2e3
formatter: extract helper function to render template
Yuya Nishihara <yuya@tcha.org>
parents:
32897
diff
changeset
|
520 self._renderitem(self._tref, item) |
12a0794fa2e3
formatter: extract helper function to render template
Yuya Nishihara <yuya@tcha.org>
parents:
32897
diff
changeset
|
521 |
32949
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
522 def _renderitem(self, part, item): |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
523 if part not in self._parts: |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
524 return |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
525 ref = self._parts[part] |
43337
7e20b705da5b
formatter: fix handling of None value in templater mapping
Yuya Nishihara <yuya@tcha.org>
parents:
43335
diff
changeset
|
526 # None can't be put in the mapping dict since it means <unset> |
7e20b705da5b
formatter: fix handling of None value in templater mapping
Yuya Nishihara <yuya@tcha.org>
parents:
43335
diff
changeset
|
527 for k, v in item.items(): |
7e20b705da5b
formatter: fix handling of None value in templater mapping
Yuya Nishihara <yuya@tcha.org>
parents:
43335
diff
changeset
|
528 if v is None: |
7e20b705da5b
formatter: fix handling of None value in templater mapping
Yuya Nishihara <yuya@tcha.org>
parents:
43335
diff
changeset
|
529 item[k] = templateutil.wrappedvalue(v) |
37103
be3f33f5e232
templater: switch 'revcache' based on new mapping items
Yuya Nishihara <yuya@tcha.org>
parents:
37102
diff
changeset
|
530 self._out.write(self._t.render(ref, item)) |
25513
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
531 |
38356
8221df643176
formatter: provide hint of referenced field names
Yuya Nishihara <yuya@tcha.org>
parents:
38285
diff
changeset
|
532 @util.propertycache |
8221df643176
formatter: provide hint of referenced field names
Yuya Nishihara <yuya@tcha.org>
parents:
38285
diff
changeset
|
533 def _symbolsused(self): |
38446
5b04a0c30f3f
formatter: look for template symbols from the associated name
Yuya Nishihara <yuya@tcha.org>
parents:
38429
diff
changeset
|
534 return self._t.symbolsused(self._tref) |
38356
8221df643176
formatter: provide hint of referenced field names
Yuya Nishihara <yuya@tcha.org>
parents:
38285
diff
changeset
|
535 |
8221df643176
formatter: provide hint of referenced field names
Yuya Nishihara <yuya@tcha.org>
parents:
38285
diff
changeset
|
536 def datahint(self): |
8221df643176
formatter: provide hint of referenced field names
Yuya Nishihara <yuya@tcha.org>
parents:
38285
diff
changeset
|
537 '''set of field names to be referenced from the template''' |
8221df643176
formatter: provide hint of referenced field names
Yuya Nishihara <yuya@tcha.org>
parents:
38285
diff
changeset
|
538 return self._symbolsused[0] |
8221df643176
formatter: provide hint of referenced field names
Yuya Nishihara <yuya@tcha.org>
parents:
38285
diff
changeset
|
539 |
32949
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
540 def end(self): |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
541 baseformatter.end(self) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
542 self._renderitem(b'docfooter', {}) |
32949
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
543 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
544 |
37839
395571419274
formatter: ditch namedtuple in favor of attr
Yuya Nishihara <yuya@tcha.org>
parents:
37770
diff
changeset
|
545 @attr.s(frozen=True) |
395571419274
formatter: ditch namedtuple in favor of attr
Yuya Nishihara <yuya@tcha.org>
parents:
37770
diff
changeset
|
546 class templatespec(object): |
395571419274
formatter: ditch namedtuple in favor of attr
Yuya Nishihara <yuya@tcha.org>
parents:
37770
diff
changeset
|
547 ref = attr.ib() |
395571419274
formatter: ditch namedtuple in favor of attr
Yuya Nishihara <yuya@tcha.org>
parents:
37770
diff
changeset
|
548 tmpl = attr.ib() |
395571419274
formatter: ditch namedtuple in favor of attr
Yuya Nishihara <yuya@tcha.org>
parents:
37770
diff
changeset
|
549 mapfile = attr.ib() |
43101
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
550 refargs = attr.ib(default=None) |
45310
f3481e4fcc3a
templater: pass opened file-like object to templatespec
Martin von Zweigbergk <martinvonz@google.com>
parents:
45309
diff
changeset
|
551 fp = attr.ib(default=None) |
32838
615ec3f14aa9
formatter: wrap (tmpl, mapfile) by named tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32835
diff
changeset
|
552 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
553 |
45264
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
554 def empty_templatespec(): |
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
555 return templatespec(None, None, None) |
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
556 |
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
557 |
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
558 def reference_templatespec(ref, refargs=None): |
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
559 return templatespec(ref, None, None, refargs) |
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
560 |
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
561 |
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
562 def literal_templatespec(tmpl): |
45266
215f08c8006c
templatespec: move check for non-unicode to lower-level function
Martin von Zweigbergk <martinvonz@google.com>
parents:
45264
diff
changeset
|
563 if pycompat.ispy3: |
215f08c8006c
templatespec: move check for non-unicode to lower-level function
Martin von Zweigbergk <martinvonz@google.com>
parents:
45264
diff
changeset
|
564 assert not isinstance(tmpl, str), b'tmpl must not be a str' |
45264
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
565 return templatespec(b'', tmpl, None) |
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
566 |
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
567 |
45310
f3481e4fcc3a
templater: pass opened file-like object to templatespec
Martin von Zweigbergk <martinvonz@google.com>
parents:
45309
diff
changeset
|
568 def mapfile_templatespec(topic, mapfile, fp=None): |
f3481e4fcc3a
templater: pass opened file-like object to templatespec
Martin von Zweigbergk <martinvonz@google.com>
parents:
45309
diff
changeset
|
569 return templatespec(topic, None, mapfile, fp=fp) |
45264
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
570 |
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
571 |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
572 def lookuptemplate(ui, topic, tmpl): |
32835
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32833
diff
changeset
|
573 """Find the template matching the given -T/--template spec 'tmpl' |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32833
diff
changeset
|
574 |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32833
diff
changeset
|
575 'tmpl' can be any of the following: |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32833
diff
changeset
|
576 |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32833
diff
changeset
|
577 - a literal template (e.g. '{rev}') |
43100
90b9a7e06c2c
formatter: parse name of built-in formatter templates in standard way
Yuya Nishihara <yuya@tcha.org>
parents:
43099
diff
changeset
|
578 - a reference to built-in template (i.e. formatter) |
32835
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32833
diff
changeset
|
579 - a map-file name or path (e.g. 'changelog') |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32833
diff
changeset
|
580 - a reference to [templates] in config file |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32833
diff
changeset
|
581 - a path to raw template file |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32833
diff
changeset
|
582 |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32833
diff
changeset
|
583 A map file defines a stand-alone template environment. If a map file |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32833
diff
changeset
|
584 selected, all templates defined in the file will be loaded, and the |
34715
f17a0e18c47e
templater: load aliases from [templatealias] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34425
diff
changeset
|
585 template matching the given topic will be rendered. Aliases won't be |
f17a0e18c47e
templater: load aliases from [templatealias] section in map file
Yuya Nishihara <yuya@tcha.org>
parents:
34425
diff
changeset
|
586 loaded from user config, but from the map file. |
32875
c8f2cf18b82e
formatter: load templates section like a map file
Yuya Nishihara <yuya@tcha.org>
parents:
32873
diff
changeset
|
587 |
c8f2cf18b82e
formatter: load templates section like a map file
Yuya Nishihara <yuya@tcha.org>
parents:
32873
diff
changeset
|
588 If no map file selected, all templates in [templates] section will be |
c8f2cf18b82e
formatter: load templates section like a map file
Yuya Nishihara <yuya@tcha.org>
parents:
32873
diff
changeset
|
589 available as well as aliases in [templatealias]. |
32835
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32833
diff
changeset
|
590 """ |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32833
diff
changeset
|
591 |
43100
90b9a7e06c2c
formatter: parse name of built-in formatter templates in standard way
Yuya Nishihara <yuya@tcha.org>
parents:
43099
diff
changeset
|
592 if not tmpl: |
45264
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
593 return empty_templatespec() |
43100
90b9a7e06c2c
formatter: parse name of built-in formatter templates in standard way
Yuya Nishihara <yuya@tcha.org>
parents:
43099
diff
changeset
|
594 |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
595 # looks like a literal template? |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
596 if b'{' in tmpl: |
45264
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
597 return literal_templatespec(tmpl) |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
598 |
43100
90b9a7e06c2c
formatter: parse name of built-in formatter templates in standard way
Yuya Nishihara <yuya@tcha.org>
parents:
43099
diff
changeset
|
599 # a reference to built-in (formatter) template |
90b9a7e06c2c
formatter: parse name of built-in formatter templates in standard way
Yuya Nishihara <yuya@tcha.org>
parents:
43099
diff
changeset
|
600 if tmpl in {b'cbor', b'json', b'pickle', b'debug'}: |
45264
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
601 return reference_templatespec(tmpl) |
43100
90b9a7e06c2c
formatter: parse name of built-in formatter templates in standard way
Yuya Nishihara <yuya@tcha.org>
parents:
43099
diff
changeset
|
602 |
43101
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
603 # a function-style reference to built-in template |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
604 func, fsep, ftail = tmpl.partition(b'(') |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
605 if func in {b'cbor', b'json'} and fsep and ftail.endswith(b')'): |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
606 templater.parseexpr(tmpl) # make sure syntax errors are confined |
45264
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
607 return reference_templatespec(func, refargs=ftail[:-1]) |
43101
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
608 |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
609 # perhaps a stock style? |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
610 if not os.path.split(tmpl)[0]: |
45320
4aa484efc926
templater: add exception-raising version of open_template()
Martin von Zweigbergk <martinvonz@google.com>
parents:
45310
diff
changeset
|
611 (mapname, fp) = templater.try_open_template( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
612 b'map-cmdline.' + tmpl |
45320
4aa484efc926
templater: add exception-raising version of open_template()
Martin von Zweigbergk <martinvonz@google.com>
parents:
45310
diff
changeset
|
613 ) or templater.try_open_template(tmpl) |
45260
653b2a439412
formatter: remove now-unnecessary check for file-ness
Martin von Zweigbergk <martinvonz@google.com>
parents:
43337
diff
changeset
|
614 if mapname: |
45310
f3481e4fcc3a
templater: pass opened file-like object to templatespec
Martin von Zweigbergk <martinvonz@google.com>
parents:
45309
diff
changeset
|
615 return mapfile_templatespec(topic, mapname, fp) |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
616 |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
617 # perhaps it's a reference to [templates] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
618 if ui.config(b'templates', tmpl): |
45264
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
619 return reference_templatespec(tmpl) |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
620 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
621 if tmpl == b'list': |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
622 ui.write(_(b"available styles: %s\n") % templater.stylelist()) |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
623 raise error.Abort(_(b"specify a template")) |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
624 |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
625 # perhaps it's a path to a map or a template |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
626 if (b'/' in tmpl or b'\\' in tmpl) and os.path.isfile(tmpl): |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
627 # is it a mapfile for a style? |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
628 if os.path.basename(tmpl).startswith(b"map-"): |
45264
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
629 return mapfile_templatespec(topic, os.path.realpath(tmpl)) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
630 with util.posixfile(tmpl, b'rb') as f: |
32828
526f9f12f707
formatter: close raw template file explicitly
Yuya Nishihara <yuya@tcha.org>
parents:
32581
diff
changeset
|
631 tmpl = f.read() |
45264
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
632 return literal_templatespec(tmpl) |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
633 |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
634 # constant string? |
45264
8cce9f77ca73
templatespec: create a factory function for each type there is
Martin von Zweigbergk <martinvonz@google.com>
parents:
45262
diff
changeset
|
635 return literal_templatespec(tmpl) |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
636 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
637 |
32949
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
638 def templatepartsmap(spec, t, partnames): |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
639 """Create a mapping of {part: ref}""" |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
640 partsmap = {spec.ref: spec.ref} # initial ref must exist in t |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
641 if spec.mapfile: |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
642 partsmap.update((p, p) for p in partnames if p in t) |
32952
61b60b28c381
formatter: add support for parts map of [templates] section
Yuya Nishihara <yuya@tcha.org>
parents:
32950
diff
changeset
|
643 elif spec.ref: |
61b60b28c381
formatter: add support for parts map of [templates] section
Yuya Nishihara <yuya@tcha.org>
parents:
32950
diff
changeset
|
644 for part in partnames: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
645 ref = b'%s:%s' % (spec.ref, part) # select config sub-section |
32952
61b60b28c381
formatter: add support for parts map of [templates] section
Yuya Nishihara <yuya@tcha.org>
parents:
32950
diff
changeset
|
646 if ref in t: |
61b60b28c381
formatter: add support for parts map of [templates] section
Yuya Nishihara <yuya@tcha.org>
parents:
32950
diff
changeset
|
647 partsmap[part] = ref |
32949
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
648 return partsmap |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32948
diff
changeset
|
649 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
650 |
35483
817a3d20dd01
templater: register keywords to defaults table
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
651 def loadtemplater(ui, spec, defaults=None, resources=None, cache=None): |
32832
11e667a8fcba
formatter: factor out function to create templater from literal or map file
Yuya Nishihara <yuya@tcha.org>
parents:
32830
diff
changeset
|
652 """Create a templater from either a literal template or loading from |
11e667a8fcba
formatter: factor out function to create templater from literal or map file
Yuya Nishihara <yuya@tcha.org>
parents:
32830
diff
changeset
|
653 a map file""" |
32838
615ec3f14aa9
formatter: wrap (tmpl, mapfile) by named tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32835
diff
changeset
|
654 assert not (spec.tmpl and spec.mapfile) |
615ec3f14aa9
formatter: wrap (tmpl, mapfile) by named tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32835
diff
changeset
|
655 if spec.mapfile: |
45262
22eafb16f1c5
formatter: inline a variable assigned from `templater.templater.frommapfile`
Martin von Zweigbergk <martinvonz@google.com>
parents:
45260
diff
changeset
|
656 return templater.templater.frommapfile( |
45310
f3481e4fcc3a
templater: pass opened file-like object to templatespec
Martin von Zweigbergk <martinvonz@google.com>
parents:
45309
diff
changeset
|
657 spec.mapfile, |
f3481e4fcc3a
templater: pass opened file-like object to templatespec
Martin von Zweigbergk <martinvonz@google.com>
parents:
45309
diff
changeset
|
658 spec.fp, |
f3481e4fcc3a
templater: pass opened file-like object to templatespec
Martin von Zweigbergk <martinvonz@google.com>
parents:
45309
diff
changeset
|
659 defaults=defaults, |
f3481e4fcc3a
templater: pass opened file-like object to templatespec
Martin von Zweigbergk <martinvonz@google.com>
parents:
45309
diff
changeset
|
660 resources=resources, |
f3481e4fcc3a
templater: pass opened file-like object to templatespec
Martin von Zweigbergk <martinvonz@google.com>
parents:
45309
diff
changeset
|
661 cache=cache, |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
662 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
663 return maketemplater( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
664 ui, spec.tmpl, defaults=defaults, resources=resources, cache=cache |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
665 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
666 |
28955
78759f78a44e
templater: factor out function that creates templater from string template
Yuya Nishihara <yuya@tcha.org>
parents:
28954
diff
changeset
|
667 |
35483
817a3d20dd01
templater: register keywords to defaults table
Yuya Nishihara <yuya@tcha.org>
parents:
35470
diff
changeset
|
668 def maketemplater(ui, tmpl, defaults=None, resources=None, cache=None): |
28955
78759f78a44e
templater: factor out function that creates templater from string template
Yuya Nishihara <yuya@tcha.org>
parents:
28954
diff
changeset
|
669 """Create a templater from a string template 'tmpl'""" |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
670 aliases = ui.configitems(b'templatealias') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
671 t = templater.templater( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
672 defaults=defaults, resources=resources, cache=cache, aliases=aliases |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
673 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
674 t.cache.update( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
675 (k, templater.unquotestring(v)) for k, v in ui.configitems(b'templates') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
676 ) |
25512
8463433c2689
formatter: add a method to build a full templater from a -T option
Matt Mackall <mpm@selenic.com>
parents:
25511
diff
changeset
|
677 if tmpl: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
678 t.cache[b''] = tmpl |
25512
8463433c2689
formatter: add a method to build a full templater from a -T option
Matt Mackall <mpm@selenic.com>
parents:
25511
diff
changeset
|
679 return t |
8463433c2689
formatter: add a method to build a full templater from a -T option
Matt Mackall <mpm@selenic.com>
parents:
25511
diff
changeset
|
680 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
681 |
39586
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
682 # marker to denote a resource to be loaded on demand based on mapping values |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
683 # (e.g. (ctx, path) -> fctx) |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
684 _placeholder = object() |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
685 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
686 |
37073
44757e6dad93
templater: introduce resourcemapper class
Yuya Nishihara <yuya@tcha.org>
parents:
36989
diff
changeset
|
687 class templateresources(templater.resourcemapper): |
44757e6dad93
templater: introduce resourcemapper class
Yuya Nishihara <yuya@tcha.org>
parents:
36989
diff
changeset
|
688 """Resource mapper designed for the default templatekw and function""" |
44757e6dad93
templater: introduce resourcemapper class
Yuya Nishihara <yuya@tcha.org>
parents:
36989
diff
changeset
|
689 |
44757e6dad93
templater: introduce resourcemapper class
Yuya Nishihara <yuya@tcha.org>
parents:
36989
diff
changeset
|
690 def __init__(self, ui, repo=None): |
44757e6dad93
templater: introduce resourcemapper class
Yuya Nishihara <yuya@tcha.org>
parents:
36989
diff
changeset
|
691 self._resmap = { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
692 b'cache': {}, # for templatekw/funcs to store reusable data |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
693 b'repo': repo, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
694 b'ui': ui, |
37073
44757e6dad93
templater: introduce resourcemapper class
Yuya Nishihara <yuya@tcha.org>
parents:
36989
diff
changeset
|
695 } |
35469
f1c54d003327
templater: move repo, ui and cache to per-engine resources
Yuya Nishihara <yuya@tcha.org>
parents:
35468
diff
changeset
|
696 |
39582
28f974d83c0a
templater: remove unused context argument from most resourcemapper functions
Yuya Nishihara <yuya@tcha.org>
parents:
38446
diff
changeset
|
697 def availablekeys(self, mapping): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
698 return { |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
699 k for k in self.knownkeys() if self._getsome(mapping, k) is not None |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
700 } |
37075
46859b437697
templater: drop symbols which should be overridden by new 'ctx' (issue5612)
Yuya Nishihara <yuya@tcha.org>
parents:
37073
diff
changeset
|
701 |
37073
44757e6dad93
templater: introduce resourcemapper class
Yuya Nishihara <yuya@tcha.org>
parents:
36989
diff
changeset
|
702 def knownkeys(self): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
703 return {b'cache', b'ctx', b'fctx', b'repo', b'revcache', b'ui'} |
37073
44757e6dad93
templater: introduce resourcemapper class
Yuya Nishihara <yuya@tcha.org>
parents:
36989
diff
changeset
|
704 |
39582
28f974d83c0a
templater: remove unused context argument from most resourcemapper functions
Yuya Nishihara <yuya@tcha.org>
parents:
38446
diff
changeset
|
705 def lookup(self, mapping, key): |
39584
109b2c2d9942
formatter: inline _gettermap and _knownkeys
Yuya Nishihara <yuya@tcha.org>
parents:
39583
diff
changeset
|
706 if key not in self.knownkeys(): |
37073
44757e6dad93
templater: introduce resourcemapper class
Yuya Nishihara <yuya@tcha.org>
parents:
36989
diff
changeset
|
707 return None |
39586
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
708 v = self._getsome(mapping, key) |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
709 if v is _placeholder: |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
710 v = mapping[key] = self._loadermap[key](self, mapping) |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
711 return v |
37073
44757e6dad93
templater: introduce resourcemapper class
Yuya Nishihara <yuya@tcha.org>
parents:
36989
diff
changeset
|
712 |
37102
638a241202a3
templater: add hook point to populate additional mapping items
Yuya Nishihara <yuya@tcha.org>
parents:
37075
diff
changeset
|
713 def populatemap(self, context, origmapping, newmapping): |
37103
be3f33f5e232
templater: switch 'revcache' based on new mapping items
Yuya Nishihara <yuya@tcha.org>
parents:
37102
diff
changeset
|
714 mapping = {} |
39585
990a0b071ea5
formatter: factor out function that detects node change and document it
Yuya Nishihara <yuya@tcha.org>
parents:
39584
diff
changeset
|
715 if self._hasnodespec(newmapping): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
716 mapping[b'revcache'] = {} # per-ctx cache |
39585
990a0b071ea5
formatter: factor out function that detects node change and document it
Yuya Nishihara <yuya@tcha.org>
parents:
39584
diff
changeset
|
717 if self._hasnodespec(origmapping) and self._hasnodespec(newmapping): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
718 orignode = templateutil.runsymbol(context, origmapping, b'node') |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
719 mapping[b'originalnode'] = orignode |
39623
34ecc0a09c76
formatter: populate ctx from repo and node value
Yuya Nishihara <yuya@tcha.org>
parents:
39586
diff
changeset
|
720 # put marker to override 'ctx'/'fctx' in mapping if any, and flag |
39586
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
721 # its existence to be reported by availablekeys() |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
722 if b'ctx' not in newmapping and self._hasliteral(newmapping, b'node'): |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
723 mapping[b'ctx'] = _placeholder |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
724 if b'fctx' not in newmapping and self._hasliteral(newmapping, b'path'): |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
725 mapping[b'fctx'] = _placeholder |
37103
be3f33f5e232
templater: switch 'revcache' based on new mapping items
Yuya Nishihara <yuya@tcha.org>
parents:
37102
diff
changeset
|
726 return mapping |
37102
638a241202a3
templater: add hook point to populate additional mapping items
Yuya Nishihara <yuya@tcha.org>
parents:
37075
diff
changeset
|
727 |
39582
28f974d83c0a
templater: remove unused context argument from most resourcemapper functions
Yuya Nishihara <yuya@tcha.org>
parents:
38446
diff
changeset
|
728 def _getsome(self, mapping, key): |
36983
036e4483d3a1
templater: process mapping dict by resource callables
Yuya Nishihara <yuya@tcha.org>
parents:
36982
diff
changeset
|
729 v = mapping.get(key) |
036e4483d3a1
templater: process mapping dict by resource callables
Yuya Nishihara <yuya@tcha.org>
parents:
36982
diff
changeset
|
730 if v is not None: |
036e4483d3a1
templater: process mapping dict by resource callables
Yuya Nishihara <yuya@tcha.org>
parents:
36982
diff
changeset
|
731 return v |
37073
44757e6dad93
templater: introduce resourcemapper class
Yuya Nishihara <yuya@tcha.org>
parents:
36989
diff
changeset
|
732 return self._resmap.get(key) |
36982
255f635c3204
templater: convert resources to a table of callables for future extension
Yuya Nishihara <yuya@tcha.org>
parents:
36921
diff
changeset
|
733 |
39586
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
734 def _hasliteral(self, mapping, key): |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
735 """Test if a literal value is set or unset in the given mapping""" |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
736 return key in mapping and not callable(mapping[key]) |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
737 |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
738 def _getliteral(self, mapping, key): |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
739 """Return value of the given name if it is a literal""" |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
740 v = mapping.get(key) |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
741 if callable(v): |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
742 return None |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
743 return v |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
744 |
39585
990a0b071ea5
formatter: factor out function that detects node change and document it
Yuya Nishihara <yuya@tcha.org>
parents:
39584
diff
changeset
|
745 def _hasnodespec(self, mapping): |
990a0b071ea5
formatter: factor out function that detects node change and document it
Yuya Nishihara <yuya@tcha.org>
parents:
39584
diff
changeset
|
746 """Test if context revision is set or unset in the given mapping""" |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
747 return b'node' in mapping or b'ctx' in mapping |
36984
939e0983c1d9
formatter: unblock storing fctx as a template resource
Yuya Nishihara <yuya@tcha.org>
parents:
36983
diff
changeset
|
748 |
39623
34ecc0a09c76
formatter: populate ctx from repo and node value
Yuya Nishihara <yuya@tcha.org>
parents:
39586
diff
changeset
|
749 def _loadctx(self, mapping): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
750 repo = self._getsome(mapping, b'repo') |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
751 node = self._getliteral(mapping, b'node') |
39623
34ecc0a09c76
formatter: populate ctx from repo and node value
Yuya Nishihara <yuya@tcha.org>
parents:
39586
diff
changeset
|
752 if repo is None or node is None: |
34ecc0a09c76
formatter: populate ctx from repo and node value
Yuya Nishihara <yuya@tcha.org>
parents:
39586
diff
changeset
|
753 return |
34ecc0a09c76
formatter: populate ctx from repo and node value
Yuya Nishihara <yuya@tcha.org>
parents:
39586
diff
changeset
|
754 try: |
34ecc0a09c76
formatter: populate ctx from repo and node value
Yuya Nishihara <yuya@tcha.org>
parents:
39586
diff
changeset
|
755 return repo[node] |
34ecc0a09c76
formatter: populate ctx from repo and node value
Yuya Nishihara <yuya@tcha.org>
parents:
39586
diff
changeset
|
756 except error.RepoLookupError: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
757 return None # maybe hidden/non-existent node |
39623
34ecc0a09c76
formatter: populate ctx from repo and node value
Yuya Nishihara <yuya@tcha.org>
parents:
39586
diff
changeset
|
758 |
39586
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
759 def _loadfctx(self, mapping): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
760 ctx = self._getsome(mapping, b'ctx') |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
761 path = self._getliteral(mapping, b'path') |
39586
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
762 if ctx is None or path is None: |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
763 return None |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
764 try: |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
765 return ctx[path] |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
766 except error.LookupError: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
767 return None # maybe removed file? |
39586
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
768 |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
769 _loadermap = { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
770 b'ctx': _loadctx, |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
771 b'fctx': _loadfctx, |
39586
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
772 } |
b1239aeef4d9
formatter: populate fctx from ctx and path value
Yuya Nishihara <yuya@tcha.org>
parents:
39585
diff
changeset
|
773 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
774 |
43101
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
775 def _internaltemplateformatter( |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
776 ui, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
777 out, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
778 topic, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
779 opts, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
780 spec, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
781 tmpl, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
782 docheader=b'', |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
783 docfooter=b'', |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
784 separator=b'', |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
785 ): |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
786 """Build template formatter that handles customizable built-in templates |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
787 such as -Tjson(...)""" |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
788 templates = {spec.ref: tmpl} |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
789 if docheader: |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
790 templates[b'%s:docheader' % spec.ref] = docheader |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
791 if docfooter: |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
792 templates[b'%s:docfooter' % spec.ref] = docfooter |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
793 if separator: |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
794 templates[b'%s:separator' % spec.ref] = separator |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
795 return templateformatter( |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
796 ui, out, topic, opts, spec, overridetemplates=templates |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
797 ) |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
798 |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
799 |
32579
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
800 def formatter(ui, out, topic, opts): |
43100
90b9a7e06c2c
formatter: parse name of built-in formatter templates in standard way
Yuya Nishihara <yuya@tcha.org>
parents:
43099
diff
changeset
|
801 spec = lookuptemplate(ui, topic, opts.get(b'template', b'')) |
43101
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
802 if spec.ref == b"cbor" and spec.refargs is not None: |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
803 return _internaltemplateformatter( |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
804 ui, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
805 out, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
806 topic, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
807 opts, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
808 spec, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
809 tmpl=b'{dict(%s)|cbor}' % spec.refargs, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
810 docheader=cborutil.BEGIN_INDEFINITE_ARRAY, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
811 docfooter=cborutil.BREAK, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
812 ) |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
813 elif spec.ref == b"cbor": |
41996
77ef3498ceb3
template: add CBOR output format
Yuya Nishihara <yuya@tcha.org>
parents:
40277
diff
changeset
|
814 return cborformatter(ui, out, topic, opts) |
43101
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
815 elif spec.ref == b"json" and spec.refargs is not None: |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
816 return _internaltemplateformatter( |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
817 ui, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
818 out, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
819 topic, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
820 opts, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
821 spec, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
822 tmpl=b'{dict(%s)|json}' % spec.refargs, |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
823 docheader=b'[\n ', |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
824 docfooter=b'\n]\n', |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
825 separator=b',\n ', |
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
826 ) |
43100
90b9a7e06c2c
formatter: parse name of built-in formatter templates in standard way
Yuya Nishihara <yuya@tcha.org>
parents:
43099
diff
changeset
|
827 elif spec.ref == b"json": |
32579
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
828 return jsonformatter(ui, out, topic, opts) |
43100
90b9a7e06c2c
formatter: parse name of built-in formatter templates in standard way
Yuya Nishihara <yuya@tcha.org>
parents:
43099
diff
changeset
|
829 elif spec.ref == b"pickle": |
43101
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
830 assert spec.refargs is None, r'function-style not supported' |
32579
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
831 return pickleformatter(ui, out, topic, opts) |
43100
90b9a7e06c2c
formatter: parse name of built-in formatter templates in standard way
Yuya Nishihara <yuya@tcha.org>
parents:
43099
diff
changeset
|
832 elif spec.ref == b"debug": |
43101
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
833 assert spec.refargs is None, r'function-style not supported' |
32579
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
834 return debugformatter(ui, out, topic, opts) |
43100
90b9a7e06c2c
formatter: parse name of built-in formatter templates in standard way
Yuya Nishihara <yuya@tcha.org>
parents:
43099
diff
changeset
|
835 elif spec.ref or spec.tmpl or spec.mapfile: |
43101
1d12ae5096d1
formatter: map -Tjson(...) and -Tcbor(...) to templater
Yuya Nishihara <yuya@tcha.org>
parents:
43100
diff
changeset
|
836 assert spec.refargs is None, r'function-style not supported' |
43099
f1c5358f0d65
formatter: pass in template spec to templateformatter as argument
Yuya Nishihara <yuya@tcha.org>
parents:
43077
diff
changeset
|
837 return templateformatter(ui, out, topic, opts, spec) |
25838
31137258ae8b
formatter: mark developer options
Matt Mackall <mpm@selenic.com>
parents:
25513
diff
changeset
|
838 # developer config: ui.formatdebug |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
839 elif ui.configbool(b'ui', b'formatdebug'): |
32579
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
840 return debugformatter(ui, out, topic, opts) |
25838
31137258ae8b
formatter: mark developer options
Matt Mackall <mpm@selenic.com>
parents:
25513
diff
changeset
|
841 # deprecated config: ui.formatjson |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
842 elif ui.configbool(b'ui', b'formatjson'): |
32579
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
843 return jsonformatter(ui, out, topic, opts) |
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32159
diff
changeset
|
844 return plainformatter(ui, out, topic, opts) |
32580
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
845 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
846 |
32580
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
847 @contextlib.contextmanager |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
848 def openformatter(ui, filename, topic, opts): |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
849 """Create a formatter that writes outputs to the specified file |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
850 |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
851 Must be invoked using the 'with' statement. |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
852 """ |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
853 with util.posixfile(filename, b'wb') as out: |
32580
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
854 with formatter(ui, out, topic, opts) as fm: |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
855 yield fm |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
856 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
857 |
32580
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
858 @contextlib.contextmanager |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
859 def _neverending(fm): |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
860 yield fm |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
861 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
41996
diff
changeset
|
862 |
37597
d110167610db
formatter: carry opts to file-based formatters by basefm
Yuya Nishihara <yuya@tcha.org>
parents:
37596
diff
changeset
|
863 def maybereopen(fm, filename): |
32580
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
864 """Create a formatter backed by file if filename specified, else return |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
865 the given formatter |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
866 |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
867 Must be invoked using the 'with' statement. This will never call fm.end() |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
868 of the given formatter. |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
869 """ |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
870 if filename: |
37597
d110167610db
formatter: carry opts to file-based formatters by basefm
Yuya Nishihara <yuya@tcha.org>
parents:
37596
diff
changeset
|
871 return openformatter(fm._ui, filename, fm._topic, fm._opts) |
32580
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
872 else: |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32579
diff
changeset
|
873 return _neverending(fm) |