This commit is contained in:
Vinícius Moreira
2020-04-13 11:49:28 -03:00
parent 83aab1ff55
commit 01a60ea686
165 changed files with 12778 additions and 6171 deletions

View File

@@ -97,7 +97,7 @@ class CacheCleaner(Thread):
class DefaultMemoryCacheFactory(MemoryCacheFactory):
def __init__(self, expiration_time: int, cleaner: CacheCleaner):
def __init__(self, expiration_time: int, cleaner: CacheCleaner = None):
"""
:param expiration_time: default expiration time for all instantiated caches
:param cleaner
@@ -108,5 +108,8 @@ class DefaultMemoryCacheFactory(MemoryCacheFactory):
def new(self, expiration: int = None) -> MemoryCache:
instance = DefaultMemoryCache(expiration if expiration is not None else self.expiration_time)
self.cleaner.register(instance)
if self.cleaner:
self.cleaner.register(instance)
return instance

View File

@@ -14,13 +14,12 @@ from bauh.api.abstract.model import SoftwarePackage
class AsyncDiskCacheLoader(Thread, DiskCacheLoader):
def __init__(self, enabled: bool, cache_map: Dict[Type[SoftwarePackage], MemoryCache], logger: logging.Logger):
def __init__(self, cache_map: Dict[Type[SoftwarePackage], MemoryCache], logger: logging.Logger):
super(AsyncDiskCacheLoader, self).__init__(daemon=True)
self.pkgs = []
self._work = True
self.lock = Lock()
self.cache_map = cache_map
self.enabled = enabled
self.logger = logger
self.processed = 0
@@ -30,64 +29,62 @@ class AsyncDiskCacheLoader(Thread, DiskCacheLoader):
:param pkg:
:return:
"""
if self.enabled and pkg and pkg.supports_disk_cache():
if pkg and pkg.supports_disk_cache():
self.pkgs.append(pkg)
def stop_working(self):
self._work = False
def run(self):
if self.enabled:
last = 0
last = 0
while True:
time.sleep(0.00001)
if len(self.pkgs) > self.processed:
pkg = self.pkgs[last]
while True:
time.sleep(0.00001)
if len(self.pkgs) > self.processed:
pkg = self.pkgs[last]
self._fill_cached_data(pkg)
self.processed += 1
last += 1
elif not self._work:
break
self._fill_cached_data(pkg)
self.processed += 1
last += 1
elif not self._work:
break
def _fill_cached_data(self, pkg: SoftwarePackage) -> bool:
if self.enabled:
if os.path.exists(pkg.get_disk_data_path()):
disk_path = pkg.get_disk_data_path()
ext = disk_path.split('.')[-1]
if os.path.exists(pkg.get_disk_data_path()):
disk_path = pkg.get_disk_data_path()
ext = disk_path.split('.')[-1]
with open(disk_path) as f:
if ext == 'json':
cached_data = json.loads(f.read())
elif ext in {'yml', 'yaml'}:
cached_data = yaml.load(f.read())
else:
raise Exception('The cached data file {} has an unsupported format'.format(disk_path))
with open(disk_path) as f:
if ext == 'json':
cached_data = json.loads(f.read())
elif ext in {'yml', 'yaml'}:
cached_data = yaml.load(f.read())
else:
raise Exception('The cached data file {} has an unsupported format'.format(disk_path))
if cached_data:
pkg.fill_cached_data(cached_data)
cache = self.cache_map.get(pkg.__class__)
if cached_data:
pkg.fill_cached_data(cached_data)
cache = self.cache_map.get(pkg.__class__)
if cache:
cache.add_non_existing(pkg.id, cached_data)
if cache:
cache.add_non_existing(str(pkg.id), cached_data)
return True
return True
return False
class DefaultDiskCacheLoaderFactory(DiskCacheLoaderFactory):
def __init__(self, disk_cache_enabled: bool, logger: logging.Logger):
def __init__(self, logger: logging.Logger):
super(DefaultDiskCacheLoaderFactory, self).__init__()
self.disk_cache_enabled = disk_cache_enabled
self.logger = logger
self.cache_map = {}
def map(self, pkg_type: Type[SoftwarePackage], cache: MemoryCache):
if pkg_type:
if pkg_type not in self.cache_map:
self.cache_map[pkg_type] = cache
self.cache_map[pkg_type] = cache
def new(self) -> AsyncDiskCacheLoader:
return AsyncDiskCacheLoader(enabled=self.disk_cache_enabled, cache_map=self.cache_map, logger=self.logger)
return AsyncDiskCacheLoader(cache_map=self.cache_map, logger=self.logger)

View File

@@ -1,10 +1,8 @@
import glob
import locale
import traceback
import os
from typing import Tuple, Set
from colorama import Fore
from bauh.view.util import resource
@@ -43,7 +41,7 @@ class I18n(dict):
def get_available_keys() -> Set[str]:
locale_dir = resource.get_path('locale')
return {file.split('/')[-1] for file in glob.glob(locale_dir + '/*')}
return {file.split('/')[-1] for file in glob.glob(locale_dir + '/*') if os.path.isfile(file)}
def get_locale_keys(key: str = None, locale_dir: str = resource.get_path('locale')) -> Tuple[str, dict]:

View File

@@ -25,25 +25,22 @@ def notify_user(msg: str, icon_path: str = None):
os.system("notify-send -a {} {} '{}'".format(__app_name__, "-i {}".format(icon_id) if icon_id else '', msg))
def get_default_icon() -> Tuple[str, QIcon]:
system_icon = QIcon.fromTheme(__app_name__)
if not system_icon.isNull():
return system_icon.name(), system_icon
else:
path = resource.get_path('img/logo.svg')
return path, QIcon(path)
def get_default_icon(system: bool = True) -> Tuple[str, QIcon]:
if system:
system_icon = QIcon.fromTheme(__app_name__)
if not system_icon.isNull():
return system_icon.name(), system_icon
path = resource.get_path('img/logo.svg')
return path, QIcon(path)
def restart_app(show_panel: bool):
def restart_app():
"""
:param show_panel: if the panel should be displayed after the app restart
:return:
"""
restart_cmd = [sys.executable, *sys.argv]
if show_panel:
restart_cmd.append('--show-panel')
subprocess.Popen(restart_cmd)
QCoreApplication.exit()