-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
extrakto.py
executable file
·245 lines (191 loc) · 6.92 KB
/
extrakto.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
239
240
241
242
243
244
245
#!/usr/bin/env python3
import os
import re
import sys
import traceback
from argparse import ArgumentParser
from collections import OrderedDict
from configparser import ConfigParser
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
# "words" consist of anything but the following characters:
# [](){}=$
# unicode range 2500-27BF which includes:
# - Box Drawing
# - Block Elements
# - Geometric Shapes
# - Miscellaneous Symbols
# - Dingbats
# unicode range E000-F8FF (private use/Powerline)
# and whitespace ( \t\n\r)
RE_WORD = "[^][(){}=$\u2500-\u27BF\uE000-\uF8FF \\t\\n\\r]+"
MIN_LENGTH_DEFAULT = 5
class ExtraktoException(Exception):
pass
class Extrakto:
def __init__(self, *, min_length=None, alt=False, prefix_name=False):
conf = ConfigParser(interpolation=None)
default_conf = os.path.join(SCRIPT_DIR, "extrakto.conf")
user_conf = os.path.join(
os.path.expanduser("~/.config"), "extrakto/extrakto.conf"
)
conf.read([default_conf, user_conf], encoding="utf-8")
sections = conf.sections()
if not "path" in sections or not "url" in sections:
raise ExtraktoException("extrakto.conf incomplete, path and url must exist")
self.min_length = min_length
self.alt = alt
self.prefix_name = prefix_name
self.in_all = []
self.fdict = {}
for name in sections:
sect = conf[name]
alt = []
for i in range(2, 10):
key = f"alt{i}"
# if alt2, alt{n} exists as a value in a section, create a variant based on that regex
if key in sect:
alt.append(sect[key])
if sect.getboolean("in_all", fallback=True):
self.in_all.append(name)
if sect.getboolean("enabled", fallback=True):
self.fdict[name] = FilterDef(
self,
name,
regex=sect.get("regex"),
exclude=sect.get("exclude", ""),
lstrip=sect.get("lstrip", ""),
rstrip=sect.get("rstrip", ""),
alt=alt,
# prefer global min_length, fallback to filter specific
min_length=(
self.min_length
if self.min_length is not None
else sect.getint("min_length", MIN_LENGTH_DEFAULT)
),
)
def __getitem__(self, key):
if not key in self.fdict:
raise ExtraktoException(f"Unknown filter {key}")
return self.fdict[key]
def all(self):
return self.in_all
def keys(self):
return list(self.fdict.keys())
class FilterDef:
def __init__(
self,
extrakto,
name,
*,
regex,
exclude,
lstrip,
rstrip,
alt,
min_length=MIN_LENGTH_DEFAULT,
):
self.extrakto = extrakto
self.name = name
self.regex = regex
self.exclude = exclude
self.lstrip = lstrip
self.rstrip = rstrip
self.alt = alt
self.min_length = min_length
def filter(self, text):
res = list()
if self.extrakto.prefix_name:
add = lambda name, value: res.append(f"{name}: {value}")
else:
add = lambda name, value: res.append(value)
for m in re.finditer(self.regex, "\n" + text, flags=re.I):
item = "".join(filter(None, m.groups()))
# strip invalid characters (like punctuation or markdown syntax)
if self.lstrip:
item = item.lstrip(self.lstrip)
if self.rstrip:
item = item.rstrip(self.rstrip)
if len(item) >= self.min_length:
if not self.exclude or not re.search(self.exclude, item, re.I):
if self.extrakto.alt:
for i, altre in enumerate(self.alt):
m = re.search(altre, item)
if m:
add(f"{self.name}{i+2}", m[1])
add(self.name, item)
return res
def get_lines(text, *, min_length=MIN_LENGTH_DEFAULT, prefix_name=False):
lines = []
for raw_line in text.splitlines():
line = raw_line.strip()
if len(line) >= min_length:
if prefix_name:
lines.append("line: " + line)
else:
lines.append(line)
return lines
def main(parser):
args = parser.parse_args()
run_list = []
if args.words:
run_list.append("word")
if args.paths:
run_list.append("path")
if args.urls:
run_list.append("url")
run_list += args.add
res = []
# input from the terminal can cause UnicodeDecodeErrors in some instances, ignore for now
text = sys.stdin.buffer.read().decode("utf-8", "ignore")
extrakto = Extrakto(min_length=args.min_length, alt=args.alt, prefix_name=args.name)
if args.all:
run_list = extrakto.all()
if args.lines:
res += get_lines(text, min_length=args.min_length, prefix_name=args.name)
for name in run_list:
res += extrakto[name].filter(text)
if res:
if args.reverse:
res.reverse()
# remove duplicates and print
for item in OrderedDict.fromkeys(res):
print(item)
elif args.warn_empty:
print("NO MATCH - use a different filter")
if __name__ == "__main__":
parser = ArgumentParser(description="Extracts tokens from plaintext.")
parser.add_argument(
"--name", action="store_true", help="prefix filter name in the output"
)
parser.add_argument(
"-w", "--words", action="store_true", help='extract "word" tokens'
)
parser.add_argument("-l", "--lines", action="store_true", help="extract lines")
parser.add_argument(
"--all",
action="store_true",
help="extract using all filters defined in extrakto.conf",
)
parser.add_argument(
"-a", "--add", action="append", default=[], help="add custom filter"
)
parser.add_argument("-p", "--paths", action="store_true", help="short for -a=path")
parser.add_argument("-u", "--urls", action="store_true", help="short for -a=url")
parser.add_argument(
"--alt",
action="store_true",
help="return alternate variants for each match (e.g. https://example.com and example.com)",
)
parser.add_argument("-r", "--reverse", action="store_true", help="reverse output")
parser.add_argument("-m", "--min-length", help="minimum token length", type=int)
parser.add_argument(
"--warn-empty", action="store_true", help="warn if result is empty"
)
try:
main(parser)
except ExtraktoException as e:
print(e, file=sys.stderr)
sys.exit(1)
except Exception as e:
print(traceback.format_exc(), file=sys.stderr)
sys.exit(1)