Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mijorus committed May 26, 2023
1 parent f38c0c2 commit 275ba28
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 32 deletions.
5 changes: 2 additions & 3 deletions src/AppDetails.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def complete_load(self, icon: Gtk.Image, load_completed_callback: Optional[Calla

self.title.set_label(cleanhtml(self.app_list_element.name))

version_label = key_in_dict(self.app_list_element.extra_data, 'version')
version_label = self.app_list_element.version
version_label = '' if not version_label else f'<small>{version_label}</small>'

self.version.set_markup(version_label)
Expand All @@ -153,8 +153,7 @@ def complete_load(self, icon: Gtk.Image, load_completed_callback: Optional[Calla
# Load the boxed list with additional information
gtk_list = Gtk.ListBox(css_classes=['boxed-list'], margin_bottom=20)

row = Adw.ActionRow(title=self.provider.name.capitalize(), subtitle='Package type')
logging.info(self.provider.icon)
row = Adw.ActionRow(title=f'{self.provider.name.capitalize()} Gen. {self.app_list_element.generation}', subtitle='Package type')
row_img = Gtk.Image(resource=self.provider.icon, pixel_size=34)
row.add_prefix(row_img)
gtk_list.append(row)
Expand Down
5 changes: 3 additions & 2 deletions src/InstalledAppsList.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re

from .providers.providers_list import appimage_provider
from .providers.AppImageProvider import AppImageListElement
from .models.AppListElement import AppListElement, InstalledStatus
from .models.Models import AppUpdateElement
from .components.FilterEntry import FilterEntry
Expand Down Expand Up @@ -80,11 +81,11 @@ def refresh_list(self):
self.installed_apps_list= Gtk.ListBox(css_classes=["boxed-list"])
self.installed_apps_list_rows = []

installed: List[AppListElement] = appimage_provider.list_installed()
installed: List[AppImageListElement] = appimage_provider.list_installed()

for i in installed:
list_row = AppListBoxItem(i, activatable=True, selectable=True, hexpand=True)
list_row.set_update_version(key_in_dict(i.extra_data, 'version'))
list_row.set_update_version(i.version)

list_row.load_icon()
self.installed_apps_list_rows.append(list_row)
Expand Down
6 changes: 1 addition & 5 deletions src/models/AppListElement.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@ class InstalledStatus(Enum):
UPDATING = 8

class AppListElement():
def __init__(self, name: str, description: str, app_id: str, provider: str, installed_status: InstalledStatus, size: float=0, **kwargs):
def __init__(self, name: str, description: str, app_id: str, provider: str, installed_status: InstalledStatus, size: float=0):
self.name: str = name
self.description: str = description if description.strip() else _('No description provided')
self.app_id = app_id
self.provider: str = provider
self.installed_status: InstalledStatus = installed_status
self.size: Optional[float] = size

self.extra_data: Dict[str, str] = {}
for k, v in kwargs.items():
self.extra_data[k] = v

def set_installed_status(self, installed_status: InstalledStatus):
self.installed_status = installed_status
68 changes: 46 additions & 22 deletions src/providers/AppImageProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import List, Callable, Union, Dict, Optional, List, TypedDict
from gi.repository import GLib, Gtk, Gdk, GdkPixbuf, Gio, GObject, Pango, Adw
from enum import Enum
from dataclasses import dataclass


class ExtractedAppImage():
Expand All @@ -30,33 +31,41 @@ class AppImageUpdateLogic(Enum):
REPLACE = 'REPLACE'
KEEP = 'KEEP'

class AppImageListElement(AppListElement):
def __init__(self, file_path: str, desktop_entry: Optional[DesktopEntry.DesktopEntry], icon: Optional[str], **kwargs):
super().__init__(**kwargs)
self.file_path = file_path
self.desktop_entry = desktop_entry
self.icon = icon
self.extracted: Optional[ExtractedAppImage] = None
self.trusted = (self.installed_status is InstalledStatus.INSTALLED)
self.update_logic: Optional[AppImageUpdateLogic] = None
self.local_file = kwargs['local_file'] if 'local_file' in kwargs else False
@dataclass
class AppImageListElement():
name: str
description: str
app_id: str
provider: str
installed_status: InstalledStatus
file_path: str
generation: int
trusted: bool = False
desktop_entry: Optional[DesktopEntry.DesktopEntry] = None
update_logic: Optional[AppImageUpdateLogic] = None
version: Optional[str] = None
icon: Optional[str] = None
extracted: Optional[ExtractedAppImage] = None
local_file: Optional[Gio.File] = None
size: Optional[float] = None

def set_installed_status(self, installed_status: InstalledStatus):
self.installed_status = installed_status


class AppImageProvider():
def __init__(self):
self.name = 'appimage'
self.name = 'AppImage'
self.icon = "/it/mijorus/gearlever/assets/App-image-logo.png"
logging.info(f'Activating {self.name} provider')

self.supported_mimes = ['application/vnd.appimage', 'application/x-iso9660-appimage']
self.supported_mimes = ['application/x-iso9660-appimage', 'application/vnd.appimage']

self.general_messages = []
self.update_messages = []

self.modal_gfile: Optional[Gio.File] = None
self.modal_gfile_createshortcut_check: Optional[Gtk.CheckButton] = None
self.extraction_folder = GLib.get_tmp_dir() + '/it.mijorus.gearlever/appimages'
self.mount_appimage_process: Optional[subprocess.Popen]
self.mount_appimage_process: Optional[subprocess.Popen] = None

def list_installed(self) -> List[AppImageListElement]:
default_folder_path = self.get_appimages_default_destination_path()
Expand All @@ -82,6 +91,8 @@ def list_installed(self) -> List[AppImageListElement]:
file_path=entry.getExec(),
provider=self.name,
desktop_entry=entry,
trusted=True,
generation=self.get_appimage_generation(Gio.File.new_for_path(entry.getExec()))
)

output.append(list_element)
Expand Down Expand Up @@ -152,7 +163,7 @@ def uninstall(self, el: AppImageListElement):
el.set_installed_status(InstalledStatus.NOT_INSTALLED)

def install(self, el: AppListElement):
print('qwe')
pass

def search(self, query: str) -> List[AppListElement]:
return []
Expand All @@ -176,7 +187,7 @@ def run(self, el: AppImageListElement):
if el.trusted:
os.chmod(el.file_path, 0o755)
terminal.threaded_sh([f'{el.file_path}'], return_stderr=True)

def can_install_file(self, file: Gio.File) -> bool:
return get_giofile_content_type(file) in self.supported_mimes

Expand Down Expand Up @@ -257,7 +268,7 @@ def install_file(self, el: AppImageListElement):
if extracted_appimage.desktop_entry:
final_app_name = f"{extracted_appimage.desktop_entry.getName()}"

version = extracted_appimage.desktop_entry.get('X-AppImage-Version')
version = extracted_appimage.desktop_entry.get('X-AppImage-Version')

if not version:
version = extracted_appimage.md5[0:6]
Expand All @@ -266,9 +277,16 @@ def install_file(self, el: AppImageListElement):
if el.update_logic is AppImageUpdateLogic.KEEP:
final_app_name += f' ({version})'

desktop_file_content = re.sub(
r'^Name\[(.*?)\]=.*$',
'',
desktop_file_content,
flags=re.MULTILINE
)

final_app_name = final_app_name.strip()
desktop_file_content = re.sub(
r'Name=.*$',
r'^Name=.*$',
f"Name={final_app_name}",
desktop_file_content,
flags=re.MULTILINE
Expand All @@ -292,7 +310,13 @@ def install_file(self, el: AppImageListElement):

terminal.sh(['update-desktop-database', '-q'])

def get_appimage_generation(self, file: Gio.File) -> int:
return self.supported_mimes.index(get_giofile_content_type(file)) + 1

def create_list_element_from_file(self, file: Gio.File) -> AppImageListElement:
if not self.can_install_file(file):
raise InternalError(message='This file type is not supported')

app_name: str = file.get_parse_name().split('/')[-1]

el = AppImageListElement(
Expand All @@ -304,7 +328,8 @@ def create_list_element_from_file(self, file: Gio.File) -> AppImageListElement:
file_path=file.get_path(),
desktop_entry=None,
icon=None,
local_file=True
local_file=True,
generation=self.get_appimage_generation(file)
)

if self.is_installed(el):
Expand All @@ -329,7 +354,6 @@ def mount_appimage(self, file_path: str) -> str:
while True:
output = self.process.stdout.readline()
if output == '' and self.process.poll() is not None:
logging.debug('qwe')
break
if output:
out = output.decode('utf-8').strip()
Expand All @@ -353,7 +377,7 @@ def extract_appimage(self, el: AppImageListElement) -> ExtractedAppImage:

file = Gio.File.new_for_path(el.file_path)

if get_giofile_content_type(file) in ['application/x-iso9660-appimage']:
if el.generation < 2:
raise Exception('This file format cannot be extracted!')

icon_file: Optional[Gio.File] = None
Expand Down

0 comments on commit 275ba28

Please sign in to comment.