-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
178 lines (167 loc) · 5.08 KB
/
parser.py
File metadata and controls
178 lines (167 loc) · 5.08 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
"github.com/sqweek/dialog"
)
type Data struct {
A string `json:"A" xml:"A" yaml:"A"`
B string `json:"B" xml:"B" yaml:"B"`
}
func parseFileContent(content []byte, fileType string) (Data, error) {
var data Data
switch fileType {
case "json":
err := json.Unmarshal(content, &data)
return data, err
case "xml":
err := xml.Unmarshal(content, &data)
return data, err
case "yaml", "yml":
err := yaml.Unmarshal(content, &data)
return data, err
default:
return data, errors.New("unsupported file type")
}
}
func main() {
filePath, err := dialog.File().Filter("JSON files", "json").Filter("XML files", "xml").Filter("YAML files", "yaml", "yml").Title("Select File").Load()
if err != nil {
fmt.Println("No file selected or error:", err)
return
}
ext := strings.ToLower(filepath.Ext(filePath))
var fileType string
switch ext {
case ".json":
fileType = "json"
case ".xml":
fileType = "xml"
case ".yaml", ".yml":
fileType = "yaml"
default:
fmt.Println("Unsupported file type")
return
}
content, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Println("Error reading file:", err)
return
}
data, err := parseFileContent(content, fileType)
if err != nil {
fmt.Println("Invalid file or format:", err)
return
}
if data.A == "" || data.B == "" {
fmt.Println("Fields A and B are required.")
return
}
if data.A == data.B {
fmt.Println("Fields A and B are the same.")
} else if data.B > data.A {
fmt.Println("Field B is greater than A.")
} else {
fmt.Println("Fields A and B are different.")
}
}
import json
import xml.etree.ElementTree as ET
import yaml
from tkinter import filedialog, messagebox
try:
except ImportError:
yaml = None
def parse_file_content(content, file_type):
if file_type == 'json':
return json.loads(content)
elif file_type == 'xml':
root = ET.fromstring(content)
A = root.findtext('A')
B = root.findtext('B')
return {'A': A, 'B': B}
elif file_type in ('yaml', 'yml'):
if yaml is None:
raise ImportError("PyYAML is not installed")
return yaml.safe_load(content)
else:
raise ValueError('Unsupported file type')
def on_file_select():
file_path = filedialog.askopenfilename(filetypes=[
("JSON files", "*.json"),
("XML files", "*.xml"),
("YAML files", "*.yaml;*.yml")
])
if not file_path:
return
ext = file_path.split('.')[-1].lower()
if ext == 'json':
file_type = 'json'
elif ext == 'xml':
file_type = 'xml'
elif ext in ('yaml', 'yml'):
file_type = 'yaml'
else:
messagebox.showerror("Error", "Unsupported file type")
return
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
data = parse_file_content(content, file_type)
if data.get('A') is None or data.get('B') is None:
messagebox.showerror("Error", "Fields A and B are required.")
return
if data['A'] == data['B']:
messagebox.showinfo("Result", "Fields A and B are the same.")
elif data['B'] > data['A']:
messagebox.showinfo("Result", "Field B is greater than A.")
else:
messagebox.showinfo("Result", "Fields A and B are different.")
except Exception:
messagebox.showerror("Error", "Invalid file or format.")
def on_file_select_duplicated():
file_path = filedialog.askopenfilename(filetypes=[
("JSON files", "*.json"),
("XML files", "*.xml"),
("YAML files", "*.yaml;*.yml")
])
if not file_path:
return
ext = file_path.split('.')[-1].lower()
if ext == 'json':
file_type = 'json'
elif ext == 'xml':
file_type = 'xml'
elif ext in ('yaml', 'yml'):
file_type = 'yaml'
else:
messagebox.showerror("Error", "Unsupported file type")
return
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
data = parse_file_content(content, file_type)
if data.get('A') is None or data.get('B') is None:
messagebox.showerror("Error", "Fields A and B are required.")
return
if data['A'] == data['B']:
messagebox.showinfo("Result", "Fields A and B are the same.")
elif data['B'] > data['A']:
messagebox.showinfo("Result", "Field B is greater than A.")
else:
messagebox.showinfo("Result", "Fields A and B are different.")
except Exception:
messagebox.showerror("Error", "Invalid file or format.")
root = tk.Tk()
root.title("File Parser")
root.geometry("300x100")
btn = tk.Button(root, text="Select File", command=on_file_select)
btn.pack(pady=30)
root.mainloop()