To make Fenrir easier to approach for new developer, start code migration to be pep8 compliant.

This commit is contained in:
Storm Dragon
2025-07-01 22:23:50 -04:00
parent 4bcf82178e
commit 7408951152
345 changed files with 8688 additions and 3852 deletions

View File

@ -8,26 +8,28 @@
from fenrirscreenreader.core import debug
from fenrirscreenreader.core.speechDriver import speechDriver
class driver(speechDriver):
def __init__(self):
speechDriver.__init__(self)
def initialize(self, environment):
self._isInitialized = True
self.env = environment
print('Speech Debug Driver: Iitialized')
def shutdown(self):
if self._isInitialized:
self.cancel()
self._isInitialized = False
print('Speech Debug Driver: Shutdown')
def speak(self,text, queueable=True, ignorePunctuation=False):
def speak(self, text, queueable=True, ignorePunctuation=False):
if not self._isInitialized:
return
if not queueable:
if not queueable:
self.cancel()
print('Speech Debug Driver: Speak:'+text)
print('Speech Debug Driver: Speak:' + text)
print('Speech Debug Driver: -----------------------------------')
def cancel(self):
@ -46,7 +48,7 @@ class driver(speechDriver):
def setVoice(self, voice):
if not self._isInitialized:
return
print('Speech Debug Driver: setVoice:' + str(voice))
print('Speech Debug Driver: setVoice:' + str(voice))
def setPitch(self, pitch):
if not self._isInitialized:

View File

@ -8,6 +8,7 @@
from fenrirscreenreader.core import debug
from fenrirscreenreader.core.speechDriver import speechDriver
class driver(speechDriver):
def __init__(self):
speechDriver.__init__(self)

View File

