mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-09 23:34:17 +02:00
suggestions button implemented respecting already installed applications
This commit is contained in:
@@ -26,6 +26,7 @@ from bauh.commons.system import SystemProcess, new_subprocess, ProcessHandler, r
|
||||
from bauh.gems.appimage import query, INSTALLATION_PATH, LOCAL_PATH, SUGGESTIONS_FILE
|
||||
from bauh.gems.appimage.config import read_config
|
||||
from bauh.gems.appimage.model import AppImage
|
||||
from bauh.gems.appimage.query import FIND_APPS_BY_NAME, FIND_APPS_BY_NAME_ONLY_NAME
|
||||
from bauh.gems.appimage.worker import DatabaseUpdater
|
||||
|
||||
DB_APPS_PATH = '{}/{}'.format(HOME_PATH, '.local/share/bauh/appimage/apps.db')
|
||||
@@ -104,7 +105,8 @@ class AppImageManager(SoftwareManager):
|
||||
res.total = len(res.installed) + len(res.new)
|
||||
return res
|
||||
|
||||
def read_installed(self, disk_loader: DiskCacheLoader, limit: int = -1, only_apps: bool = False, pkg_types: Set[Type[SoftwarePackage]] = None, internet_available: bool = None) -> SearchResult:
|
||||
def read_installed(self, disk_loader: DiskCacheLoader, limit: int = -1, only_apps: bool = False,
|
||||
pkg_types: Set[Type[SoftwarePackage]] = None, internet_available: bool = None, connection: sqlite3.Connection = None) -> SearchResult:
|
||||
res = SearchResult([], [], 0)
|
||||
|
||||
if os.path.exists(INSTALLATION_PATH):
|
||||
@@ -122,7 +124,7 @@ class AppImageManager(SoftwareManager):
|
||||
names.add("'{}'".format(app.name.lower()))
|
||||
|
||||
if res.installed:
|
||||
con = self._get_db_connection(DB_APPS_PATH)
|
||||
con = self._get_db_connection(DB_APPS_PATH) if not connection else connection
|
||||
|
||||
if con:
|
||||
try:
|
||||
@@ -142,7 +144,8 @@ class AppImageManager(SoftwareManager):
|
||||
except:
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
self._close_connection(DB_APPS_PATH, con)
|
||||
if not connection:
|
||||
self._close_connection(DB_APPS_PATH, con)
|
||||
|
||||
res.total = len(res.installed)
|
||||
return res
|
||||
@@ -432,14 +435,23 @@ class AppImageManager(SoftwareManager):
|
||||
try:
|
||||
sugs = [l for l in file.text.split('\n') if l]
|
||||
|
||||
if 0 < limit < len(sugs):
|
||||
sugs = sugs[0:limit]
|
||||
if filter_installed:
|
||||
installed = {i.name.lower() for i in self.read_installed(disk_loader=None, connection=connection).installed}
|
||||
else:
|
||||
installed = None
|
||||
|
||||
sugs_map = {}
|
||||
|
||||
for s in sugs:
|
||||
lsplit = s.split('=')
|
||||
sugs_map[lsplit[1].strip()] = SuggestionPriority(int(lsplit[0]))
|
||||
|
||||
name = lsplit[1].strip()
|
||||
|
||||
if limit <= 0 or len(sugs_map) < limit:
|
||||
if not installed or not name.lower() in installed:
|
||||
sugs_map[name] = SuggestionPriority(int(lsplit[0]))
|
||||
else:
|
||||
break
|
||||
|
||||
cursor = connection.cursor()
|
||||
cursor.execute(query.FIND_APPS_BY_NAME_FULL.format(','.join(["'{}'".format(s) for s in sugs_map.keys()])))
|
||||
|
||||
@@ -5,5 +5,6 @@ RELEASE_ATTRS = ('version', 'url_download', 'published_at')
|
||||
SEARCH_APPS_BY_NAME_OR_DESCRIPTION = "SELECT {} FROM apps".format(','.join(APP_ATTRS)) + " WHERE lower(name) LIKE '%{}%' or lower(description) LIKE '%{}%'"
|
||||
FIND_APP_ID_BY_NAME_AND_GITHUB = "SELECT id FROM apps WHERE lower(name) = '{}' and lower(github) = '{}'"
|
||||
FIND_APPS_BY_NAME = "SELECT name, github, version, url_download FROM apps WHERE lower(name) IN ({})"
|
||||
FIND_APPS_BY_NAME_ONLY_NAME = "SELECT name FROM apps WHERE lower(name) IN ({})"
|
||||
FIND_APPS_BY_NAME_FULL = "SELECT {} FROM apps".format(','.join(APP_ATTRS)) + " WHERE lower(name) IN ({})"
|
||||
FIND_RELEASES_BY_APP_ID = "SELECT {} FROM releases".format(','.join(RELEASE_ATTRS)) + " WHERE app_id = {} ORDER BY version desc"
|
||||
|
||||
@@ -848,7 +848,10 @@ class ArchManager(SoftwareManager):
|
||||
if l:
|
||||
if limit <= 0 or len(suggestions) < limit:
|
||||
lsplit = l.split('=')
|
||||
suggestions[lsplit[1].strip()] = SuggestionPriority(int(lsplit[0]))
|
||||
name = lsplit[1].strip()
|
||||
|
||||
if not filter_installed or not pacman.check_installed(name):
|
||||
suggestions[name] = SuggestionPriority(int(lsplit[0]))
|
||||
|
||||
api_res = self.aur_client.get_info(suggestions.keys())
|
||||
|
||||
|
||||
@@ -147,7 +147,12 @@ class FlatpakManager(SoftwareManager):
|
||||
return ProcessHandler(watcher).handle(SystemProcess(subproc=flatpak.update(pkg.ref)))
|
||||
|
||||
def uninstall(self, pkg: FlatpakApplication, root_password: str, watcher: ProcessWatcher) -> bool:
|
||||
return ProcessHandler(watcher).handle(SystemProcess(subproc=flatpak.uninstall(pkg.ref)))
|
||||
uninstalled = ProcessHandler(watcher).handle(SystemProcess(subproc=flatpak.uninstall(pkg.ref)))
|
||||
|
||||
if self.suggestions_cache:
|
||||
self.suggestions_cache.delete(pkg.id)
|
||||
|
||||
return uninstalled
|
||||
|
||||
def get_info(self, app: FlatpakApplication) -> dict:
|
||||
if app.installed:
|
||||
@@ -272,12 +277,19 @@ class FlatpakManager(SoftwareManager):
|
||||
return res
|
||||
else:
|
||||
self.logger.info("Mapping suggestions")
|
||||
installed = {i.id for i in self.read_installed(disk_loader=None).installed} if filter_installed else None
|
||||
|
||||
for line in file.text.split('\n'):
|
||||
if line:
|
||||
if limit <= 0 or len(res) < limit:
|
||||
sug = line.split('=')
|
||||
appid, priority = sug[1], SuggestionPriority(int(sug[0]))
|
||||
appid = sug[1].strip()
|
||||
|
||||
if installed and appid in installed:
|
||||
continue
|
||||
|
||||
priority = SuggestionPriority(int(sug[0]))
|
||||
|
||||
cached_sug = self.suggestions_cache.get(appid)
|
||||
|
||||
if cached_sug:
|
||||
|
||||
@@ -8,6 +8,7 @@ from bauh.api.exception import NoInternetException
|
||||
from bauh.commons.system import new_subprocess, run_cmd, new_root_subprocess
|
||||
|
||||
BASE_CMD = 'flatpak'
|
||||
RE_SEVERAL_SPACES = re.compile(r'\s+')
|
||||
|
||||
|
||||
def get_app_info_fields(app_id: str, branch: str, fields: List[str] = [], check_runtime: bool = False):
|
||||
|
||||
@@ -126,7 +126,12 @@ class SnapManager(SoftwareManager):
|
||||
raise Exception("'update' is not supported by {}".format(pkg.__class__.__name__))
|
||||
|
||||
def uninstall(self, pkg: SnapApplication, root_password: str, watcher: ProcessWatcher) -> bool:
|
||||
return ProcessHandler(watcher).handle(SystemProcess(subproc=snap.uninstall_and_stream(pkg.name, root_password)))
|
||||
uninstalled = ProcessHandler(watcher).handle(SystemProcess(subproc=snap.uninstall_and_stream(pkg.name, root_password)))
|
||||
|
||||
if self.suggestions_cache:
|
||||
self.suggestions_cache.delete(pkg.name)
|
||||
|
||||
return uninstalled
|
||||
|
||||
def get_managed_types(self) -> Set[Type[SoftwarePackage]]:
|
||||
return {SnapApplication}
|
||||
@@ -250,20 +255,24 @@ class SnapManager(SoftwareManager):
|
||||
self.categories_downloader.join()
|
||||
|
||||
suggestions, threads = [], []
|
||||
installed = {i.name.lower() for i in self.read_installed(disk_loader=None).installed} if filter_installed else None
|
||||
|
||||
for l in file.text.split('\n'):
|
||||
if l:
|
||||
if limit <= 0 or len(suggestions) < limit:
|
||||
sug = l.strip().split('=')
|
||||
cached_sug = self.suggestions_cache.get(sug[1])
|
||||
name = sug[1]
|
||||
|
||||
if cached_sug:
|
||||
res.append(cached_sug)
|
||||
else:
|
||||
t = Thread(target=self._fill_suggestion, args=(sug[1], SuggestionPriority(int(sug[0])), res))
|
||||
t.start()
|
||||
threads.append(t)
|
||||
time.sleep(0.001) # to avoid being blocked
|
||||
if not installed or name not in installed:
|
||||
cached_sug = self.suggestions_cache.get(name)
|
||||
|
||||
if cached_sug:
|
||||
res.append(cached_sug)
|
||||
else:
|
||||
t = Thread(target=self._fill_suggestion, args=(name, SuggestionPriority(int(sug[0])), res))
|
||||
t.start()
|
||||
threads.append(t)
|
||||
time.sleep(0.001) # to avoid being blocked
|
||||
else:
|
||||
break
|
||||
|
||||
|
||||
@@ -782,6 +782,17 @@ class WebApplicationManager(SoftwareManager):
|
||||
if suggestions:
|
||||
suggestion_list = list(suggestions.values())
|
||||
suggestion_list.sort(key=lambda s: s.get('priority', 0), reverse=True)
|
||||
|
||||
if filter_installed:
|
||||
installed = self.read_installed(disk_loader=None)
|
||||
url_map = {self._strip_url_protocol(s['url']): idx for idx, s in enumerate(suggestion_list)}
|
||||
|
||||
if installed.installed:
|
||||
for i in installed.installed:
|
||||
s_url = self._strip_url_protocol(i.url)
|
||||
if s_url in url_map:
|
||||
suggestion_list.pop(url_map[s_url])
|
||||
|
||||
to_map = suggestion_list if limit <= 0 else suggestion_list[0:limit]
|
||||
res = [self._map_suggestion(s) for s in to_map]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user