forked from Cirn09/calibre-do-not-translate-my-path
/
xmltool.py
69 lines (49 loc) · 1.84 KB
/
xmltool.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
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
import hashlib
import click
from lxml import etree
def _find(xml: str, filename: str):
with open(xml, "rb") as f:
root: etree._Element = etree.XML(f.read()) # type:ignore
r: list[etree._Element] = root.xpath(f'//td[contains(.,"|{filename}")]/..')
if len(r) != 1:
raise ValueError(
f'xpath "//td[contains(.,"|{filename}")]/.." result more than 1'
)
target: etree._Element = list(r[0])[0] # type:ignore
path = target.attrib["href"]
print(path)
return path
def _patch(xml: str, filename: str, newfile: str):
with open(newfile, "rb") as f:
md5 = hashlib.md5(f.read()).hexdigest()
print(f"new hash: {md5}")
with open(xml, "rb") as f:
root: etree._Element = etree.XML(f.read()) # type:ignore
r: list[etree._Element] = root.xpath(f'//td[contains(.,"|{filename}")]/..')
if len(r) != 1:
raise ValueError(
f'xpath "//td[contains(.,"|{filename}")]/.." result more than 1'
)
target: etree._Element = list(r[0])[0] # type:ignore
old_md5 = target.attrib["md5"]
with open(xml, "r", encoding="utf8") as f:
data = f.read()
data = data.replace(old_md5, md5)
with open(xml, "w", encoding="utf8") as f:
f.write(data)
@click.group()
def cli():
...
@cli.command()
@click.argument("xml", required=True, type=click.Path(exists=True))
@click.argument("filename", required=True, type=click.Path(exists=False))
def find(xml: str, filename: str):
_find(xml, filename)
@cli.command()
@click.argument("xml", required=True, type=click.Path(exists=True))
@click.argument("filename", required=True, type=click.Path(exists=False))
@click.argument("newfile", required=True, type=click.Path(exists=True))
def patch(xml: str, filename: str, newfile: str):
_patch(xml, filename, newfile)
if __name__ == "__main__":
cli()