Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
remortalite committed Jun 26, 2024
1 parent c74f57e commit 33c3fd1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
12 changes: 6 additions & 6 deletions gendiff/formatters/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
28 changes: 12 additions & 16 deletions gendiff/formatters/stylish.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
INDENT_SYMBOL=" "
INDENT_SIZE=4
SHIFT_SIZE=2


def prepare_value(value):
if isinstance(value, dict):
return "{"
Expand All @@ -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
Expand All @@ -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])

Expand All @@ -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 ""
3 changes: 2 additions & 1 deletion tests/test_format_diff.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from gendiff.formatters import plain_formatter
from gendiff.formatters.plain import iter_recursive

import json
import pytest
Expand Down Expand Up @@ -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({}) == {}

0 comments on commit 33c3fd1

Please sign in to comment.