本文整理汇总了Python中difflib._mdiff方法的典型用法代码示例。如果您正苦于以下问题:Python difflib._mdiff方法的具体用法?Python difflib._mdiff怎么用?Python difflib._mdiff使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类difflib
的用法示例。
在下文中一共展示了difflib._mdiff方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_file
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import _mdiff [as 别名]
def make_file(self, lhs, rhs):
rows = []
for left, right, changed in difflib._mdiff(lhs, rhs):
lno, ltxt = left
rno, rtxt = right
ltxt = self._stop_wasting_space(ltxt)
rtxt = self._stop_wasting_space(rtxt)
ltxt = self._trunc(ltxt, changed).replace(" ", " ")
rtxt = self._trunc(rtxt, changed).replace(" ", " ")
row = self._row_template % (str(lno), ltxt, str(rno), rtxt)
rows.append(row)
all_the_rows = "\n".join(rows)
all_the_rows = all_the_rows.replace(
"\x00+", '<span class="diff_add">').replace(
"\x00-", '<span class="diff_sub">').replace(
"\x00^", '<span class="diff_chg">').replace(
"\x01", '</span>').replace(
"\t", 4 * " ")
res = self._html_template % {"style": self._style, "rows": all_the_rows}
return res
示例2: make_file
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import _mdiff [as 别名]
def make_file(self, lhs, rhs):
rows = []
for left, right, changed in difflib._mdiff(lhs, rhs, charjunk=difflib.IS_CHARACTER_JUNK):
lno, ltxt = left
rno, rtxt = right
ltxt = self._stop_wasting_space(ltxt)
rtxt = self._stop_wasting_space(rtxt)
ltxt = self._trunc(ltxt, changed).replace(" ", " ")
rtxt = self._trunc(rtxt, changed).replace(" ", " ")
row = self._row_template % (str(lno), ltxt, str(rno), rtxt)
rows.append(row)
all_the_rows = "\n".join(rows)
all_the_rows = all_the_rows.replace(
"\x00+", '<span class="diff_add">').replace(
"\x00-", '<span class="diff_sub">').replace(
"\x00^", '<span class="diff_chg">').replace(
"\x01", '</span>').replace(
"\t", 4 * " ")
res = self._html_template % {"style": self._style, "rows": all_the_rows}
return res
示例3: getDiffDetails
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import _mdiff [as 别名]
def getDiffDetails(self, fromdesc='', todesc='', context=False, numlines=5, tabSize=8):
# change tabs to spaces before it gets more difficult after we insert
# markkup
def expand_tabs(line):
# hide real spaces
line = line.replace(' ', '\0')
# expand tabs into spaces
line = line.expandtabs(tabSize)
# replace spaces from expanded tabs back into tab characters
# (we'll replace them with markup after we do differencing)
line = line.replace(' ', '\t')
return line.replace('\0', ' ').rstrip('\n')
self.fromlines = [expand_tabs(line) for line in self.fromlines]
self.tolines = [expand_tabs(line) for line in self.tolines]
# create diffs iterator which generates side by side from/to data
if context:
context_lines = numlines
else:
context_lines = None
diffs = difflib._mdiff(self.fromlines, self.tolines, context_lines,
linejunk=None, charjunk=difflib.IS_CHARACTER_JUNK)
return list(diffs)
示例4: make_table
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import _mdiff [as 别名]
def make_table(self, a, b, adesc=None, bdesc=None, context=5):
"""Make html table that displays side-by-side diff
Arguments:
- a -- list of text lines to be compared to b
- b -- list of text lines to be compared to a
- adesc -- description of the 'a' lines (e.g. filename)
- bdesc -- description of the 'b' lines (e.g. filename)
- context -- number of context lines to display
Uses difflib._mdiff function to generate diff.
"""
adesc = six.ensure_text(adesc) or ''
bdesc = six.ensure_text(bdesc) or ''
diff = difflib._mdiff(a, b, context=context)
lines = [self._make_line(d) for d in diff]
return h.really_unicode(
self.table_tmpl % (adesc, bdesc, '\n'.join(lines)))
示例5: make_file
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import _mdiff [as 别名]
def make_file(self, lhs, rhs, fmt, lex):
rows = []
for left, right, changed in difflib._mdiff(lhs, rhs):
lno, ltxt = left
rno, rtxt = right
if not changed:
ltxt = highlight(ltxt, lex, fmt)
rtxt = highlight(rtxt, lex, fmt)
else:
ltxt = self._stop_wasting_space(ltxt)
rtxt = self._stop_wasting_space(rtxt)
ltxt = ltxt.replace(" ", " ")
rtxt = rtxt.replace(" ", " ")
ltxt = ltxt.replace("<", "<")
ltxt = ltxt.replace(">", ">")
rtxt = rtxt.replace("<", "<")
rtxt = rtxt.replace(">", ">")
row = self._row_template % (str(lno), ltxt, str(rno), rtxt)
rows.append(row)
all_the_rows = "\n".join(rows)
all_the_rows = all_the_rows.replace(
"\x00+", '<span class="diff_add">').replace(
"\x00-", '<span class="diff_sub">').replace(
"\x00^", '<span class="diff_chg">').replace(
"\x01", '</span>').replace(
"\t", 4 * " ")
res = self._html_template % {"style": self._style, "rows": all_the_rows}
return res
示例6: test_mdiff_catch_stop_iteration
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import _mdiff [as 别名]
def test_mdiff_catch_stop_iteration(self):
# Issue #33224
self.assertEqual(
list(difflib._mdiff(["2"], ["3"], 1)),
[((1, '\x00-2\x01'), (1, '\x00+3\x01'), True)],
)
示例7: diff
# 需要导入模块: import difflib [as 别名]
# 或者: from difflib import _mdiff [as 别名]
def diff(self, prev_commit, fmt=None, prev_file=None, **kw):
'''
:param prev_commit: previous commit to compare against
:param fmt: "sidebyside", or anything else for "unified"
:param prev_file: previous filename, if different
:return:
'''
try:
path, filename = os.path.split(self._blob.path())
a_ci = c.app.repo.commit(prev_commit)
a = a_ci.get_path(prev_file or self._blob.path())
apath = a.path()
except Exception:
# prev commit doesn't have the file
a = M.repository.EmptyBlob()
apath = ''
b = self._blob
if not self._blob.has_html_view:
diff = "Cannot display: file marked as a binary type."
return dict(a=a, b=b, diff=diff)
la = list(a)
lb = list(b)
adesc = ('a' + h.really_unicode(apath)).encode('utf-8')
bdesc = ('b' + h.really_unicode(b.path())).encode('utf-8')
if not fmt:
fmt = web_session.get('diformat', '')
else:
web_session['diformat'] = fmt
web_session.save()
if fmt == 'sidebyside':
if max(a.size, b.size) > asint(tg.config.get('scm.view.max_syntax_highlight_bytes', 500000)):
# have to check the original file size, not diff size, because difflib._mdiff inside HtmlSideBySideDiff
# can take an extremely long time on large files (and its even a generator)
diff = '<em>File too large for side-by-side view</em>'
else:
hd = HtmlSideBySideDiff()
diff = hd.make_table(la, lb, adesc, bdesc)
else:
diff = str('').join(difflib.unified_diff(la, lb, adesc, bdesc))
return dict(a=a, b=b, diff=diff)