本文整理汇总了Python中difflib.HtmlDiff方法的典型用法代码示例。如果您正苦于以下问题:Python difflib.HtmlDiff方法的具体用法?Python difflib.HtmlDiff怎么用?Python difflib.HtmlDiff使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类difflib
的用法示例。
在下文中一共展示了difflib.HtmlDiff方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: html_diff
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def html_diff(self, fromfile, tofile):
try:
import difflib
fromlines = open(fromfile, 'U').readlines()
tolines = open(tofile, 'U').readlines()
diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile)
path = '/tmp/' + os.path.basename(fromfile) + '_' + os.path.basename(tofile) + '_diff.html'
f = open(path,'w')
f.write(diff)
f.close()
return path
#import webbrowser
#webbrowser.open_new_tab(path)
except Exception as e:
self.logger.error("failed to diff files %s, %s: %s", fromfile, tofile, str(e))
return None
示例2: make_diff
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def make_diff(old, new):
"""
Render in HTML the diff between two texts
"""
df = HtmlDiff()
old_lines = old.splitlines(1)
new_lines = new.splitlines(1)
html = df.make_table(old_lines, new_lines, context=True)
html = html.replace(' nowrap="nowrap"','')
return html
示例3: test_main
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def test_main():
difflib.HtmlDiff._default_prefix = 0
Doctests = doctest.DocTestSuite(difflib)
run_unittest(
TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs,
TestOutputFormat, TestJunkAPIs)
示例4: test_main
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def test_main():
difflib.HtmlDiff._default_prefix = 0
Doctests = doctest.DocTestSuite(difflib)
run_unittest(
TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs,
TestOutputFormat, Doctests)
示例5: main
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def main():
usage = "usage: %prog [options] fromfile tofile"
parser = optparse.OptionParser(usage)
parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)')
parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff')
parser.add_option("-m", action="store_true", default=False, help='Produce HTML side by side diff (can use -c and -l in conjunction)')
parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff')
parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)')
(options, args) = parser.parse_args()
if len(args) == 0:
parser.print_help()
sys.exit(1)
if len(args) != 2:
parser.error("need to specify both a fromfile and tofile")
n = options.lines
fromfile, tofile = args
fromdate = time.ctime(os.stat(fromfile).st_mtime)
todate = time.ctime(os.stat(tofile).st_mtime)
fromlines = open(fromfile, 'U').readlines()
tolines = open(tofile, 'U').readlines()
if options.u:
diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
elif options.n:
diff = difflib.ndiff(fromlines, tolines)
elif options.m:
diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
else:
diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
sys.stdout.writelines(diff)
示例6: main
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-c', action='store_true', default=False,
help='Produce a context format diff (default)')
parser.add_argument('-u', action='store_true', default=False,
help='Produce a unified format diff')
parser.add_argument('-m', action='store_true', default=False,
help='Produce HTML side by side diff '
'(can use -c and -l in conjunction)')
parser.add_argument('-n', action='store_true', default=False,
help='Produce a ndiff format diff')
parser.add_argument('-l', '--lines', type=int, default=3,
help='Set number of context lines (default 3)')
parser.add_argument('fromfile')
parser.add_argument('tofile')
options = parser.parse_args()
n = options.lines
fromfile = options.fromfile
tofile = options.tofile
fromdate = file_mtime(fromfile)
todate = file_mtime(tofile)
with open(fromfile) as ff:
fromlines = ff.readlines()
with open(tofile) as tf:
tolines = tf.readlines()
if options.u:
diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
elif options.n:
diff = difflib.ndiff(fromlines, tolines)
elif options.m:
diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
else:
diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
sys.stdout.writelines(diff)
示例7: test_make_file_default_charset
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def test_make_file_default_charset(self):
html_diff = difflib.HtmlDiff()
output = html_diff.make_file(patch914575_from1.splitlines(),
patch914575_to1.splitlines())
self.assertIn('content="text/html; charset=utf-8"', output)
示例8: test_make_file_iso88591_charset
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def test_make_file_iso88591_charset(self):
html_diff = difflib.HtmlDiff()
output = html_diff.make_file(patch914575_from1.splitlines(),
patch914575_to1.splitlines(),
charset='iso-8859-1')
self.assertIn('content="text/html; charset=iso-8859-1"', output)
示例9: test_make_file_usascii_charset_with_nonascii_input
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def test_make_file_usascii_charset_with_nonascii_input(self):
html_diff = difflib.HtmlDiff()
output = html_diff.make_file(patch914575_nonascii_from1.splitlines(),
patch914575_nonascii_to1.splitlines(),
charset='us-ascii')
self.assertIn('content="text/html; charset=us-ascii"', output)
self.assertIn('ımplıcıt', output)
示例10: test_main
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def test_main():
difflib.HtmlDiff._default_prefix = 0
Doctests = doctest.DocTestSuite(difflib)
run_unittest(
TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs,
TestOutputFormat, TestBytes, Doctests)
示例11: test_main
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def test_main():
difflib.HtmlDiff._default_prefix = 0
Doctests = doctest.DocTestSuite(difflib)
run_unittest(
TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs,
TestOutputFormat, TestJunkAPIs, Doctests)
示例12: showDiff
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def showDiff():
s1 = request.args.get('s1', '')
s2 = request.args.get('s2', '')
p1 = Paste.Paste(s1)
p2 = Paste.Paste(s2)
maxLengthLine1 = p1.get_lines_info()[1]
maxLengthLine2 = p2.get_lines_info()[1]
if maxLengthLine1 > DiffMaxLineLength or maxLengthLine2 > DiffMaxLineLength:
return "Can't make the difference as the lines are too long."
htmlD = difflib.HtmlDiff()
lines1 = p1.get_p_content().splitlines()
lines2 = p2.get_p_content().splitlines()
the_html = htmlD.make_file(lines1, lines2)
return the_html
示例13: create_diff_report
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def create_diff_report(file1, file2, file_name, from_desc, to_desc, is_64_bit):
""" Create a report showing the differences between two files
Arguments :
file1 : path to a file to disassemble
file2 : path to a file to disassemble
report_name : The name to use for the report
from_desc : A description of the 'from' element
to_desc : A description of the 'to' element
"""
# Disassemble each file and split into lines
disassembled_1 = OPcacheDisassembler(is_64_bit).disassemble(file1).split("\n")
disassembled_2 = OPcacheDisassembler(is_64_bit).disassemble(file2).split("\n")
# Differ
html_differ = difflib.HtmlDiff()
# Generate the report and write into a file
file_name = file_name.replace("/", "%2f") + '.html'
hash_name = hashlib.sha1(file_name).hexdigest()
with open(hunt_report + "/" + hash_name + ".html", "w") as f:
content = html_differ.make_file(disassembled_1, disassembled_2, from_desc, to_desc)
f.write(content)
# Return the name of the report
return (file_name, hash_name + ".html")
示例14: test_main
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def test_main():
difflib.HtmlDiff._default_prefix = 0
Doctests = doctest.DocTestSuite(difflib)
run_unittest(
TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs,
TestOutputFormat, TestBytes, TestJunkAPIs, Doctests)
示例15: test_main
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import HtmlDiff [as 别名]
def test_main():
difflib.HtmlDiff._default_prefix = 0
Doctests = doctest.DocTestSuite(difflib)
run_unittest(TestSFpatches, TestSFbugs, Doctests)