Skip to content

Commit 5d1c203

Browse files
committed
update method for storing data
Storing settings and other files that need to persist now use flet's storage environment variable so settings and other files persist after app updates
1 parent 2808659 commit 5d1c203

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

main.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55
import text_values as text_values # Long text values stored in separate file
66
from dynamic_modal import DynamicModal
77
from are_you_sure import YouSure
8-
from settings_values import settings_values, custom_scripts, load_settings, update_scripts, settings_path
8+
from settings_values import *
99

1010
# Create settings.json if not exists and/or load saved values
1111
load_settings(e=None, update=False)
1212

1313
# Create recent_computers.json
14-
recent_computers_path = "assets/settings/recent_computers.json"
1514
if os.path.exists(recent_computers_path) == False:
1615
with open(recent_computers_path, "w") as file:
1716
print(f"{recent_computers_path} created")
1817
init_data = {"computers":[]}
1918
json.dump(init_data, file, indent=4)
2019

21-
list_path = "assets/settings/lists/computers.txt"
20+
computerlist_path = "assets/settings/lists/computers.txt"
2221

2322
#Cleanup old files
2423
printers_path = "assets/results/Printers"
@@ -50,15 +49,13 @@
5049
for filename in os.listdir(uptime_path):
5150
pathlib.Path(f"{uptime_path}/{filename}").unlink()
5251

53-
54-
5552
# Program
5653
def main(page: ft.Page):
5754
#Setup PowerShell
5855
powershell = the_shell.Power_Shell()
5956

6057
page.fonts = {
61-
"Consola": "assets/fonts/Consola.ttf"
58+
"Consola": "fonts/Consola.ttf"
6259
}
6360

