def compare(self, block_name, base, target, belong_to='block'):
ls_delete, ls_add, ls_update = [], [], []
match_func = CustomTextStrategyMiddleware(self._path_base).match
if settings.MATCH_WITH_CHAPTER:
text_delete_list, text_add_list, old_new_text_matched = self.do_match_with_chapter(base, target,match_func)
else:
text_delete_list, text_add_list, old_new_text_matched = self.do_match_normal(base, target,match_func)
for old_text_item, new_text_item in old_new_text_matched:
not_equal = []
not_equal_values = []
if old_text_item.text != new_text_item.text:
not_equal.append('text')
not_equal_values.append((old_text_item.text, new_text_item.text))
else:
for old_char, new_char in zip_longest(old_text_item.get_chars(), new_text_item.get_chars(),fillvalue=None):
if old_char is None or new_char is None:
item_char = getattr(old_char, 'char', '') or getattr(new_char, 'char', '')
# 忽略制表符和空格的差异项
if item_char in ('\t', ' '):
continue
not_equal.append('text')
not_equal_values.append((getattr(old_char, 'char', ''), getattr(new_char, 'char', '')))
elif not getattr(old_char, 'char', '').strip() and not getattr(new_char, 'char', '').strip():
continue
else:
attrs, values = self.get_not_equal_attrs(old_char, new_char, settings.TEXT_COMPARE_ATTRS)
not_equal.extend(attrs)
not_equal_values.extend(values)
unique_not_equal = list(set(not_equal))
unique_not_equal_values = [not_equal_values[not_equal.index(v)] for v in unique_not_equal]
if not_equal:
ls_update.append(
DiffItem('update', 'text', 'text', block_name=block_name, old=old_text_item, new=new_text_item,
belong_to=belong_to, diff_point=' '.join(unique_not_equal),diff_values=unique_not_equal_values))
for delete_item in text_delete_list:
ls_delete.append(DiffItem('delete', 'text', 'text', block_name=block_name, old=delete_item, new=None,
belong_to=belong_to))
for add_item in text_add_list:
ls_add.append(DiffItem('add', 'text', 'text', block_name=block_name, old=None, new=add_item,
belong_to=belong_to))
ret = {
'update': ls_update,
'add': ls_add,
'delete': ls_delete
}
return ret def compare(self, block_name, base, target, belong_to='block'):
ls_delete, ls_add, ls_update = [], [], []
match_func = CustomTextStrategyMiddleware(self._path_base).match
if settings.MATCH_WITH_CHAPTER:
text_delete_list, text_add_list, old_new_text_matched = self.do_match_with_chapter(base, target,match_func)
else:
text_delete_list, text_add_list, old_new_text_matched = self.do_match_normal(base, target,match_func)
for old_text_item, new_text_item in old_new_text_matched:
not_equal = []
not_equal_values = []
if old_text_item.text != new_text_item.text:
not_equal.append('text')
not_equal_values.append((old_text_item.text, new_text_item.text))
else:
for old_char, new_char in zip_longest(old_text_item.get_chars(), new_text_item.get_chars(),fillvalue=None):
if old_char is None or new_char is None:
item_char = getattr(old_char, 'char', '') or getattr(new_char, 'char', '')
# 忽略制表符和空格的差异项
if item_char in ('\t', ' '):
continue
not_equal.append('text')
not_equal_values.append((getattr(old_char, 'char', ''), getattr(new_char, 'char', '')))
elif not getattr(old_char, 'char', '').strip() and not getattr(new_char, 'char', '').strip():
continue
else:
attrs, values = self.get_not_equal_attrs(old_char, new_char, settings.TEXT_COMPARE_ATTRS)
not_equal.extend(attrs)
not_equal_values.extend(values)
unique_not_equal = list(set(not_equal))
unique_not_equal_values = [not_equal_values[not_equal.index(v)] for v in unique_not_equal]
if not_equal:
ls_update.append(
DiffItem('update', 'text', 'text', block_name=block_name, old=old_text_item, new=new_text_item,
belong_to=belong_to, diff_point=' '.join(unique_not_equal),diff_values=unique_not_equal_values))
for delete_item in text_delete_list:
ls_delete.append(DiffItem('delete', 'text', 'text', block_name=block_name, old=delete_item, new=None,
belong_to=belong_to))
for add_item in text_add_list:
ls_add.append(DiffItem('add', 'text', 'text', block_name=block_name, old=None, new=add_item,
belong_to=belong_to))
ret = {
'update': ls_update,
'add': ls_add,
'delete': ls_delete
}
return ret def compare(self, block_name, base, target, belong_to='block'):
ls_delete, ls_add, ls_update = [], [], []
match_func = CustomTextStrategyMiddleware(self._path_base).match
if settings.MATCH_WITH_CHAPTER:
text_delete_list, text_add_list, old_new_text_matched = self.do_match_with_chapter(base, target,match_func)
else:
text_delete_list, text_add_list, old_new_text_matched = self.do_match_normal(base, target,match_func)
for old_text_item, new_text_item in old_new_text_matched:
not_equal = []
not_equal_values = []
if old_text_item.text != new_text_item.text:
not_equal.append('text')
not_equal_values.append((old_text_item.text, new_text_item.text))
else:
for old_char, new_char in zip_longest(old_text_item.get_chars(), new_text_item.get_chars(),fillvalue=None):
if old_char is None or new_char is None:
item_char = getattr(old_char, 'char', '') or getattr(new_char, 'char', '')
# 忽略制表符和空格的差异项
if item_char in ('\t', ' '):
continue
not_equal.append('text')
not_equal_values.append((getattr(old_char, 'char', ''), getattr(new_char, 'char', '')))
elif not getattr(old_char, 'char', '').strip() and not getattr(new_char, 'char', '').strip():
continue
else:
attrs, values = self.get_not_equal_attrs(old_char, new_char, settings.TEXT_COMPARE_ATTRS)
not_equal.extend(attrs)
not_equal_values.extend(values)
unique_not_equal = list(set(not_equal))
unique_not_equal_values = [not_equal_values[not_equal.index(v)] for v in unique_not_equal]
if not_equal:
ls_update.append(
DiffItem('update', 'text', 'text', block_name=block_name, old=old_text_item, new=new_text_item,
belong_to=belong_to, diff_point=' '.join(unique_not_equal),diff_values=unique_not_equal_values))
for delete_item in text_delete_list:
ls_delete.append(DiffItem('delete', 'text', 'text', block_name=block_name, old=delete_item, new=None,
belong_to=belong_to))
for add_item in text_add_list:
ls_add.append(DiffItem('add', 'text', 'text', block_name=block_name, old=None, new=add_item,
belong_to=belong_to))
ret = {
'update': ls_update,
'add': ls_add,
'delete': ls_delete
}
return ret
详细解析