Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/formatters/other.py: 51%
86 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:45 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:45 +0000
1"""
2 pygments.formatters.other
3 ~~~~~~~~~~~~~~~~~~~~~~~~~
5 Other formatters: NullFormatter, RawTokenFormatter.
7 :copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
11from pygments.formatter import Formatter
12from pygments.util import get_choice_opt
13from pygments.token import Token
14from pygments.console import colorize
16__all__ = ['NullFormatter', 'RawTokenFormatter', 'TestcaseFormatter']
19class NullFormatter(Formatter):
20 """
21 Output the text unchanged without any formatting.
22 """
23 name = 'Text only'
24 aliases = ['text', 'null']
25 filenames = ['*.txt']
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)
36class RawTokenFormatter(Formatter):
37 r"""
38 Format tokens as a raw representation for storing token streams.
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>`.
44 Only two options are accepted:
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'``.
53 .. versionadded:: 0.11
55 """
56 name = 'Raw tokens'
57 aliases = ['raw', 'tokens']
58 filenames = ['*.raw']
60 unicodeoutput = False
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)
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)
90 write = outfile.write
91 flush = outfile.close
92 elif self.compress == 'bz2':
93 import bz2
94 compressor = bz2.BZ2Compressor(9)
96 def write(text):
97 outfile.write(compressor.compress(text))
99 def flush():
100 outfile.write(compressor.flush())
101 outfile.flush()
102 else:
103 write = outfile.write
104 flush = outfile.flush
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()
119TESTCASE_BEFORE = '''\
120 def testNeedsName(lexer):
121 fragment = %r
122 tokens = [
123'''
124TESTCASE_AFTER = '''\
125 ]
126 assert list(lexer.get_tokens(fragment)) == tokens
127'''
130class TestcaseFormatter(Formatter):
131 """
132 Format tokens as appropriate for a new testcase.
134 .. versionadded:: 2.0
135 """
136 name = 'Testcase'
137 aliases = ['testcase']
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.")
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))
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()