Skip to content

Commit

Permalink
cosmetic refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-mixas committed Dec 30, 2019
1 parent 98ceea1 commit cf2646e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
78 changes: 43 additions & 35 deletions nested_diff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,27 @@ def diff(self, a, b):
:param b: Second object to diff.
"""
if self.__diff_method is not None and hasattr(a, self.__diff_method):
return getattr(a, self.__diff_method)(
b,
A=self.op_a,
N=self.op_n,
O=self.op_o, # noqa: E741
R=self.op_r,
U=self.op_u,
trimR=self.op_trim_r,
diff_method=self.__diff_method,
)
if self.__diff_method is not None:
try:
method = a.__getattribute__(self.__diff_method)
except AttributeError:
pass
else:
return method(
b,
A=self.op_a,
N=self.op_n,
O=self.op_o, # noqa: E741
R=self.op_r,
U=self.op_u,
trimR=self.op_trim_r,
diff_method=self.__diff_method,
)

if a.__class__ is b.__class__:
if a == b:
return {'U': a} if self.op_u else {}
else:
return self.get_differ(a.__class__)(a, b)
return self.get_differ(a.__class__)(a, b)

return self.diff__default(a, b)

Expand All @@ -163,14 +167,14 @@ def diff__default(self, a, b):
Return default diff.
"""
ret = {}
dif = {}

if self.op_n:
ret['N'] = b
dif['N'] = b
if self.op_o:
ret['O'] = a
dif['O'] = a

return ret
return dif

def diff_dict(self, a, b):
"""
Expand All @@ -191,23 +195,23 @@ def diff_dict(self, a, b):

for key in set(a).union(b):
try:
one = a[key]
old = a[key]
try:
two = b[key]
new = b[key]
except KeyError: # removed
if self.op_r:
dif[key] = {'R': None if self.op_trim_r else one}
dif[key] = {'R': None if self.op_trim_r else old}
continue
except KeyError: # added
if self.op_a:
dif[key] = {'A': b[key]}
continue

if one == two:
if old == new:
if self.op_u:
dif[key] = {'U': one}
dif[key] = {'U': old}
else:
subdiff = self.diff(one, two)
subdiff = self.diff(old, new)
if subdiff:
dif[key] = subdiff

Expand Down Expand Up @@ -372,12 +376,12 @@ def diff_tuple(self, a, b):
>>>
"""
ret = self.diff_list(a, b)
dif = self.diff_list(a, b)

if 'D' in ret:
ret['D'] = tuple(ret['D'])
if 'D' in dif:
dif['D'] = tuple(dif['D'])

return ret
return dif

def get_differ(self, type_):
"""
Expand Down Expand Up @@ -451,18 +455,22 @@ def patch(self, target, ndiff):
:param ndiff: Nested diff.
"""
if self.__patch_method is not None and \
hasattr(target, self.__patch_method):
return getattr(target, self.__patch_method)(ndiff)
if self.__patch_method is not None:
try:
method = target.__getattribute__(self.__patch_method)
except AttributeError:
pass
else:
return method(ndiff)

if 'D' in ndiff:
return self.get_patcher(
ndiff.get('E', ndiff['D']).__class__,
ndiff['E' if 'E' in ndiff else 'D'].__class__,
)(target, ndiff)
elif 'N' in ndiff:
return ndiff['N']
else:
return target

return target

def patch_dict(self, target, ndiff):
"""
Expand Down Expand Up @@ -658,12 +666,12 @@ def iterate(self, ndiff):

while stack:
try:
diff, key, subdiff = next(stack[-1])
ndiff, key, subdiff = next(stack[-1])
except StopIteration:
stack.pop()
continue

yield diff, key, subdiff
yield ndiff, key, subdiff

if subdiff is None:
continue
Expand Down
4 changes: 2 additions & 2 deletions nested_diff/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_argparser(self, description=None):
default=self.default_ifmt,
choices=sorted(self.supported_ifmts),
help='input files format; "' + self.default_ifmt +
'" used by default',
'" used by default',
)

parser.add_argument(
Expand All @@ -91,7 +91,7 @@ def get_argparser(self, description=None):
default=self.default_ofmt,
choices=sorted(self.supported_ofmts),
help='output files format; "' + self.default_ofmt +
'" used by default',
'" used by default',
)

parser.add_argument(
Expand Down
8 changes: 4 additions & 4 deletions nested_diff/diff_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ def get_opts(opts):
class TermDumper(AbstractFmtDumper):
def __init__(self, **kwargs):
super().__init__()
import nested_diff.fmt
self.encoder = nested_diff.fmt.TermFormatter(**self.get_opts(kwargs))
from nested_diff import fmt
self.encoder = fmt.TermFormatter(**self.get_opts(kwargs))


class TextDumper(AbstractFmtDumper):
def __init__(self, **kwargs):
super().__init__()
import nested_diff.fmt
self.encoder = nested_diff.fmt.TextFormatter(**self.get_opts(kwargs))
from nested_diff import fmt
self.encoder = fmt.TextFormatter(**self.get_opts(kwargs))


def cli():
Expand Down

0 comments on commit cf2646e

Please sign in to comment.