ยปCore Development>Code coverage>Tools/scripts/pindent.py

Python code coverage for Tools/scripts/pindent.py

#countcontent
1n/a#! /usr/bin/env python3
2n/a
3n/a# This file contains a class and a main program that perform three
4n/a# related (though complimentary) formatting operations on Python
5n/a# programs. When called as "pindent -c", it takes a valid Python
6n/a# program as input and outputs a version augmented with block-closing
7n/a# comments. When called as "pindent -d", it assumes its input is a
8n/a# Python program with block-closing comments and outputs a commentless
9n/a# version. When called as "pindent -r" it assumes its input is a
10n/a# Python program with block-closing comments but with its indentation
11n/a# messed up, and outputs a properly indented version.
12n/a
13n/a# A "block-closing comment" is a comment of the form '# end <keyword>'
14n/a# where <keyword> is the keyword that opened the block. If the
15n/a# opening keyword is 'def' or 'class', the function or class name may
16n/a# be repeated in the block-closing comment as well. Here is an
17n/a# example of a program fully augmented with block-closing comments:
18n/a
19n/a# def foobar(a, b):
20n/a# if a == b:
21n/a# a = a+1
22n/a# elif a < b:
23n/a# b = b-1
24n/a# if b > a: a = a-1
25n/a# # end if
26n/a# else:
27n/a# print 'oops!'
28n/a# # end if
29n/a# # end def foobar
30n/a
31n/a# Note that only the last part of an if...elif...else... block needs a
32n/a# block-closing comment; the same is true for other compound
33n/a# statements (e.g. try...except). Also note that "short-form" blocks
34n/a# like the second 'if' in the example must be closed as well;
35n/a# otherwise the 'else' in the example would be ambiguous (remember
36n/a# that indentation is not significant when interpreting block-closing
37n/a# comments).
38n/a
39n/a# The operations are idempotent (i.e. applied to their own output
40n/a# they yield an identical result). Running first "pindent -c" and
41n/a# then "pindent -r" on a valid Python program produces a program that
42n/a# is semantically identical to the input (though its indentation may
43n/a# be different). Running "pindent -e" on that output produces a
44n/a# program that only differs from the original in indentation.
45n/a
46n/a# Other options:
47n/a# -s stepsize: set the indentation step size (default 8)
48n/a# -t tabsize : set the number of spaces a tab character is worth (default 8)
49n/a# -e : expand TABs into spaces
50n/a# file ... : input file(s) (default standard input)
51n/a# The results always go to standard output
52n/a
53n/a# Caveats:
54n/a# - comments ending in a backslash will be mistaken for continued lines
55n/a# - continuations using backslash are always left unchanged
56n/a# - continuations inside parentheses are not extra indented by -r
57n/a# but must be indented for -c to work correctly (this breaks
58n/a# idempotency!)
59n/a# - continued lines inside triple-quoted strings are totally garbled
60n/a
61n/a# Secret feature:
62n/a# - On input, a block may also be closed with an "end statement" --
63n/a# this is a block-closing comment without the '#' sign.
64n/a
65n/a# Possible improvements:
66n/a# - check syntax based on transitions in 'next' table
67n/a# - better error reporting
68n/a# - better error recovery
69n/a# - check identifier after class/def
70n/a
71n/a# The following wishes need a more complete tokenization of the source:
72n/a# - Don't get fooled by comments ending in backslash
73n/a# - reindent continuation lines indicated by backslash
74n/a# - handle continuation lines inside parentheses/braces/brackets
75n/a# - handle triple quoted strings spanning lines
76n/a# - realign comments
77n/a# - optionally do much more thorough reformatting, a la C indent
78n/a
79n/a# Defaults
80n/aSTEPSIZE = 8
81n/aTABSIZE = 8
82n/aEXPANDTABS = False
83n/a
84n/aimport io
85n/aimport re
86n/aimport sys
87n/a
88n/anext = {}
89n/anext['if'] = next['elif'] = 'elif', 'else', 'end'
90n/anext['while'] = next['for'] = 'else', 'end'
91n/anext['try'] = 'except', 'finally'
92n/anext['except'] = 'except', 'else', 'finally', 'end'
93n/anext['else'] = next['finally'] = next['with'] = \
94n/a next['def'] = next['class'] = 'end'
95n/anext['end'] = ()
96n/astart = 'if', 'while', 'for', 'try', 'with', 'def', 'class'
97n/a
98n/aclass PythonIndenter:
99n/a
100n/a def __init__(self, fpi = sys.stdin, fpo = sys.stdout,
101n/a indentsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
102n/a self.fpi = fpi
103n/a self.fpo = fpo
104n/a self.indentsize = indentsize
105n/a self.tabsize = tabsize
106n/a self.lineno = 0
107n/a self.expandtabs = expandtabs
108n/a self._write = fpo.write
109n/a self.kwprog = re.compile(
110n/a r'^(?:\s|\\\n)*(?P<kw>[a-z]+)'
111n/a r'((?:\s|\\\n)+(?P<id>[a-zA-Z_]\w*))?'
112n/a r'[^\w]')
113n/a self.endprog = re.compile(
114n/a r'^(?:\s|\\\n)*#?\s*end\s+(?P<kw>[a-z]+)'
115n/a r'(\s+(?P<id>[a-zA-Z_]\w*))?'
116n/a r'[^\w]')
117n/a self.wsprog = re.compile(r'^[ \t]*')
118n/a # end def __init__
119n/a
120n/a def write(self, line):
121n/a if self.expandtabs:
122n/a self._write(line.expandtabs(self.tabsize))
123n/a else:
124n/a self._write(line)
125n/a # end if
126n/a # end def write
127n/a
128n/a def readline(self):
129n/a line = self.fpi.readline()
130n/a if line: self.lineno += 1
131n/a # end if
132n/a return line
133n/a # end def readline
134n/a
135n/a def error(self, fmt, *args):
136n/a if args: fmt = fmt % args
137n/a # end if
138n/a sys.stderr.write('Error at line %d: %s\n' % (self.lineno, fmt))
139n/a self.write('### %s ###\n' % fmt)
140n/a # end def error
141n/a
142n/a def getline(self):
143n/a line = self.readline()
144n/a while line[-2:] == '\\\n':
145n/a line2 = self.readline()
146n/a if not line2: break
147n/a # end if
148n/a line += line2
149n/a # end while
150n/a return line
151n/a # end def getline
152n/a
153n/a def putline(self, line, indent):
154n/a tabs, spaces = divmod(indent*self.indentsize, self.tabsize)
155n/a i = self.wsprog.match(line).end()
156n/a line = line[i:]
157n/a if line[:1] not in ('\n', '\r', ''):
158n/a line = '\t'*tabs + ' '*spaces + line
159n/a # end if
160n/a self.write(line)
161n/a # end def putline
162n/a
163n/a def reformat(self):
164n/a stack = []
165n/a while True:
166n/a line = self.getline()
167n/a if not line: break # EOF
168n/a # end if
169n/a m = self.endprog.match(line)
170n/a if m:
171n/a kw = 'end'
172n/a kw2 = m.group('kw')
173n/a if not stack:
174n/a self.error('unexpected end')
175n/a elif stack.pop()[0] != kw2:
176n/a self.error('unmatched end')
177n/a # end if
178n/a self.putline(line, len(stack))
179n/a continue
180n/a # end if
181n/a m = self.kwprog.match(line)
182n/a if m:
183n/a kw = m.group('kw')
184n/a if kw in start:
185n/a self.putline(line, len(stack))
186n/a stack.append((kw, kw))
187n/a continue
188n/a # end if
189n/a if kw in next and stack:
190n/a self.putline(line, len(stack)-1)
191n/a kwa, kwb = stack[-1]
192n/a stack[-1] = kwa, kw
193n/a continue
194n/a # end if
195n/a # end if
196n/a self.putline(line, len(stack))
197n/a # end while
198n/a if stack:
199n/a self.error('unterminated keywords')
200n/a for kwa, kwb in stack:
201n/a self.write('\t%s\n' % kwa)
202n/a # end for
203n/a # end if
204n/a # end def reformat
205n/a
206n/a def delete(self):
207n/a begin_counter = 0
208n/a end_counter = 0
209n/a while True:
210n/a line = self.getline()
211n/a if not line: break # EOF
212n/a # end if
213n/a m = self.endprog.match(line)
214n/a if m:
215n/a end_counter += 1
216n/a continue
217n/a # end if
218n/a m = self.kwprog.match(line)
219n/a if m:
220n/a kw = m.group('kw')
221n/a if kw in start:
222n/a begin_counter += 1
223n/a # end if
224n/a # end if
225n/a self.write(line)
226n/a # end while
227n/a if begin_counter - end_counter < 0:
228n/a sys.stderr.write('Warning: input contained more end tags than expected\n')
229n/a elif begin_counter - end_counter > 0:
230n/a sys.stderr.write('Warning: input contained less end tags than expected\n')
231n/a # end if
232n/a # end def delete
233n/a
234n/a def complete(self):
235n/a stack = []
236n/a todo = []
237n/a currentws = thisid = firstkw = lastkw = topid = ''
238n/a while True:
239n/a line = self.getline()
240n/a i = self.wsprog.match(line).end()
241n/a m = self.endprog.match(line)
242n/a if m:
243n/a thiskw = 'end'
244n/a endkw = m.group('kw')
245n/a thisid = m.group('id')
246n/a else:
247n/a m = self.kwprog.match(line)
248n/a if m:
249n/a thiskw = m.group('kw')
250n/a if thiskw not in next:
251n/a thiskw = ''
252n/a # end if
253n/a if thiskw in ('def', 'class'):
254n/a thisid = m.group('id')
255n/a else:
256n/a thisid = ''
257n/a # end if
258n/a elif line[i:i+1] in ('\n', '#'):
259n/a todo.append(line)
260n/a continue
261n/a else:
262n/a thiskw = ''
263n/a # end if
264n/a # end if
265n/a indentws = line[:i]
266n/a indent = len(indentws.expandtabs(self.tabsize))
267n/a current = len(currentws.expandtabs(self.tabsize))
268n/a while indent < current:
269n/a if firstkw:
270n/a if topid:
271n/a s = '# end %s %s\n' % (
272n/a firstkw, topid)
273n/a else:
274n/a s = '# end %s\n' % firstkw
275n/a # end if
276n/a self.write(currentws + s)
277n/a firstkw = lastkw = ''
278n/a # end if
279n/a currentws, firstkw, lastkw, topid = stack.pop()
280n/a current = len(currentws.expandtabs(self.tabsize))
281n/a # end while
282n/a if indent == current and firstkw:
283n/a if thiskw == 'end':
284n/a if endkw != firstkw:
285n/a self.error('mismatched end')
286n/a # end if
287n/a firstkw = lastkw = ''
288n/a elif not thiskw or thiskw in start:
289n/a if topid:
290n/a s = '# end %s %s\n' % (
291n/a firstkw, topid)
292n/a else:
293n/a s = '# end %s\n' % firstkw
294n/a # end if
295n/a self.write(currentws + s)
296n/a firstkw = lastkw = topid = ''
297n/a # end if
298n/a # end if
299n/a if indent > current:
300n/a stack.append((currentws, firstkw, lastkw, topid))
301n/a if thiskw and thiskw not in start:
302n/a # error
303n/a thiskw = ''
304n/a # end if
305n/a currentws, firstkw, lastkw, topid = \
306n/a indentws, thiskw, thiskw, thisid
307n/a # end if
308n/a if thiskw:
309n/a if thiskw in start:
310n/a firstkw = lastkw = thiskw
311n/a topid = thisid
312n/a else:
313n/a lastkw = thiskw
314n/a # end if
315n/a # end if
316n/a for l in todo: self.write(l)
317n/a # end for
318n/a todo = []
319n/a if not line: break
320n/a # end if
321n/a self.write(line)
322n/a # end while
323n/a # end def complete
324n/a# end class PythonIndenter
325n/a
326n/a# Simplified user interface
327n/a# - xxx_filter(input, output): read and write file objects
328n/a# - xxx_string(s): take and return string object
329n/a# - xxx_file(filename): process file in place, return true iff changed
330n/a
331n/adef complete_filter(input = sys.stdin, output = sys.stdout,
332n/a stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
333n/a pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
334n/a pi.complete()
335n/a# end def complete_filter
336n/a
337n/adef delete_filter(input= sys.stdin, output = sys.stdout,
338n/a stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
339n/a pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
340n/a pi.delete()
341n/a# end def delete_filter
342n/a
343n/adef reformat_filter(input = sys.stdin, output = sys.stdout,
344n/a stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
345n/a pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
346n/a pi.reformat()
347n/a# end def reformat_filter
348n/a
349n/adef complete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
350n/a input = io.StringIO(source)
351n/a output = io.StringIO()
352n/a pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
353n/a pi.complete()
354n/a return output.getvalue()
355n/a# end def complete_string
356n/a
357n/adef delete_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
358n/a input = io.StringIO(source)
359n/a output = io.StringIO()
360n/a pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
361n/a pi.delete()
362n/a return output.getvalue()
363n/a# end def delete_string
364n/a
365n/adef reformat_string(source, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
366n/a input = io.StringIO(source)
367n/a output = io.StringIO()
368n/a pi = PythonIndenter(input, output, stepsize, tabsize, expandtabs)
369n/a pi.reformat()
370n/a return output.getvalue()
371n/a# end def reformat_string
372n/a
373n/adef make_backup(filename):
374n/a import os, os.path
375n/a backup = filename + '~'
376n/a if os.path.lexists(backup):
377n/a try:
378n/a os.remove(backup)
379n/a except OSError:
380n/a print("Can't remove backup %r" % (backup,), file=sys.stderr)
381n/a # end try
382n/a # end if
383n/a try:
384n/a os.rename(filename, backup)
385n/a except OSError:
386n/a print("Can't rename %r to %r" % (filename, backup), file=sys.stderr)
387n/a # end try
388n/a# end def make_backup
389n/a
390n/adef complete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
391n/a with open(filename, 'r') as f:
392n/a source = f.read()
393n/a # end with
394n/a result = complete_string(source, stepsize, tabsize, expandtabs)
395n/a if source == result: return 0
396n/a # end if
397n/a make_backup(filename)
398n/a with open(filename, 'w') as f:
399n/a f.write(result)
400n/a # end with
401n/a return 1
402n/a# end def complete_file
403n/a
404n/adef delete_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
405n/a with open(filename, 'r') as f:
406n/a source = f.read()
407n/a # end with
408n/a result = delete_string(source, stepsize, tabsize, expandtabs)
409n/a if source == result: return 0
410n/a # end if
411n/a make_backup(filename)
412n/a with open(filename, 'w') as f:
413n/a f.write(result)
414n/a # end with
415n/a return 1
416n/a# end def delete_file
417n/a
418n/adef reformat_file(filename, stepsize = STEPSIZE, tabsize = TABSIZE, expandtabs = EXPANDTABS):
419n/a with open(filename, 'r') as f:
420n/a source = f.read()
421n/a # end with
422n/a result = reformat_string(source, stepsize, tabsize, expandtabs)
423n/a if source == result: return 0
424n/a # end if
425n/a make_backup(filename)
426n/a with open(filename, 'w') as f:
427n/a f.write(result)
428n/a # end with
429n/a return 1
430n/a# end def reformat_file
431n/a
432n/a# Test program when called as a script
433n/a
434n/ausage = """
435n/ausage: pindent (-c|-d|-r) [-s stepsize] [-t tabsize] [-e] [file] ...
436n/a-c : complete a correctly indented program (add #end directives)
437n/a-d : delete #end directives
438n/a-r : reformat a completed program (use #end directives)
439n/a-s stepsize: indentation step (default %(STEPSIZE)d)
440n/a-t tabsize : the worth in spaces of a tab (default %(TABSIZE)d)
441n/a-e : expand TABs into spaces (default OFF)
442n/a[file] ... : files are changed in place, with backups in file~
443n/aIf no files are specified or a single - is given,
444n/athe program acts as a filter (reads stdin, writes stdout).
445n/a""" % vars()
446n/a
447n/adef error_both(op1, op2):
448n/a sys.stderr.write('Error: You can not specify both '+op1+' and -'+op2[0]+' at the same time\n')
449n/a sys.stderr.write(usage)
450n/a sys.exit(2)
451n/a# end def error_both
452n/a
453n/adef test():
454n/a import getopt
455n/a try:
456n/a opts, args = getopt.getopt(sys.argv[1:], 'cdrs:t:e')
457n/a except getopt.error as msg:
458n/a sys.stderr.write('Error: %s\n' % msg)
459n/a sys.stderr.write(usage)
460n/a sys.exit(2)
461n/a # end try
462n/a action = None
463n/a stepsize = STEPSIZE
464n/a tabsize = TABSIZE
465n/a expandtabs = EXPANDTABS
466n/a for o, a in opts:
467n/a if o == '-c':
468n/a if action: error_both(o, action)
469n/a # end if
470n/a action = 'complete'
471n/a elif o == '-d':
472n/a if action: error_both(o, action)
473n/a # end if
474n/a action = 'delete'
475n/a elif o == '-r':
476n/a if action: error_both(o, action)
477n/a # end if
478n/a action = 'reformat'
479n/a elif o == '-s':
480n/a stepsize = int(a)
481n/a elif o == '-t':
482n/a tabsize = int(a)
483n/a elif o == '-e':
484n/a expandtabs = True
485n/a # end if
486n/a # end for
487n/a if not action:
488n/a sys.stderr.write(
489n/a 'You must specify -c(omplete), -d(elete) or -r(eformat)\n')
490n/a sys.stderr.write(usage)
491n/a sys.exit(2)
492n/a # end if
493n/a if not args or args == ['-']:
494n/a action = eval(action + '_filter')
495n/a action(sys.stdin, sys.stdout, stepsize, tabsize, expandtabs)
496n/a else:
497n/a action = eval(action + '_file')
498n/a for filename in args:
499n/a action(filename, stepsize, tabsize, expandtabs)
500n/a # end for
501n/a # end if
502n/a# end def test
503n/a
504n/aif __name__ == '__main__':
505n/a test()
506n/a# end if