Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hhhrrrttt222111
GitHub Repository: hhhrrrttt222111/Dorkify
Path: blob/master/venv/Lib/site-packages/lxml/html/_diffcommand.py
811 views
1
from __future__ import absolute_import
2
3
import optparse
4
import sys
5
import re
6
import os
7
from .diff import htmldiff
8
9
description = """\
10
"""
11
12
parser = optparse.OptionParser(
13
usage="%prog [OPTIONS] FILE1 FILE2\n"
14
"%prog --annotate [OPTIONS] INFO1 FILE1 INFO2 FILE2 ...",
15
description=description,
16
)
17
18
parser.add_option(
19
'-o', '--output',
20
metavar="FILE",
21
dest="output",
22
default="-",
23
help="File to write the difference to",
24
)
25
26
parser.add_option(
27
'-a', '--annotation',
28
action="store_true",
29
dest="annotation",
30
help="Do an annotation")
31
32
def main(args=None):
33
if args is None:
34
args = sys.argv[1:]
35
options, args = parser.parse_args(args)
36
if options.annotation:
37
return annotate(options, args)
38
if len(args) != 2:
39
print('Error: you must give two files')
40
parser.print_help()
41
sys.exit(1)
42
file1, file2 = args
43
input1 = read_file(file1)
44
input2 = read_file(file2)
45
body1 = split_body(input1)[1]
46
pre, body2, post = split_body(input2)
47
result = htmldiff(body1, body2)
48
result = pre + result + post
49
if options.output == '-':
50
if not result.endswith('\n'):
51
result += '\n'
52
sys.stdout.write(result)
53
else:
54
with open(options.output, 'wb') as f:
55
f.write(result)
56
57
def read_file(filename):
58
if filename == '-':
59
c = sys.stdin.read()
60
elif not os.path.exists(filename):
61
raise OSError(
62
"Input file %s does not exist" % filename)
63
else:
64
with open(filename, 'rb') as f:
65
c = f.read()
66
return c
67
68
body_start_re = re.compile(
69
r"<body.*?>", re.I|re.S)
70
body_end_re = re.compile(
71
r"</body.*?>", re.I|re.S)
72
73
def split_body(html):
74
pre = post = ''
75
match = body_start_re.search(html)
76
if match:
77
pre = html[:match.end()]
78
html = html[match.end():]
79
match = body_end_re.search(html)
80
if match:
81
post = html[match.start():]
82
html = html[:match.start()]
83
return pre, html, post
84
85
def annotate(options, args):
86
print("Not yet implemented")
87
sys.exit(1)
88
89
90