-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvrt-redact-long
executable file
·329 lines (256 loc) · 10.5 KB
/
vrt-redact-long
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#! /usr/bin/env python3
# -*- mode: Python; -*-
from itertools import groupby, chain
from collections import OrderedDict
import os, re, sys, traceback
from vrtargslib import trans_args, trans_main
from vrtargslib import BadData, BadCode
from vrtnamelib import isbinnames, binnamelist, nameindices
from vrtdatalib import binasrecord
from vrtdatalib import binunescape as unescape, binescape as escape
from libvrt.nameargs import bagtype, parsenames
from libvrt.structargs import structattrbagtype, parsestructattrs
from libvrt.metaline import unescape as metaunescape, escape as metaescape
from libvrt.metaline import starttag
def parsearguments():
description = '''
Redact the value in the specified field if it is "long" and looks
like a word or link or something else, or regardless of what it
looks like. The redaction text replaces all text in the field
after and before the specified number of original characters.
This process is remarkably irreversible.
'''
parser = trans_args(description = description)
parser.add_argument('--field', '-f',
metavar = 'name,*',
dest = 'fields', default = [],
type = bagtype, action = 'append',
help = '''
fields to redact, separate names by commas or
spaces, or repeat the option (default: word,
or none if --struct is specified)
''')
parser.add_argument('--word', '-w',
metavar = 'name,*',
dest = 'fields', default = [],
type = bagtype, action = 'append',
help = '(deprecated) use --field/-f instead')
parser.add_argument('--struct', '-s',
metavar = 'attr,*',
dest = 'structs', default = [],
type = structattrbagtype, action = 'append',
help = '''
structural attributes to redact, of the form
"struct_attrname" or
"struct:attrname1,attrname2" separate
structure or attribute names by commas or
spaces, or repeat the option (default: none)
''')
parser.add_argument('--long', type = int, default = 70,
metavar = 'N',
# number should be *positive*
help = '''
how many characters (or more) is long
(default: 70)
''')
parser.add_argument('--after', '-A', type = int, default = 20,
metavar = 'N',
# number should be *a number*
help = '''
the number of initial characters to keep
(default: 20)
''')
parser.add_argument('--before', '-B', type = int, default = 12,
metavar = 'N',
# number should be *a number*
help = '''
the number of final characters to keep
(default: 12)
''')
parser.add_argument('--bytes', action = 'store_true',
help = '''
the arguments of --long, --after and --before
denote bytes instead of characters; to keep
the UTF-8 encoding valid, the prefix and
suffix of the redacted value may be slightly
shorter than specified with --after and
--before
''')
parser.add_argument('--like', choices = [ 'word',
'link',
'other',
'any' ],
default = 'any',
help = '''
redact only values that look like they might
be of the specified type: a "word" consists of
letters, digits, and hyphens (and
underscores); a "link" is meant to look like a
URI but is recognized only approximately
(default: any)
''')
parser.add_argument('--text', '-t', default = '/REDACTED/',
help = '''
the text with which to replace the long text
(default: /REDACTED/)
''')
args = parser.parse_args()
args.prog = parser.prog
return args
def main(args, inf, ouf):
if args.fields:
args.fields = parsenames(args.fields)
elif not args.structs:
# default to --field word
args.fields = [b'word']
if args.structs:
args.structs = parsestructattrs(args.structs, attrstype=set)
if args.after < 0 or args.before < 0 or args.long < 1:
raise BadData('numbers make no sense')
if args.after + args.before > args.long:
raise BadData('after and before are already long')
if args.bytes:
args.text = args.text.encode('UTF-8')
# *all* these exceptions should probably go to trans_main, q.v.
status = 1
try:
implement_main(args, inf, ouf)
status = 0
except BadData as exn:
print(args.prog + ':', exn, file = sys.stderr)
except BadCode as exn:
print(args.prog + ':', exn, file = sys.stderr)
except BrokenPipeError:
print(args.prog + ': broken pipe in main', file = sys.stderr)
except KeyboardInterrupt:
print(args.prog + ': keyboard interrupt', file = sys.stderr)
except Exception as exn:
print(traceback.format_exc(), file = sys.stderr)
return status
def implement_main(args, ins, ous):
def issome(line): return not line.isspace()
def ismeta(line): return line.startswith(b'<')
if args.bytes:
maybe_redact = maybe_redact_bin
islike = dict(word = binislikeword,
link = binislikelink,
other = binisother,
any = isany) [args.like]
else:
maybe_redact = maybe_redact_str
islike = dict(word = islikeword,
link = islikelink,
other = isother,
any = isany) [args.like]
wordixs = None
for groupismeta, group in groupby(filter(issome, ins), ismeta):
if groupismeta:
for line in group:
if isbinnames(line):
wordixs = nameindices(binnamelist(line), *args.fields)
elif args.structs and line[1] not in b'/!' and b' ' in line:
line = redact_struct_attrs(args, line, maybe_redact, islike)
ous.write(line)
continue
# groupisdata aka token lines
if not args.fields:
for line in group:
ous.write(line)
continue
if wordixs is None:
raise BadData('no names before tokens')
for line in group:
record = binasrecord(line)
for wordix in wordixs:
record[wordix] = maybe_redact(
args, record[wordix], islike, escape, unescape)
ous.write(b'\t'.join(record) + b'\n')
def redact_struct_attrs(args, line, maybe_redact, islike):
def attributes(line):
return OrderedDict(re.findall(br'(\S+?)="([^"]*)"', line))
struct, _, attrstr = line[1:].partition(b' ')
if struct in args.structs:
attrs = attributes(attrstr)
for attrname in args.structs[struct]:
attrval = attrs.get(attrname)
if attrval is None:
continue
attrs[attrname] = maybe_redact(
args, attrval, islike, metaescape, metaunescape)
return starttag(struct, attrs)
else:
return line
def maybe_redact_str(args, binvalue, islike, escape, unescape):
value = unescape(binvalue).decode('UTF-8')
if len(value) >= args.long and islike(value):
return escape(redact(args, value).encode('UTF-8'))
return binvalue
def maybe_redact_bin(args, binvalue, islike, escape, unescape):
value = unescape(binvalue)
if len(value) >= args.long and islike(value):
return escape(binredact(args, value))
return binvalue
def redact(args, word):
return '{}{}{}'.format(word[:args.after],
args.text,
word[-args.before:] if args.before else '')
def binredact(args, word):
return (binprefix(word, args.after)
+ args.text
+ (binsuffix(word, args.before) if args.before else b''))
def binprefix(bs, length):
result = bs[:length]
# Would the validity of a UTF-8 byte string be checked more easily
# and efficiently?
while len(result) > 0:
# remove additional bytes from the end until the result is
# valid UTF-8
try:
_ = result.decode('UTF-8')
except UnicodeDecodeError:
result = result[:-1]
else:
break
return result
def binsuffix(bs, length):
result = bs[-length:]
while len(result) > 0:
# remove additional bytes from the beginning until the result
# is valid UTF-8
try:
_ = result.decode('UTF-8')
except UnicodeDecodeError:
result = result[1:]
else:
break
return result
def islikeword(word):
return re.fullmatch(R'[\w\-]+', word)
def islikelink(word):
# embarrassingly approximate - improve eventually? though ocular
# inspection of a corpus indicates that "any" is a good choice
if word.startswith(('http:', 'https:', 'ftp:')):
return True
return False
def isother(word):
return not (
islikeword(word) or
islikelink(word)
)
def isany(word):
return True
def binislikeword(word):
# allow Unicode letters; re.fullmatch on bytes would only take
# ASCII letters into account
return islikeword(word.decode('UTF-8'))
def binislikelink(word):
if word.startswith((b'http:', b'https:', b'ftp:')):
return True
return False
def binisother(word):
return not (
binislikeword(word) or
binislikelink(word)
)
if __name__ == '__main__':
trans_main(parsearguments(), main, in_as_text=False, out_as_text=False)