From d23c0599477b4df0cecbb3d8910ea85b120a9bde Mon Sep 17 00:00:00 2001 From: Colton Loftus <70598503+C-Loftus@users.noreply.github.com> Date: Tue, 27 Feb 2024 15:19:15 -0500 Subject: [PATCH] cleanup code --- core/core-agnostic.py | 18 +++++++++----- core/screenreader_ipc/ipc_client.py | 5 ++-- nvda/nvda.py | 7 ++---- orca/orca.py | 38 ++++++++++++++--------------- 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/core/core-agnostic.py b/core/core-agnostic.py index a43137c..96643f9 100644 --- a/core/core-agnostic.py +++ b/core/core-agnostic.py @@ -49,6 +49,7 @@ def cancel_current_speaker(): def braille(text: str): """Output braille with the screenreader""" + raise NotImplementedError def toggle_braille(): """Toggles braille on and off""" @@ -64,7 +65,7 @@ def echo_dictation_enabled() -> bool: # Catch potential race condition where settings haven't been loaded at startup try: return ctx.settings["user.echo_dictation"] - except: + except Exception: return False def braille_enabled() -> bool: @@ -72,7 +73,7 @@ def braille_enabled() -> bool: # Catch potential race condition where settings haven't been loaded at startup try: return ctx.settings["user.echo_braille"] - except: + except Exception: return False def echo_context_enabled() -> bool: @@ -80,10 +81,10 @@ def echo_context_enabled() -> bool: # Catch potential race condition where settings haven't been loaded at startup try: return ctx.settings["user.echo_context"] - except: + except Exception: return False - def toggle_echo(): + def toggle_echo() -> None: """Toggles echo dictation on and off""" if actions.user.echo_dictation_enabled(): @@ -93,7 +94,7 @@ def toggle_echo(): actions.user.tts("echo enabled") ctx.settings["user.echo_dictation"] = True - def toggle_echo_context(): + def toggle_echo_context() -> None: """Toggles echo context on and off""" if actions.user.echo_context_enabled(): @@ -103,7 +104,7 @@ def toggle_echo_context(): actions.user.tts("echo context enabled") ctx.settings["user.echo_context"] = True - def toggle_echo_all(): + def toggle_echo_all() -> None: """Toggles echo dictation and echo context on and off""" dictation, context = ( @@ -129,19 +130,24 @@ def tts(text: str, interrupt: bool = True): def espeak(text: str): """text to speech with espeak""" actions.user.tts("Espeak Not Supported In This Context") + raise NotImplementedError def toggle_reader(): """Toggles the screen reader on and off""" actions.user.tts("Toggle Reader Not Supported In This Context") + raise NotImplementedError def base_win_tts(text: str, interrupt: bool): """Base function for windows tts. We expose this so we can share the speaker object across files since it won't get overridden by the other tts functions""" + raise NotImplementedError def switch_voice(): """Switches the tts voice""" actions.user.tts("Switching Not Supported In This Context") + raise NotImplementedError def piper(text: str): """Text to speech with a piper model""" + raise NotImplementedError diff --git a/core/screenreader_ipc/ipc_client.py b/core/screenreader_ipc/ipc_client.py index 42418f1..533ad12 100644 --- a/core/screenreader_ipc/ipc_client.py +++ b/core/screenreader_ipc/ipc_client.py @@ -41,7 +41,6 @@ def send_ipc_command(command: IPC_COMMAND): @NVDAContext.action_class("user") class NVDAActions: - def addon_server_endpoint() -> Tuple[str, str, str]: """Returns the address, port, and valid commands for the addon server""" SPEC_FILE = os.path.expanduser( @@ -103,8 +102,8 @@ def send_ipc_commands(commands: list[IPC_COMMAND]): print(e) if "debug" in commands: actions.user.tts("Clientside connection timed out") - except: - print("Error Communicating with NVDA extension") + except Exception as e: + print(f"Error Communicating with NVDA extension: {e}") if "debug" in commands: actions.user.tts("Error Communicating with NVDA extension") finally: diff --git a/nvda/nvda.py b/nvda/nvda.py index fc3c2f0..48e26d3 100644 --- a/nvda/nvda.py +++ b/nvda/nvda.py @@ -14,7 +14,7 @@ def set_nvda_running_tag(): """Update tags based on if NVDA is running""" try: ctx.tags = ["user.nvda_running"] if actions.user.is_nvda_running() else [] - except: + except Exception: ctx.tags = [] @@ -23,7 +23,6 @@ def set_nvda_running_tag(): dir_path = os.path.dirname(os.path.realpath(__file__)) dll_path = os.path.join(dir_path, "nvdaControllerClient64.dll") nvda_client: ctypes.WinDLL = ctypes.windll.LoadLibrary(dll_path) - cron.interval("3s", set_nvda_running_tag.update) else: @@ -32,7 +31,6 @@ def set_nvda_running_tag(): @mod.action_class class Actions: - def toggle_nvda(): """Toggles NVDA on and off""" if not actions.user.is_nvda_running(): @@ -101,7 +99,6 @@ def test_reader_addon(): @ctxWindowsNVDARunning.action_class("user") class UserActions: - def nvda_tts(text: str, use_clipboard: bool = False): """text to speech with NVDA""" @@ -154,7 +151,7 @@ def _ready_to_send_ipc(): # By default the screen reader will allow you to press a key and interrupt the ph # rase however this does not work alongside typing given the fact that we are pres -# sing keys. so we need to temporally disable it then re enable it at the end of +# sing keys. So we need to temporally disable it then re enable it at the end of # the phrase def disable_interrupt(_): if not _ready_to_send_ipc(): diff --git a/orca/orca.py b/orca/orca.py index d1104f9..2f57f00 100644 --- a/orca/orca.py +++ b/orca/orca.py @@ -1,4 +1,4 @@ -from talon import actions, Module, settings, cron, Context +from talon import actions, Module, settings, cron, Context import os import sys @@ -7,34 +7,32 @@ mod.tag("orca_running", desc="If set, orca is running") + @mod.scope def set_orca_running_tag(): - '''Update tags based on if Orca is running''' + """Update tags based on if Orca is running""" # TODO edge case on startup this might not be set yet try: - ctx.tags = ["user.orca_running"] \ - if actions.user.is_orca_running() \ - else [] - except: + ctx.tags = ["user.orca_running"] if actions.user.is_orca_running() else [] + except Exception: ctx.tags = [] + if sys.platform == "linux" or sys.platform.startswith("linux"): cron.interval("3s", set_orca_running_tag.update) - @mod.action_class class Actions: def is_orca_running() -> bool: - '''Returns true if orca is running''' + """Returns true if orca is running""" return True if "user.orca_running" in ctx.tags else False - - def orca_tts(text: str, use_clipboard: bool= False): - '''text to speech with orca''' - + + def orca_tts(text: str, use_clipboard: bool = False): + """text to speech with orca""" def with_orca_mod_press(key: str): - ''' Presses the given key with the orca modifier key''' + """Presses the given key with the orca modifier key""" ctxLinux = Context() @@ -42,24 +40,26 @@ def with_orca_mod_press(key: str): os: linux """ + @ctxLinux.action_class("user") class LinuxActions: def toggle_reader(): - '''Toggles orca on and off''' + """Toggles orca on and off""" if not os.path.exists("/usr/bin/orca"): actions.user.tts("Orca is not installed") return actions.key("alt-super-s") - + def with_orca_mod_press(key: str): - ''' Presses the given key with the orca modifier key''' + """Presses the given key with the orca modifier key""" orca_key = settings.get("user.orca_key") - actions.key(f'{orca_key}:down') + actions.key(f"{orca_key}:down") actions.sleep("50ms") actions.key(key) actions.sleep("10ms") - actions.key(f'{orca_key}:up') + actions.key(f"{orca_key}:up") + ctxorcaRunning = Context() ctxorcaRunning.matches = r""" @@ -69,6 +69,6 @@ def with_orca_mod_press(key: str): @ctxorcaRunning.action_class("user") class orcaActions: - def orca_tts(text: str, use_clipboard: bool= False): + def orca_tts(text: str, use_clipboard: bool = False): """text to speech with orca""" # blocked until orca supports tts via a socket