Files
shared_libs/gitea.py

134 lines
4.1 KiB
Python

#!/usr/bin/python3
import gettext
import locale
import requests
from pathlib import Path
import subprocess
import shutil
from shared_libs.common_tools import LxTools
from shared_libs.message import MessageDialog
class GiteaUpdate:
"""
Calling download requests the download URL of the running script,
the taskbar image for the “Download OK” window, the taskbar image for the
“Download error” window, and the variable res
"""
@staticmethod
def api_down(update_api_url: str, version: str, update_setting: str = None) -> str:
"""
Checks for updates via API
Args:
update_api_url: Update API URL
version: Current version
update_setting: Update setting from ConfigManager (on/off)
Returns:
New version or status message
"""
# If updates are disabled, return immediately
if update_setting != "on":
return "False"
try:
response: requests.Response = requests.get(update_api_url, timeout=10)
response.raise_for_status() # Raise exception for HTTP errors
response_data = response.json()
if not response_data:
return "No Updates"
latest_version = response_data[0].get("tag_name")
if not latest_version:
return "Invalid API Response"
# Compare versions (strip 'v. ' prefix if present)
current_version = version[3:] if version.startswith("v. ") else version
if current_version != latest_version:
return latest_version
else:
return "No Updates"
except requests.exceptions.RequestException:
return "No Internet Connection!"
except (ValueError, KeyError, IndexError):
return "Invalid API Response"
@staticmethod
def download(urld: str, res: str) -> None:
"""
Downloads new version of application
:param urld: Download URL
:param res: Result filename
"""
try:
to_down: str = f"wget -qP {Path.home()} {" "} {urld}"
result: int = subprocess.call(to_down, shell=True)
if result == 0:
shutil.chown(f"{Path.home()}/{res}.zip", 1000, 1000)
MessageDialog("info", text=Msg.STR["ok_message"])
else:
MessageDialog(
"error", text=Msg.STR["error_message"], title=Msg.STR["error_title"]
)
except subprocess.CalledProcessError:
MessageDialog(
"error", text=Msg.STR["error_no_internet"], title=Msg.STR["error_title"]
)
class AppConfig:
# Localization
APP_NAME: str = "gitea"
LOCALE_DIR: Path = Path("/usr/share/locale/")
@staticmethod
def setup_translations() -> gettext.gettext:
"""
Initialize translations and set the translation function
Special method for translating strings in this file
Returns:
The gettext translation function
"""
locale.bindtextdomain(AppConfig.APP_NAME, AppConfig.LOCALE_DIR)
gettext.bindtextdomain(AppConfig.APP_NAME, AppConfig.LOCALE_DIR)
gettext.textdomain(AppConfig.APP_NAME)
return gettext.gettext
# Images and icons paths
IMAGE_PATHS: dict[str, Path] = {
"icon_info": "/usr/share/icons/lx-icons/64/info.png",
"icon_error": "/usr/share/icons/lx-icons/64/error.png",
"icon_download": "/usr/share/icons/lx-icons/48/download.png",
"icon_download_error": "/usr/share/icons/lx-icons/48/download_error.png",
}
# here is initializing the class for translation strings
_ = AppConfig.setup_translations()
class Msg:
STR: dict[str, str] = {
# Strings for messages
"title": _("Download Successful"),
"ok_message": _("Your zip file is in home directory"),
"error_title": _("Download error"),
"error_message": _("Download failed! Please try again"),
"error_no_internet": _("Download failed! No internet connection!"),
}