internet checker decorator

This commit is contained in:
faroukbmiled 2023-09-20 18:45:20 +01:00
parent 1195433771
commit 5d16064f12
3 changed files with 29 additions and 17 deletions

View File

@ -1,7 +1,14 @@
import socket
import src.shared_vars as main_app import src.shared_vars as main_app
from src.imports import * from src.imports import *
# Start helper functions # Start helper functions
#testing app offline
def guard(*args, **kwargs):
pass
socket.socket = guard
def check_config(name, fallback=None): def check_config(name, fallback=None):
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(CONFIG_FILE_PATH) config.read(CONFIG_FILE_PATH)
@ -68,17 +75,19 @@ def create_update_script(current_exe, new_exe, updater_folder, program_name):
return script_path return script_path
def is_internet_available(): def if_internet_available(func):
try: def wrapper(*args, **kwargs):
requests.get("https://www.google.com", timeout=3) try:
return True requests.get("https://www.google.com", timeout=3)
except: return func(*args, **kwargs)
return False except:
show_message("Offline", "No internet connection. Please check your internet connection and try again.")
return
return wrapper
@if_internet_available
def check_for_updates_func(window, ignore_up_todate=False): def check_for_updates_func(window, ignore_up_todate=False):
if not is_internet_available():
show_message("Error!", "Internet connection is not available. Please check your internet connection and try again.")
return
try: try:
latest_version = get_latest_release_version() latest_version = get_latest_release_version()
current_version = VERSION current_version = VERSION
@ -154,6 +163,7 @@ def initialize_steam(master):
show_message("Error!", "An error occurred please check your paths and try again.", icon="cancel") show_message("Error!", "An error occurred please check your paths and try again.", icon="cancel")
master.attributes('-alpha', 1.0) master.attributes('-alpha', 1.0)
@if_internet_available
def valid_id(workshop_id): def valid_id(workshop_id):
url = f"https://steamcommunity.com/sharedfiles/filedetails/?id={workshop_id}" url = f"https://steamcommunity.com/sharedfiles/filedetails/?id={workshop_id}"
response = requests.get(url) response = requests.get(url)

View File

@ -218,10 +218,10 @@ class LibraryTab(ctk.CTkScrollableFrame):
curr_folder_name = zone_path.parent.name curr_folder_name = zone_path.parent.name
workshop_id = extract_json_data(json_path, "PublisherID") or "None" workshop_id = extract_json_data(json_path, "PublisherID") or "None"
name = re.sub(r'\^\w+', '', extract_json_data(json_path, "Title")) name = re.sub(r'\^\w+', '', extract_json_data(json_path, "Title")) or "None"
name = name[:45] + "..." if len(name) > 45 else name name = name[:45] + "..." if len(name) > 45 else name
item_type = extract_json_data(json_path, "Type") item_type = extract_json_data(json_path, "Type") or "None"
folder_name = extract_json_data(json_path, "FolderName") folder_name = extract_json_data(json_path, "FolderName") or "None"
folder_size_bytes = get_folder_size(zone_path.parent) folder_size_bytes = get_folder_size(zone_path.parent)
size = convert_bytes_to_readable(folder_size_bytes) size = convert_bytes_to_readable(folder_size_bytes)
total_size += folder_size_bytes total_size += folder_size_bytes
@ -394,6 +394,7 @@ class LibraryTab(ctk.CTkScrollableFrame):
self.no_items_label.forget() self.no_items_label.forget()
# i know i know ,please make a pull request i cant be bother # i know i know ,please make a pull request i cant be bother
@if_internet_available
def show_map_info(self, workshop, invalid_warn=False): def show_map_info(self, workshop, invalid_warn=False):
for button_view in self.button_view_list: for button_view in self.button_view_list:
button_view.configure(state="disabled") button_view.configure(state="disabled")
@ -468,7 +469,7 @@ class LibraryTab(ctk.CTkScrollableFrame):
self.toplevel_info_window(map_name, map_mod_type, map_size, image, image_size, date_created, self.toplevel_info_window(map_name, map_mod_type, map_size, image, image_size, date_created,
date_updated, stars_image, stars_image_size, ratings_text, url, workshop_id, invalid_warn) date_updated, stars_image, stars_image_size, ratings_text, url, workshop_id, invalid_warn)
except requests.exceptions.RequestException as e: except Exception as e:
show_message("Error", f"Failed to fetch map information.\nError: {e}", icon="cancel") show_message("Error", f"Failed to fetch map information.\nError: {e}", icon="cancel")
for button_view in self.button_view_list: for button_view in self.button_view_list:
button_view.configure(state="normal") button_view.configure(state="normal")
@ -597,10 +598,8 @@ class LibraryTab(ctk.CTkScrollableFrame):
button_view.configure(state="normal") button_view.configure(state="normal")
self.after(0, main_thread) self.after(0, main_thread)
@if_internet_available
def check_for_updates(self, on_launch=False): def check_for_updates(self, on_launch=False):
if not is_internet_available():
show_message("Error!", "Internet connection is not available. Please check your internet connection and try again.")
return
self.after(1, self.update_button.configure(state="disabled")) self.after(1, self.update_button.configure(state="disabled"))
self.update_tooltip.configure(message='Still loading please wait...') self.update_tooltip.configure(message='Still loading please wait...')
cevent = Event() cevent = Event()

View File

@ -513,6 +513,7 @@ class BOIIIWD(ctk.CTk):
link = "https://steamcommunity.com/app/311210/workshop/" link = "https://steamcommunity.com/app/311210/workshop/"
webbrowser.open(link) webbrowser.open(link)
@if_internet_available
def download_steamcmd(self): def download_steamcmd(self):
self.edit_steamcmd_path.delete(0, "end") self.edit_steamcmd_path.delete(0, "end")
self.edit_steamcmd_path.insert(0, application_path) self.edit_steamcmd_path.insert(0, application_path)
@ -554,6 +555,7 @@ class BOIIIWD(ctk.CTk):
show_message("Error", "Failed to extract SteamCMD. The downloaded file might be corrupted.", icon="cancel") show_message("Error", "Failed to extract SteamCMD. The downloaded file might be corrupted.", icon="cancel")
os.remove(fr"{steamcmd_zip_path}") os.remove(fr"{steamcmd_zip_path}")
@if_internet_available
def show_map_info(self): def show_map_info(self):
def show_map_thread(): def show_map_thread():
workshop_id = self.edit_workshop_id.get().strip() workshop_id = self.edit_workshop_id.get().strip()
@ -627,7 +629,7 @@ class BOIIIWD(ctk.CTk):
image_size = image.size image_size = image.size
self.toplevel_info_window(map_name, map_mod_type, map_size, image, image_size, date_created , self.toplevel_info_window(map_name, map_mod_type, map_size, image, image_size, date_created ,
date_updated, stars_image, stars_image_size, ratings_text, url) date_updated, stars_image, stars_image_size, ratings_text, url)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
show_message("Error", f"Failed to fetch map information.\nError: {e}", icon="cancel") show_message("Error", f"Failed to fetch map information.\nError: {e}", icon="cancel")
@ -904,6 +906,7 @@ class BOIIIWD(ctk.CTk):
return return
self.after(0, callback) self.after(0, callback)
@if_internet_available
def download_map(self, update=False): def download_map(self, update=False):
self.is_downloading = False self.is_downloading = False
self.fail_threshold = 0 self.fail_threshold = 0