@@ -62,7 +62,10 @@ def register_comparator(ext, comparator, force=False):
62
62
registered and the comparator is replaced.
63
63
64
64
.. note::
65
- The given comparator should have the following signature:
65
+ It is possible to create and register custom comparators. The easiest way to do it is to
66
+ derive a class from :class:`dir_content_diff.BaseComparator`.
67
+
68
+ Otherwise, the given comparator should be a callable with the following signature:
66
69
67
70
.. code-block:: python
68
71
@@ -74,7 +77,8 @@ def register_comparator(ext, comparator, force=False):
74
77
**diff_kwargs: Mapping[str, Any],
75
78
) -> Union[False, str]
76
79
77
- The return type can be Any when used with `return_raw_diffs == True`.
80
+ The return type can be Any when used with `return_raw_diffs == True`, else it should be a
81
+ string object.
78
82
"""
79
83
ext = format_ext (ext )
80
84
if not force and ext in _COMPARATORS :
@@ -229,6 +233,7 @@ def compare_trees(
229
233
230
234
{
231
235
<relative_file_path>: {
236
+ comparator: ComparatorInstance,
232
237
args: [arg1, arg2, ...],
233
238
kwargs: {
234
239
kwarg_name_1: kwarg_value_1,
@@ -237,6 +242,8 @@ def compare_trees(
237
242
},
238
243
<another_file_path>: {...}
239
244
}
245
+
246
+ Note that all entries in this ``dict`` are optional.
240
247
return_raw_diffs (bool): If set to ``True``, only the raw differences are returned instead
241
248
of a formatted report.
242
249
export_formatted_files (bool or str): If set to ``True`` or a not empty string, create a
@@ -265,6 +272,8 @@ def compare_trees(
265
272
266
273
if specific_args is None :
267
274
specific_args = {}
275
+ else :
276
+ specific_args = copy .deepcopy (specific_args )
268
277
269
278
# Loop over all files and call the correct comparator
270
279
different_files = {}
@@ -277,15 +286,21 @@ def compare_trees(
277
286
278
287
if comp_file .exists ():
279
288
specific_file_args = specific_args .get (relative_path , {})
280
- comparator = comparators .get (ref_file .suffix , _COMPARATORS .get (None ))
281
- comparator_kwargs = {k : v for k , v in specific_file_args .items () if k != "args" }
289
+ comparator = specific_file_args .pop (
290
+ "comparator" ,
291
+ comparators .get (
292
+ ref_file .suffix ,
293
+ _COMPARATORS .get (None ),
294
+ ),
295
+ )
296
+ comparator_args = specific_file_args .pop ("args" , [])
282
297
res = compare_files (
283
298
ref_file ,
284
299
comp_file ,
285
300
comparator ,
286
- * specific_file_args . get ( "args" , []) ,
301
+ * comparator_args ,
287
302
return_raw_diffs = return_raw_diffs ,
288
- ** comparator_kwargs ,
303
+ ** specific_file_args ,
289
304
)
290
305
if res is not False :
291
306
different_files [relative_path ] = res
@@ -294,7 +309,7 @@ def compare_trees(
294
309
comp_file ,
295
310
formatted_data_path / relative_path ,
296
311
comparator ,
297
- ** comparator_kwargs ,
312
+ ** specific_file_args ,
298
313
)
299
314
else :
300
315
msg = f"The file '{ relative_path } ' does not exist in '{ comp_path } '."
0 commit comments