Mercurial > hg-stable
annotate mercurial/formatter.py @ 34262:d6af8da4a3b8
py3: rewrite stdout hack of doctest by using ui.pushbuffer()
We can't use pycompat.stdout here because the doctest runner replaces
sys.stdout with a string buffer.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 17 Sep 2017 12:39:14 +0900 |
parents | 0fa781320203 |
children | ebe3d0095c69 |
rev | line source |
---|---|
16134 | 1 # formatter.py - generic output formatting for mercurial |
2 # | |
3 # Copyright 2012 Matt Mackall <mpm@selenic.com> | |
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 | |
30565
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
8 """Generic output formatting for Mercurial |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
9 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
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:
29953
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:
29953
diff
changeset
|
12 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
13 - fm.write() for unconditional output |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
14 - fm.condwrite() to show some extra data conditionally in plain output |
31182
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31180
diff
changeset
|
15 - fm.context() to provide changectx to template output |
30565
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
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:
29953
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:
29953
diff
changeset
|
18 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
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:
29953
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:
29953
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:
29953
diff
changeset
|
22 supported by the formatter API. |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
23 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
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:
29953
diff
changeset
|
25 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
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:
29953
diff
changeset
|
27 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
28 fm.condwrite() vs 'if cond:': |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
29 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
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:
29953
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:
29953
diff
changeset
|
32 fm.write(). |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
33 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
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:
29953
diff
changeset
|
35 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
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:
29953
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:
29953
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:
29953
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:
29953
diff
changeset
|
40 "{get(foo, key)}" function. |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
41 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
42 Doctest helper: |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
43 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
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:
29953
diff
changeset
|
45 ... import sys |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
46 ... from . import ui as uimod |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
47 ... ui = uimod.ui() |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
48 ... ui.verbose = verbose |
34262
d6af8da4a3b8
py3: rewrite stdout hack of doctest by using ui.pushbuffer()
Yuya Nishihara <yuya@tcha.org>
parents:
34146
diff
changeset
|
49 ... ui.pushbuffer() |
d6af8da4a3b8
py3: rewrite stdout hack of doctest by using ui.pushbuffer()
Yuya Nishihara <yuya@tcha.org>
parents:
34146
diff
changeset
|
50 ... try: |
d6af8da4a3b8
py3: rewrite stdout hack of doctest by using ui.pushbuffer()
Yuya Nishihara <yuya@tcha.org>
parents:
34146
diff
changeset
|
51 ... return fn(ui, ui.formatter(fn.__name__, opts)) |
d6af8da4a3b8
py3: rewrite stdout hack of doctest by using ui.pushbuffer()
Yuya Nishihara <yuya@tcha.org>
parents:
34146
diff
changeset
|
52 ... finally: |
d6af8da4a3b8
py3: rewrite stdout hack of doctest by using ui.pushbuffer()
Yuya Nishihara <yuya@tcha.org>
parents:
34146
diff
changeset
|
53 ... print(pycompat.sysstr(ui.popbuffer()), end='') |
30565
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
54 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
55 Basic example: |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
56 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
57 >>> def files(ui, fm): |
34146
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33102
diff
changeset
|
58 ... files = [(b'foo', 123, (0, 0)), (b'bar', 456, (1, 0))] |
30565
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
59 ... for f in files: |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
60 ... fm.startitem() |
34146
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33102
diff
changeset
|
61 ... 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:
33102
diff
changeset
|
62 ... 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:
33102
diff
changeset
|
63 ... fm.formatdate(f[2], b'%Y-%m-%d %H:%M:%S')) |
30565
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
64 ... fm.data(size=f[1]) |
34146
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33102
diff
changeset
|
65 ... fm.plain(b'\\n') |
30565
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
66 ... fm.end() |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
67 >>> show(files) |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
68 foo |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
69 bar |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
70 >>> show(files, verbose=True) |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
71 foo 1970-01-01 00:00:00 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
72 bar 1970-01-01 00:00:01 |
34146
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33102
diff
changeset
|
73 >>> show(files, template=b'json') |
30565
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
74 [ |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
75 { |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
76 "date": [0, 0], |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
77 "path": "foo", |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
78 "size": 123 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
79 }, |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
80 { |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
81 "date": [1, 0], |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
82 "path": "bar", |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
83 "size": 456 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
84 } |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
85 ] |
34146
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33102
diff
changeset
|
86 >>> show(files, template=b'path: {path}\\ndate: {date|rfc3339date}\\n') |
30565
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
87 path: foo |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
88 date: 1970-01-01T00:00:00+00:00 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
89 path: bar |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
90 date: 1970-01-01T00:00:01+00:00 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
91 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
92 Nested example: |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
93 |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
94 >>> def subrepos(ui, fm): |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
95 ... fm.startitem() |
34146
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33102
diff
changeset
|
96 ... fm.write(b'repo', b'[%s]\\n', b'baz') |
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33102
diff
changeset
|
97 ... files(ui, fm.nested(b'files')) |
30565
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
98 ... fm.end() |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
99 >>> show(subrepos) |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
100 [baz] |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
101 foo |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
102 bar |
34146
0fa781320203
doctest: bulk-replace string literals with b'' for Python 3
Yuya Nishihara <yuya@tcha.org>
parents:
33102
diff
changeset
|
103 >>> show(subrepos, template=b'{repo}: {join(files % "{path}", ", ")}\\n') |
30565
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
104 baz: foo, bar |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
105 """ |
783016005122
formatter: add overview of API and example as doctest
Yuya Nishihara <yuya@tcha.org>
parents:
29953
diff
changeset
|
106 |
34262
d6af8da4a3b8
py3: rewrite stdout hack of doctest by using ui.pushbuffer()
Yuya Nishihara <yuya@tcha.org>
parents:
34146
diff
changeset
|
107 from __future__ import absolute_import, print_function |
25950
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
108 |
32856
615ec3f14aa9
formatter: wrap (tmpl, mapfile) by named tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32853
diff
changeset
|
109 import collections |
32606
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
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 ) |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
119 |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
120 from . import ( |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26373
diff
changeset
|
121 error, |
32208
0fd15522a848
py3: use pycompat.byteskwargs to converts kwargs to bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31925
diff
changeset
|
122 pycompat, |
31785
654e9a1c8a6c
formatter: use templatefilters.json()
Yuya Nishihara <yuya@tcha.org>
parents:
31405
diff
changeset
|
123 templatefilters, |
29690
c3a9cd78b151
formatter: add function to convert list to appropriate format (issue5217)
Yuya Nishihara <yuya@tcha.org>
parents:
29324
diff
changeset
|
124 templatekw, |
25950
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
125 templater, |
29324
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28957
diff
changeset
|
126 util, |
25950
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
127 ) |
175873e36d03
formatter: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25838
diff
changeset
|
128 |
29324
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28957
diff
changeset
|
129 pickle = util.pickle |
b501579147f1
py3: conditionalize cPickle import by adding in util
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28957
diff
changeset
|
130 |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
131 class _nullconverter(object): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
132 '''convert non-primitive data types to be processed by formatter''' |
33102
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
133 |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
134 # set to True if context object should be stored as item |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
135 storecontext = False |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
136 |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
137 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
138 def formatdate(date, fmt): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
139 '''convert date tuple to appropriate format''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
140 return date |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
141 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
142 def formatdict(data, key, value, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
143 '''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:
29805
diff
changeset
|
144 # 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:
29805
diff
changeset
|
145 # serialized as a builtin dict in pickle output |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
146 return dict(data) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
147 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
148 def formatlist(data, name, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
149 '''convert iterable to appropriate list format''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
150 return list(data) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
151 |
16134 | 152 class baseformatter(object): |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
153 def __init__(self, ui, topic, opts, converter): |
16134 | 154 self._ui = ui |
155 self._topic = topic | |
156 self._style = opts.get("style") | |
157 self._template = opts.get("template") | |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
158 self._converter = converter |
16134 | 159 self._item = None |
22701
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
160 # 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
|
161 self.hexfunc = hex |
29886
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29848
diff
changeset
|
162 def __enter__(self): |
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29848
diff
changeset
|
163 return self |
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29848
diff
changeset
|
164 def __exit__(self, exctype, excvalue, traceback): |
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29848
diff
changeset
|
165 if exctype is None: |
307b20e5e505
formatter: add context manager interface for convenience
Yuya Nishihara <yuya@tcha.org>
parents:
29848
diff
changeset
|
166 self.end() |
16134 | 167 def _showitem(self): |
168 '''show a formatted item once all data is collected''' | |
169 pass | |
170 def startitem(self): | |
171 '''begin an item in the format list''' | |
172 if self._item is not None: | |
173 self._showitem() | |
174 self._item = {} | |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
175 def formatdate(self, date, fmt='%a %b %d %H:%M:%S %Y %1%2'): |
29692
2f3f18ad55a2
formatter: add function to convert date tuple to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29690
diff
changeset
|
176 '''convert date tuple to appropriate format''' |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
177 return self._converter.formatdate(date, fmt) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
178 def formatdict(self, data, key='key', value='value', fmt='%s=%s', sep=' '): |
29805
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
179 '''convert dict or key-value pairs to appropriate dict format''' |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
180 return self._converter.formatdict(data, key, value, fmt, sep) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
181 def formatlist(self, data, name, fmt='%s', sep=' '): |
29690
c3a9cd78b151
formatter: add function to convert list to appropriate format (issue5217)
Yuya Nishihara <yuya@tcha.org>
parents:
29324
diff
changeset
|
182 '''convert iterable to appropriate list format''' |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
183 # 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:
29805
diff
changeset
|
184 # we have default template keyword, e.g. {item} |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
185 return self._converter.formatlist(data, name, fmt, sep) |
31182
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31180
diff
changeset
|
186 def context(self, **ctxs): |
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31180
diff
changeset
|
187 '''insert context objects to be used to render template keywords''' |
33102
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
188 ctxs = pycompat.byteskwargs(ctxs) |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
189 assert all(k == 'ctx' for k in ctxs) |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
190 if self._converter.storecontext: |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
191 self._item.update(ctxs) |
16134 | 192 def data(self, **data): |
193 '''insert data into item that's not shown in default output''' | |
32208
0fd15522a848
py3: use pycompat.byteskwargs to converts kwargs to bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
31925
diff
changeset
|
194 data = pycompat.byteskwargs(data) |
17630
ff5ed1ecd43a
formatter: improve implementation of data method
David M. Carr <david@carrclan.us>
parents:
17597
diff
changeset
|
195 self._item.update(data) |
16134 | 196 def write(self, fields, deftext, *fielddata, **opts): |
197 '''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
|
198 fieldkeys = fields.split() |
55de800937e0
formatter: verify number of arguments passed to write functions
Yuya Nishihara <yuya@tcha.org>
parents:
25950
diff
changeset
|
199 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
|
200 self._item.update(zip(fieldkeys, fielddata)) |
17909
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
201 def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
202 '''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
|
203 fieldkeys = fields.split() |
55de800937e0
formatter: verify number of arguments passed to write functions
Yuya Nishihara <yuya@tcha.org>
parents:
25950
diff
changeset
|
204 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
|
205 self._item.update(zip(fieldkeys, fielddata)) |
16134 | 206 def plain(self, text, **opts): |
207 '''show raw text for non-templated mode''' | |
208 pass | |
29953
e7cacb45c4be
formatter: introduce isplain() to replace (the inverse of) __nonzero__() (API)
Mathias De Maré <mathias.demare@gmail.com>
parents:
29886
diff
changeset
|
209 def isplain(self): |
e7cacb45c4be
formatter: introduce isplain() to replace (the inverse of) __nonzero__() (API)
Mathias De Maré <mathias.demare@gmail.com>
parents:
29886
diff
changeset
|
210 '''check for plain formatter usage''' |
e7cacb45c4be
formatter: introduce isplain() to replace (the inverse of) __nonzero__() (API)
Mathias De Maré <mathias.demare@gmail.com>
parents:
29886
diff
changeset
|
211 return False |
29848
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
212 def nested(self, field): |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
213 '''sub formatter to store nested data in the specified field''' |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
214 self._item[field] = data = [] |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
215 return _nestedformatter(self._ui, self._converter, data) |
16134 | 216 def end(self): |
217 '''end output for the formatter''' | |
218 if self._item is not None: | |
219 self._showitem() | |
220 | |
32607
e9bf3e132ea9
formatter: add nullformatter
Yuya Nishihara <yuya@tcha.org>
parents:
32606
diff
changeset
|
221 def nullformatter(ui, topic): |
e9bf3e132ea9
formatter: add nullformatter
Yuya Nishihara <yuya@tcha.org>
parents:
32606
diff
changeset
|
222 '''formatter that prints nothing''' |
e9bf3e132ea9
formatter: add nullformatter
Yuya Nishihara <yuya@tcha.org>
parents:
32606
diff
changeset
|
223 return baseformatter(ui, topic, opts={}, converter=_nullconverter) |
e9bf3e132ea9
formatter: add nullformatter
Yuya Nishihara <yuya@tcha.org>
parents:
32606
diff
changeset
|
224 |
29848
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
225 class _nestedformatter(baseformatter): |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
226 '''build sub items and store them in the parent formatter''' |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
227 def __init__(self, ui, converter, data): |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
228 baseformatter.__init__(self, ui, topic='', opts={}, converter=converter) |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
229 self._data = data |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
230 def _showitem(self): |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
231 self._data.append(self._item) |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
232 |
29805
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
233 def _iteritems(data): |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
234 '''iterate key-value pairs in stable order''' |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
235 if isinstance(data, dict): |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
236 return sorted(data.iteritems()) |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
237 return data |
4891f3b93182
formatter: add function to convert dict to appropriate format
Yuya Nishihara <yuya@tcha.org>
parents:
29692
diff
changeset
|
238 |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
239 class _plainconverter(object): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
240 '''convert non-primitive data types to text''' |
33102
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
241 |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
242 storecontext = False |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
243 |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
244 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
245 def formatdate(date, fmt): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
246 '''stringify date tuple in the given format''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
247 return util.datestr(date, fmt) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
248 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
249 def formatdict(data, key, value, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
250 '''stringify key-value pairs separated by sep''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
251 return sep.join(fmt % (k, v) for k, v in _iteritems(data)) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
252 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
253 def formatlist(data, name, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
254 '''stringify iterable separated by sep''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
255 return sep.join(fmt % e for e in data) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
256 |
16134 | 257 class plainformatter(baseformatter): |
258 '''the default text output scheme''' | |
32605
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
259 def __init__(self, ui, out, topic, opts): |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
260 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
|
261 if ui.debugflag: |
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
262 self.hexfunc = hex |
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
263 else: |
cb28d2b3db0b
formatter: add general way to switch hex/short functions
Yuya Nishihara <yuya@tcha.org>
parents:
22674
diff
changeset
|
264 self.hexfunc = short |
32605
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
265 if ui is out: |
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
266 self._write = ui.write |
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
267 else: |
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
268 self._write = lambda s, **opts: out.write(s) |
16134 | 269 def startitem(self): |
270 pass | |
271 def data(self, **data): | |
272 pass | |
273 def write(self, fields, deftext, *fielddata, **opts): | |
32605
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
274 self._write(deftext % fielddata, **opts) |
17909
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
275 def condwrite(self, cond, fields, deftext, *fielddata, **opts): |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
276 '''do conditional write''' |
3326fd05eb1f
formatter: add condwrite method
Matt Mackall <mpm@selenic.com>
parents:
17630
diff
changeset
|
277 if cond: |
32605
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
278 self._write(deftext % fielddata, **opts) |
16134 | 279 def plain(self, text, **opts): |
32605
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
280 self._write(text, **opts) |
29953
e7cacb45c4be
formatter: introduce isplain() to replace (the inverse of) __nonzero__() (API)
Mathias De Maré <mathias.demare@gmail.com>
parents:
29886
diff
changeset
|
281 def isplain(self): |
e7cacb45c4be
formatter: introduce isplain() to replace (the inverse of) __nonzero__() (API)
Mathias De Maré <mathias.demare@gmail.com>
parents:
29886
diff
changeset
|
282 return True |
29848
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
283 def nested(self, field): |
5b886289a1ca
formatter: add fm.nested(field) to either write or build sub items
Yuya Nishihara <yuya@tcha.org>
parents:
29847
diff
changeset
|
284 # 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:
29847
diff
changeset
|
285 return self |
16134 | 286 def end(self): |
287 pass | |
288 | |
289 class debugformatter(baseformatter): | |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
290 def __init__(self, ui, out, topic, opts): |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
291 baseformatter.__init__(self, ui, topic, opts, _nullconverter) |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
292 self._out = out |
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
293 self._out.write("%s = [\n" % self._topic) |
16134 | 294 def _showitem(self): |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
295 self._out.write(" " + repr(self._item) + ",\n") |
16134 | 296 def end(self): |
297 baseformatter.end(self) | |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
298 self._out.write("]\n") |
16134 | 299 |
22430
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
300 class pickleformatter(baseformatter): |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
301 def __init__(self, ui, out, topic, opts): |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
302 baseformatter.__init__(self, ui, topic, opts, _nullconverter) |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
303 self._out = out |
22430
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
304 self._data = [] |
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
305 def _showitem(self): |
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
306 self._data.append(self._item) |
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
307 def end(self): |
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
308 baseformatter.end(self) |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
309 self._out.write(pickle.dumps(self._data)) |
22430
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
310 |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
311 class jsonformatter(baseformatter): |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
312 def __init__(self, ui, out, topic, opts): |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
313 baseformatter.__init__(self, ui, topic, opts, _nullconverter) |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
314 self._out = out |
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
315 self._out.write("[") |
31307
59d09565ac77
formatter: set _first on formatter, not ui
Martin von Zweigbergk <martinvonz@google.com>
parents:
31192
diff
changeset
|
316 self._first = True |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
317 def _showitem(self): |
31307
59d09565ac77
formatter: set _first on formatter, not ui
Martin von Zweigbergk <martinvonz@google.com>
parents:
31192
diff
changeset
|
318 if self._first: |
59d09565ac77
formatter: set _first on formatter, not ui
Martin von Zweigbergk <martinvonz@google.com>
parents:
31192
diff
changeset
|
319 self._first = False |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
320 else: |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
321 self._out.write(",") |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
322 |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
323 self._out.write("\n {\n") |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
324 first = True |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
325 for k, v in sorted(self._item.items()): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
326 if first: |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
327 first = False |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
328 else: |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
329 self._out.write(",\n") |
31785
654e9a1c8a6c
formatter: use templatefilters.json()
Yuya Nishihara <yuya@tcha.org>
parents:
31405
diff
changeset
|
330 u = templatefilters.json(v, paranoid=False) |
654e9a1c8a6c
formatter: use templatefilters.json()
Yuya Nishihara <yuya@tcha.org>
parents:
31405
diff
changeset
|
331 self._out.write(' "%s": %s' % (k, u)) |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
332 self._out.write("\n }") |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
333 def end(self): |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
334 baseformatter.end(self) |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
335 self._out.write("\n]\n") |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
336 |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
337 class _templateconverter(object): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
338 '''convert non-primitive data types to be processed by templater''' |
33102
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
339 |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
340 storecontext = True |
04b3743c1d7c
formatter: proxy fm.context() through converter
Yuya Nishihara <yuya@tcha.org>
parents:
32970
diff
changeset
|
341 |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
342 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
343 def formatdate(date, fmt): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
344 '''return date tuple''' |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
345 return date |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
346 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
347 def formatdict(data, key, value, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
348 '''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:
29805
diff
changeset
|
349 data = util.sortdict(_iteritems(data)) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
350 def f(): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
351 yield _plainconverter.formatdict(data, key, value, fmt, sep) |
31925
5b2241e84982
templatekw: add public function to wrap a dict by _hybrid object
Yuya Nishihara <yuya@tcha.org>
parents:
31924
diff
changeset
|
352 return templatekw.hybriddict(data, key=key, value=value, fmt=fmt, |
5b2241e84982
templatekw: add public function to wrap a dict by _hybrid object
Yuya Nishihara <yuya@tcha.org>
parents:
31924
diff
changeset
|
353 gen=f()) |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
354 @staticmethod |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
355 def formatlist(data, name, fmt, sep): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
356 '''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:
29805
diff
changeset
|
357 data = list(data) |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
358 def f(): |
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
359 yield _plainconverter.formatlist(data, name, fmt, sep) |
31924
21f129354dd0
templatekw: add public function to wrap a list by _hybrid object
Yuya Nishihara <yuya@tcha.org>
parents:
31807
diff
changeset
|
360 return templatekw.hybridlist(data, name=name, fmt=fmt, gen=f()) |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
361 |
25513
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
362 class templateformatter(baseformatter): |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
363 def __init__(self, ui, out, topic, opts): |
29847
18bac830eef3
formatter: factor out format*() functions to separate classes
Yuya Nishihara <yuya@tcha.org>
parents:
29805
diff
changeset
|
364 baseformatter.__init__(self, ui, topic, opts, _templateconverter) |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
365 self._out = out |
32851
99df35499cae
formatter: inline gettemplater()
Yuya Nishihara <yuya@tcha.org>
parents:
32850
diff
changeset
|
366 spec = lookuptemplate(ui, topic, opts.get('template', '')) |
32859
883adaea9e80
formatter: render template specified by templatespec tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32858
diff
changeset
|
367 self._tref = spec.ref |
32858
57c13c0d1cde
formatter: put topic in templatespec tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32856
diff
changeset
|
368 self._t = loadtemplater(ui, spec, cache=templatekw.defaulttempl) |
32967
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
369 self._parts = templatepartsmap(spec, self._t, |
32968
5100ce217dfa
formatter: add support for separator template
Yuya Nishihara <yuya@tcha.org>
parents:
32967
diff
changeset
|
370 ['docheader', 'docfooter', 'separator']) |
31807
e6eb86b154c5
templater: provide loop counter as "index" keyword
Yuya Nishihara <yuya@tcha.org>
parents:
31805
diff
changeset
|
371 self._counter = itertools.count() |
31182
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31180
diff
changeset
|
372 self._cache = {} # for templatekw/funcs to store reusable data |
32967
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
373 self._renderitem('docheader', {}) |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
374 |
25513
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
375 def _showitem(self): |
32966
12a0794fa2e3
formatter: extract helper function to render template
Yuya Nishihara <yuya@tcha.org>
parents:
32915
diff
changeset
|
376 item = self._item.copy() |
32968
5100ce217dfa
formatter: add support for separator template
Yuya Nishihara <yuya@tcha.org>
parents:
32967
diff
changeset
|
377 item['index'] = index = next(self._counter) |
5100ce217dfa
formatter: add support for separator template
Yuya Nishihara <yuya@tcha.org>
parents:
32967
diff
changeset
|
378 if index > 0: |
5100ce217dfa
formatter: add support for separator template
Yuya Nishihara <yuya@tcha.org>
parents:
32967
diff
changeset
|
379 self._renderitem('separator', {}) |
32966
12a0794fa2e3
formatter: extract helper function to render template
Yuya Nishihara <yuya@tcha.org>
parents:
32915
diff
changeset
|
380 self._renderitem(self._tref, item) |
12a0794fa2e3
formatter: extract helper function to render template
Yuya Nishihara <yuya@tcha.org>
parents:
32915
diff
changeset
|
381 |
32967
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
382 def _renderitem(self, part, item): |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
383 if part not in self._parts: |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
384 return |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
385 ref = self._parts[part] |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
386 |
31182
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31180
diff
changeset
|
387 # TODO: add support for filectx. probably each template keyword or |
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31180
diff
changeset
|
388 # function will have to declare dependent resources. e.g. |
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31180
diff
changeset
|
389 # @templatekeyword(..., requires=('ctx',)) |
31805
dca9b6922514
formatter: reorder code that builds template mapping
Yuya Nishihara <yuya@tcha.org>
parents:
31785
diff
changeset
|
390 props = {} |
32966
12a0794fa2e3
formatter: extract helper function to render template
Yuya Nishihara <yuya@tcha.org>
parents:
32915
diff
changeset
|
391 if 'ctx' in item: |
31805
dca9b6922514
formatter: reorder code that builds template mapping
Yuya Nishihara <yuya@tcha.org>
parents:
31785
diff
changeset
|
392 props.update(templatekw.keywords) |
dca9b6922514
formatter: reorder code that builds template mapping
Yuya Nishihara <yuya@tcha.org>
parents:
31785
diff
changeset
|
393 # explicitly-defined fields precede templatekw |
32966
12a0794fa2e3
formatter: extract helper function to render template
Yuya Nishihara <yuya@tcha.org>
parents:
32915
diff
changeset
|
394 props.update(item) |
12a0794fa2e3
formatter: extract helper function to render template
Yuya Nishihara <yuya@tcha.org>
parents:
32915
diff
changeset
|
395 if 'ctx' in item: |
31182
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31180
diff
changeset
|
396 # but template resources must be always available |
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31180
diff
changeset
|
397 props['templ'] = self._t |
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31180
diff
changeset
|
398 props['repo'] = props['ctx'].repo() |
16272d8c24f6
formatter: add support for changeset templating
Yuya Nishihara <yuya@tcha.org>
parents:
31180
diff
changeset
|
399 props['revcache'] = {} |
32914
e14484e7f562
py3: use pycompat.strkwargs() to convert kwargs keys to str before passing
Pulkit Goyal <7895pulkit@gmail.com>
parents:
32894
diff
changeset
|
400 props = pycompat.strkwargs(props) |
32966
12a0794fa2e3
formatter: extract helper function to render template
Yuya Nishihara <yuya@tcha.org>
parents:
32915
diff
changeset
|
401 g = self._t(ref, ui=self._ui, cache=self._cache, **props) |
31192
5660c45ecba6
formatter: add argument to change output file of non-plain formatter
Yuya Nishihara <yuya@tcha.org>
parents:
31182
diff
changeset
|
402 self._out.write(templater.stringify(g)) |
25513
0c6f98398f8a
formatter: add template support
Matt Mackall <mpm@selenic.com>
parents:
25512
diff
changeset
|
403 |
32967
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
404 def end(self): |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
405 baseformatter.end(self) |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
406 self._renderitem('docfooter', {}) |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
407 |
32856
615ec3f14aa9
formatter: wrap (tmpl, mapfile) by named tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32853
diff
changeset
|
408 templatespec = collections.namedtuple(r'templatespec', |
32858
57c13c0d1cde
formatter: put topic in templatespec tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32856
diff
changeset
|
409 r'ref tmpl mapfile') |
32856
615ec3f14aa9
formatter: wrap (tmpl, mapfile) by named tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32853
diff
changeset
|
410 |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
411 def lookuptemplate(ui, topic, tmpl): |
32853
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
412 """Find the template matching the given -T/--template spec 'tmpl' |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
413 |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
414 'tmpl' can be any of the following: |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
415 |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
416 - a literal template (e.g. '{rev}') |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
417 - a map-file name or path (e.g. 'changelog') |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
418 - a reference to [templates] in config file |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
419 - a path to raw template file |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
420 |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
421 A map file defines a stand-alone template environment. If a map file |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
422 selected, all templates defined in the file will be loaded, and the |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
423 template matching the given topic will be rendered. No aliases will be |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
424 loaded from user config. |
32893
c8f2cf18b82e
formatter: load templates section like a map file
Yuya Nishihara <yuya@tcha.org>
parents:
32891
diff
changeset
|
425 |
c8f2cf18b82e
formatter: load templates section like a map file
Yuya Nishihara <yuya@tcha.org>
parents:
32891
diff
changeset
|
426 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:
32891
diff
changeset
|
427 available as well as aliases in [templatealias]. |
32853
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
428 """ |
9d76812f9b0b
formatter: document lookuptemplate()
Yuya Nishihara <yuya@tcha.org>
parents:
32851
diff
changeset
|
429 |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
430 # looks like a literal template? |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
431 if '{' in tmpl: |
32893
c8f2cf18b82e
formatter: load templates section like a map file
Yuya Nishihara <yuya@tcha.org>
parents:
32891
diff
changeset
|
432 return templatespec('', tmpl, None) |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
433 |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
434 # perhaps a stock style? |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
435 if not os.path.split(tmpl)[0]: |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
436 mapname = (templater.templatepath('map-cmdline.' + tmpl) |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
437 or templater.templatepath(tmpl)) |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
438 if mapname and os.path.isfile(mapname): |
32858
57c13c0d1cde
formatter: put topic in templatespec tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32856
diff
changeset
|
439 return templatespec(topic, None, mapname) |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
440 |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
441 # perhaps it's a reference to [templates] |
32893
c8f2cf18b82e
formatter: load templates section like a map file
Yuya Nishihara <yuya@tcha.org>
parents:
32891
diff
changeset
|
442 if ui.config('templates', tmpl): |
c8f2cf18b82e
formatter: load templates section like a map file
Yuya Nishihara <yuya@tcha.org>
parents:
32891
diff
changeset
|
443 return templatespec(tmpl, None, None) |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
444 |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
445 if tmpl == 'list': |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
446 ui.write(_("available styles: %s\n") % templater.stylelist()) |
26587
56b2bcea2529
error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
26373
diff
changeset
|
447 raise error.Abort(_("specify a template")) |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
448 |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
449 # perhaps it's a path to a map or a template |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
450 if ('/' in tmpl or '\\' in tmpl) and os.path.isfile(tmpl): |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
451 # is it a mapfile for a style? |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
452 if os.path.basename(tmpl).startswith("map-"): |
32858
57c13c0d1cde
formatter: put topic in templatespec tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32856
diff
changeset
|
453 return templatespec(topic, None, os.path.realpath(tmpl)) |
32848
470820c2418d
formatter: open raw template file in posix semantics
Yuya Nishihara <yuya@tcha.org>
parents:
32847
diff
changeset
|
454 with util.posixfile(tmpl, 'rb') as f: |
32846
526f9f12f707
formatter: close raw template file explicitly
Yuya Nishihara <yuya@tcha.org>
parents:
32607
diff
changeset
|
455 tmpl = f.read() |
32893
c8f2cf18b82e
formatter: load templates section like a map file
Yuya Nishihara <yuya@tcha.org>
parents:
32891
diff
changeset
|
456 return templatespec('', tmpl, None) |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
457 |
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
458 # constant string? |
32893
c8f2cf18b82e
formatter: load templates section like a map file
Yuya Nishihara <yuya@tcha.org>
parents:
32891
diff
changeset
|
459 return templatespec('', tmpl, None) |
25511
c2a4dfe2a336
formatter: move most of template option helper to formatter
Matt Mackall <mpm@selenic.com>
parents:
24321
diff
changeset
|
460 |
32967
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
461 def templatepartsmap(spec, t, partnames): |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
462 """Create a mapping of {part: ref}""" |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
463 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:
32966
diff
changeset
|
464 if spec.mapfile: |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
465 partsmap.update((p, p) for p in partnames if p in t) |
32970
61b60b28c381
formatter: add support for parts map of [templates] section
Yuya Nishihara <yuya@tcha.org>
parents:
32968
diff
changeset
|
466 elif spec.ref: |
61b60b28c381
formatter: add support for parts map of [templates] section
Yuya Nishihara <yuya@tcha.org>
parents:
32968
diff
changeset
|
467 for part in partnames: |
61b60b28c381
formatter: add support for parts map of [templates] section
Yuya Nishihara <yuya@tcha.org>
parents:
32968
diff
changeset
|
468 ref = '%s:%s' % (spec.ref, part) # select config sub-section |
61b60b28c381
formatter: add support for parts map of [templates] section
Yuya Nishihara <yuya@tcha.org>
parents:
32968
diff
changeset
|
469 if ref in t: |
61b60b28c381
formatter: add support for parts map of [templates] section
Yuya Nishihara <yuya@tcha.org>
parents:
32968
diff
changeset
|
470 partsmap[part] = ref |
32967
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
471 return partsmap |
13eebc189ff3
formatter: add support for docheader and docfooter templates
Yuya Nishihara <yuya@tcha.org>
parents:
32966
diff
changeset
|
472 |
32858
57c13c0d1cde
formatter: put topic in templatespec tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32856
diff
changeset
|
473 def loadtemplater(ui, spec, cache=None): |
32850
11e667a8fcba
formatter: factor out function to create templater from literal or map file
Yuya Nishihara <yuya@tcha.org>
parents:
32848
diff
changeset
|
474 """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:
32848
diff
changeset
|
475 a map file""" |
32856
615ec3f14aa9
formatter: wrap (tmpl, mapfile) by named tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32853
diff
changeset
|
476 assert not (spec.tmpl and spec.mapfile) |
615ec3f14aa9
formatter: wrap (tmpl, mapfile) by named tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32853
diff
changeset
|
477 if spec.mapfile: |
615ec3f14aa9
formatter: wrap (tmpl, mapfile) by named tuple
Yuya Nishihara <yuya@tcha.org>
parents:
32853
diff
changeset
|
478 return templater.templater.frommapfile(spec.mapfile, cache=cache) |
32894
8da65da039c3
formatter: always store a literal template unnamed
Yuya Nishihara <yuya@tcha.org>
parents:
32893
diff
changeset
|
479 return maketemplater(ui, spec.tmpl, cache=cache) |
28955
78759f78a44e
templater: factor out function that creates templater from string template
Yuya Nishihara <yuya@tcha.org>
parents:
28954
diff
changeset
|
480 |
32891
2ecce24dfcd3
templater: add simple interface for unnamed template (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32859
diff
changeset
|
481 def maketemplater(ui, tmpl, cache=None): |
28955
78759f78a44e
templater: factor out function that creates templater from string template
Yuya Nishihara <yuya@tcha.org>
parents:
28954
diff
changeset
|
482 """Create a templater from a string template 'tmpl'""" |
28957
d813132ea361
templater: load and expand aliases by template engine (API) (issue4842)
Yuya Nishihara <yuya@tcha.org>
parents:
28955
diff
changeset
|
483 aliases = ui.configitems('templatealias') |
31180
e64b70c96338
formatter: drop filters argument from maketemplater()
Yuya Nishihara <yuya@tcha.org>
parents:
30565
diff
changeset
|
484 t = templater.templater(cache=cache, aliases=aliases) |
32893
c8f2cf18b82e
formatter: load templates section like a map file
Yuya Nishihara <yuya@tcha.org>
parents:
32891
diff
changeset
|
485 t.cache.update((k, templater.unquotestring(v)) |
c8f2cf18b82e
formatter: load templates section like a map file
Yuya Nishihara <yuya@tcha.org>
parents:
32891
diff
changeset
|
486 for k, v in ui.configitems('templates')) |
25512
8463433c2689
formatter: add a method to build a full templater from a -T option
Matt Mackall <mpm@selenic.com>
parents:
25511
diff
changeset
|
487 if tmpl: |
32894
8da65da039c3
formatter: always store a literal template unnamed
Yuya Nishihara <yuya@tcha.org>
parents:
32893
diff
changeset
|
488 t.cache[''] = 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
|
489 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
|
490 |
32605
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
491 def formatter(ui, out, topic, opts): |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
492 template = opts.get("template", "") |
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
493 if template == "json": |
32605
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
494 return jsonformatter(ui, out, topic, opts) |
22430
968247e8f4ac
formatter: add pickle format
Matt Mackall <mpm@selenic.com>
parents:
22428
diff
changeset
|
495 elif template == "pickle": |
32605
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
496 return pickleformatter(ui, out, topic, opts) |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
497 elif template == "debug": |
32605
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
498 return debugformatter(ui, out, topic, opts) |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
499 elif template != "": |
32605
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
500 return templateformatter(ui, out, topic, opts) |
25838
31137258ae8b
formatter: mark developer options
Matt Mackall <mpm@selenic.com>
parents:
25513
diff
changeset
|
501 # developer config: ui.formatdebug |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
502 elif ui.configbool('ui', 'formatdebug'): |
32605
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
503 return debugformatter(ui, out, topic, opts) |
25838
31137258ae8b
formatter: mark developer options
Matt Mackall <mpm@selenic.com>
parents:
25513
diff
changeset
|
504 # deprecated config: ui.formatjson |
22428
427e80a18ef8
formatter: add json formatter
Matt Mackall <mpm@selenic.com>
parents:
22424
diff
changeset
|
505 elif ui.configbool('ui', 'formatjson'): |
32605
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
506 return jsonformatter(ui, out, topic, opts) |
012e0da5b759
formatter: add option to redirect output to file object
Yuya Nishihara <yuya@tcha.org>
parents:
32208
diff
changeset
|
507 return plainformatter(ui, out, topic, opts) |
32606
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
508 |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
509 @contextlib.contextmanager |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
510 def openformatter(ui, filename, topic, opts): |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
511 """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:
32605
diff
changeset
|
512 |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
513 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:
32605
diff
changeset
|
514 """ |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
515 with util.posixfile(filename, 'wb') as out: |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
516 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:
32605
diff
changeset
|
517 yield fm |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
518 |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
519 @contextlib.contextmanager |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
520 def _neverending(fm): |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
521 yield fm |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
522 |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
523 def maybereopen(fm, filename, opts): |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
524 """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:
32605
diff
changeset
|
525 the given formatter |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
526 |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
527 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:
32605
diff
changeset
|
528 of the given formatter. |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
529 """ |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
530 if filename: |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
531 return openformatter(fm._ui, filename, fm._topic, opts) |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
532 else: |
35985d407d49
formatter: add helper to create a formatter optionally backed by file
Yuya Nishihara <yuya@tcha.org>
parents:
32605
diff
changeset
|
533 return _neverending(fm) |