Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Loftus committed Feb 27, 2024
1 parent 4f3e3ed commit d23c059
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
18 changes: 12 additions & 6 deletions core/core-agnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -64,26 +65,26 @@ 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:
"""Returns true if braille is enabled"""
# 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:
"""Returns true if echo context is enabled"""
# 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():
Expand All @@ -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():
Expand All @@ -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 = (
Expand All @@ -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
5 changes: 2 additions & 3 deletions core/screenreader_ipc/ipc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
7 changes: 2 additions & 5 deletions nvda/nvda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []


Expand All @@ -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:
Expand All @@ -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():
Expand Down Expand Up @@ -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"""

Expand Down Expand Up @@ -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():
Expand Down
38 changes: 19 additions & 19 deletions orca/orca.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from talon import actions, Module, settings, cron, Context
from talon import actions, Module, settings, cron, Context
import os
import sys

Expand All @@ -7,59 +7,59 @@

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()
ctx.matches = r"""
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"""
Expand All @@ -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

0 comments on commit d23c059

Please sign in to comment.