-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_editor.py
72 lines (62 loc) · 3.2 KB
/
file_editor.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
#file_editor.py
# ####################################################################### #
# This file is part of the FMRadioLog distribution. #
# Copyright (c) 2024 Augusto Burzo. #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, version 3. #
# #
# This program is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# ####################################################################### #
from tkinter import filedialog as fd, messagebox as mb
import json
class FileHandler:
def __init__(self, mode=None, file=None):
self.file = file
if mode == "read":
self.read_file()
elif mode == "write":
self.write_file()
elif mode == "export_csv":
self.write_csv()
else:
raise ValueError("File handler selection unknown")
@staticmethod
def read_file(file):
if file is None:
file_name = fd.askopenfilename(defaultextension=".swl", filetypes=(("FMRL File", "*.swl"),
("FMRL Bin File", "*.fml")))
with open(file_name, "r", encoding="utf-8") as f:
data = json.loads(f.read())
return data
else:
with open(file, "r", encoding="utf-8") as f:
data = json.loads(f.read())
return data
def write_file(self):
#Saves the bandscan file in plain file or bin file
file_name = fd.asksaveasfilename(defaultextension=".swl", filetypes=(("FMRL File", "*.swl"),
("FMRL Bin File", "*.fml")))
if file_name.endswith(".swl"):
with open(file_name, "w", encoding="utf-8") as f:
json.dump(self.file, f, ensure_ascii=False, indent=4)
return True
elif file_name.endswith(".fml"):
#TODO Add the BIN file section
mb.showwarning(title="Function unavailable", message="The function is not yet available")
else:
pass
def write_csv(self):
file_name = fd.asksaveasfilename(defaultextension=".csv", filetypes=(("Comma separated values", "*.csv"),))
try:
with open(file_name, "w") as f:
f.write(self.file)
except FileNotFoundError:
mb.showinfo(title="File not saved", message="The file was not saved")