To make Fenrir easier to approach for new developer, start code migration to be pep8 compliant.
This commit is contained in:
@@ -5,12 +5,20 @@
|
||||
# By Chrys, Storm Dragon, and contributers.
|
||||
|
||||
from fenrirscreenreader.core import debug
|
||||
from fenrirscreenreader.core.i18n import _
|
||||
from fenrirscreenreader.utils import module_utils
|
||||
import os, inspect, time
|
||||
import os
|
||||
import inspect
|
||||
import time
|
||||
|
||||
currentdir = os.path.dirname(os.path.realpath(os.path.abspath(inspect.getfile(inspect.currentframe()))))
|
||||
currentdir = os.path.dirname(
|
||||
os.path.realpath(
|
||||
os.path.abspath(
|
||||
inspect.getfile(
|
||||
inspect.currentframe()))))
|
||||
fenrirPath = os.path.dirname(currentdir)
|
||||
|
||||
|
||||
class vmenuManager():
|
||||
def __init__(self):
|
||||
self.menuDict = {}
|
||||
@@ -21,25 +29,32 @@ class vmenuManager():
|
||||
self.useTimeout = True
|
||||
self.searchText = ''
|
||||
self.lastSearchTime = time.time()
|
||||
|
||||
def initialize(self, environment):
|
||||
self.env = environment
|
||||
# use default path
|
||||
self.defaultVMenuPath = fenrirPath+ "/commands/vmenu-profiles/" + self.env['runtime']['inputManager'].getShortcutType()
|
||||
self.defaultVMenuPath = fenrirPath + "/commands/vmenu-profiles/" + \
|
||||
self.env['runtime']['inputManager'].getShortcutType()
|
||||
# if there is no user configuration
|
||||
if self.env['runtime']['settingsManager'].getSetting('menu', 'vmenuPath') != '':
|
||||
self.defaultVMenuPath = self.env['runtime']['settingsManager'].getSetting('menu', 'vmenuPath')
|
||||
if self.env['runtime']['settingsManager'].getSetting(
|
||||
'menu', 'vmenuPath') != '':
|
||||
self.defaultVMenuPath = self.env['runtime']['settingsManager'].getSetting(
|
||||
'menu', 'vmenuPath')
|
||||
if not self.defaultVMenuPath.endswith('/'):
|
||||
self.defaultVMenuPath += '/'
|
||||
self.defaultVMenuPath += self.env['runtime']['inputManager'].getShortcutType()
|
||||
|
||||
self.createMenuTree()
|
||||
self.closeAfterAction = False
|
||||
|
||||
def shutdown(self):
|
||||
pass
|
||||
|
||||
def clearSearchText(self):
|
||||
self.searchText = ''
|
||||
def searchEntry(self, value, forceReset = False):
|
||||
if self.currIndex == None:
|
||||
|
||||
def searchEntry(self, value, forceReset=False):
|
||||
if self.currIndex is None:
|
||||
return ''
|
||||
if self.reset or forceReset:
|
||||
self.clearSearchText()
|
||||
@@ -59,7 +74,7 @@ class vmenuManager():
|
||||
if startIndex == self.getCurrIndex():
|
||||
return ''
|
||||
|
||||
def setCurrMenu(self, currMenu = ''):
|
||||
def setCurrMenu(self, currMenu=''):
|
||||
self.currIndex = None
|
||||
self.currMenu = ''
|
||||
if currMenu != '':
|
||||
@@ -78,13 +93,17 @@ class vmenuManager():
|
||||
else:
|
||||
self.currMenu = ''
|
||||
self.currIndex = None
|
||||
|
||||
def getCurrMenu(self):
|
||||
return self.currMenu
|
||||
|
||||
def getActive(self):
|
||||
return self.active
|
||||
def togglelVMenuMode(self, closeAfterAction = True):
|
||||
|
||||
def togglelVMenuMode(self, closeAfterAction=True):
|
||||
self.setActive(not self.getActive(), closeAfterAction)
|
||||
def setActive(self, active, closeAfterAction = True):
|
||||
|
||||
def setActive(self, active, closeAfterAction=True):
|
||||
if self.env['runtime']['helpManager'].isTutorialMode():
|
||||
return
|
||||
self.active = active
|
||||
@@ -97,20 +116,26 @@ class vmenuManager():
|
||||
try:
|
||||
if self.currMenu != '':
|
||||
self.setCurrMenu(self.currMenu)
|
||||
if self.currIndex == None:
|
||||
if self.currIndex is None:
|
||||
if len(self.menuDict) > 0:
|
||||
self.currIndex = [0]
|
||||
except Exception as e:
|
||||
print(e)
|
||||
try:
|
||||
# navigation
|
||||
self.env['bindings'][str([1, ['KEY_ESC']])] = 'TOGGLE_VMENU_MODE'
|
||||
self.env['bindings'][str(
|
||||
[1, ['KEY_ESC']])] = 'TOGGLE_VMENU_MODE'
|
||||
self.env['bindings'][str([1, ['KEY_UP']])] = 'PREV_VMENU_ENTRY'
|
||||
self.env['bindings'][str([1, ['KEY_DOWN']])] = 'NEXT_VMENU_ENTRY'
|
||||
self.env['bindings'][str([1, ['KEY_SPACE']])] = 'CURR_VMENU_ENTRY'
|
||||
self.env['bindings'][str([1, ['KEY_LEFT']])] = 'DEC_LEVEL_VMENU'
|
||||
self.env['bindings'][str([1, ['KEY_RIGHT']])] = 'INC_LEVEL_VMENU'
|
||||
self.env['bindings'][str([1, ['KEY_ENTER']])] = 'EXEC_VMENU_ENTRY'
|
||||
self.env['bindings'][str(
|
||||
[1, ['KEY_DOWN']])] = 'NEXT_VMENU_ENTRY'
|
||||
self.env['bindings'][str(
|
||||
[1, ['KEY_SPACE']])] = 'CURR_VMENU_ENTRY'
|
||||
self.env['bindings'][str(
|
||||
[1, ['KEY_LEFT']])] = 'DEC_LEVEL_VMENU'
|
||||
self.env['bindings'][str(
|
||||
[1, ['KEY_RIGHT']])] = 'INC_LEVEL_VMENU'
|
||||
self.env['bindings'][str(
|
||||
[1, ['KEY_ENTER']])] = 'EXEC_VMENU_ENTRY'
|
||||
# search
|
||||
self.env['bindings'][str([1, ['KEY_A']])] = 'SEARCH_A'
|
||||
self.env['bindings'][str([1, ['KEY_B']])] = 'SEARCH_B'
|
||||
@@ -139,45 +164,54 @@ class vmenuManager():
|
||||
self.env['bindings'][str([1, ['KEY_Y']])] = 'SEARCH_Y'
|
||||
self.env['bindings'][str([1, ['KEY_Z']])] = 'SEARCH_Z'
|
||||
# page navigation
|
||||
self.env['bindings'][str([1, ['KEY_PAGEUP']])] = 'PAGE_UP_VMENU'
|
||||
self.env['bindings'][str([1, ['KEY_PAGEDOWN']])] = 'PAGE_DOWN_VMENU'
|
||||
self.env['bindings'][str(
|
||||
[1, ['KEY_PAGEUP']])] = 'PAGE_UP_VMENU'
|
||||
self.env['bindings'][str(
|
||||
[1, ['KEY_PAGEDOWN']])] = 'PAGE_DOWN_VMENU'
|
||||
except Exception as e:
|
||||
print(e)
|
||||
else:
|
||||
try:
|
||||
self.currIndex = None
|
||||
self.env['bindings'] = self.env['runtime']['settingsManager'].getBindingBackup()
|
||||
self.env['bindings'] = self.env['runtime']['settingsManager'].getBindingBackup(
|
||||
)
|
||||
except Exception as e:
|
||||
self.env['runtime']['debug'].writeDebugOut('vmenuManager setActive: Error loading binding backup: ' + str(e), debug.debugLevel.ERROR)
|
||||
def createMenuTree(self, resetIndex = True):
|
||||
self.env['runtime']['debug'].writeDebugOut(
|
||||
'vmenuManager setActive: Error loading binding backup: ' + str(e),
|
||||
debug.debugLevel.ERROR)
|
||||
|
||||
def createMenuTree(self, resetIndex=True):
|
||||
if resetIndex:
|
||||
self.currIndex = None
|
||||
menu = self.fs_tree_to_dict( self.defaultVMenuPath)
|
||||
menu = self.fs_tree_to_dict(self.defaultVMenuPath)
|
||||
if menu:
|
||||
self.menuDict = menu
|
||||
|
||||
|
||||
# Add dynamic voice menus
|
||||
try:
|
||||
from fenrirscreenreader.core.dynamicVoiceMenu import addDynamicVoiceMenus
|
||||
addDynamicVoiceMenus(self)
|
||||
except Exception as e:
|
||||
print(f"Error adding dynamic voice menus: {e}")
|
||||
|
||||
|
||||
# index still valid?
|
||||
if self.currIndex != None:
|
||||
if self.currIndex is not None:
|
||||
try:
|
||||
r = self.getValueByPath(self.menuDict, self.currIndex)
|
||||
if r == {}:
|
||||
self.currIndex = None
|
||||
except Exception as e:
|
||||
self.env['runtime']['debug'].writeDebugOut('vmenuManager createMenuTree: Error checking menu index validity: ' + str(e), debug.debugLevel.ERROR)
|
||||
self.currIndex = None
|
||||
self.env['runtime']['debug'].writeDebugOut(
|
||||
'vmenuManager createMenuTree: Error checking menu index validity: ' + str(e),
|
||||
debug.debugLevel.ERROR)
|
||||
self.currIndex = None
|
||||
|
||||
def executeMenu(self):
|
||||
if self.currIndex == None:
|
||||
if self.currIndex is None:
|
||||
return
|
||||
try:
|
||||
command = self.getValueByPath(self.menuDict, self.currIndex)
|
||||
if not command == None:
|
||||
if command is not None:
|
||||
command.run()
|
||||
if self.closeAfterAction:
|
||||
self.setActive(False)
|
||||
@@ -185,24 +219,30 @@ class vmenuManager():
|
||||
try:
|
||||
self.incLevel()
|
||||
text = self.getCurrentEntry()
|
||||
self.env['runtime']['outputManager'].presentText(text, interrupt=True)
|
||||
self.env['runtime']['outputManager'].presentText(
|
||||
text, interrupt=True)
|
||||
except Exception as ex:
|
||||
self.env['runtime']['debug'].writeDebugOut('vmenuManager executeMenu: Error presenting menu text: ' + str(ex), debug.debugLevel.ERROR)
|
||||
self.env['runtime']['debug'].writeDebugOut(
|
||||
'vmenuManager executeMenu: Error presenting menu text: ' + str(ex),
|
||||
debug.debugLevel.ERROR)
|
||||
|
||||
def incLevel(self):
|
||||
if self.currIndex == None:
|
||||
if self.currIndex is None:
|
||||
return False
|
||||
try:
|
||||
r = self.getValueByPath(self.menuDict, self.currIndex +[0])
|
||||
r = self.getValueByPath(self.menuDict, self.currIndex + [0])
|
||||
if r == {}:
|
||||
return False
|
||||
except Exception as e:
|
||||
self.env['runtime']['debug'].writeDebugOut('vmenuManager incLevel: Error accessing menu path: ' + str(e), debug.debugLevel.ERROR)
|
||||
self.env['runtime']['debug'].writeDebugOut(
|
||||
'vmenuManager incLevel: Error accessing menu path: ' + str(e),
|
||||
debug.debugLevel.ERROR)
|
||||
return False
|
||||
self.currIndex.append(0)
|
||||
return True
|
||||
|
||||
def decLevel(self):
|
||||
if self.currIndex == None:
|
||||
if self.currIndex is None:
|
||||
return False
|
||||
if self.currMenu != '':
|
||||
if len(self.currIndex) <= 2:
|
||||
@@ -211,31 +251,37 @@ class vmenuManager():
|
||||
return False
|
||||
self.currIndex = self.currIndex[:len(self.currIndex) - 1]
|
||||
return True
|
||||
|
||||
def nextIndex(self):
|
||||
if self.currIndex == None:
|
||||
if self.currIndex is None:
|
||||
return False
|
||||
if self.currIndex[len(self.currIndex) - 1] + 1 >= len(self.getNestedByPath(self.menuDict, self.currIndex[:-1])):
|
||||
self.currIndex[len(self.currIndex) - 1] = 0
|
||||
if self.currIndex[len(self.currIndex) - 1] + 1 >= len(
|
||||
self.getNestedByPath(self.menuDict, self.currIndex[:-1])):
|
||||
self.currIndex[len(self.currIndex) - 1] = 0
|
||||
else:
|
||||
self.currIndex[len(self.currIndex) - 1] += 1
|
||||
return True
|
||||
|
||||
def getCurrIndex(self):
|
||||
if self.currIndex == None:
|
||||
return 0
|
||||
if self.currIndex is None:
|
||||
return 0
|
||||
return self.currIndex[len(self.currIndex) - 1]
|
||||
|
||||
def prevIndex(self):
|
||||
if self.currIndex == None:
|
||||
if self.currIndex is None:
|
||||
return False
|
||||
if self.currIndex[len(self.currIndex) - 1] == 0:
|
||||
self.currIndex[len(self.currIndex) - 1] = len(self.getNestedByPath(self.menuDict, self.currIndex[:-1])) - 1
|
||||
self.currIndex[len(self.currIndex) - 1] = len(
|
||||
self.getNestedByPath(self.menuDict, self.currIndex[:-1])) - 1
|
||||
else:
|
||||
self.currIndex[len(self.currIndex) - 1] -= 1
|
||||
return True
|
||||
|
||||
|
||||
def pageUp(self):
|
||||
if self.currIndex == None:
|
||||
if self.currIndex is None:
|
||||
return False
|
||||
menuSize = len(self.getNestedByPath(self.menuDict, self.currIndex[:-1]))
|
||||
menuSize = len(self.getNestedByPath(
|
||||
self.menuDict, self.currIndex[:-1]))
|
||||
if menuSize <= 1:
|
||||
return False
|
||||
jumpSize = max(1, int(menuSize * 0.1)) # 10% of menu size, minimum 1
|
||||
@@ -244,11 +290,12 @@ class vmenuManager():
|
||||
newIndex = 0
|
||||
self.currIndex[len(self.currIndex) - 1] = newIndex
|
||||
return True
|
||||
|
||||
|
||||
def pageDown(self):
|
||||
if self.currIndex == None:
|
||||
if self.currIndex is None:
|
||||
return False
|
||||
menuSize = len(self.getNestedByPath(self.menuDict, self.currIndex[:-1]))
|
||||
menuSize = len(self.getNestedByPath(
|
||||
self.menuDict, self.currIndex[:-1]))
|
||||
if menuSize <= 1:
|
||||
return False
|
||||
jumpSize = max(1, int(menuSize * 0.1)) # 10% of menu size, minimum 1
|
||||
@@ -259,10 +306,18 @@ class vmenuManager():
|
||||
return True
|
||||
|
||||
def getCurrentEntry(self):
|
||||
return self.getKeysByPath(self.menuDict, self.currIndex)[self.currIndex[-1]]
|
||||
return self.getKeysByPath(self.menuDict, self.currIndex)[
|
||||
self.currIndex[-1]]
|
||||
|
||||
def fs_tree_to_dict(self, path_):
|
||||
for root, dirs, files in os.walk(path_):
|
||||
tree = {d + ' ' + _('Menu'): self.fs_tree_to_dict(os.path.join(root, d)) for d in dirs if not d.startswith('__')}
|
||||
tree = {
|
||||
d +
|
||||
' ' +
|
||||
_('Menu'): self.fs_tree_to_dict(
|
||||
os.path.join(
|
||||
root,
|
||||
d)) for d in dirs if not d.startswith('__')}
|
||||
for f in files:
|
||||
try:
|
||||
fileName, fileExtension = os.path.splitext(f)
|
||||
@@ -272,11 +327,13 @@ class vmenuManager():
|
||||
# Skip base classes that shouldn't be loaded as commands
|
||||
if fileName.endswith('_base'):
|
||||
continue
|
||||
command = self.env['runtime']['commandManager'].loadFile(root + '/' + f)
|
||||
command = self.env['runtime']['commandManager'].loadFile(
|
||||
root + '/' + f)
|
||||
tree.update({fileName + ' ' + _('Action'): command})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return tree # note we discontinue iteration trough os.walk
|
||||
|
||||
def getNestedByPath(self, complete, path):
|
||||
path = path.copy()
|
||||
if path != []:
|
||||
@@ -286,10 +343,9 @@ class vmenuManager():
|
||||
else:
|
||||
return complete
|
||||
|
||||
|
||||
def getKeysByPath(self, complete, path):
|
||||
if not isinstance(complete, dict):
|
||||
return[]
|
||||
return []
|
||||
d = complete
|
||||
for i in path[:-1]:
|
||||
d = d[list(d.keys())[i]]
|
||||
|
||||
Reference in New Issue
Block a user