1e39cd42a8cc6ad2a4eceae5c2fb07a477a51dd6
[SubU] /
1 """
2     pygments.formatters.other
3     ~~~~~~~~~~~~~~~~~~~~~~~~~
4
5     Other formatters: NullFormatter, RawTokenFormatter.
6
7     :copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS.
8     :license: BSD, see LICENSE for details.
9 """
10
11 from pip._vendor.pygments.formatter import Formatter
12 from pip._vendor.pygments.util import get_choice_opt
13 from pip._vendor.pygments.token import Token
14 from pip._vendor.pygments.console import colorize
15
16 __all__ = ['NullFormatter', 'RawTokenFormatter', 'TestcaseFormatter']
17
18
19 class NullFormatter(Formatter):
20     """
21     Output the text unchanged without any formatting.
22     """
23     name = 'Text only'
24     aliases = ['text', 'null']
25     filenames = ['*.txt']
26
27     def format(self, tokensource, outfile):
28         enc = self.encoding
29         for ttype, value in tokensource:
30             if enc:
31                 outfile.write(value.encode(enc))
32             else:
33                 outfile.write(value)
34
35
36 class RawTokenFormatter(Formatter):
37     r"""
38     Format tokens as a raw representation for storing token streams.
39
40     The format is ``tokentype<TAB>repr(tokenstring)\n``. The output can later
41     be converted to a token stream with the `RawTokenLexer`, described in the
42     :doc:`lexer list <lexers>`.
43
44     Only two options are accepted:
45
46     `compress`
47         If set to ``'gz'`` or ``'bz2'``, compress the output with the given
48         compression algorithm after encoding (default: ``''``).
49     `error_color`
50         If set to a color name, highlight error tokens using that color.  If
51         set but with no value, defaults to ``'red'``.
52
53         .. versionadded:: 0.11
54
55     """
56     name = 'Raw tokens'
57     aliases = ['raw', 'tokens']
58     filenames = ['*.raw']
59
60     unicodeoutput = False
61
62     def __init__(self, **options):
63         Formatter.__init__(self, **options)
64         # We ignore self.encoding if it is set, since it gets set for lexer
65         # and formatter if given with -Oencoding on the command line.
66         # The RawTokenFormatter outputs only ASCII. Override here.
67         self.encoding = 'ascii'  # let pygments.format() do the right thing
68         self.compress = get_choice_opt(options, 'compress',
69                                        ['', 'none', 'gz', 'bz2'], '')
70         self.error_color = options.get('error_color', None)
71         if self.error_color is True:
72             self.error_color = 'red'
73         if self.error_color is not None:
74             try:
75                 colorize(self.error_color, '')
76             except KeyError:
77                 raise ValueError("Invalid color %r specified" %
78                                  self.error_color)
79
80     def format(self, tokensource, outfile):
81         try:
82             outfile.write(b'')
83         except TypeError:
84             raise TypeError('The raw tokens formatter needs a binary '
85                             'output file')
86         if self.compress == 'gz':
87             import gzip
88             outfile = gzip.GzipFile('', 'wb', 9, outfile)
89
90             write = outfile.write
91             flush = outfile.close
92         elif self.compress == 'bz2':
93             import bz2
94             compressor = bz2.BZ2Compressor(9)
95
96             def write(text):
97                 outfile.write(compressor.compress(text))
98
99             def flush():
100                 outfile.write(compressor.flush())
101                 outfile.flush()
102         else:
103             write = outfile.write
104             flush = outfile.flush
105
106         if self.error_color:
107             for ttype, value in tokensource:
108                 line = b"%r\t%r\n" % (ttype, value)
109                 if ttype is Token.Error:
110                     write(colorize(self.error_color, line))
111                 else:
112                     write(line)
113         else:
114             for ttype, value in tokensource:
115                 write(b"%r\t%r\n" % (ttype, value))
116         flush()
117
118
119 TESTCASE_BEFORE = '''\
120     def testNeedsName(lexer):
121         fragment = %r
122         tokens = [
123 '''
124 TESTCASE_AFTER = '''\
125         ]
126         assert list(lexer.get_tokens(fragment)) == tokens
127 '''
128
129
130 class TestcaseFormatter(Formatter):
131     """
132     Format tokens as appropriate for a new testcase.
133
134     .. versionadded:: 2.0
135     """
136     name = 'Testcase'
137     aliases = ['testcase']
138
139     def __init__(self, **options):
140         Formatter.__init__(self, **options)
141         if self.encoding is not None and self.encoding != 'utf-8':
142             raise ValueError("Only None and utf-8 are allowed encodings.")
143
144     def format(self, tokensource, outfile):
145         indentation = ' ' * 12
146         rawbuf = []
147         outbuf = []
148         for ttype, value in tokensource:
149             rawbuf.append(value)
150             outbuf.append('%s(%s, %r),\n' % (indentation, ttype, value))
151
152         before = TESTCASE_BEFORE % (''.join(rawbuf),)
153         during = ''.join(outbuf)
154         after = TESTCASE_AFTER
155         if self.encoding is None:
156             outfile.write(before + during + after)
157         else:
158             outfile.write(before.encode('utf-8'))
159             outfile.write(during.encode('utf-8'))
160             outfile.write(after.encode('utf-8'))
161         outfile.flush()