File: remove-comments.py

package info (click to toggle)
rdiff-backup 2.2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,640 kB
  • sloc: python: 24,129; javascript: 9,512; sh: 1,230; ansic: 580; makefile: 36
file content (38 lines) | stat: -rwxr-xr-x 856 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
"""remove-comments.py

Given a python program on standard input, spit one out on stdout that
should work the same, but has blank and comment lines removed.

"""

import sys
import re

triple_regex = re.compile('"""')


def eattriple(initial_line_stripped):
    """Keep reading until end of doc string"""
    assert initial_line_stripped.startswith('"""')
    if triple_regex.search(initial_line_stripped[3:]):
        return
    while 1:
        line = sys.stdin.readline()
        if not line or triple_regex.search(line):
            break


while 1:
    line = sys.stdin.readline()
    if not line:
        break
    stripped = line.strip()
    if not stripped:
        continue
    if stripped[0] == "#":
        continue
    if stripped.startswith('"""'):
        eattriple(stripped)
        continue
    sys.stdout.write(line)