@ -13,6 +13,7 @@ from subprocess import Popen
import subprocess
from fenrirscreenreader.core.speechDriver import speechDriver
class speakQueue(Queue):
def clear(self):
try:
@ -21,6 +22,7 @@ class speakQueue(Queue):
except Empty:
pass
class driver(speechDriver):
def __init__(self):
speechDriver.__init__(self)
@ -28,44 +30,53 @@ class driver(speechDriver):
self.speechThread = Thread(target=self.worker)
self.lock = Lock()
self.textQueue = speakQueue()
def initialize(self, environment):
self.env = environment
self.minVolume = self.env['runtime']['settingsManager'].getSettingAsInt('speech', 'fenrirMinVolume')
self.maxVolume = self.env['runtime']['settingsManager'].getSettingAsInt('speech', 'fenrirMaxVolume')
self.minPitch = self.env['runtime']['settingsManager'].getSettingAsInt('speech', 'fenrirMinPitch')
self.maxPitch = self.env['runtime']['settingsManager'].getSettingAsInt('speech', 'fenrirMaxPitch')
self.minRate = self.env['runtime']['settingsManager'].getSettingAsInt('speech', 'fenrirMinRate')
self.maxRate = self.env['runtime']['settingsManager'].getSettingAsInt('speech', 'fenrirMaxRate')
self.speechCommand = self.env['runtime']['settingsManager'].getSetting('speech', 'genericSpeechCommand')
self.env = environment
self.minVolume = self.env['runtime']['settingsManager'].getSettingAsInt(
'speech', 'fenrirMinVolume')
self.maxVolume = self.env['runtime']['settingsManager'].getSettingAsInt(
'speech', 'fenrirMaxVolume')
self.minPitch = self.env['runtime']['settingsManager'].getSettingAsInt(
'speech', 'fenrirMinPitch')
self.maxPitch = self.env['runtime']['settingsManager'].getSettingAsInt(
'speech', 'fenrirMaxPitch')
self.minRate = self.env['runtime']['settingsManager'].getSettingAsInt(
'speech', 'fenrirMinRate')
self.maxRate = self.env['runtime']['settingsManager'].getSettingAsInt(
'speech', 'fenrirMaxRate')
self.speechCommand = self.env['runtime']['settingsManager'].getSetting(
'speech', 'genericSpeechCommand')
if self.speechCommand == '':
self.speechCommand = 'espeak -a fenrirVolume -s fenrirRate -p fenrirPitch -v fenrirVoice -- "fenrirText"'
if False: #for debugging overwrite here
#self.speechCommand = 'spd-say --wait -r 100 -i 100 "fenrirText"'
if False: # for debugging overwrite here
# self.speechCommand = 'spd-say --wait -r 100 -i 100 "fenrirText"'
self.speechCommand = 'flite -t "fenrirText"'
self._isInitialized = True
if self._isInitialized:
self.speechThread.start()
def shutdown(self):
if not self._isInitialized:
return
self.cancel()
self.textQueue.put(-1)
def speak(self,text, queueable=True, ignorePunctuation=False):
def speak(self, text, queueable=True, ignorePunctuation=False):
if not self._isInitialized:
return
if not queueable:
self.cancel()
utterance = {
'text': text,
'volume': self.volume,
'rate': self.rate,
'pitch': self.pitch,
'module': self.module,
'language': self.language,
'voice': self.voice,
'text': text,
'volume': self.volume,
'rate': self.rate,
'pitch': self.pitch,
'module': self.module,
'language': self.language,
'voice': self.voice,
}
self.textQueue.put(utterance.copy())
@ -86,16 +97,20 @@ class driver(speechDriver):
self.proc.kill()
self.proc.wait(timeout=1.0)
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('speechDriver:Cancel:self.proc.terminate():' + str(e),debug.debugLevel.WARNING)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver:Cancel:self.proc.terminate():' + str(e), debug.debugLevel.WARNING)
try:
self.proc.kill()
self.proc.wait(timeout=1.0) # Wait after kill to prevent zombies
# Wait after kill to prevent zombies
self.proc.wait(timeout=1.0)
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('speechDriver:Cancel:self.proc.kill():' + str(e),debug.debugLevel.WARNING)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver:Cancel:self.proc.kill():' + str(e), debug.debugLevel.WARNING)
self.proc = None
finally:
# Ensure lock is always released, even if process termination fails
self.lock.release()
def setCallback(self, callback):
print('SpeechDummyDriver: setCallback')
@ -103,7 +118,7 @@ class driver(speechDriver):
if not self._isInitialized:
return
self.textQueue.clear()
def setVoice(self, voice):
if not self._isInitialized:
return
@ -112,16 +127,17 @@ class driver(speechDriver):
def setPitch(self, pitch):
if not self._isInitialized:
return
self.pitch = str(self.minPitch + pitch * (self.maxPitch - self.minPitch ))
self.pitch = str(self.minPitch + pitch *
(self.maxPitch - self.minPitch))
def setRate(self, rate):
if not self._isInitialized:
return
self.rate = str(self.minRate + rate * (self.maxRate - self.minRate ))
self.rate = str(self.minRate + rate * (self.maxRate - self.minRate))
def setModule(self, module):
if not self._isInitialized:
return
return
self.module = str(module)
def setLanguage(self, language):
@ -131,9 +147,10 @@ class driver(speechDriver):
def setVolume(self, volume):
if not self._isInitialized:
return
self.volume = str(self.minVolume + volume * (self.maxVolume - self.minVolume ))
return
self.volume = str(self.minVolume + volume *
(self.maxVolume - self.minVolume))
def worker(self):
while True:
utterance = self.textQueue.get()
@ -146,60 +163,69 @@ class driver(speechDriver):
elif not isinstance(utterance, dict):
continue
# no text means nothing to speak
if not 'text' in utterance:
if 'text' not in utterance:
continue
if not isinstance(utterance['text'],str):
if not isinstance(utterance['text'], str):
continue
if utterance['text'] == '':
continue
# check for valid data fields
if not 'volume' in utterance:
if 'volume' not in utterance:
utterance['volume'] = ''
if not isinstance(utterance['volume'],str):
if not isinstance(utterance['volume'], str):
utterance['volume'] = ''
if not 'module' in utterance:
if 'module' not in utterance:
utterance['module'] = ''
if not isinstance(utterance['module'],str):
if not isinstance(utterance['module'], str):
utterance['module'] = ''
if not 'language' in utterance:
if 'language' not in utterance:
utterance['language'] = ''
if not isinstance(utterance['language'],str):
if not isinstance(utterance['language'], str):
utterance['language'] = ''
if not 'voice' in utterance:
if 'voice' not in utterance:
utterance['voice'] = ''
if not isinstance(utterance['voice'],str):
if not isinstance(utterance['voice'], str):
utterance['voice'] = ''
if not 'pitch' in utterance:
if 'pitch' not in utterance:
utterance['pitch'] = ''
if not isinstance(utterance['pitch'],str):
if not isinstance(utterance['pitch'], str):
utterance['pitch'] = ''
if not 'rate' in utterance:
if 'rate' not in utterance:
utterance['rate'] = ''
if not isinstance(utterance['rate'],str):
if not isinstance(utterance['rate'], str):
utterance['rate'] = ''
popenSpeechCommand = shlex.split(self.speechCommand)
for idx, word in enumerate(popenSpeechCommand):
word = word.replace('fenrirVolume', str(utterance['volume'] ))
word = word.replace('fenrirVolume', str(utterance['volume']))
word = word.replace('fenrirModule', str(utterance['module']))
word = word.replace('fenrirLanguage', str(utterance['language']))
word = word.replace(
'fenrirLanguage', str(
utterance['language']))
word = word.replace('fenrirVoice', str(utterance['voice']))
word = word.replace('fenrirPitch', str(utterance['pitch']))
word = word.replace('fenrirRate', str(utterance['rate']))
# Properly quote text to prevent command injection
word = word.replace('fenrirText', shlex.quote(str(utterance['text'])))
word = word.replace('fenrirText',
shlex.quote(str(utterance['text'])))
popenSpeechCommand[idx] = word
try:
self.env['runtime']['debug'].writeDebugOut('speechDriver:worker:' + ' '.join(popenSpeechCommand),debug.debugLevel.INFO)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver:worker:' + ' '.join(popenSpeechCommand), debug.debugLevel.INFO)
self.lock.acquire(True)
self.proc = Popen(popenSpeechCommand, stdin=None, stdout=None, stderr=None, shell=False)
self.lock.release()
self.proc = Popen(
popenSpeechCommand,
stdin=None,
stdout=None,
stderr=None,
shell=False)
self.lock.release()
self.proc.wait()
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('speechDriver:worker:' + str(e),debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver:worker:' + str(e), debug.debugLevel.ERROR)
self.lock.acquire(True)
self.proc = None
self.lock.release()

View File

@ -8,6 +8,7 @@
from fenrirscreenreader.core import debug
from fenrirscreenreader.core.speechDriver import speechDriver
class driver(speechDriver):
def __init__(self):
speechDriver.__init__(self)
@ -16,22 +17,24 @@ class driver(speechDriver):
self._sd = None
self.env = environment
self._isInitialized = False
# Only set these if they haven't been set yet (preserve existing values)
# Only set these if they haven't been set yet (preserve existing
# values)
if not hasattr(self, 'language') or self.language is None:
self.language = ''
if not hasattr(self, 'voice') or self.voice is None:
self.voice = ''
self.voice = ''
if not hasattr(self, 'module') or self.module is None:
self.module = ''
try:
import speechd
self._sd = speechd.SSIPClient('fenrir')
import speechd
self._sd = speechd.SSIPClient('fenrir')
self._punct = speechd.PunctuationMode()
self._isInitialized = True
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('speechDriver initialize:' + str(e),debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver initialize:' + str(e), debug.debugLevel.ERROR)
def shutdown(self):
if not self._isInitialized:
@ -43,7 +46,7 @@ class driver(speechDriver):
pass
self._isInitialized = False
def speak(self,text, queueable=True, ignorePunctuation=False):
def speak(self, text, queueable=True, ignorePunctuation=False):
if not queueable:
self.cancel()
if not self._isInitialized:
@ -57,19 +60,22 @@ class driver(speechDriver):
if self.module != '':
self._sd.set_output_module(self.module)
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('speechDriver setModule:' + str(e),debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver setModule:' + str(e), debug.debugLevel.ERROR)
try:
if self.language != '':
self._sd.set_language(self.language)
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('speechDriver set_language:' + str(e),debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver set_language:' + str(e), debug.debugLevel.ERROR)
try:
if self.voice != '':
self._sd.set_synthesis_voice(self.voice)
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('speechDriver setVoice:' + str(e),debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver setVoice:' + str(e), debug.debugLevel.ERROR)
try:
if ignorePunctuation:
@ -77,12 +83,14 @@ class driver(speechDriver):
else:
self._sd.set_punctuation(self._punct.NONE)
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('speechDriver set_punctuation:' + str(e),debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver set_punctuation:' + str(e), debug.debugLevel.ERROR)
try:
self._sd.speak(text)
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('speechDriver speak:' + str(e),debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver speak:' + str(e), debug.debugLevel.ERROR)
self._isInitialized = False
def cancel(self):
@ -93,29 +101,33 @@ class driver(speechDriver):
try:
self._sd.cancel()
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('speechDriver cancel:' + str(e),debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver cancel:' + str(e), debug.debugLevel.ERROR)
self._isInitialized = False
def setPitch(self, pitch):
if not self._isInitialized:
return
try:
self._sd.set_pitch(int(-100 + pitch * 200))
self._sd.set_pitch(int(-100 + pitch * 200))
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('speechDriver setPitch:' + str(e),debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver setPitch:' + str(e), debug.debugLevel.ERROR)
def setRate(self, rate):
if not self._isInitialized:
return
return
try:
self._sd.set_rate(int(-100 + rate * 200))
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('speechDriver setRate:' + str(e),debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver setRate:' + str(e), debug.debugLevel.ERROR)
def setVolume(self, volume):
if not self._isInitialized:
return
return
try:
self._sd.set_volume(int(-100 + volume * 200))
except Exception as e:
self.env['runtime']['debug'].writeDebugOut('speechDriver setVolume:' + str(e),debug.debugLevel.ERROR)
self.env['runtime']['debug'].writeDebugOut(
'speechDriver setVolume:' + str(e), debug.debugLevel.ERROR)