-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter-sliders
executable file
·103 lines (79 loc) · 2.71 KB
/
filter-sliders
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#! /usr/bin/env python3
"""Print out only the sliders from stdin.
Omit blank and comment lines.
usage:
filter-sliders OPTIONS
The sliders on standard input and in the SLIDERFILEs should be in the usual format:
<old-sha1>:<old-filename> <new-sha1>:<new-filename> {-/+} <line-number> [<shift>]
Filter the sliders from stdin and write them to stdout.
"""
import sys
import os
import argparse
sys.path.insert(0, os.path.dirname(sys.argv[0]))
import diff_heuristics
from diff_heuristics import SliderName
def main(args):
parser = argparse.ArgumentParser(
description='Filter the sliders on stdin and write them to stdout'
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
'--rated', action='store_true',
help='Only include rated sliders in the output',
)
group.add_argument(
'--unrated', action='store_true',
help='Only include unrated sliders in the output',
)
parser.add_argument(
'--omit-shifts', action='store_true',
help='Do not include any shifts in the output',
)
parser.add_argument(
'--only-rated', action='append', default=[],
help=(
'Include only sliders with ratings in the specified file. '
'This option can be provided multiple times.'
),
)
parser.add_argument(
'--omit-rated', action='append', default=[],
help=(
'Omit any sliders with ratings in the specified file. '
'This option can be provided multiple times.'
),
)
parser.add_argument('--verbose', '-v', action='store_true')
options = parser.parse_args(args)
if options.verbose:
diff_heuristics.verbose = True
only = set()
for filename in options.only_rated:
with open(filename) as f:
for (slider, shifts) in SliderName.read(f):
if shifts:
only.add(slider)
omit = set()
for filename in options.omit_rated:
with open(filename) as f:
for (slider, shifts) in SliderName.read(f):
if shifts:
omit.add(slider)
try:
for (slider, shifts) in SliderName.read(sys.stdin):
if options.rated and not shifts:
continue
if options.unrated and shifts:
continue
if options.only_rated and slider not in only:
continue
if options.omit_rated and slider in omit:
continue
if options.omit_shifts:
shifts = []
slider.write(sys.stdout, shifts)
except BrokenPipeError:
pass
if __name__ == '__main__':
main(sys.argv[1:])