6461
def min_max(e):
@@ -78,7 +75,7 @@ def min_max(e):
7875
drag_window = ft.Container(
7976
content=ft.Row([
8077
ft.Container(
81-
content=ft.Image(src="assets/images/itremote.svg", width=32),
78+
content=ft.Image(src="images/itremote.svg", width=32),
8279
padding=ft.padding.only(left=10, top=3)
8380
),
8481
ft.WindowDragArea(ft.Container(
@@ -614,18 +611,18 @@ def check_computer_name():
614611
def check_list():
615612

616613
try:
617-
with open(list_path, "r") as file:
614+
with open(computerlist_path, "r") as file:
618615

619616
contents = file.read()
620617
if contents == "":
621618
show_message("List is empty.")
622619
return False
623620
except FileNotFoundError as e:
624-
with open(list_path, "x") as file:
621+
with open(computerlist_path, "x") as file:
625622
print("Computer list file created.")
626623

627624
try:
628-
with open(list_path, "r") as list:
625+
with open(computerlist_path, "r") as list:
629626
computers = list.read()
630627
if computers == "":
631628
show_message("List is empty.")
@@ -637,7 +634,7 @@ def check_list():
637634
else:
638635
return True
639636
except FileNotFoundError as e:
640-
with open(list_path, "x") as file:
637+
with open(computerlist_path, "x") as file:
641638
print("Computer list file created.")
642639

643640
def find_day(date):
@@ -2172,7 +2169,7 @@ def run_operation(computer):
21722169
# an array of them.
21732170
if computer == "list of computers":
21742171
list_of_pcs = []
2175-
list = open(list_path, "r")
2172+
list = open(computerlist_path, "r")
21762173
computers = list.readlines()
21772174
for pc in computers:
21782175
list_of_pcs.append(pc.strip("\\n"))
@@ -2850,7 +2847,7 @@ def clicked(e):
28502847

28512848
def open_errors(e):
28522849
try:
2853-
with open("assets/settings/log.txt", "r") as file:
2850+
with open(logging_path, "r") as file:
28542851
errors = file.readlines()
28552852
list_of_errors = []
28562853

@@ -3429,7 +3426,7 @@ def on_github_hover(e):
34293426
width=210
34303427
),
34313428
ft.Container(
3432-
content=ft.Image(src="assets/images/Github.png", width=70),
3429+
content=ft.Image(src="images/Github.png", width=70),
34333430
url="https://github.com/knightlygains/it-remote",
34343431
offset=ft.transform.Offset(2, 2),
34353432
on_hover=on_github_hover,
@@ -3446,7 +3443,7 @@ def on_github_hover(e):
34463443
ft.Row([
34473444
ft.Container(
34483445
content=ft.Column([
3449-
ft.Image(src="assets/images/ko-fi.png", width=70),
3446+
ft.Image(src="images/ko-fi.png", width=70),
34503447
ft.Text("Ko-Fi", weight=ft.FontWeight.BOLD)
34513448
], horizontal_alignment=ft.CrossAxisAlignment.CENTER),
34523449
margin=ft.margin.only(top=20),
@@ -3456,7 +3453,7 @@ def on_github_hover(e):
34563453
),
34573454
ft.Container(
34583455
content=ft.Column([
3459-
ft.Image(src="assets/images/patreon.png", width=70),
3456+
ft.Image(src="images/patreon.png", width=70),
34603457
ft.Text("Patreon", weight=ft.FontWeight.BOLD)
34613458
], horizontal_alignment=ft.CrossAxisAlignment.CENTER),
34623459
margin=ft.margin.only(top=20),

settings_values.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,41 @@
1414
"home_tab": 1
1515
}
1616

17+
# Dict to store custom_script info
1718
custom_scripts = {
1819

1920
}
2021

21-
custom_scripts_path = "assets/settings/custom_scripts.json"
22-
settings_path = "assets/settings/settings.json"
23-
computerlist_path = "assets/settings/lists/computers.txt"
24-
logging_path = "assets/settings/log.txt"
22+
# Flet's storage directory that persists after updates
23+
storage_directory = os.getenv("FLET_APP_STORAGE_DATA")
24+
25+
# Define paths for different settings/files
26+
custom_scripts_path = f"{storage_directory}/custom_scripts.json"
27+
settings_path = f"{storage_directory}/settings.json"
28+
computerlist_path = f"{storage_directory}/computers.txt"
29+
logging_path = f"{storage_directory}/log.txt"
30+
recent_computers_path = f"{storage_directory}/recent_computers.json"
2531

2632
# Make settings directory
27-
if not os.path.exists("assets/settings"):
33+
if not os.path.exists(settings_path):
2834
try:
29-
os.mkdir("assets/settings")
35+
with open(settings_path, "w") as file:
36+
print(f"Created {settings_path}")
3037
except Exception as e:
3138
print(e)
3239

3340
if not os.path.exists(logging_path):
3441
try:
3542
with open(logging_path, "w") as file:
36-
print("Created log.txt")
43+
print(f"Created {logging_path}")
3744
except Exception as e:
3845
print(e)
3946

4047
# Make lists directory
41-
if not os.path.exists("assets/settings/lists"):
48+
if not os.path.exists(computerlist_path):
4249
try:
43-
os.mkdir("assets/settings/lists")
50+
with open(computerlist_path, "w") as file:
51+
print(f"Created {computerlist_path}")
4452
except Exception as e:
4553
print(e)
4654

@@ -80,7 +88,7 @@ def load_settings(e, update):
8088
if update:
8189
try:
8290
with open(settings_path, "r") as file:
83-
print("settings.json exists, updating")
91+
print(f"{settings_path} exists, updating")
8492
data = json.load(file)
8593
# Update each setting with value
8694
# stored in settings_values
@@ -118,10 +126,10 @@ def load_settings(e, update):
118126
settings_values[key] = value
119127
except json.decoder.JSONDecodeError:
120128
print("No settings data found")
121-
except FileNotFoundError:
122-
print("No settings.json found. Creating a new one.")
129+
except Exception as e:
130+
print(f"No {settings_path} found. Creating a new one.")
123131
with open(settings_path, "w") as file:
124-
print("settings.json created")
132+
print(f"{settings_path} created")
125133
json.dump(settings_values, file, indent=4)
126134

127135
# Create custom_scripts json

0 commit comments

Comments
 (0)