-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrep.py
executable file
·238 lines (194 loc) · 5.94 KB
/
grep.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python3
import json
import re
import sys
from argparse import ArgumentParser
from collections import namedtuple
from functools import partial
from colorama import Fore, Style
WIKIDOT_SITE_REGEX = re.compile(r"^https?://([^\.]+)\.wikidot\.com/.+")
USE_COLOR = None
RegexOptions = namedtuple('RegexOptions', ('invert', 'flags', 'sites'))
Match = namedtuple('Match', ('line_number', 'line_content', 'spans'))
# Always open files using UTF-8
open = partial(open, encoding='utf-8')
# Utility functions
def get_color_use(option):
if option == 'always':
return True
elif option == 'never':
return False
else:
return sys.stdout.isatty()
def get_regex_options(args):
flags = re.MULTILINE
if args.ignore_case:
flags |= re.IGNORECASE
if args.filter_sites:
sites = args.filter_sites.split(",")
else:
sites = None
return RegexOptions(
invert=args.invert_match,
flags=flags,
sites=sites,
)
def eprint(message):
if USE_COLOR:
message = f"{Fore.RED}{message}{Fore.RESET}"
print(message, file=sys.stderr)
def load(path):
with open(path) as file:
data = json.load(file)
return data['pages']
# Search
def grep(regex, pages, options):
if options.invert:
def line_matches(line):
match = regex.search(line)
return (0, 0) if match is None else ()
else:
def line_matches(line):
return [match.span() for match in regex.finditer(line)]
page_matches = {}
for slug, page in pages.items():
lines = page['source'].split('\n')
site = WIKIDOT_SITE_REGEX.match(page['url'])[1]
if options.sites:
# Check site filter
if site not in options.sites:
continue
matches = []
for i, line in enumerate(lines):
spans = line_matches(line)
if spans:
matches.append(Match(
line_number=i,
line_content=line,
spans=spans,
))
if matches:
page_matches[(site, slug)] = matches
return page_matches
# Printing results
def print_filename(site, slug):
if USE_COLOR:
print(f"({Fore.BLUE}{site}{Fore.RESET}) {Fore.MAGENTA}{slug}{Fore.RESET}:", end='')
else:
print(f"({site}) {slug}:", end='')
def print_line_no(line_number):
if USE_COLOR:
print(f"{Fore.GREEN}{line_number}{Fore.RESET}:", end='')
else:
print(f"{line_number}:", end='')
def print_line_matches(match):
if USE_COLOR:
assert match.spans, "Spans list is empty"
# Slice out matches
index = 0
for start, end in match.spans:
message = ''.join((
# Before
match.line_content[index:start],
# Match
Fore.CYAN,
Style.BRIGHT,
match.line_content[start:end],
Style.RESET_ALL,
))
print(message, end='')
# After
print(match.line_content[end:], end='')
else:
print(match.line_content, end='')
def print_match_compact(site, slug, matches):
for match in matches:
print_filename(site, slug)
print_line_no(match.line_number)
print_line_matches(match)
print()
def print_match_page(site, slug, matches):
print_filename(site, slug)
print()
for match in matches:
print_line_no(match.line_number)
print_line_matches(match)
print()
print()
def print_grep_results(page_matches, compact):
print_match = print_match_compact if compact else print_match_page
for (site, slug), matches in page_matches.items():
print_match(site, slug, matches)
if __name__ == '__main__':
argparser = ArgumentParser(description="grep for wikidot sites")
argparser.add_argument(
"-F",
"--fixed",
"--fixed-string",
action="store_true",
default=False,
dest="fixed_string",
help="Search for a string literal rather than a regular expression",
)
argparser.add_argument(
"-i",
"--ignore-case",
action="store_true",
default=False,
help="Whether to ignore case when searching",
)
argparser.add_argument(
"-v",
"--invert-match",
action="store_true",
default=False,
help="Invert the sense of matching, selecting all lines which don't match",
)
argparser.add_argument(
"--compact",
action="store_true",
default=False,
help="Whether to display the results in compact / line mode",
)
argparser.add_argument(
"--color",
"--colour",
default="auto",
choices=["always", "never", "auto"],
help="Whether to use colors to highlight results",
)
argparser.add_argument(
"-S",
"--site",
default=None,
dest="filter_sites",
help="Only search from the following sites (comma-separated)",
)
argparser.add_argument(
"pattern",
help="The regular expression to search for",
)
argparser.add_argument(
"path",
nargs="?",
default="output/results.json",
help="The file containing page sources to look through",
)
args = argparser.parse_args()
options = get_regex_options(args)
USE_COLOR = get_color_use(args.color)
try:
pattern = args.pattern
if args.fixed_string:
pattern = re.escape(pattern)
regex = re.compile(pattern, options.flags)
except re.error as error:
eprint(f"Invalid regular expression: {error}")
sys.exit(1)
try:
pages = load(args.path)
except (FileNotFoundError, json.JSONDecodeError) as error:
eprint(f"Unable to load page data: {error}")
sys.exit(1)
results = grep(regex, pages, options)
print_grep_results(results, args.compact)