appsmanager.py 2.58 KB
Newer Older
1 2 3 4 5 6 7 8
import subprocess
import threading

from gi.repository import GLib

class ApplicationManager:
    @staticmethod
    def get_available_applications(callback):
9
        threading.Thread(target=ApplicationManager._get_available_applications, daemon=True, args=(callback,)).start()
10 11 12 13 14 15 16 17 18 19 20 21 22

    @staticmethod
    def _get_available_applications(callback):
        process = subprocess.Popen(["epm", "play", "--list-all"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

        def read_output():
            stdout, stderr = process.communicate()
            if process.returncode == 0:
                applications = ApplicationManager.parse_applications_output(stdout)
                GLib.idle_add(callback, applications)
            else:
                GLib.idle_add(callback, [], stderr)  # Pass error message

23
        threading.Thread(target=read_output, daemon=True).start()
24 25 26

    @staticmethod
    def get_installed_applications(callback):
27
        threading.Thread(target=ApplicationManager._get_installed_applications, daemon=True, args=(callback,)).start()
28 29 30 31 32 33 34 35 36 37 38 39 40

    @staticmethod
    def _get_installed_applications(callback):
        process = subprocess.Popen(["epm", "play", "--list"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

        def read_output():
            stdout, stderr = process.communicate()
            if process.returncode == 0:
                installed_apps = ApplicationManager.parse_installed_applications_output(stdout)
                GLib.idle_add(callback, installed_apps)
            else:
                GLib.idle_add(callback, [], stderr)  # Pass error message

41
        threading.Thread(target=read_output, daemon=True).start()
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

    @staticmethod
    def parse_applications_output(output):
        applications = []

        for line in output.splitlines()[1:]:  # Пропустить заголовок
            parts = line.split(' - ', 1)  # Ограничить сплит до 2 частей
            if len(parts) == 2:
                name, dscr = map(str.strip, parts)
                applications.append({
                    'name': name.replace('&', '&'),
                    'dscr': dscr.replace('&', '&')
                })
            else:
                # Логгирование некорректной строки для диагностики
                print(f"Неправильный формат строки: {line}")

        return applications

    @staticmethod
    def parse_installed_applications_output(output):
        lines = output.splitlines()[1:]
        return [line.split(' - ')[0].strip().split()[0] for line in lines]