From 18ad529b168b85ce9a2df8bcd8ec8c24fbec4b62 Mon Sep 17 00:00:00 2001 From: Leo Araya Date: Thu, 7 Nov 2024 13:56:45 -0300 Subject: [PATCH] fix(rpp.browser): first lint phase --- rpp/browser.py | 138 ++++++++++++++++--------------------------------- 1 file changed, 45 insertions(+), 93 deletions(-) diff --git a/rpp/browser.py b/rpp/browser.py index 744f59b..23b175d 100644 --- a/rpp/browser.py +++ b/rpp/browser.py @@ -1,3 +1,8 @@ +""" +Browser module for managing browser instances. +Based on pybrinf (https://github.com/manucabral/pybrinf) +""" + import os import winreg import subprocess @@ -7,7 +12,7 @@ class Browser: """ - Browser class for managing the browser instances. + Browser class for managing browser instances. """ def __init__(self): @@ -19,16 +24,28 @@ def __init__(self): self.path = self.get_path() self.name: str = self.get_name() self.process: str = self.path.split("\\")[-1] - self._microsoft_store: bool = self.check_microsoft_store_app() self.log.info("Initialized.") self.executor = subprocess.run + self._microsoft_store = self.check_microsoft_store_app() @property def microsoft_store(self) -> bool: + """ + Get the microsoft_store property. + + Returns: + bool: True if the browser is a Microsoft Store app, False otherwise. + """ return self._microsoft_store @microsoft_store.setter def microsoft_store(self, value: bool) -> None: + """ + Set the microsoft_store property. + + Args: + value (bool): The new value for the microsoft_store property. + """ self._microsoft_store = value def get_progid(self) -> str: @@ -86,13 +103,10 @@ def check_microsoft_store_app(self) -> bool: Returns: bool: True if the browser is a Microsoft Store app, False otherwise. """ - command = [ - "powershell", - "-Command", - "Get-StartApps | Where-Object { $_.Name -like '{}*' }".format( - self.process.replace(".exe", "") - ), - ] + command = ( + 'powershell -Command "Get-StartApps | ' + f'Where-Object {{ $_.Name -like \'{self.process}*\' }}"' + ) try: result = subprocess.run( command, @@ -106,39 +120,6 @@ def check_microsoft_store_app(self) -> bool: except subprocess.CalledProcessError: return False - def get_app_id(self) -> str: - """ - Get the application ID for the Microsoft Store app. - - Returns: - str: The application ID. - """ - command = [ - "powershell", - "-Command", - "Get-StartApps | Where-Object { $_.Name -like '{}*' }".format( - self.process.replace(".exe", "") - ), - ] - try: - result = subprocess.run( - command, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - shell=True, - text=True, - check=True, - ) - app_info = result.stdout.strip() - if app_info: - # Extract the AppID from the output - app_id = app_info.split()[-1] - return app_id - else: - raise RuntimeError("AppID not found.") - except subprocess.CalledProcessError: - raise RuntimeError("Failed to get AppID.") - def kill(self, admin: bool = False) -> None: """ Kill the browser process using PowerShell with admin privileges if needed. @@ -169,7 +150,6 @@ def as_admin(): self.kill(admin=True) try: - # pylint: disable=consider-using-with process = subprocess.Popen( command, stdout=subprocess.PIPE, @@ -178,7 +158,6 @@ def as_admin(): ) self.log.info("Killed with PID: %s (%s)", process.pid, self.process) if self.running() and self.process == "Arc.exe": - # Arc is a special case as_admin() except subprocess.CalledProcessError as exc: if not self.running(): @@ -224,53 +203,27 @@ def start(self, remote_port: int = 9222, admin: bool = False) -> None: if self.running(): self.log.warning("Refusing to start browser, already running.") return - - if self.microsoft_store: - app_id = self.get_app_id() - command = ( - [ - "powershell", - "-Command", - "Start-Process", - "powershell", - "-ArgumentList", - f'"explorer.exe shell:appsFolder\\{app_id} --remote-debugging-port={remote_port} ' - f"--remote-allow-origins=http://127.0.0.1:{remote_port} " - f'--remote-allow-origins=http://localhost:{remote_port}"', - "-Verb", - "RunAs", - ] - if admin - else [ - "explorer.exe", - f"shell:appsFolder\\{app_id}", - f"--remote-debugging-port={remote_port}", - f"--remote-allow-origins=http://127.0.0.1:{remote_port}", - f"--remote-allow-origins=http://localhost:{remote_port}", - ] - ) - else: - command = ( - [ - "powershell", - "-Command", - "Start-Process", - f"'{self.path}'", - "-ArgumentList", - f"'--remote-debugging-port={remote_port}'," - f"'--remote-allow-origins=http://127.0.0.1:{remote_port}'," - f"'--remote-allow-origins=http://localhost:{remote_port}'", - "-Verb", - "RunAs", - ] - if admin - else [ - self.path, - f"--remote-debugging-port={remote_port}", - f"--remote-allow-origins=http://127.0.0.1:{remote_port}", - f"--remote-allow-origins=http://localhost:{remote_port}", - ] - ) + command = ( + [ + "powershell", + "-Command", + "Start-Process", + f"'{self.path}'", + "-ArgumentList", + f"'--remote-debugging-port={remote_port}'," + f"'--remote-allow-origins=http://127.0.0.1:{remote_port}'," + f"'--remote-allow-origins=http://localhost:{remote_port}'", + "-Verb", + "RunAs", + ] + if admin + else [ + self.path, + f"--remote-debugging-port={remote_port}", + f"--remote-allow-origins=http://127.0.0.1:{remote_port}", + f"--remote-allow-origins=http://localhost:{remote_port}", + ] + ) def as_admin(): self.log.warning( @@ -280,7 +233,6 @@ def as_admin(): self.start(remote_port, True) try: - # pylint: disable=consider-using-with process = subprocess.Popen( command, stdout=subprocess.PIPE, @@ -289,7 +241,7 @@ def as_admin(): text=True, ) self.log.info("Started with PID: %s (%s)", process.pid, self.process) - special_cases = ["Arc.exe"] # This is an special case + special_cases = ["Arc.exe"] if self.process in special_cases: _, stderr = process.communicate() result = stderr.strip()