5 from collections
import namedtuple
9 "Config", [
"remove_lines",
"replace_lines",
"ignore_files"], defaults=[[], [], []]
14 skip_file: bool =
False
19 for s
in config[
"remove_lines"]:
20 remove_lines.append(re.compile(s))
23 for s
in config[
"replace_lines"]:
24 s, r = list(s.items())[0]
25 replace_lines.append((re.compile(s), r))
28 for s
in config[
"ignore_files"]:
29 ignore_files.append(re.compile(s))
32 remove_lines=remove_lines,
33 replace_lines=replace_lines,
34 ignore_files=ignore_files,
38 def filter(line: str, config: Config, state: State):
40 if line.endswith(
"---\n"):
41 state.skip_file =
False
44 if line.endswith(
" should add these lines:\n"):
45 for s
in config.ignore_files:
47 state.skip_file =
True
50 for s
in config.remove_lines:
54 for s, r
in config.replace_lines:
61 parser = argparse.ArgumentParser()
62 parser.add_argument(
"config")
63 parser.add_argument(
"input")
64 parser.add_argument(
"output")
65 args = parser.parse_args()
67 with
open(args.config,
"r") as config_file:
69 config = yaml.safe_load(config_file)
71 except yaml.YAMLError
as exc:
75 with
open(args.input,
"r") as input_file, open(args.output, "w") as output_file:
78 for line
in input_file:
79 filtered_line =
filter(line, config, state)
80 if filtered_line
is not None:
81 output_file.write(filtered_line)