]> Sergey Matveev's repositories - dotfiles.git/blob - bin/bin/mail_dup_hdrs_remove.py
Huge tmux-fzf simplification with files quoting
[dotfiles.git] / bin / bin / mail_dup_hdrs_remove.py
1 #!/usr/bin/env python3.6
2
3 import sys
4
5 fn = sys.argv[1]
6 out = sys.argv[2]
7 with open(fn, "rb") as fd:
8     lines = fd.read().split(b"\n")
9
10 def dup(lines, what):
11     idx = []
12     met = False
13     for i, line in enumerate(lines):
14         if line == "":
15             break
16         if met:
17             if line.startswith(b" "):
18                 idx.append(i)
19                 continue
20             else:
21                 met = False
22         if line.startswith(what):
23             if len(idx) == 0:
24                 idx.append(i)
25                 met = True
26             else:
27                 return [l for n, l in enumerate(lines) if n not in idx]
28     return lines
29
30 lines = dup(lines, b"To")
31 lines = dup(lines, b"From")
32 lines = dup(lines, b"Subject")
33 with open(out, "wb") as fd:
34     fd.write(b"\n".join(lines))