From 33c3fd1544e21fa53e50e00c2bebb0532a5f60ca Mon Sep 17 00:00:00 2001 From: "Rem M." Date: Wed, 26 Jun 2024 18:06:34 +0700 Subject: [PATCH] Some fixes --- gendiff/formatters/plain.py | 12 ++++++------ gendiff/formatters/stylish.py | 28 ++++++++++++---------------- tests/test_format_diff.py | 3 ++- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/gendiff/formatters/plain.py b/gendiff/formatters/plain.py index 26343a8..dff10d1 100644 --- a/gendiff/formatters/plain.py +++ b/gendiff/formatters/plain.py @@ -10,7 +10,7 @@ def transform_to_str(value): return f"'{value}'" -def _iter_format(data, path=''): +def iter_recursive(data, path=''): result = [] templates_dict = { @@ -54,17 +54,17 @@ def _iter_format(data, path=''): result.append(item_string) case "nested": - result.extend(_iter_format(item["children"], + result.extend(iter_recursive(item["children"], path=item_name)) result = sorted(result) return result -def format(data, raw=False): +def format(data): if not data: return data - result = _iter_format(data) + result = iter_recursive(data) - result_str = "\n".join(result) - return result if raw else result_str.strip() + result_str = "\n".join(result).strip() + return result_str diff --git a/gendiff/formatters/stylish.py b/gendiff/formatters/stylish.py index 9c2c9d4..0625813 100644 --- a/gendiff/formatters/stylish.py +++ b/gendiff/formatters/stylish.py @@ -1,3 +1,8 @@ +INDENT_SYMBOL=" " +INDENT_SIZE=4 +SHIFT_SIZE=2 + + def prepare_value(value): if isinstance(value, dict): return "{" @@ -8,8 +13,8 @@ def prepare_value(value): return value -def iter_(current_data, level=1, *, indent_symbol, indent_size, shift_size): - indent_size_counted = level * indent_size - shift_size +def iter_(current_data, level=1): + indent_size_counted = level * INDENT_SIZE - SHIFT_SIZE if not isinstance(current_data, dict): return current_data @@ -19,7 +24,7 @@ def iter_(current_data, level=1, *, indent_symbol, indent_size, shift_size): # prepare dict for key in current_data.keys(): - indent = indent_size_counted * indent_symbol + indent = indent_size_counted * INDENT_SYMBOL key = key value = prepare_value(current_data[key]) @@ -29,24 +34,15 @@ def iter_(current_data, level=1, *, indent_symbol, indent_size, shift_size): if isinstance(current_data[key], dict): nested_result = iter_(current_data[key], - level + 1, - indent_symbol=indent_symbol, - indent_size=indent_size, - shift_size=shift_size) + level + 1) if nested_result: result.extend(nested_result) - result.append(f"{indent}{shift_size * indent_symbol}}}") + result.append(f"{indent}{SHIFT_SIZE * INDENT_SYMBOL}}}") return result -def format(data, raw=False, *, - indent_symbol=" ", indent_size=4, shift_size=2): - if raw: - return data - result = iter_(data, - indent_symbol=indent_symbol, - indent_size=indent_size, - shift_size=shift_size) +def format(data): + result = iter_(data) return "{\n" + "\n".join(result) + "\n}" if result else "" diff --git a/tests/test_format_diff.py b/tests/test_format_diff.py index 36f67d8..6d7cdee 100644 --- a/tests/test_format_diff.py +++ b/tests/test_format_diff.py @@ -1,4 +1,5 @@ from gendiff.formatters import plain_formatter +from gendiff.formatters.plain import iter_recursive import json import pytest @@ -37,5 +38,5 @@ def test_format_diff_plain(): "Property 'three' was updated. From 3 to 4", "Property 'two' was removed"] - assert plain_formatter(data, raw=True) == result + assert iter_recursive(data) == result assert plain_formatter({}) == {}