[snap] mapping categories | fix: not caching installed applications data

This commit is contained in:
Vinicius Moreira
2019-10-14 12:15:12 -03:00
parent 5baa697f0d
commit 4702f473d4
5 changed files with 78 additions and 16 deletions

View File

@@ -13,7 +13,7 @@ from bauh.commons.system import SystemProcess, ProcessHandler
from bauh.gems.snap import snap, suggestions
from bauh.gems.snap.constants import SNAP_API_URL
from bauh.gems.snap.model import SnapApplication
from bauh.gems.snap.worker import SnapAsyncDataLoader
from bauh.gems.snap.worker import SnapAsyncDataLoader, CategoriesDownloader
class SnapManager(SoftwareManager):
@@ -27,6 +27,8 @@ class SnapManager(SoftwareManager):
self.http_client = context.http_client
self.logger = context.logger
self.ubuntu_distro = context.distro == 'ubuntu'
self.categories = {}
self.categories_downloader = CategoriesDownloader(self.http_client, self.logger)
def map_json(self, app_json: dict, installed: bool, disk_loader: DiskCacheLoader, internet: bool = True) -> SnapApplication:
app = SnapApplication(publisher=app_json.get('publisher'),
@@ -42,6 +44,8 @@ class SnapManager(SoftwareManager):
if app.publisher:
app.publisher = app.publisher.replace('*', '')
app.categories = self.categories.get(app.name)
app.installed = installed
api_data = self.api_cache.get(app_json['name'])
@@ -139,7 +143,7 @@ class SnapManager(SoftwareManager):
return ProcessHandler(watcher).handle(SystemProcess(subproc=snap.refresh_and_stream(pkg.name, root_password)))
def prepare(self):
pass
self.categories = self.categories_downloader.get_categories()
def list_updates(self, internet_available: bool) -> List[PackageUpdate]:
pass

View File

@@ -28,6 +28,9 @@ class SnapApplication(SoftwarePackage):
self.confinement = confinement
self.has_apps_field = has_apps_field
def supports_disk_cache(self):
return self.installed
def has_history(self):
return False
@@ -49,13 +52,6 @@ class SnapApplication(SoftwarePackage):
def is_application(self) -> bool:
return not self.installed or ((self.has_apps_field is None or self.has_apps_field) and self.name.lower() not in KNOWN_RUNTIME_NAMES)
def _name_starts_with(self, words: set):
for word in words:
if self.name.startswith(word):
return True
return False
def get_disk_cache_path(self):
return super(SnapApplication, self).get_disk_cache_path() + '/installed/' + self.name
@@ -63,18 +59,16 @@ class SnapApplication(SoftwarePackage):
return {
"icon_url": self.icon_url,
'confinement': self.confinement,
'description': self.description
'description': self.description,
'categories': self.categories
}
def fill_cached_data(self, data: dict):
if data:
for base_attr in ('icon_url', 'description'):
for base_attr in ('icon_url', 'description', 'confinement', 'categories'):
if data.get(base_attr):
setattr(self, base_attr, data[base_attr])
if data.get('confinement'):
self.confinement = data['confinement']
def can_be_run(self) -> bool:
return self.installed and self.is_application()

View File

@@ -1,10 +1,14 @@
import logging
import os
import traceback
from threading import Thread
from typing import Dict, List
from bauh.api.abstract.cache import MemoryCache
from bauh.api.abstract.context import ApplicationContext
from bauh.api.abstract.controller import SoftwareManager
from bauh.api.abstract.model import PackageStatus
from bauh.api.http import HttpClient
from bauh.gems.snap import snap
from bauh.gems.snap.constants import SNAP_API_URL
from bauh.gems.snap.model import SnapApplication
@@ -68,3 +72,29 @@ class SnapAsyncDataLoader(Thread):
if self.persist:
self.manager.cache_to_disk(pkg=self.app, icon_bytes=None, only_icon=False)
class CategoriesDownloader:
URL_CATEGORIES_FILE = 'https://raw.githubusercontent.com/vinifmor/bauh-files/master/snap/categories.txt'
def __init__(self, http_client: HttpClient, logger: logging.Logger):
self.http_client = http_client
self.logger = logger
def get_categories(self) -> Dict[str, List[str]]:
self.logger.info('Downloading Snap category definitions from {}'.format(self.URL_CATEGORIES_FILE))
res = self.http_client.get(self.URL_CATEGORIES_FILE, headers={'Authorization': 'token {}'.format(os.getenv('GITHUB_TOKEN'))})
if res:
categories_map = {}
for l in res.text.split('\n'):
if l:
data = l.split('=')
categories_map[data[0]] = [c.strip() for c in data[1].split(',') if c]
self.logger.info('Loaded categories for {} Snap applications'.format(len(categories_map)))
return categories_map
else:
self.logger.info('Could not download {}'.format(self.URL_CATEGORIES_FILE))

View File

@@ -146,4 +146,21 @@ movies=películas
movie=película
library=biblioteca
books=libros
applications=aplicativos
applications=aplicativos
social=social
productivity=productividad
utilities=utilidades
photo=foto
video=vídeo
entertainment=entretenimiento
art=arte
design=diseño
reference=referencia
personalisation=personalización
education=educación
health=salud
fitness=fitness
science=ciencias
news=noticias
weather=tiempo
finance=finanzas

View File

@@ -148,4 +148,21 @@ movie=filme
media=mídia
library=biblioteca
books=livros
applications=aplicativos
applications=aplicativos
social=social
productivity=produtividade
utilities=utilidades
photo=foto
video=vídeo
entertainment=entretenimento
art=arte
design=desenho
reference=referência
personalisation=personalização
education=educação
health=saúde
fitness=fitness
science=ciências
news=notícias
weather=tempo
finance=finanças