mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-07 01:14:15 +02:00
0.9.0
This commit is contained in:
@@ -14,22 +14,23 @@ from bauh.api.http import HttpClient
|
||||
class CategoriesDownloader(Thread):
|
||||
|
||||
def __init__(self, id_: str, http_client: HttpClient, logger: logging.Logger, manager: SoftwareManager,
|
||||
disk_cache: bool, url_categories_file: str, disk_cache_dir: str, categories_path: str):
|
||||
url_categories_file: str, disk_cache_dir: str, categories_path: str, before=None, after=None):
|
||||
super(CategoriesDownloader, self).__init__(daemon=True)
|
||||
self.id_ = id_
|
||||
self.http_client = http_client
|
||||
self.logger = logger
|
||||
self.manager = manager
|
||||
self.disk_cache = disk_cache
|
||||
self.url_categories_file = url_categories_file
|
||||
self.disk_cache_dir = disk_cache_dir
|
||||
self.categories_path = categories_path
|
||||
self.before = before
|
||||
self.after = after
|
||||
|
||||
def _msg(self, msg: str):
|
||||
return '{}({}): {}'.format(self.__class__.__name__, self.id_, msg)
|
||||
|
||||
def _read_categories_from_disk(self) -> Dict[str, List[str]]:
|
||||
if self.disk_cache and os.path.exists(self.categories_path):
|
||||
if os.path.exists(self.categories_path):
|
||||
self.logger.info(self._msg("Reading cached categories from the disk"))
|
||||
|
||||
with open(self.categories_path) as f:
|
||||
@@ -73,7 +74,7 @@ class CategoriesDownloader(Thread):
|
||||
categories = self._map_categories(res.text)
|
||||
self.logger.info(self._msg('Loaded categories for {} applications'.format(len(categories))))
|
||||
|
||||
if self.disk_cache and categories:
|
||||
if categories:
|
||||
Thread(target=self._cache_categories_to_disk, args=(res.text,), daemon=True).start()
|
||||
|
||||
return categories
|
||||
@@ -97,6 +98,9 @@ class CategoriesDownloader(Thread):
|
||||
self._set_categories(self.download_categories())
|
||||
|
||||
def run(self):
|
||||
if self.before:
|
||||
self.before()
|
||||
|
||||
cached = self._read_categories_from_disk()
|
||||
|
||||
if cached:
|
||||
@@ -105,4 +109,7 @@ class CategoriesDownloader(Thread):
|
||||
else:
|
||||
self._download_and_set()
|
||||
|
||||
if self.after:
|
||||
self.after()
|
||||
|
||||
self.logger.info(self._msg('Finished'))
|
||||
|
||||
@@ -9,3 +9,7 @@ def strip_html(string: str):
|
||||
|
||||
def bold(text: str) -> str:
|
||||
return '<span style="font-weight: bold">{}</span>'.format(text)
|
||||
|
||||
|
||||
def link(url: str) -> str:
|
||||
return '<a href="{}">{}</a>'.format(url, url)
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
import logging
|
||||
|
||||
import requests
|
||||
|
||||
from bauh.api.http import HttpClient
|
||||
import subprocess
|
||||
import traceback
|
||||
from subprocess import Popen
|
||||
|
||||
|
||||
def is_available(client: HttpClient, logger: logging.Logger) -> bool:
|
||||
def is_available() -> bool:
|
||||
try:
|
||||
client.exists('https://google.com')
|
||||
return True
|
||||
except (requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout):
|
||||
if logger:
|
||||
logger.warning('Internet connection seems to be off')
|
||||
res = Popen(['ping', '-q', '-w1', '-c1', 'google.com'], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
|
||||
res.wait()
|
||||
return res.returncode == 0
|
||||
except:
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ class ProcessHandler:
|
||||
if self.watcher:
|
||||
self.watcher.print(msg)
|
||||
|
||||
def handle(self, process: SystemProcess, error_output: StringIO = None) -> bool:
|
||||
def handle(self, process: SystemProcess, error_output: StringIO = None, output_handler=None) -> bool:
|
||||
self._notify_watcher(' '.join(process.subproc.args) + '\n')
|
||||
|
||||
already_succeeded = False
|
||||
@@ -117,6 +117,9 @@ class ProcessHandler:
|
||||
if line:
|
||||
self._notify_watcher(line)
|
||||
|
||||
if output_handler:
|
||||
output_handler(line)
|
||||
|
||||
if process.success_phrases and [p in line for p in process.success_phrases]:
|
||||
already_succeeded = True
|
||||
|
||||
@@ -132,6 +135,9 @@ class ProcessHandler:
|
||||
if line:
|
||||
self._notify_watcher(line)
|
||||
|
||||
if output_handler:
|
||||
output_handler(line)
|
||||
|
||||
if error_output is not None:
|
||||
error_output.write(line)
|
||||
|
||||
@@ -241,6 +247,10 @@ def get_dir_size(start_path='.'):
|
||||
|
||||
def get_human_size_str(size) -> str:
|
||||
int_size = int(size)
|
||||
|
||||
if int_size == 0:
|
||||
return '0'
|
||||
|
||||
for m in SIZE_MULTIPLIERS:
|
||||
size_str = str(int_size * m[0])
|
||||
|
||||
|
||||
@@ -9,3 +9,20 @@ def deep_update(source: dict, overrides: dict):
|
||||
else:
|
||||
source[key] = overrides[key]
|
||||
return source
|
||||
|
||||
|
||||
def size_to_byte(size: float, unit: str) -> int:
|
||||
lower_unit = unit.lower()
|
||||
|
||||
if lower_unit[0] == 'b':
|
||||
final_size = size
|
||||
elif lower_unit[0] == 'k':
|
||||
final_size = size * 1000
|
||||
elif lower_unit[0] == 'm':
|
||||
final_size = size * 1000000
|
||||
elif lower_unit[0] == 't':
|
||||
final_size = size * 1000000000000
|
||||
else:
|
||||
final_size = size * 1000000000000000
|
||||
|
||||
return int(final_size)
|
||||
|
||||
Reference in New Issue
Block a user