Skip to content

Commit

Permalink
Standardize logging in browser.py
Browse files Browse the repository at this point in the history
  • Loading branch information
TrainDoctor committed Aug 8, 2022
1 parent 56d4784 commit 7fc33f8
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions backend/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from helpers import get_ssl_context, get_user, get_user_group
from injector import get_tab, inject_to_tab

logger = getLogger("Browser")

class PluginInstallContext:
def __init__(self, artifact, name, version, hash) -> None:
self.artifact = artifact
Expand All @@ -27,7 +29,6 @@ def __init__(self, artifact, name, version, hash) -> None:

class PluginBrowser:
def __init__(self, plugin_path, server_instance, plugins) -> None:
self.log = getLogger("browser")
self.plugin_path = plugin_path
self.plugins = plugins
self.install_requests = {}
Expand Down Expand Up @@ -59,7 +60,7 @@ def find_plugin_folder(self, name):
if plugin['name'] == name:
return path.join(self.plugin_path, folder)
except:
self.log.debug(f"skipping {folder}")
logger.debug(f"skipping {folder}")

async def uninstall_plugin(self, name):
tab = await get_tab("SP")
Expand All @@ -68,34 +69,34 @@ async def uninstall_plugin(self, name):
if type(name) != str:
data = await name.post()
name = data.get("name", "undefined")
self.log.info("uninstalling " + name)
self.log.info(" at dir " + self.find_plugin_folder(name))
logger.info("uninstalling " + name)
logger.info(" at dir " + self.find_plugin_folder(name))
await tab.evaluate_js(f"DeckyPluginLoader.unloadPlugin('{name}')")
if self.plugins[name]:
self.plugins[name].stop()
self.plugins.pop(name, None)
rmtree(self.find_plugin_folder(name))
except FileNotFoundError:
self.log.warning(f"Plugin {name} not installed, skipping uninstallation")
logger.warning(f"Plugin {name} not installed, skipping uninstallation")

return web.Response(text="Requested plugin uninstall")

async def _install(self, artifact, name, version, hash):
try:
await self.uninstall_plugin(name)
except:
self.log.error(f"Plugin {name} not installed, skipping uninstallation")
self.log.info(f"Installing {name} (Version: {version})")
logger.error(f"Plugin {name} not installed, skipping uninstallation")
logger.info(f"Installing {name} (Version: {version})")
async with ClientSession() as client:
self.log.debug(f"Fetching {artifact}")
logger.debug(f"Fetching {artifact}")
res = await client.get(artifact, ssl=get_ssl_context())
if res.status == 200:
self.log.debug("Got 200. Reading...")
logger.debug("Got 200. Reading...")
data = await res.read()
self.log.debug(f"Read {len(data)} bytes")
logger.debug(f"Read {len(data)} bytes")
res_zip = BytesIO(data)
with ProcessPoolExecutor() as executor:
self.log.debug("Unzipping...")
logger.debug("Unzipping...")
ret = await get_event_loop().run_in_executor(
executor,
self._unzip_to_plugin_dir,
Expand All @@ -104,12 +105,12 @@ async def _install(self, artifact, name, version, hash):
hash
)
if ret:
self.log.info(f"Installed {name} (Version: {version})")
logger.info(f"Installed {name} (Version: {version})")
await inject_to_tab("SP", "window.syncDeckyPlugins()")
else:
self.log.fatal(f"SHA-256 Mismatch!!!! {name} (Version: {version})")
logger.fatal(f"SHA-256 Mismatch!!!! {name} (Version: {version})")
else:
self.log.fatal(f"Could not fetch from URL. {await res.text()}")
logger.fatal(f"Could not fetch from URL. {await res.text()}")

async def install_plugin(self, request):
data = await request.post()
Expand Down

0 comments on commit 7fc33f8

Please sign in to comment.