Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
имя фамилия committed Jun 21, 2023
1 parent 99407e4 commit f679517
Show file tree
Hide file tree
Showing 25 changed files with 88 additions and 222 deletions.
Binary file removed gendiff/.diff.py.swp
Binary file not shown.
7 changes: 0 additions & 7 deletions gendiff/formats/correct_view.py

This file was deleted.

47 changes: 27 additions & 20 deletions gendiff/formats/plain.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
LAST_ADDED_ELEM = -1


def get_correct_view(data_tree):
replacement_values = (('True', 'true'),
('False', 'false'),
('None', 'null'))
for values in replacement_values:
data_tree = data_tree.replace(*values)
return data_tree


def get_formatted_value(value):
if isinstance(value, dict):
return '[complex value]'
Expand All @@ -10,24 +19,22 @@ def get_formatted_value(value):
return f'\'{value}\''


def get_plain(data):
def iter_(current_value, path=[], result=[]):
for key, value in current_value.items():
path.append(key)
if value['diff'] == 'nested':
iter_(value['children'], path)
path = path[:LAST_ADDED_ELEM]
elif value['diff'] == 'deleted':
result.append(f'Property \'{".".join(path)}\' was removed')
elif value['diff'] == 'added':
result.append(f'Property \'{".".join(path)}\' was added '
f'with value: '
f'{get_formatted_value(value["value"])}')
elif value['diff'] == 'changed':
result.append(f'Property \'{".".join(path)}\''
f' was updated. From '
f'{get_formatted_value(value["value1"])} to '
f'{get_formatted_value(value["value2"])}')
def get_plain(current_value, path=[], result=[]):
for key, value in current_value.items():
path.append(key)
if value['diff'] == 'nested':
get_plain(value['children'], path)
path = path[:LAST_ADDED_ELEM]
return '\n'.join(result)
return iter_(data)
elif value['diff'] == 'deleted':
result.append(f'Property \'{".".join(path)}\' was removed')
elif value['diff'] == 'added':
result.append(f'Property \'{".".join(path)}\' was added '
f'with value: '
f'{get_formatted_value(value["value"])}')
elif value['diff'] == 'changed':
result.append(f'Property \'{".".join(path)}\''
f' was updated. From '
f'{get_formatted_value(value["value1"])} to '
f'{get_formatted_value(value["value2"])}')
path = path[:LAST_ADDED_ELEM]
return get_correct_view('\n'.join(result))
11 changes: 10 additions & 1 deletion gendiff/formats/stylish.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
THIRD_KEY_CHAR = 2


def get_correct_view(data_tree):
replacement_values = (('True', 'true'),
('False', 'false'),
('None', 'null'))
for values in replacement_values:
data_tree = data_tree.replace(*values)
return data_tree


def get_formated_dict(data):
result = {}
for key, value in data.items():
Expand Down Expand Up @@ -46,4 +55,4 @@ def iter_(current_data, depth=STARTING_DEPTH_VALUE):
f'{iter_(val, deep_indent_size)}')
result = chain('{', lines, [current_indent + '}'])
return '\n'.join(result)
return iter_(formated_data)
return get_correct_view(iter_(formated_data))
14 changes: 6 additions & 8 deletions gendiff/generate_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
from gendiff.formats.json import get_json
from gendiff.read_file import get_decoder_data
from gendiff.data_parsing import get_format_diff
from gendiff.formats.correct_view import get_correct_view


def generate_diff(data1, data2, format_name='stylish'):
format_dictionary = {'stylish': get_stylish,
'plain': get_plain,
'json': get_json}
read_file1 = get_decoder_data(data1)
read_file2 = get_decoder_data(data2)
data_difference = get_format_diff(read_file1,
read_file2)
file_data1 = get_decoder_data(data1)
file_data2 = get_decoder_data(data2)
data_difference = get_format_diff(file_data1,
file_data2)
convert_format = format_dictionary[format_name](data_difference)
if format_name == 'json':
return convert_format
return get_correct_view(convert_format)

return convert_format
5 changes: 3 additions & 2 deletions gendiff/read_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

def get_decoder_data(file_path):
with open(file_path) as data:
if '.json' in os.path.splitext(file_path):
_, extension = os.path.splitext(file_path)
if extension == '.json':
return json.load(data)
else:
elif extension in ('.yml', '.yaml'):
return yaml.safe_load(data)
Binary file removed gendiff/scripts/.gendiff.py.swp
Binary file not shown.
6 changes: 2 additions & 4 deletions gendiff/scripts/gendiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ def get_pars_args():
choices=['stylish', 'plain', 'json'])

args = parser.parse_args()
return generate_diff(args.first_file,
args.second_file,
args.format)
return args.first_file, args.second_file, args.format


def main():
print(get_pars_args())
print(generate_diff(*get_pars_args()))


if __name__ == '__main__':
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
[tool.poetry]
name = "hexlet-code"
version = "0.1.0"
description = ""
description = "The program calculates the differences between two files in the specified format."
authors = ["StanislavSol <vandemar@bk.ru>"]
readme = "README.md"
packages = [{include = "gendiff"}]
repository = "https://github.com/StanislavSol/python-project-50.git"
classifiers = ["Programming Language :: Python :: 3.10"]

