|
11 | 11 | from datacompy.core import Compare |
12 | 12 | import re |
13 | 13 | import jsondiff |
| 14 | +from lxml import etree |
| 15 | +from xmldiff import main, formatting |
14 | 16 |
|
15 | 17 |
|
16 | 18 | def is_float(num: str): |
@@ -253,10 +255,11 @@ def compare_files(file_1, file_2, name, config: dict = None) -> bool: |
253 | 255 | raise ValueError('File, ' + file_1 + ', requires a comparison configuration but none given.') |
254 | 256 | if ext_2 == '.csv': |
255 | 257 | raise ValueError('File, ' + file_2 + ', requires a comparison configuration but none given.') |
256 | | - |
257 | | - if comp_config is None: |
258 | | - # If there was no configuration then fall back to a straight file comparison. |
259 | | - result = compare_files_direct(name, file_1, file_2) |
| 258 | + if ext_1 == '.xml' and ext_2 == '.xml': |
| 259 | + result = compare_files_xml(name, file_1, file_2) |
| 260 | + else: |
| 261 | + # If there was no configuration then fall back to a straight file comparison. |
| 262 | + result = compare_files_direct(name, file_1, file_2) |
260 | 263 | else: |
261 | 264 | config_type = comp_config.get('type') |
262 | 265 | if config_type == 'csv': |
@@ -549,6 +552,15 @@ def compare_files_direct(name, file_1, file_2): |
549 | 552 |
|
550 | 553 | return match |
551 | 554 |
|
| 555 | +def compare_files_xml(name, file_1, file_2): |
| 556 | + logger = logging.getLogger(__name__) |
| 557 | + logger.debug('%s: Comparing file %s against %s using xml diff', name, file_1, file_2) |
| 558 | + diff = main.diff_files('file1.xml', 'file2.xml', formatter=formatting.XMLFormatter()) |
| 559 | + match = True |
| 560 | + for line in diff: |
| 561 | + match = False |
| 562 | + logger.warning(line.rstrip('\n')) |
| 563 | + return match |
552 | 564 |
|
553 | 565 | def compare_files_json(name, file_1, file_2, config) -> bool: |
554 | 566 | # Compare JSON files using configuration. |
|
0 commit comments