Path: blob/master/venv/Lib/site-packages/lxml/html/_diffcommand.py
811 views
from __future__ import absolute_import12import optparse3import sys4import re5import os6from .diff import htmldiff78description = """\9"""1011parser = optparse.OptionParser(12usage="%prog [OPTIONS] FILE1 FILE2\n"13"%prog --annotate [OPTIONS] INFO1 FILE1 INFO2 FILE2 ...",14description=description,15)1617parser.add_option(18'-o', '--output',19metavar="FILE",20dest="output",21default="-",22help="File to write the difference to",23)2425parser.add_option(26'-a', '--annotation',27action="store_true",28dest="annotation",29help="Do an annotation")3031def main(args=None):32if args is None:33args = sys.argv[1:]34options, args = parser.parse_args(args)35if options.annotation:36return annotate(options, args)37if len(args) != 2:38print('Error: you must give two files')39parser.print_help()40sys.exit(1)41file1, file2 = args42input1 = read_file(file1)43input2 = read_file(file2)44body1 = split_body(input1)[1]45pre, body2, post = split_body(input2)46result = htmldiff(body1, body2)47result = pre + result + post48if options.output == '-':49if not result.endswith('\n'):50result += '\n'51sys.stdout.write(result)52else:53with open(options.output, 'wb') as f:54f.write(result)5556def read_file(filename):57if filename == '-':58c = sys.stdin.read()59elif not os.path.exists(filename):60raise OSError(61"Input file %s does not exist" % filename)62else:63with open(filename, 'rb') as f:64c = f.read()65return c6667body_start_re = re.compile(68r"<body.*?>", re.I|re.S)69body_end_re = re.compile(70r"</body.*?>", re.I|re.S)7172def split_body(html):73pre = post = ''74match = body_start_re.search(html)75if match:76pre = html[:match.end()]77html = html[match.end():]78match = body_end_re.search(html)79if match:80post = html[match.start():]81html = html[:match.start()]82return pre, html, post8384def annotate(options, args):85print("Not yet implemented")86sys.exit(1)87888990