Commit 894296a8 authored by Ayush Tiwari's avatar Ayush Tiwari

DiffTool: Add patch for DeepDiff to not use default diff function for iterables

Because of recursive diff for iterables which provides with ugly unified_diff, we
patched the DeepDiff class to treat iterables as string itself and then generate
a unified diff which could be displayed in ERP5.
parent 914d58f5
......@@ -42,6 +42,7 @@ except ImportError:
warnings.warn("Please install unidiff, it is needed by Diff Tool",
DeprecationWarning)
from AccessControl import ClassSecurityInfo
from Products.ERP5Type.patches.diff import DeepDiff
from Products.ERP5Type import Permissions
from Products.ERP5Type.Globals import InitializeClass
from Products.ERP5Type.Tool.BaseTool import BaseTool
......@@ -134,7 +135,7 @@ class PortalPatch:
new_value_dict = self.removePropertyList(self.new_value, export=True)
# Get the DeepDiff in tree format.
tree_diff = deepdiff.DeepDiff(old_value_dict,
tree_diff = DeepDiff(old_value_dict,
new_value_dict,
view='tree')
diff_tree_list = []
......
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2018 Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
##############################################################################
import warnings
from collections import Mapping
from collections import Iterable
try:
from deepdiff import DeepDiff
except ImportError:
DeepDiff = None
warnings.warn("Please install deepdiff, it is needed by json_representable mixin",
DeprecationWarning)
try:
from deepdiff.helper import strings, numbers
except ImportError:
strings = None
numbers = None
warnings.warn("Please install deepdiff, it is needed by json_representable mixin",
DeprecationWarning)
def DeepDiff__diff_iterable(self, level, parents_ids=frozenset({})):
"""Difference of iterables"""
level.t1 = str(level.t1)
level.t2 = str(level.t2)
self._DeepDiff__diff_str(level)
def DeepDiff__diff(self, level, parents_ids=frozenset({})):
"""The main diff method"""
if level.t1 is level.t2:
return
if self._DeepDiff__skip_this(level):
return
if type(level.t1) != type(level.t2):
self._DeepDiff__diff_types(level)
elif isinstance(level.t1, strings):
self._DeepDiff__diff_str(level)
elif isinstance(level.t1, numbers):
self._DeepDiff__diff_numbers(level)
elif isinstance(level.t1, Mapping):
self._DeepDiff__diff_dict(level, parents_ids)
elif isinstance(level.t1, tuple):
self._DeepDiff__diff_str(level)
elif isinstance(level.t1, (set, frozenset)):
self._DeepDiff__diff_set(level)
elif isinstance(level.t1, Iterable):
if self.ignore_order:
self._DeepDiff__diff_iterable_with_contenthash(level)
else:
self._DeepDiff__diff_iterable(level, parents_ids)
else:
self._DeepDiff__diff_obj(level, parents_ids)
return
# Monkey patch the functions for DeepDiff class.
# This patch fixes the requirement of diffing the iterables which by default
# was too complicated in DeepDiff(doing recursive diff inside the iterables).
# Rather than doing that, now we treat the iterables as string and calculate
# the diff accordingly
DeepDiff._DeepDiff__diff_iterable = DeepDiff__diff_iterable
DeepDiff._DeepDiff__diff = DeepDiff__diff
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment