Skip to content

Commit 8310bf3

Browse files
authored
Merge pull request #43 from relic-se/release-data-refresh
Improve application updating
2 parents a50d605 + a12917c commit 8310bf3

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

code.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def is_application_installed(name: str, category: str = None) -> bool:
141141

142142
# file download + caching
143143

144-
def _download_file(url: str, extension: str, name: str|None = None) -> str:
144+
def _download_file(url: str, extension: str, name: str|None = None, cache: bool = True) -> str:
145145
if not extension.startswith("."):
146146
extension = "." + extension
147147

@@ -151,35 +151,42 @@ def _download_file(url: str, extension: str, name: str|None = None) -> str:
151151
name = name[:-len(extension)]
152152
path = get_path([CACHE_DIR, name + extension])
153153

154+
# remove file from cache if it exists and we're refreshing
155+
if not cache and exists(path):
156+
os.remove(path)
157+
154158
# download file if it doesn't already exist
155-
if not exists(path):
159+
if not cache or not exists(path):
156160
fj.network.connect() # ensure we're connected to wifi
157161
fj.network.wget(url, path)
158162
# TODO: Cache duration
159163
return path
160164

161-
def download_image(url: str, name: str|None = None) -> str:
165+
def download_image(url: str, name: str|None = None, cache: bool = True) -> str:
162166
return _download_file(
163167
url=url,
164168
extension=".bmp",
165169
name=name,
170+
cache=cache,
166171
)
167172

168-
def download_json(url: str, name: str|None = None) -> str:
173+
def download_json(url: str, name: str|None = None, cache: bool = True) -> str:
169174
path = _download_file(
170175
url=url,
171176
extension=".json",
172177
name=name,
178+
cache=cache,
173179
)
174180
with open(path, "r") as f:
175181
data = json.loads(f.read())
176182
return data
177183

178-
def download_zip(url: str, name: str|None = None) -> str:
184+
def download_zip(url: str, name: str|None = None, cache: bool = True) -> str:
179185
return _download_file(
180186
url=url,
181187
extension=".zip",
182188
name=name,
189+
cache=cache,
183190
)
184191

185192
# get Fruit Jam OS config if available
@@ -764,6 +771,7 @@ def download_application(full_name: str = None) -> bool:
764771
release = download_json(
765772
url=RELEASE_URL.format(full_name),
766773
name=full_name.replace("/", "_") + "_release",
774+
cache=False,
767775
)
768776
except (OSError, ValueError, HttpError) as e:
769777
log("Unable to read release data from {:s}! {:s}".format(full_name, str(e)))
@@ -774,6 +782,7 @@ def download_application(full_name: str = None) -> bool:
774782
repository = download_json(
775783
url=REPO_URL.format(full_name),
776784
name=full_name.replace("/", "_"),
785+
cache=False,
777786
)
778787
except (OSError, ValueError, HttpError) as e:
779788
log("Unable to read repository data from {:s}! {:s}".format(full_name, str(e)))
@@ -793,7 +802,7 @@ def download_application(full_name: str = None) -> bool:
793802
# download project bundle
794803
log("Downloading release assets...")
795804
try:
796-
zip_path = download_zip(download_url, repo_name)
805+
zip_path = download_zip(download_url, repo_name, cache=False)
797806
except (OSError, ValueError, HttpError) as e:
798807
log("Failed to download release assets for {:s}! {:s}".format(full_name, str(e)))
799808
return False
@@ -941,6 +950,7 @@ def select_application(index: int|tuple) -> None:
941950
actions=list(filter(lambda x: x, [
942951
("Cancel", deselect_application),
943952
("Remove", toggle_application),
953+
("Update", update_application),
944954
("Open", open_application) if not is_screensaver else None,
945955
])),
946956
)
@@ -976,6 +986,27 @@ def toggle_application(full_name: str = None) -> bool:
976986

977987
return result
978988

989+
def update_application(full_name: str = None) -> bool:
990+
global selected_application, current_page
991+
if full_name is None:
992+
if selected_application is None:
993+
return False
994+
full_name = selected_application
995+
repo_owner, repo_name = selected_application.split("/")
996+
997+
result = False
998+
if is_application_installed(repo_name):
999+
if remove_application(full_name):
1000+
result = download_application(full_name)
1001+
else:
1002+
log("Unable to locate application")
1003+
1004+
# hide dialog and update installed state
1005+
deselect_application()
1006+
refresh_page()
1007+
1008+
return result
1009+
9791010
# item selection
9801011

9811012
selected_item = None

0 commit comments

Comments
 (0)