-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51c0652
commit 4df4fd7
Showing
7 changed files
with
36 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import argparse | ||
|
||
|
||
def parse_file(): | ||
parser = argparse.ArgumentParser( | ||
description='Compares two configuration files and shows a difference.' | ||
) | ||
parser.add_argument('first_file') | ||
parser.add_argument('second_file') | ||
parser.add_argument( | ||
'-f', '--format', default='stylish', | ||
choices=['stylish', 'plain', 'json'], | ||
help='set format of output (default: \'stylish\')', | ||
) | ||
return parser.parse_args() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import json | ||
|
||
|
||
def json_format(current_data): | ||
def get_jsoned(current_data): | ||
return json.dumps(current_data, indent=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,23 @@ | ||
import argparse | ||
import json | ||
import yaml | ||
|
||
|
||
def parse_file(): | ||
parser = argparse.ArgumentParser( | ||
description='Compares two configuration files and shows a difference.' | ||
) | ||
parser.add_argument('first_file') | ||
parser.add_argument('second_file') | ||
parser.add_argument( | ||
'-f', '--format', default='stylish', | ||
choices=['stylish', 'plain', 'json'], | ||
help='set format of output (default: \'stylish\')', | ||
) | ||
return parser.parse_args() | ||
|
||
|
||
def get_file_format(filepath): | ||
file = open(filepath) | ||
if filepath.endswith('.json'): | ||
return 'json' | ||
return file, 'json' | ||
elif filepath.endswith('.yml') or filepath.endswith('.yaml'): | ||
return 'yaml' | ||
return file, 'yaml' | ||
else: | ||
file.close() | ||
raise TypeError("Invalid file format!") | ||
|
||
|
||
def read_file_data(filepath): | ||
with open(filepath) as file: | ||
if get_file_format(filepath) == 'json': | ||
return json.load(file) | ||
elif get_file_format(filepath) == 'yaml': | ||
return yaml.safe_load(file) | ||
else: | ||
raise TypeError("Invalid file format!") | ||
file, file_format = get_file_format(filepath) | ||
if file_format == 'json': | ||
return json.load(file) | ||
elif file_format == 'yaml': | ||
return yaml.safe_load(file) | ||
else: | ||
raise TypeError("Invalid file format!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters