Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
имя фамилия committed Jul 2, 2023
1 parent b7c19d2 commit 89793c0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 52 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
Compress two configuration files and shows a difference.

positional arguments:
first_file
second_file
first_file
second_file

options:
-h, --help show this help message and exit
-f FORMAT, --format FORMAT
set format of output
-h, --help show this help message and exit
-f FORMAT, --format FORMAT
set format of output

# [FLAT_DATA](https://asciinema.org/a/CV49Sn25feHZCN2AXYYmHf2sP)

Expand Down
74 changes: 33 additions & 41 deletions gendiff/formats/stylish.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
INDENT_WIDTH = 4
STARTING_DEPTH_VALUE = 0
THIRD_KEY_CHAR = 2
REPLACER = ' '
STATUSES = {'added': '+ ', 'deleted': '- ', 'unchanged': ' '}


def get_correct_view(data_tree):
Expand All @@ -16,45 +18,35 @@ def get_correct_view(data_tree):
return data_tree


def get_stylish(data):
replacer = ' '
spaces_count = INDENT_WIDTH

def iter_(current_data, depth=STARTING_DEPTH_VALUE):
if not isinstance(current_data, dict):
return str(current_data)
deep_indent_size = depth + spaces_count
deep_indent = replacer * deep_indent_size
current_indent = replacer * depth
lines = []
for key, value in current_data.items():
if isinstance(value, dict) and 'diff' in value:

if value['diff'] == 'nested':
lines.append(f'{deep_indent[THIRD_KEY_CHAR:]} {key}: '
f'{iter_(value["children"], deep_indent_size)}')

elif value['diff'] == 'added':
lines.append(f'{deep_indent[THIRD_KEY_CHAR:]}+ {key}: '
f'{iter_(value["value"], deep_indent_size)}')

elif value['diff'] == 'deleted':
lines.append(f'{deep_indent[THIRD_KEY_CHAR:]}- {key}: '
f'{iter_(value["value"], deep_indent_size)}')

elif value['diff'] == 'changed':
lines.append(f'{deep_indent[THIRD_KEY_CHAR:]}- {key}: '
f'{iter_(value["value1"], deep_indent_size)}')
lines.append(f'{deep_indent[THIRD_KEY_CHAR:]}+ {key}: '
f'{iter_(value["value2"], deep_indent_size)}')

elif value['diff'] == 'unchanged':
lines.append(f'{deep_indent[THIRD_KEY_CHAR:]} {key}: '
f'{iter_(value["value"], deep_indent_size)}')
else:
lines.append(f'{deep_indent}{key}: '
f'{iter_(value, deep_indent_size)}')
def get_stylish(current_data, depth=STARTING_DEPTH_VALUE):
if not isinstance(current_data, dict):
return str(current_data)

indent_size = depth + INDENT_WIDTH
deep_indent = REPLACER * indent_size
current_indent = REPLACER * depth
lines = []

for key, value in current_data.items():
if isinstance(value, dict) and 'diff' in value:

result = chain('{', lines, [current_indent + '}'])
return '\n'.join(result)
return get_correct_view(iter_(data))
if value['diff'] == 'nested':
lines.append(f'{deep_indent[THIRD_KEY_CHAR:]} {key}: '
f'{get_stylish(value["children"], indent_size)}')

elif value['diff'] == 'changed':
lines.append(f'{deep_indent[THIRD_KEY_CHAR:]}- {key}: '
f'{get_stylish(value["value1"], indent_size)}')

lines.append(f'{deep_indent[THIRD_KEY_CHAR:]}+ {key}: '
f'{get_stylish(value["value2"], indent_size)}')
else:
lines.append(f'{deep_indent[THIRD_KEY_CHAR:]}'
f'{STATUSES[value["diff"]]}{key}: '
f'{get_stylish(value["value"], indent_size)}')
else:
lines.append(f'{deep_indent}{key}: '
f'{get_stylish(value, indent_size)}')

result = chain('{', lines, [current_indent + '}'])
return get_correct_view('\n'.join(result))
10 changes: 5 additions & 5 deletions gendiff/read_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@


def get_read_file(data, extension):
if extension == '.json':
return json.loads(data)
elif extension in ('.yml', '.yaml'):
return yaml.safe_load(data)
if extension == '.json':
return json.loads(data)
elif extension in ('.yml', '.yaml'):
return yaml.safe_load(data)


def get_decoder_data(file_path):
with open(file_path) as data:
_, extension = os.path.splitext(file_path)
read_file = data.read()
return get_read_file(read_file, extension)
return get_read_file(read_file, extension)
2 changes: 1 addition & 1 deletion gendiff/scripts/gendiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_args():


def main():
args = get_args()
args = get_args()
print(generate_diff(args.first_file, args.second_file, args.format))


Expand Down

0 comments on commit 89793c0

Please sign in to comment.