src/script_text_replace.py
1"""Replace literal text within UTF-8 lines without buffering a complete stream."""
2from script_support import parameters, lines, infer, preview
3from xstream_operator import run_operator
4
5
6def validate(params):
7 p = parameters(params, ('find',), ('replacement',))
8 if not isinstance(p['find'], str) or not p['find'] or '\n' in p['find'] or '\r' in p['find']:
9 raise ValueError('find must be nonempty text without line endings')
10 if not isinstance(p.get('replacement', ''), str):
11 raise ValueError('replacement must be text')
12 if len(p['find']) > 4096 or len(p.get('replacement', '')) > 4096:
13 raise ValueError('find and replacement are limited to 4096 characters')
14 return p
15
16
17def process(ctx):
18 p = validate(ctx.params)
19 out = ctx.bytes_output('result', 'text')
20 for line in lines(ctx.input('source')):
21 # Split/join avoids allocating an expanded line when a short match repeats.
22 for index, part in enumerate(line.split(p['find'])):
23 if index:
24 out.write(p.get('replacement', '').encode('utf-8'))
25 if part:
26 out.write(part.encode('utf-8'))
27
28
29def infer_outputs(inputs, params):
30 return infer(inputs, params, validate)
31
32
33def preview_outputs(inputs, params, limits):
34 return preview(process, inputs, params, limits)
35
36
37if __name__ == '__main__':
38 run_operator(process, infer_outputs, preview_outputs=preview_outputs)