From aef617842d30400575ac0c2914fe36dc52971989 Mon Sep 17 00:00:00 2001 From: Srevin Saju Date: Mon, 20 Apr 2020 22:41:12 +0300 Subject: [PATCH] Refactor lib.check --- appimage/entrypoint.sh | 1 + guiscrcpy/launcher.py | 72 ++++++++++++++++++++++------------------ guiscrcpy/lib/check.py | 41 +++++++++++------------ guiscrcpy/lib/config.py | 8 ++--- guiscrcpy/lib/mapper.py | 8 ++--- guiscrcpy/lib/notify.py | 8 ++--- guiscrcpy/lib/toolkit.py | 28 ++++++++-------- guiscrcpy/ux/network.py | 8 ++--- 8 files changed, 90 insertions(+), 84 deletions(-) diff --git a/appimage/entrypoint.sh b/appimage/entrypoint.sh index 78acd2ec..998bc70f 100644 --- a/appimage/entrypoint.sh +++ b/appimage/entrypoint.sh @@ -4,5 +4,6 @@ export GUISCRCPY_ADB="${APPDIR}/usr/bin/adb" export ADB="${APPDIR}/usr/bin/adb" export SCRCPY_SERVER_PATH="${APPDIR}/usr/share/scrcpy/scrcpy-server" export PATH="${APPDIR}/usr/bin:${PATH}" +export GUISCRCPY_APPIMAGE="TRUE" {{ python-executable }} -s ${APPDIR}/opt/python{{ python-version }}/bin/guiscrcpy "$@" diff --git a/guiscrcpy/launcher.py b/guiscrcpy/launcher.py index d9b20be2..657adee0 100644 --- a/guiscrcpy/launcher.py +++ b/guiscrcpy/launcher.py @@ -47,8 +47,8 @@ from PyQt5.QtWidgets import QMainWindow, QApplication from PyQt5.QtWidgets import QMessageBox -from guiscrcpy.lib.check import adb -from guiscrcpy.lib.check import scrcpy +from guiscrcpy.lib.check import Adb +from guiscrcpy.lib.check import Scrcpy from guiscrcpy.lib.config import InterfaceConfig from guiscrcpy.lib.process import is_running from guiscrcpy.lib.toolkit import UXMapper @@ -75,16 +75,16 @@ # Add precedence for guiscrcpy to check environment variables # for the paths of `adb` and `scrcpy` over the configuration files. if os.getenv('GUISCRCPY_ADB', None) is None: - adb.path = config['adb'] + Adb.path = config['adb'] else: - adb.path = os.getenv('GUISCRCPY_ADB') + Adb.path = os.getenv('GUISCRCPY_ADB') if os.getenv('GUISCRCPY_SCRCPY', None) is None: - scrcpy.path = config['scrcpy'] + Scrcpy.path = config['scrcpy'] else: - scrcpy.path = os.getenv('GUISCRCPY_SCRCPY') + Scrcpy.path = os.getenv('GUISCRCPY_SCRCPY') -scrcpy.server_path = config['scrcpy-server'] +Scrcpy.server_path = config['scrcpy-server'] # Initialize argument parser parser = argparse.ArgumentParser('guiscrcpy v{}'.format(VERSION)) @@ -147,7 +147,7 @@ args += " -t " if config['dispRO']: args += " --turn-screen-off " - scrcpy.start(scrcpy.path, args) + Scrcpy.start(Scrcpy.path, args) logging.debug("Importing modules...") @@ -221,7 +221,8 @@ def __init__(self): self.flaglineedit.setText(config['extra']) else: pass - except Exception as e: + except Exception as err: + logging.debug(f"Exception: flaglineedit.text(config[extra]) {err}") pass # set bottom instance and side instance as enabled by default @@ -251,7 +252,7 @@ def settings_mgr(self): def network_mgr(self): from guiscrcpy.ux.network import InterfaceNetwork - self.nm = InterfaceNetwork(adb.path) + self.nm = InterfaceNetwork(Adb.path) self.nm.init() self.nm.show() @@ -262,8 +263,11 @@ def mapp(self): mapper.file_check() else: logging.warning( - "guiscrcpy ~ mapper is not initialized. Initialize by running" + - "$ guiscrcpy-mapper" + "reset points by" + "$ guiscrcpy-mapper -r") + "guiscrcpy ~ mapper is not initialized. " + "Initialize by running" + + "$ guiscrcpy-mapper" + "reset points by" + + "$ guiscrcpy-mapper -r" + ) @staticmethod def launch_usb_audio(): @@ -283,7 +287,8 @@ def about(self): about_message_box.about( self.pushButton, "Info", - "Please restart guiscrcpy to reset the settings. guiscrcpy will now exit", + "Please restart guiscrcpy to reset the settings. " + "guiscrcpy will now exit", ) about_message_box.addButton("OK", about_message_box.hide()) about_message_box.show() @@ -297,7 +302,8 @@ def reset(self): message_box.about( self.pushButton, "Info", - "Please restart guiscrcpy to reset the settings. guiscrcpy will now exit", + "Please restart guiscrcpy to reset the settings. " + "guiscrcpy will now exit", ) message_box.addButton("OK", self.quit_window()) message_box.show() @@ -334,7 +340,7 @@ def __dial_change_cb(self): pass def __refresh_devices_combo_box_cb(self): - devices_list = adb.devices(adb.path) + devices_list = Adb.devices(Adb.path) if len(devices_list) == 0: self.private_message_box_adb.setText("DEVICE IS NOT CONNECTED") @@ -502,7 +508,7 @@ def start_act(self): f"connected; (color id " f"matches toolkit color)") - scrcpy.start(scrcpy.path, arguments_scrcpy) + Scrcpy.start(Scrcpy.path, arguments_scrcpy) final_time = time.time() eta = final_time - initial_time print("scrcpy launched in {:.2f}s".format(eta)) @@ -542,43 +548,43 @@ def bootstrap0(): app.processEvents() cfedited = False - if (adb.path is None) or (not os.path.exists(adb.path)): - adb.path = openFileNameDialog(None, 'adb') + if (Adb.path is None) or (not os.path.exists(Adb.path)): + Adb.path = openFileNameDialog(None, 'adb') cfedited = True - config['adb'] = adb.path + config['adb'] = Adb.path - if (scrcpy.path is None) or (not os.path.exists(scrcpy.path)): - scrcpy.path = openFileNameDialog(None, 'scrcpy') + if (Scrcpy.path is None) or (not os.path.exists(Scrcpy.path)): + Scrcpy.path = openFileNameDialog(None, 'scrcpy') cfedited = True - config['scrcpy'] = scrcpy.path + config['scrcpy'] = Scrcpy.path # on windows, users are likely not to add the scrcpy-server to the # SCRCPY_SERVER_PATH scrcpy_server_path_env = os.getenv('SCRCPY_SERVER_PATH', None) if scrcpy_server_path_env: if os.path.exists(scrcpy_server_path_env): - config['scrcpy-server'] = scrcpy.server_path + config['scrcpy-server'] = Scrcpy.server_path else: - scrcpy.server_path = openFileNameDialog(None, 'scrcpy-server') + Scrcpy.server_path = openFileNameDialog(None, 'scrcpy-server') cfedited = True - config['scrcpy-server'] = scrcpy.server_path - os.environ['SCRCPY_SERVER_PATH'] = scrcpy.server_path - elif ((scrcpy.server_path is None) or - (not os.path.exists(scrcpy.server_path))) and ( + config['scrcpy-server'] = Scrcpy.server_path + os.environ['SCRCPY_SERVER_PATH'] = Scrcpy.server_path + elif ((Scrcpy.server_path is None) or + (not os.path.exists(Scrcpy.server_path))) and ( platform.System().system() == 'Windows' ): - scrcpy.server_path = openFileNameDialog(None, 'scrcpy-server') + Scrcpy.server_path = openFileNameDialog(None, 'scrcpy-server') cfedited = True - config['scrcpy-server'] = scrcpy.server_path - os.environ['SCRCPY_SERVER_PATH'] = scrcpy.server_path + config['scrcpy-server'] = Scrcpy.server_path + os.environ['SCRCPY_SERVER_PATH'] = Scrcpy.server_path elif platform.System().system() == "Windows": - os.environ['SCRCPY_SERVER_PATH'] = scrcpy.server_path + os.environ['SCRCPY_SERVER_PATH'] = Scrcpy.server_path if cfedited: cfgmgr.update_config(config) cfgmgr.write_file() - adb.devices(adb.path) + Adb.devices(Adb.path) guiscrcpy = InterfaceGuiscrcpy() guiscrcpy.show() app.processEvents() diff --git a/guiscrcpy/lib/check.py b/guiscrcpy/lib/check.py index 6fadd1f5..ec74373f 100644 --- a/guiscrcpy/lib/check.py +++ b/guiscrcpy/lib/check.py @@ -26,11 +26,7 @@ environment = System() -def check(binary): - pass - - -class scrcpy: +class Scrcpy: def __init__(self): pass @@ -59,7 +55,7 @@ def check(): environment.paths())) -class adb: +class Adb: def __init__(self): pass @@ -76,13 +72,13 @@ def check(): @staticmethod def shell_input(path, command, device_id=None): if device_id: - shellx = Popen( + Popen( _("{} -s {} shell input {}".format(path, device_id, command)), stdout=PIPE, stderr=PIPE, ) else: - shellx = Popen( + Popen( _("{} shell input {}".format(path, command)), stdout=PIPE, stderr=PIPE, @@ -91,52 +87,55 @@ def shell_input(path, command, device_id=None): @staticmethod def get_dimensions(path, device_id=None): if device_id: - shellx = Popen( + shell_adb = Popen( _("{} -s {} shell wm size".format(path, device_id)), stdout=PIPE, stderr=PIPE) else: - shellx = Popen(_("{} shell wm size".format(path)), + shell_adb = Popen(_("{} shell wm size".format(path)), stdout=PIPE, stderr=PIPE) - raw_dimensions = shellx.stdout.read().decode().strip('\n') + raw_dimensions = shell_adb.stdout.read().decode().strip('\n') for i in ['Override size', 'Physical size']: if i in raw_dimensions: out = raw_dimensions[raw_dimensions.find(i):] out_decoded = out.split(':')[1].strip() - dimValues = out_decoded.split('x') - return dimValues + dimension_values = out_decoded.split('x') + return dimension_values else: logging.error( - "AndroidDeviceError: adb shell wm size did not return 'Physical Size' or 'Override Size'" + "AndroidDeviceError: adb shell wm size did not return " + "'Physical Size' or 'Override Size'" ) return False @staticmethod def shell(path, command, device_id=None): if device_id: - shellx = Popen(_("{} -s {} shell {}".format(path, + Popen(_("{} -s {} shell {}".format(path, device_id, command)), stdout=PIPE, stderr=PIPE) else: - shellx = Popen(_("{} shell {}".format(path, command)), + Popen(_("{} shell {}".format(path, command)), stdout=PIPE, stderr=PIPE) return True @staticmethod def command(path, command, device_id=None): if device_id: - shellx = Popen( + adb_shell_output = Popen( _("{} -s {} {}".format(path, device_id, command)), stdout=PIPE, stderr=PIPE) else: - shellx = Popen(_("{} {}".format(path, command)), - stdout=PIPE, stderr=PIPE) - return shellx + adb_shell_output = Popen(_("{} {}".format(path, command)), + stdout=PIPE, stderr=PIPE) + return adb_shell_output @staticmethod def devices(increment=''): if increment is None: raise FileNotFoundError( - "guiscrcpy couldn't find adb. Please specify path to adb in configuration file") + "guiscrcpy couldn't find adb. " + "Please specify path to adb in configuration file" + ) proc = Popen(_(increment + " devices"), stdout=PIPE) output = [[y.strip() for y in x.split('\t')] for x in decode_process(proc)[1:]][:-1] diff --git a/guiscrcpy/lib/config.py b/guiscrcpy/lib/config.py index 730a438a..67df0483 100644 --- a/guiscrcpy/lib/config.py +++ b/guiscrcpy/lib/config.py @@ -20,8 +20,8 @@ import json import os -from guiscrcpy.lib.check import adb -from guiscrcpy.lib.check import scrcpy +from guiscrcpy.lib.check import Adb +from guiscrcpy.lib.check import Scrcpy from guiscrcpy.platform import platform @@ -51,11 +51,11 @@ def validate(self): # check scrcpy and adb are not None, else replace it with original # values if self.config['adb'] is None: - adb_path = adb.check() + adb_path = Adb.check() if adb_path: self.config['adb'] = adb_path if self.config['scrcpy'] is None: - scrcpy_path = scrcpy.check() + scrcpy_path = Scrcpy.check() if scrcpy_path: self.config['scrcpy'] = scrcpy_path if (self.config['scrcpy-server'] is not None) and ( diff --git a/guiscrcpy/lib/mapper.py b/guiscrcpy/lib/mapper.py index 454aae7e..6c7a6e9c 100644 --- a/guiscrcpy/lib/mapper.py +++ b/guiscrcpy/lib/mapper.py @@ -33,7 +33,7 @@ from PyQt5.QtGui import QPixmap from pynput import keyboard -from guiscrcpy.lib.check import adb +from guiscrcpy.lib.check import Adb from guiscrcpy.lib.config import InterfaceConfig get1 = False @@ -42,7 +42,7 @@ cfgmgr = InterfaceConfig() config = cfgmgr.get_config() -adb.path = config['adb'] +Adb.path = config['adb'] jsong = 'guiscrcpy.mapper.json' @@ -53,7 +53,7 @@ print('With USB debugging turned on.') print("+++++++++++++++++++++++++++++++++++++++") print("Waiting for device") -adb.command(adb.path, 'wait-for-any-device') +Adb.command(Adb.path, 'wait-for-any-device') print("Device : OK!") cfgpath = cfgmgr.cfgpath @@ -65,7 +65,7 @@ help="Remove prefernces") args = parser.parse_args() -dimensions = adb.get_dimensions(adb.path) +dimensions = Adb.get_dimensions(Adb.path) class MapperUI(QtWidgets.QWidget): diff --git a/guiscrcpy/lib/notify.py b/guiscrcpy/lib/notify.py index 87dda459..66c5f3c0 100644 --- a/guiscrcpy/lib/notify.py +++ b/guiscrcpy/lib/notify.py @@ -25,7 +25,7 @@ import pystray from PIL import Image, ImageDraw -from guiscrcpy.lib.check import adb +from guiscrcpy.lib.check import Adb logging.warning("Launching notification auditor") @@ -39,7 +39,7 @@ def __init__(self): def callback(self, icon): notif = Popen( - adb.path + + Adb.path + "adb shell dumpsys notification | grep ticker | cut -d= -f2", stdout=PIPE, shell=True, @@ -54,7 +54,7 @@ def callback(self, icon): "Notif Auditor is experimental on Windows. If you wish to help out on this issue. Open a PR on github" ) notif = Popen( - adb.path + + Adb.path + "adb shell dumpsys notification | findstr ticker ", stdout=PIPE, shell=True, @@ -62,7 +62,7 @@ def callback(self, icon): else: "Notif Auditor is experimental on Linux. If you wish to help out on this issue. Open a PR on github" notif = Popen( - adb.path + + Adb.path + "adb shell dumpsys notification | grep ticker | cut -d= -f2", stdout=PIPE, shell=True, diff --git a/guiscrcpy/lib/toolkit.py b/guiscrcpy/lib/toolkit.py index 53ecdfa1..84825dbf 100644 --- a/guiscrcpy/lib/toolkit.py +++ b/guiscrcpy/lib/toolkit.py @@ -20,7 +20,7 @@ import logging import os -from guiscrcpy.lib.check import adb +from guiscrcpy.lib.check import Adb try: import pyautogui as auto @@ -36,8 +36,8 @@ def __init__(self, device_id=None): logging.debug("Launching UX Mapper") self.has_modules = getWindowsWithTitle and auto logging.debug("Calculating Screen Size") - self.android_dimensions = adb.get_dimensions( - adb.path, device_id=device_id) + self.android_dimensions = Adb.get_dimensions( + Adb.path, device_id=device_id) self.deviceId = device_id # each device connected is uniquely identified by the tools by @@ -49,13 +49,13 @@ def get_sha(self): return self.sha def do_swipe(self, x1=10, y1=10, x2=10, y2=10): - adb.shell_input(adb.path, "swipe {} {} {} {}".format( + Adb.shell_input(Adb.path, "swipe {} {} {} {}".format( x1, y1, x2, y2), device_id=self.deviceId) return True def do_keyevent(self, key): - adb.shell_input(adb.path, "keyevent {}".format(key), - device_id=self.deviceId) + Adb.shell_input(Adb.path, "keyevent {}".format(key), + device_id=self.deviceId) return True def copy_devpc(self): @@ -97,17 +97,17 @@ def key_switch(self): def reorientP(self): logging.debug("Passing REORIENT [POTRAIT]") - adb.shell(adb.path, 'settings put system accelerometer_rotation 0', - device_id=self.deviceId) - adb.shell(adb.path, "settings put system rotation 1", - device_id=self.deviceId) + Adb.shell(Adb.path, 'settings put system accelerometer_rotation 0', + device_id=self.deviceId) + Adb.shell(Adb.path, "settings put system rotation 1", + device_id=self.deviceId) def reorientL(self): logging.debug("Passing REORIENT [LANDSCAPE]") - adb.shell(adb.path, 'settings put system accelerometer_rotation 0', - device_id=self.deviceId) - adb.shell(adb.path, "settings put system rotation 1", - device_id=self.deviceId) + Adb.shell(Adb.path, 'settings put system accelerometer_rotation 0', + device_id=self.deviceId) + Adb.shell(Adb.path, "settings put system rotation 1", + device_id=self.deviceId) def expand_notifications(self): logging.debug("Passing NOTIF EXPAND") diff --git a/guiscrcpy/ux/network.py b/guiscrcpy/ux/network.py index e59fe510..ccc79f9b 100644 --- a/guiscrcpy/ux/network.py +++ b/guiscrcpy/ux/network.py @@ -21,7 +21,7 @@ from PyQt5.QtWidgets import QMainWindow -from guiscrcpy.lib.check import adb +from guiscrcpy.lib.check import Adb from guiscrcpy.network.network import NetworkManager from guiscrcpy.platform.platform import System from guiscrcpy.ui.network import Ui_NetworkUI @@ -39,7 +39,7 @@ def __init__(self, adb_path=None): Ui_NetworkUI.__init__(self) self.os = System() self.setupUi(self) - adb.path = adb_path + Adb.path = adb_path self.nm = NetworkManager() def init(self): @@ -59,7 +59,7 @@ def init(self): self.tcpip.pressed.connect(self.tcpip_launch) def tcpip_launch(self): - adb.command(adb.path, 'tcpip') + Adb.command(Adb.path, 'tcpip') def connect(self): try: @@ -83,7 +83,7 @@ def connect(self): "Please enter an IP address in the text box. / Click refresh") return - sp = adb.command(adb.path, 'connect {}:5555'.format(ip)) + sp = Adb.command(Adb.path, 'connect {}:5555'.format(ip)) count = 0 while True: count += 1