Python difflib.HtmlDiff() Examples
The following are 30
code examples of difflib.HtmlDiff().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
difflib
, or try the search function
.
Example #1
Source File: view.py From osspolice with GNU General Public License v3.0 | 7 votes |
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
Example #2
Source File: diff.py From oss-ftp with MIT License | 6 votes |
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)
Example #3
Source File: test_difflib.py From medicare-demo with Apache License 2.0 | 5 votes |
def test_main(): difflib.HtmlDiff._default_prefix = 0 Doctests = doctest.DocTestSuite(difflib) run_unittest(TestSFpatches, TestSFbugs, Doctests)
Example #4
Source File: test_difflib.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_main(): difflib.HtmlDiff._default_prefix = 0 Doctests = doctest.DocTestSuite(difflib) run_unittest( TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs, TestOutputFormat, Doctests)
Example #5
Source File: diff.py From datafari with Apache License 2.0 | 5 votes |
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)
Example #6
Source File: opcache_malware_hunt.py From php7-opcache-override with MIT License | 5 votes |
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")
Example #7
Source File: app.py From oabot with MIT License | 5 votes |
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
Example #8
Source File: test_difflib.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #9
Source File: test_difflib.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #10
Source File: test_difflib.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #11
Source File: test_difflib.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_main(): difflib.HtmlDiff._default_prefix = 0 Doctests = doctest.DocTestSuite(difflib) run_unittest( TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs, TestOutputFormat, TestBytes, TestJunkAPIs, Doctests)
Example #12
Source File: diff.py From android_universal with MIT License | 5 votes |
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)
Example #13
Source File: Flask_showpaste.py From AIL-framework with GNU Affero General Public License v3.0 | 5 votes |
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
Example #14
Source File: diff.py From odoo13-x64 with GNU General Public License v3.0 | 5 votes |
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)
Example #15
Source File: pdfdiff.py From pdfminer3 with MIT License | 5 votes |
def compare(file1,file2,**args): if args.get('_py2_no_more_posargs', None) is not None: raise ValueError("Too many positional arguments passed.") # If any LAParams group arguments were passed, create an LAParams object and # populate with given args. Otherwise, set it to None. if args.get('laparams', None) is None: laparams = pdfminer3.layout.LAParams() for param in ("all_texts", "detect_vertical", "word_margin", "char_margin", "line_margin", "boxes_flow"): paramv = args.get(param, None) if paramv is not None: laparams[param]=paramv args['laparams']=laparams s1 = io.StringIO() with open(file1, "rb") as fp: pdfminer3.high_level.extract_text_to_fp(fp, s1, **args) s2 = io.StringIO() with open(file2, "rb") as fp: pdfminer3.high_level.extract_text_to_fp(fp, s2, **args) import difflib s1.seek(0) s2.seek(0) s1, s2=s1.readlines(), s2.readlines() import os.path try: extension = os.path.splitext(args['outfile'])[1][1:4] if extension.lower()=='htm': return difflib.HtmlDiff().make_file(s1, s2) except KeyError: pass return difflib.unified_diff(s1, s2, n=args['context_lines']) # main
Example #16
Source File: diff.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)
Example #17
Source File: test_difflib.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def test_main(): difflib.HtmlDiff._default_prefix = 0 Doctests = doctest.DocTestSuite(difflib) run_unittest( TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs, TestOutputFormat, Doctests)
Example #18
Source File: config_handler.py From kerrigan with GNU General Public License v3.0 | 5 votes |
def get(self, *args, **kwargs): config_id = self.get_argument('config_id', default=None, strip=True) history_id = self.get_argument('history_id', default=None, strip=True) if config_id or history_id: with DBContext('r') as session: config_info = session.query(KerriganConfig).filter(KerriganConfig.id == config_id).first() history_info = session.query(KerriganHistory).filter(KerriganHistory.id == history_id).first() if config_id: diff_data = config_info.content.splitlines() config_key = "/{}/{}/{}/{}".format(config_info.project_code, config_info.environment, config_info.service, config_info.filename) else: diff_data = history_info.content.splitlines() config_key = history_info.config with DBContext('r') as session: publish_info = session.query(KerriganPublish).filter(KerriganPublish.config == config_key).first() if not publish_info: html = difflib.HtmlDiff().make_file('', diff_data, context=True, numlines=3) return self.write(dict(code=0, msg='对比内容获取成功', data=html)) src_data = publish_info.content.splitlines() html = difflib.HtmlDiff().make_file(src_data, diff_data, context=True, numlines=3) return self.write(dict(code=0, msg='对比内容获取成功', data=html)) else: return self.write(dict(code=-1, msg='关键参数不能为空'))
Example #19
Source File: git_diff.py From pythonista-scripts with MIT License | 5 votes |
def diff_working(repo,file,src=source.PATH): store=repo.object_store index=repo.open_index() tree=store[store[repo.head()].tree] parent_tree=store[store[store[repo.head()].parents[0]].tree] tree_ver=store[tree.lookup_path(store.peel_sha,file)[1]].data local_ver=open(os.path.join(repo.path,file)).read() h=HtmlDiff(wrapcolumn=70,tabsize=4) if src==source.PATH: f=h.make_file(tree_ver.splitlines(),local_ver.splitlines(), file, 'last commit:'+repo.head()) elif src==source.INDEX: index_ver=store[index._byname[file].sha].data f=h.make_file(tree_ver.splitlines(),index_ver.splitlines(),file+' staged change', 'last commit'+repo.head()) else: parent_tree_ver=store[parent_tree.lookup_path(store.peel_sha,file)[1]].data f=h.make_file(parent_tree_ver.splitlines(),tree_ver.splitlines(), file+' HEAD','HEAD^1') return f #f=diff_working(porcelain.open_repo('.'),'gitui.py') #w=ui.WebView() #w.load_html(f) #w.present()
Example #20
Source File: side_by_side.py From PerfKitBenchmarker with Apache License 2.0 | 5 votes |
def _CompareSamples(a, b, context=True, numlines=1): """Generate an HTML table showing differences between 'a' and 'b'. Args: a: dict, as output by PerfKitBenchmarker. b: dict, as output by PerfKitBenchmarker. context: boolean. Show context in diff? If False, all lines are output, even those which are equal. numlines: int. Passed to difflib.Htmldiff.make_table. Returns: string or None. An HTML table, or None if there are no differences. """ a = a.copy() b = b.copy() a['metadata'] = _SplitLabels(a.pop('labels', '')) b['metadata'] = _SplitLabels(b.pop('labels', '')) # Prune the keys in VARYING_KEYS prior to comparison to make the diff more # informative. for d in (a, b): for key in VARYING_KEYS: d.pop(key, None) astr = pprint.pformat(a).splitlines() bstr = pprint.pformat(b).splitlines() if astr == bstr and context: return None differ = difflib.HtmlDiff() return differ.make_table(astr, bstr, context=context, numlines=numlines)
Example #21
Source File: diff.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)
Example #22
Source File: test_difflib.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def test_main(): difflib.HtmlDiff._default_prefix = 0 Doctests = doctest.DocTestSuite(difflib) run_unittest( TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs, TestOutputFormat, Doctests)
Example #23
Source File: test_difflib.py From ironpython2 with Apache License 2.0 | 5 votes |
def test_main(): difflib.HtmlDiff._default_prefix = 0 Doctests = doctest.DocTestSuite(difflib) run_unittest( TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs, TestOutputFormat, TestJunkAPIs)
Example #24
Source File: symbol.py From elf_diff with GNU General Public License v3.0 | 5 votes |
def getDifferencesAsHTML(self, other, indent): import difflib diff_class = difflib.HtmlDiff(tabsize=3, wrapcolumn=200) diff_table = diff_class.make_table(self.instruction_lines, \ other.instruction_lines, \ fromdesc='Old', \ todesc='New', \ context=True, \ numlines=1000) return postHighlightSourceCode(diff_table)
Example #25
Source File: test_difflib.py From BinderFilter with MIT License | 5 votes |
def test_main(): difflib.HtmlDiff._default_prefix = 0 Doctests = doctest.DocTestSuite(difflib) run_unittest( TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs, TestOutputFormat, Doctests)
Example #26
Source File: app.py From asphalt with Apache License 2.0 | 5 votes |
def run(self, ctx): diff = HtmlDiff() async with aclosing(ctx.detector.changed.stream_events()) as stream: async for event in stream: difference = diff.make_file(event.old_lines, event.new_lines, context=True) await ctx.mailer.create_and_deliver( subject='Change detected in %s' % event.source.url, html_body=difference) logger.info('Sent notification email')
Example #27
Source File: test_difflib.py From oss-ftp with MIT License | 5 votes |
def test_main(): difflib.HtmlDiff._default_prefix = 0 Doctests = doctest.DocTestSuite(difflib) run_unittest( TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs, TestOutputFormat, Doctests)
Example #28
Source File: test_difflib.py From android_universal with MIT License | 5 votes |
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)
Example #29
Source File: test_difflib.py From android_universal with MIT License | 5 votes |
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)
Example #30
Source File: test_difflib.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)