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

@@ -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()