-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
105 lines (89 loc) · 3.18 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import sys
import json
import yaml
import xmltodict
def parse_arguments():
if len(sys.argv) != 3:
print("Sposób użycia: program.py pathFile1.x pathFile2.y")
print("gdzie x i y to jeden z formatów .xml, .json i .yml (.yaml).")
sys.exit(1)
path_file1 = sys.argv[1]
path_file2 = sys.argv[2]
format1 = sys.argv[1].split(".")[-1]
format2 = sys.argv[2].split(".")[-1]
if format1 not in ["xml", "json", "yml"] or format2 not in ["xml", "json", "yml"]:
print("Nieprawidłowy format pliku. Obsługiwane formaty to .xml, .json i .yml (.yaml).")
sys.exit(1)
return path_file1, path_file2, format1, format2
def load_json(file_path):
try:
with open(file_path, 'r') as file:
data = json.load(file)
print("JSON data loaded successfully")
return data
except Exception as e:
print(f"Failed to load JSON file: {e}")
sys.exit(1)
def save_json(data, file_path):
try:
with open(file_path, 'w') as file:
json.dump(data, file, indent=4)
except Exception as e:
print(f"Wystąpił błąd podczas zapisywania danych do pliku JSON: {e}")
def load_yaml(file_path):
try:
with open(file_path, 'r') as file:
data = yaml.safe_load(file)
return data
except FileNotFoundError:
print(f"Plik '{file_path}' nie istnieje.")
sys.exit(1)
except yaml.YAMLError:
print(f"Niewłaściwy format pliku YAML: '{file_path}'.")
sys.exit(1)
def save_yaml(data, file_path):
try:
with open(file_path, 'w') as file:
yaml.dump(data, file, default_flow_style=False)
except Exception as e:
print(f"Wystąpił błąd podczas zapisywania danych do pliku YAML: {e}")
def load_xml(file_path):
try:
with open(file_path, 'r') as file:
data = xmltodict.parse(file.read())
return data
except FileNotFoundError:
print(f"Plik '{file_path}' nie istnieje.")
sys.exit(1)
except Exception as e:
print(f"Niewłaściwy format pliku XML: '{file_path}': {e}")
sys.exit(1)
def save_xml(data, file_path):
try:
with open(file_path, 'w') as file:
xml_str = xmltodict.unparse(data, pretty=True)
file.write(xml_str)
except Exception as e:
print(f"Wystąpił błąd podczas zapisywania danych do pliku XML: {e}")
def convert_data(data, input_format, output_format):
if input_format == output_format:
return data
if output_format == 'xml':
return data
return data
if __name__ == "__main__":
path_file1, path_file2, format1, format2 = parse_arguments()
if format1 == "json":
data = load_json(path_file1)
elif format1 == "yml":
data = load_yaml(path_file1)
elif format1 == "xml":
data = load_xml(path_file1)
converted_data = convert_data(data, format1, format2)
if format2 == "json":
save_json(converted_data, path_file2)
elif format2 == "yml":
save_yaml(converted_data, path_file2)
elif format2 == "xml":
save_xml(converted_data, path_file2)
print(f"Dane przekonwertowane i zapisane do pliku: {path_file2}")