[tool.poetry.dependencies]
python = "^3.10"
pytest-cov = "^4.1.0"
pyyaml = "^6.0"

[tool.poetry.scripts]
Expand Down
19 changes: 19 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[flake8]
accept-encodings = utf-8
max-complexity = 6
statistics = False
max-line-length = 80
enable-extensions = G
isort-show-traceback = True

exclude = .venv,dist,build

per-file-ignores =
# init modules can contain the local imports, logic, unused imports
__init__.py: F401

[isort]
multi_line_output = 3
include_trailing_comma = true
default_section = FIRSTPARTY
line_length = 80
6 changes: 0 additions & 6 deletions tests/fixtures/flat_data_yml/file1.yml

This file was deleted.

5 changes: 0 additions & 5 deletions tests/fixtures/flat_data_yml/file2.yml

This file was deleted.

8 changes: 0 additions & 8 deletions tests/fixtures/flat_data_yml/sample_output.yml

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 0 additions & 26 deletions tests/fixtures/recursive_data_yml/file1.yaml

This file was deleted.

31 changes: 0 additions & 31 deletions tests/fixtures/recursive_data_yml/file2.yaml

This file was deleted.

44 changes: 0 additions & 44 deletions tests/fixtures/recursive_data_yml/sample_output_recursion.yaml

This file was deleted.

File renamed without changes.
File renamed without changes.
76 changes: 18 additions & 58 deletions tests/test_recursion_comparison.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,22 @@
from gendiff import generate_diff
import pytest
LAST_ELEM = -1


def get_recursion_yaml():
path_file1 = 'tests/fixtures/recursive_data_yml/file1.yaml'
path_file2 = 'tests/fixtures/recursive_data_yml/file2.yaml'
path_sample_file = 'tests/fixtures/recursive_data_yml/' \
'sample_output_recursion.yaml'
with open(path_sample_file, 'r', encoding='utf=8') as sample:
return path_file1, path_file2, sample.read().rstrip()


def get_recursion_json():
path_file1 = 'tests/fixtures/recursive_data_json/file1.json'
path_file2 = 'tests/fixtures/recursive_data_json/file2.json'
path_sample_file = 'tests/fixtures/recursive_data_json/' \
'sample_output_recursion.json'
with open(path_sample_file, 'r', encoding='utf=8') as sample:
return path_file1, path_file2, sample.read().rstrip()


def get_files_json():
path_file1 = 'tests/fixtures/flat_data_json/file1.json'
path_file2 = 'tests/fixtures/flat_data_json/file2.json'
path_sample_file = 'tests/fixtures/flat_data_json/sample_output.json'
with open(path_sample_file, 'r', encoding='utf=8') as sample:
return path_file1, path_file2, sample.read().rstrip()


def get_files_yaml():
path_file1 = 'tests/fixtures/flat_data_yml/file1.yml'
path_file2 = 'tests/fixtures/flat_data_yml/file2.yml'
path_sample_file = 'tests/fixtures/flat_data_yml/sample_output.yml'
with open(path_sample_file, 'r', encoding='utf=8') as sample:
return path_file1, path_file2, sample.read().rstrip()


def test_json():
result_data = generate_diff(*get_recursion_json()[:LAST_ELEM], 'json')
sample_file = open('tests/fixtures/check_json.json', 'r').read().rstrip()
verification_file = sample_file
assert result_data == verification_file


def test_stylish():
files_for_test = (get_files_yaml(),
get_files_json(),
get_recursion_json(),
get_recursion_yaml())

for files in files_for_test:
result_data = generate_diff(*files[:LAST_ELEM], 'stylish')
verification_file = files[LAST_ELEM]
assert result_data == verification_file


def test_plain():
result_data = generate_diff(*get_recursion_json()[:LAST_ELEM], 'plain')
sample_file = open('tests/fixtures/sample_file.json', 'r').read().rstrip()
verification_file = sample_file
assert result_data == verification_file
@pytest.mark.parametrize('path1, path2, formatter, expected', [
('tests/fixtures/flat_file1.json',
'tests/fixtures/flat_file2.json', 'stylish',
'tests/fixtures/result_flat_file.txt'),
('tests/fixtures/recursion_file1.json',
'tests/fixtures/recursion_file2.yaml', 'stylish',
'tests/fixtures/recursion_result.txt'),
('tests/fixtures/recursion_file1.json',
'tests/fixtures/recursion_file2.yaml', 'plain',
'tests/fixtures/plain_result.txt'),
('tests/fixtures/recursion_file1.json',
'tests/fixtures/recursion_file2.yaml', 'json',
'tests/fixtures/result_json_format.txt')])
def test_generat_diff(path1, path2, formatter, expected):
with open(expected, 'r') as file:
res = file.read().strip()
assert generate_diff(path1, path2, formatter) == res

0 comments on commit f679517

Please sign in to comment.