forked from Animenosekai/japanterebi-xmltv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix.py
38 lines (30 loc) · 931 Bytes
/
fix.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
import argparse
import pathlib
import re
# The '&' character is not escaped in some XMLTV document.
REGEX = re.compile(r"&(?!amp;)(?!lt;)(?!gt;)(?!apos;)(?!quot;)")
def fix(data: str):
"""
Fixes the XMLTV document.
Returns
-------
str
The fixed document.
"""
return REGEX.sub("&", data)
def entry():
"""The main entrypoint"""
parser = argparse.ArgumentParser(description="Fixes the XMLTV document")
parser.add_argument("--input", "-i", type=pathlib.Path, help="Input file")
parser.add_argument("output", type=pathlib.Path, help="Output file")
args = parser.parse_args()
with pathlib.Path(args.input).open() as file:
data = file.read()
fixed = fix(data)
stdout = not (args.output and args.output != "-")
if stdout:
print(fixed)
else:
pathlib.Path(args.output).write_text(fixed)
if __name__ == "__main__":
entry()