More pep8 fixes. A tiny bit of refactoring.
This commit is contained in:
@@ -2,47 +2,56 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Fenrir TTY screen reader
|
||||
# By Chrys, Storm Dragon, and contributers.
|
||||
# By Chrys, Storm Dragon, and contributors.
|
||||
|
||||
import inspect
|
||||
import os
|
||||
import time
|
||||
|
||||
from fenrirscreenreader.core import debug
|
||||
from fenrirscreenreader.core.i18n import _
|
||||
from fenrirscreenreader.utils import module_utils
|
||||
import os
|
||||
import inspect
|
||||
import time
|
||||
|
||||
currentdir = os.path.dirname(
|
||||
os.path.realpath(
|
||||
os.path.abspath(
|
||||
inspect.getfile(
|
||||
inspect.currentframe()))))
|
||||
os.path.realpath(os.path.abspath(inspect.getfile(inspect.currentframe())))
|
||||
)
|
||||
fenrir_path = os.path.dirname(currentdir)
|
||||
|
||||
|
||||
class VmenuManager():
|
||||
class VmenuManager:
|
||||
def __init__(self):
|
||||
self.menuDict = {}
|
||||
self.curr_index = None
|
||||
self.currMenu = ''
|
||||
self.currMenu = ""
|
||||
self.active = False
|
||||
self.reset = True
|
||||
self.useTimeout = True
|
||||
self.searchText = ''
|
||||
self.searchText = ""
|
||||
self.lastSearchTime = time.time()
|
||||
|
||||
def initialize(self, environment):
|
||||
self.env = environment
|
||||
# use default path
|
||||
self.defaultVMenuPath = fenrir_path + "/commands/vmenu-profiles/" + \
|
||||
self.env['runtime']['InputManager'].get_shortcut_type()
|
||||
self.defaultVMenuPath = (
|
||||
fenrir_path
|
||||
+ "/commands/vmenu-profiles/"
|
||||
+ self.env["runtime"]["InputManager"].get_shortcut_type()
|
||||
)
|
||||
# if there is no user configuration
|
||||
if self.env['runtime']['SettingsManager'].get_setting(
|
||||
'menu', 'vmenuPath') != '':
|
||||
self.defaultVMenuPath = self.env['runtime']['SettingsManager'].get_setting(
|
||||
'menu', 'vmenuPath')
|
||||
if not self.defaultVMenuPath.endswith('/'):
|
||||
self.defaultVMenuPath += '/'
|
||||
self.defaultVMenuPath += self.env['runtime']['InputManager'].get_shortcut_type()
|
||||
if (
|
||||
self.env["runtime"]["SettingsManager"].get_setting(
|
||||
"menu", "vmenuPath"
|
||||
)
|
||||
!= ""
|
||||
):
|
||||
self.defaultVMenuPath = self.env["runtime"][
|
||||
"SettingsManager"
|
||||
].get_setting("menu", "vmenuPath")
|
||||
if not self.defaultVMenuPath.endswith("/"):
|
||||
self.defaultVMenuPath += "/"
|
||||
self.defaultVMenuPath += self.env["runtime"][
|
||||
"InputManager"
|
||||
].get_shortcut_type()
|
||||
|
||||
self.create_menu_tree()
|
||||
self.closeAfterAction = False
|
||||
@@ -51,11 +60,11 @@ class VmenuManager():
|
||||
pass
|
||||
|
||||
def clear_search_text(self):
|
||||
self.searchText = ''
|
||||
self.searchText = ""
|
||||
|
||||
def search_entry(self, value, forceReset=False):
|
||||
if self.curr_index is None:
|
||||
return ''
|
||||
return ""
|
||||
if self.reset or forceReset:
|
||||
self.clear_search_text()
|
||||
else:
|
||||
@@ -67,31 +76,31 @@ class VmenuManager():
|
||||
start_index = self.get_curr_index()
|
||||
while True:
|
||||
if not self.next_index():
|
||||
return ''
|
||||
return ""
|
||||
entry = self.get_current_entry()
|
||||
if entry.upper().startswith(self.searchText):
|
||||
return entry
|
||||
if start_index == self.get_curr_index():
|
||||
return ''
|
||||
return ""
|
||||
|
||||
def set_curr_menu(self, currMenu=''):
|
||||
def set_curr_menu(self, currMenu=""):
|
||||
self.curr_index = None
|
||||
self.currMenu = ''
|
||||
if currMenu != '':
|
||||
currMenu += ' ' + _('Menu')
|
||||
self.currMenu = ""
|
||||
if currMenu != "":
|
||||
currMenu += " " + _("Menu")
|
||||
try:
|
||||
t = self.menuDict[currMenu]
|
||||
l = list(self.menuDict.keys())
|
||||
self.curr_index = [l.index(currMenu)]
|
||||
except Exception as e:
|
||||
print(e)
|
||||
self.currMenu = ''
|
||||
self.currMenu = ""
|
||||
self.curr_index = None
|
||||
return
|
||||
if self.inc_level():
|
||||
self.currMenu = currMenu
|
||||
else:
|
||||
self.currMenu = ''
|
||||
self.currMenu = ""
|
||||
self.curr_index = None
|
||||
|
||||
def get_curr_menu(self):
|
||||
@@ -104,7 +113,7 @@ class VmenuManager():
|
||||
self.set_active(not self.get_active(), closeAfterAction)
|
||||
|
||||
def set_active(self, active, closeAfterAction=True):
|
||||
if self.env['runtime']['HelpManager'].is_tutorial_mode():
|
||||
if self.env["runtime"]["HelpManager"].is_tutorial_mode():
|
||||
return
|
||||
self.active = active
|
||||
if self.active:
|
||||
@@ -114,7 +123,7 @@ class VmenuManager():
|
||||
except Exception as e:
|
||||
print(e)
|
||||
try:
|
||||
if self.currMenu != '':
|
||||
if self.currMenu != "":
|
||||
self.set_curr_menu(self.currMenu)
|
||||
if self.curr_index is None:
|
||||
if len(self.menuDict) > 0:
|
||||
@@ -123,62 +132,73 @@ class VmenuManager():
|
||||
print(e)
|
||||
try:
|
||||
# navigation
|
||||
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_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"
|
||||
# search
|
||||
self.env['bindings'][str([1, ['KEY_A']])] = 'SEARCH_A'
|
||||
self.env['bindings'][str([1, ['KEY_B']])] = 'SEARCH_B'
|
||||
self.env['bindings'][str([1, ['KEY_C']])] = 'SEARCH_C'
|
||||
self.env['bindings'][str([1, ['KEY_D']])] = 'SEARCH_D'
|
||||
self.env['bindings'][str([1, ['KEY_E']])] = 'SEARCH_E'
|
||||
self.env['bindings'][str([1, ['KEY_F']])] = 'SEARCH_F'
|
||||
self.env['bindings'][str([1, ['KEY_G']])] = 'SEARCH_G'
|
||||
self.env['bindings'][str([1, ['KEY_H']])] = 'SEARCH_H'
|
||||
self.env['bindings'][str([1, ['KEY_I']])] = 'SEARCH_I'
|
||||
self.env['bindings'][str([1, ['KEY_J']])] = 'SEARCH_J'
|
||||
self.env['bindings'][str([1, ['KEY_K']])] = 'SEARCH_K'
|
||||
self.env['bindings'][str([1, ['KEY_L']])] = 'SEARCH_L'
|
||||
self.env['bindings'][str([1, ['KEY_M']])] = 'SEARCH_M'
|
||||
self.env['bindings'][str([1, ['KEY_N']])] = 'SEARCH_N'
|
||||
self.env['bindings'][str([1, ['KEY_O']])] = 'SEARCH_O'
|
||||
self.env['bindings'][str([1, ['KEY_P']])] = 'SEARCH_P'
|
||||
self.env['bindings'][str([1, ['KEY_Q']])] = 'SEARCH_Q'
|
||||
self.env['bindings'][str([1, ['KEY_R']])] = 'SEARCH_R'
|
||||
self.env['bindings'][str([1, ['KEY_S']])] = 'SEARCH_S'
|
||||
self.env['bindings'][str([1, ['KEY_T']])] = 'SEARCH_T'
|
||||
self.env['bindings'][str([1, ['KEY_U']])] = 'SEARCH_U'
|
||||
self.env['bindings'][str([1, ['KEY_V']])] = 'SEARCH_V'
|
||||
self.env['bindings'][str([1, ['KEY_W']])] = 'SEARCH_W'
|
||||
self.env['bindings'][str([1, ['KEY_X']])] = 'SEARCH_X'
|
||||
self.env['bindings'][str([1, ['KEY_Y']])] = 'SEARCH_Y'
|
||||
self.env['bindings'][str([1, ['KEY_Z']])] = 'SEARCH_Z'
|
||||
self.env["bindings"][str([1, ["KEY_A"]])] = "SEARCH_A"
|
||||
self.env["bindings"][str([1, ["KEY_B"]])] = "SEARCH_B"
|
||||
self.env["bindings"][str([1, ["KEY_C"]])] = "SEARCH_C"
|
||||
self.env["bindings"][str([1, ["KEY_D"]])] = "SEARCH_D"
|
||||
self.env["bindings"][str([1, ["KEY_E"]])] = "SEARCH_E"
|
||||
self.env["bindings"][str([1, ["KEY_F"]])] = "SEARCH_F"
|
||||
self.env["bindings"][str([1, ["KEY_G"]])] = "SEARCH_G"
|
||||
self.env["bindings"][str([1, ["KEY_H"]])] = "SEARCH_H"
|
||||
self.env["bindings"][str([1, ["KEY_I"]])] = "SEARCH_I"
|
||||
self.env["bindings"][str([1, ["KEY_J"]])] = "SEARCH_J"
|
||||
self.env["bindings"][str([1, ["KEY_K"]])] = "SEARCH_K"
|
||||
self.env["bindings"][str([1, ["KEY_L"]])] = "SEARCH_L"
|
||||
self.env["bindings"][str([1, ["KEY_M"]])] = "SEARCH_M"
|
||||
self.env["bindings"][str([1, ["KEY_N"]])] = "SEARCH_N"
|
||||
self.env["bindings"][str([1, ["KEY_O"]])] = "SEARCH_O"
|
||||
self.env["bindings"][str([1, ["KEY_P"]])] = "SEARCH_P"
|
||||
self.env["bindings"][str([1, ["KEY_Q"]])] = "SEARCH_Q"
|
||||
self.env["bindings"][str([1, ["KEY_R"]])] = "SEARCH_R"
|
||||
self.env["bindings"][str([1, ["KEY_S"]])] = "SEARCH_S"
|
||||
self.env["bindings"][str([1, ["KEY_T"]])] = "SEARCH_T"
|
||||
self.env["bindings"][str([1, ["KEY_U"]])] = "SEARCH_U"
|
||||
self.env["bindings"][str([1, ["KEY_V"]])] = "SEARCH_V"
|
||||
self.env["bindings"][str([1, ["KEY_W"]])] = "SEARCH_W"
|
||||
self.env["bindings"][str([1, ["KEY_X"]])] = "SEARCH_X"
|
||||
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.curr_index = None
|
||||
self.env['bindings'] = self.env['runtime']['SettingsManager'].get_binding_backup(
|
||||
)
|
||||
self.env["bindings"] = self.env["runtime"][
|
||||
"SettingsManager"
|
||||
].get_binding_backup()
|
||||
except Exception as e:
|
||||
self.env['runtime']['DebugManager'].write_debug_out(
|
||||
'VmenuManager set_active: Error loading binding backup: ' + str(e),
|
||||
debug.DebugLevel.ERROR)
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
"VmenuManager set_active: Error loading binding backup: "
|
||||
+ str(e),
|
||||
debug.DebugLevel.ERROR,
|
||||
)
|
||||
|
||||
def create_menu_tree(self, resetIndex=True):
|
||||
if resetIndex:
|
||||
@@ -189,7 +209,10 @@ class VmenuManager():
|
||||
|
||||
# Add dynamic voice menus
|
||||
try:
|
||||
from fenrirscreenreader.core.dynamicVoiceMenu import add_dynamic_voice_menus
|
||||
from fenrirscreenreader.core.dynamicVoiceMenu import (
|
||||
add_dynamic_voice_menus,
|
||||
)
|
||||
|
||||
add_dynamic_voice_menus(self)
|
||||
except Exception as e:
|
||||
print(f"Error adding dynamic voice menus: {e}")
|
||||
@@ -201,9 +224,11 @@ class VmenuManager():
|
||||
if r == {}:
|
||||
self.curr_index = None
|
||||
except Exception as e:
|
||||
self.env['runtime']['DebugManager'].write_debug_out(
|
||||
'VmenuManager create_menu_tree: Error checking menu index validity: ' + str(e),
|
||||
debug.DebugLevel.ERROR)
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
"VmenuManager create_menu_tree: Error checking menu index validity: "
|
||||
+ str(e),
|
||||
debug.DebugLevel.ERROR,
|
||||
)
|
||||
self.curr_index = None
|
||||
|
||||
def execute_menu(self):
|
||||
@@ -219,12 +244,15 @@ class VmenuManager():
|
||||
try:
|
||||
self.inc_level()
|
||||
text = self.get_current_entry()
|
||||
self.env['runtime']['OutputManager'].present_text(
|
||||
text, interrupt=True)
|
||||
self.env["runtime"]["OutputManager"].present_text(
|
||||
text, interrupt=True
|
||||
)
|
||||
except Exception as ex:
|
||||
self.env['runtime']['DebugManager'].write_debug_out(
|
||||
'VmenuManager execute_menu: Error presenting menu text: ' + str(ex),
|
||||
debug.DebugLevel.ERROR)
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
"VmenuManager execute_menu: Error presenting menu text: "
|
||||
+ str(ex),
|
||||
debug.DebugLevel.ERROR,
|
||||
)
|
||||
|
||||
def inc_level(self):
|
||||
if self.curr_index is None:
|
||||
@@ -234,9 +262,10 @@ class VmenuManager():
|
||||
if r == {}:
|
||||
return False
|
||||
except Exception as e:
|
||||
self.env['runtime']['DebugManager'].write_debug_out(
|
||||
'VmenuManager inc_level: Error accessing menu path: ' + str(e),
|
||||
debug.DebugLevel.ERROR)
|
||||
self.env["runtime"]["DebugManager"].write_debug_out(
|
||||
"VmenuManager inc_level: Error accessing menu path: " + str(e),
|
||||
debug.DebugLevel.ERROR,
|
||||
)
|
||||
return False
|
||||
self.curr_index.append(0)
|
||||
return True
|
||||
@@ -244,19 +273,20 @@ class VmenuManager():
|
||||
def dec_level(self):
|
||||
if self.curr_index is None:
|
||||
return False
|
||||
if self.currMenu != '':
|
||||
if self.currMenu != "":
|
||||
if len(self.curr_index) <= 2:
|
||||
return False
|
||||
elif len(self.curr_index) == 1:
|
||||
return False
|
||||
self.curr_index = self.curr_index[:len(self.curr_index) - 1]
|
||||
self.curr_index = self.curr_index[: len(self.curr_index) - 1]
|
||||
return True
|
||||
|
||||
def next_index(self):
|
||||
if self.curr_index is None:
|
||||
return False
|
||||
if self.curr_index[len(self.curr_index) - 1] + 1 >= len(
|
||||
self.get_nested_by_path(self.menuDict, self.curr_index[:-1])):
|
||||
self.get_nested_by_path(self.menuDict, self.curr_index[:-1])
|
||||
):
|
||||
self.curr_index[len(self.curr_index) - 1] = 0
|
||||
else:
|
||||
self.curr_index[len(self.curr_index) - 1] += 1
|
||||
@@ -271,8 +301,14 @@ class VmenuManager():
|
||||
if self.curr_index is None:
|
||||
return False
|
||||
if self.curr_index[len(self.curr_index) - 1] == 0:
|
||||
self.curr_index[len(self.curr_index) - 1] = len(
|
||||
self.get_nested_by_path(self.menuDict, self.curr_index[:-1])) - 1
|
||||
self.curr_index[len(self.curr_index) - 1] = (
|
||||
len(
|
||||
self.get_nested_by_path(
|
||||
self.menuDict, self.curr_index[:-1]
|
||||
)
|
||||
)
|
||||
- 1
|
||||
)
|
||||
else:
|
||||
self.curr_index[len(self.curr_index) - 1] -= 1
|
||||
return True
|
||||
@@ -280,8 +316,9 @@ class VmenuManager():
|
||||
def page_up(self):
|
||||
if self.curr_index is None:
|
||||
return False
|
||||
menu_size = len(self.get_nested_by_path(
|
||||
self.menuDict, self.curr_index[:-1]))
|
||||
menu_size = len(
|
||||
self.get_nested_by_path(self.menuDict, self.curr_index[:-1])
|
||||
)
|
||||
if menu_size <= 1:
|
||||
return False
|
||||
jump_size = max(1, int(menu_size * 0.1)) # 10% of menu size, minimum 1
|
||||
@@ -294,8 +331,9 @@ class VmenuManager():
|
||||
def page_down(self):
|
||||
if self.curr_index is None:
|
||||
return False
|
||||
menu_size = len(self.get_nested_by_path(
|
||||
self.menuDict, self.curr_index[:-1]))
|
||||
menu_size = len(
|
||||
self.get_nested_by_path(self.menuDict, self.curr_index[:-1])
|
||||
)
|
||||
if menu_size <= 1:
|
||||
return False
|
||||
jump_size = max(1, int(menu_size * 0.1)) # 10% of menu size, minimum 1
|
||||
@@ -307,29 +345,31 @@ class VmenuManager():
|
||||
|
||||
def get_current_entry(self):
|
||||
return self.get_keys_by_path(self.menuDict, self.curr_index)[
|
||||
self.curr_index[-1]]
|
||||
self.curr_index[-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('__')}
|
||||
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:
|
||||
file_name, file_extension = os.path.splitext(f)
|
||||
file_name = file_name.split('/')[-1]
|
||||
if file_name.startswith('__'):
|
||||
file_name = file_name.split("/")[-1]
|
||||
if file_name.startswith("__"):
|
||||
continue
|
||||
# Skip base classes that shouldn't be loaded as commands
|
||||
if file_name.endswith('_base'):
|
||||
if file_name.endswith("_base"):
|
||||
continue
|
||||
command = self.env['runtime']['CommandManager'].load_file(
|
||||
root + '/' + f)
|
||||
tree.update({file_name + ' ' + _('Action'): command})
|
||||
command = self.env["runtime"]["CommandManager"].load_file(
|
||||
root + "/" + f
|
||||
)
|
||||
tree.update({file_name + " " + _("Action"): command})
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return tree # note we discontinue iteration trough os.walk
|
||||
|
||||
Reference in New Issue
Block a user