dictation-toolbox / Caster

Dragonfly-Based Voice Programming and Accessibility Toolkit
Other
336 stars 122 forks source link

make printer.out more flexible #909

Closed synkarius closed 2 years ago

synkarius commented 2 years ago

Improve Console Output Flexibility

Description

After speaking with @LexiconCode about some upcoming changes, I suggested that he could avoid adding lots of UI code to _caster.py by making printer.py a bit more flexible. This PR is the implementation of that suggestion.

dh = printer.get_delegating_handler()
dh.register_handler(SimplePrintMessageHandler())
dh.start()

I put the above chunk of code in settings.py, but it could be moved to anywhere (anywhere that it's called only once) and it would work the same. (You don't have to worry about printer.out being called first, or import order or anything.) Switching output to the HUD would be as simple as replacing SimplePrintMessageHandler with some other implementation of BaseMessageHandler which does HUD stuff. Absolutely do not put said implementation in printer.py.

If the HUD BaseMessageHandler implementation is broken or throws errors when handling printer.out messages, those messages will be printed to the console by a default SimplePrintMessageHandler instead.

Incidentally, you can throw as many printer.out handlers at this thing as you want. It can use more than one.

Related Issue

Hud PR https://github.com/dictation-toolbox/Caster/pull/773

Motivation and Context

See description.

How Has This Been Tested

Additional unit tests + manual testing to make sure nothing was broken.

Types of changes

Checklist

Maintainer/Reviewer Checklist

LexiconCode commented 2 years ago

One last issue referring back to the comment with get_current_engine with DNS

Error loading _caster from D:\Backup\Library\Documents\Caster\_caster.py
Traceback (most recent call last):
  File "c:\users\main\appdata\local\programs\python\python38-32\lib\site-packages\natlinkcore\natlinkmain.py", line 386, in loadFile
    imp.load_module(modName,fndFile,fndName,fndDesc)
  File "C:\Users\Main\AppData\Local\Programs\Python\Python38-32\Lib\imp.py", line 234, in load_module
    return load_source(name, filename, file)
  File "C:\Users\Main\AppData\Local\Programs\Python\Python38-32\Lib\imp.py", line 171, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 702, in _load
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\Backup\Library\Documents\Caster\_caster.py", line 17, in <module>
    settings.initialize()
  File "D:\Backup\Library\Documents\Caster\castervoice\lib\settings.py", line 512, in initialize
    dh.start()
  File "D:\Backup\Library\Documents\Caster\castervoice\lib\printer.py", line 32, in start
    self._timer = get_current_engine() \
AttributeError: 'NoneType' object has no attribute 'create_timer'

I will fix post-merge as discussed in Chat