fenrir/src/fenrirscreenreader/core/vmenuManager.py

221 lines
7.3 KiB
Python
Raw Normal View History

2019-01-21 18:16:41 -05:00
#!/bin/python
# -*- coding: utf-8 -*-
# Fenrir TTY screen reader
# By Chrys, Storm Dragon, and contributers.
from fenrirscreenreader.core import debug
2019-02-04 13:05:31 -05:00
from fenrirscreenreader.utils import module_utils
2019-02-04 13:39:36 -05:00
import os, inspect
2019-02-04 13:05:31 -05:00
2019-02-04 13:39:36 -05:00
currentdir = os.path.dirname(os.path.realpath(os.path.abspath(inspect.getfile(inspect.currentframe()))))
fenrirPath = os.path.dirname(currentdir)
2019-01-21 18:16:41 -05:00
class vmenuManager():
def __init__(self):
2019-01-21 18:37:07 -05:00
self.menuDict = {}
self.currIndex = None
2019-01-22 18:33:11 -05:00
self.currMenu = ''
2019-01-21 18:37:07 -05:00
self.active = False
2019-01-21 18:16:41 -05:00
def initialize(self, environment):
self.env = environment
2019-02-04 15:56:36 -05:00
self.defaultVMenuPath = fenrirPath+ "/commands/vmenu-profiles/" + self.env['runtime']['inputManager'].getShortcutType()
2019-01-21 18:16:41 -05:00
def shutdown(self):
2019-01-21 18:37:07 -05:00
pass
2019-01-28 17:29:49 -05:00
def setCurrMenu(self, currMenu = ''):
2019-02-04 16:32:48 -05:00
if currMenu == '':
2019-01-28 17:29:49 -05:00
self.currIndex = None
self.currMenu = ''
2019-02-04 16:32:48 -05:00
else:
try:
t = self.menuDict[currMenu]
l = list(menuDict.keys())
except:
return
self.currIndex = [l.index(currMenu)]
self.currMenu = currMenu
2019-01-28 17:29:49 -05:00
def getCurrMenu(self):
return self.currMenu
2019-01-21 18:37:07 -05:00
def getActive(self):
return self.active
2019-01-28 18:03:31 -05:00
def togglelVMenuMode(self):
2019-01-28 17:29:49 -05:00
self.setActive(not self.getActive())
def setActive(self, active):
2019-01-21 18:37:07 -05:00
self.active = active
2019-02-03 18:19:21 -05:00
if self.active:
#try:
2019-01-28 18:03:31 -05:00
self.createMenuTree()
2019-02-03 18:19:21 -05:00
#except Exception as e:
# print(e)
try:
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'
except Exception as e:
print(e)
2019-01-21 18:16:41 -05:00
else:
try:
2019-01-22 18:33:11 -05:00
self.menuDict = {}
self.currIndex = None
self.currMenu = ''
2019-01-21 18:16:41 -05:00
del(self.env['bindings'][str([1, ['KEY_ESC']])])
del(self.env['bindings'][str([1, ['KEY_UP']])])
del(self.env['bindings'][str([1, ['KEY_DOWN']])])
del(self.env['bindings'][str([1, ['KEY_SPACE']])])
2019-01-22 18:33:11 -05:00
del(self.env['bindings'][str([1, ['KEY_LEFT']])])
2019-02-03 18:19:21 -05:00
del(self.env['bindings'][str([1, ['KEY_RIGHT']])])
del(self.env['bindings'][str([1, ['KEY_ENTER']])])
2019-01-21 18:16:41 -05:00
except:
2019-02-03 18:19:21 -05:00
pass
2019-01-22 18:33:11 -05:00
def createMenuTree(self):
2019-02-04 13:39:36 -05:00
self.currIndex = None
menu = self.fs_tree_to_dict( self.defaultVMenuPath)
2019-02-03 18:19:21 -05:00
if menu:
self.menuDict = menu
if len(self.menuDict) > 0:
self.currIndex = [0]
2019-01-22 18:33:11 -05:00
def executeMenu(self):
if self.currIndex == None:
2019-02-03 18:19:21 -05:00
return
2019-02-04 13:39:36 -05:00
try:
2019-02-04 15:21:10 -05:00
command = self.getValueByPath(self.menuDict, self.currIndex)
if not command == None:
command.run()
2019-02-04 15:56:36 -05:00
except Exception as e:
print(e)
2019-02-04 13:39:36 -05:00
self.incLevel()
2019-01-22 18:33:11 -05:00
def incLevel(self):
if self.currIndex == None:
2019-02-03 18:19:21 -05:00
return
2019-01-22 18:33:11 -05:00
try:
2019-01-29 17:19:49 -05:00
r = self.getValueByPath(self.menuDict, self.currIndex +[0])
2019-02-04 15:21:10 -05:00
print(r)
2019-01-22 18:33:11 -05:00
if r == {}:
return
except:
return
self.currIndex.append(0)
2019-02-04 13:39:36 -05:00
print(self.currIndex)
2019-01-22 18:33:11 -05:00
def decLevel(self):
2019-01-21 18:37:07 -05:00
if self.currIndex == None:
2019-02-04 13:39:36 -05:00
return
2019-02-04 16:32:48 -05:00
if self.currMenu != '':
if len(self.currIndex) == 2:
return
elif len(self.currIndex) == 1:
return
2019-02-04 16:26:51 -05:00
self.currIndex = self.currIndex[:len(self.currIndex) - 1]
2019-02-04 13:39:36 -05:00
print(self.currIndex)
2019-01-21 18:16:41 -05:00
def nextIndex(self):
2019-01-21 18:37:07 -05:00
if self.currIndex == None:
2019-02-04 13:39:36 -05:00
return
if self.currIndex[len(self.currIndex) - 1] + 1 >= len(self.getNestedByPath(self.menuDict, self.currIndex[:-1])):
2019-01-22 18:33:11 -05:00
self.currIndex[len(self.currIndex) - 1] = 0
2019-02-04 13:39:36 -05:00
else:
self.currIndex[len(self.currIndex) - 1] += 1
print(self.currIndex)
2019-01-21 18:16:41 -05:00
def prevIndex(self):
2019-01-21 18:37:07 -05:00
if self.currIndex == None:
2019-01-22 18:33:11 -05:00
return
2019-02-04 13:39:36 -05:00
if self.currIndex[len(self.currIndex) - 1] == 0:
2019-01-22 18:33:11 -05:00
self.currIndex[len(self.currIndex) - 1] = len(self.getNestedByPath(self.menuDict, self.currIndex[:-1])) - 1
2019-02-04 13:39:36 -05:00
else:
self.currIndex[len(self.currIndex) - 1] -= 1
print(self.currIndex)
2019-01-28 18:03:31 -05:00
def getCurrentEntry(self):
2019-02-04 13:39:36 -05:00
print( self.getKeysByPath(self.menuDict, self.currIndex)[self.currIndex[-1]])
2019-01-28 18:03:31 -05:00
return self.getKeysByPath(self.menuDict, self.currIndex)[self.currIndex[-1]]
2019-01-22 15:29:55 -05:00
def fs_tree_to_dict(self, path_):
for root, dirs, files in os.walk(path_):
2019-02-04 16:26:51 -05:00
tree = {d + ' ' + _('Menu'): self.fs_tree_to_dict(os.path.join(root, d)) for d in dirs if not d.startswith('__')}
2019-02-04 15:21:10 -05:00
for f in files:
try:
2019-02-04 16:26:51 -05:00
fileName, fileExtension = os.path.splitext(f)
fileName = fileName.split('/')[-1]
if fileName.startswith('__'):
continue
2019-02-04 15:21:10 -05:00
command = self.env['runtime']['commandManager'].loadFile(root + '/' + f)
2019-02-04 16:26:51 -05:00
tree.update({fileName + ' ' + _('Action'): command})
2019-02-04 15:21:10 -05:00
except Exception as e:
print(e)
2019-01-22 15:29:55 -05:00
return tree # note we discontinue iteration trough os.walk
2019-02-04 13:39:36 -05:00
def getNestedByPath(self, complete, path):
path = path.copy()
if path != []:
index = list(complete.keys())[path[0]]
2019-02-04 15:21:10 -05:00
nested = self.getNestedByPath(complete[index], path[1:])
2019-02-04 13:39:36 -05:00
return nested
else:
return complete
2019-01-22 18:33:11 -05:00
def getKeysByPath(self, complete, path):
if not isinstance(complete, dict):
return[]
d = complete
for i in path[:-1]:
d = d[list(d.keys())[i]]
return list(d.keys())
def getValueByPath(self, complete, path):
if not isinstance(complete, dict):
2019-02-04 16:26:51 -05:00
return complete
2019-01-22 18:33:11 -05:00
d = complete.copy()
for i in path:
d = d[list(d.keys())[i]]
return d
2019-01-29 17:19:49 -05:00
'''
2019-01-22 15:29:55 -05:00
import os
level = [0]
def fs_tree_to_dict( path_):
file_token = ''
for root, dirs, files in os.walk(path_):
tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs}
print(files, root, dirs)
2019-01-22 18:33:11 -05:00
tree.update({f: root + '/' + f for f in files})
2019-01-22 15:29:55 -05:00
return tree # note we discontinue iteration trough os.walk
2019-01-22 18:33:11 -05:00
v = fs_tree_to_dict( '/home/chrys/Projekte/fenrir/src/fenrirscreenreader/commands/vmenu/KEY')
def getNestedByPath(complete, path):
path = path.copy()
if path != []:
index = list(complete.keys())[path[0]]
path.remove(0)
nested = getNestedByPath(complete[index], path)
return nested
else:
return complete
def getKeysByPath(complete, path):
d = complete
for i in path[:-1]:
d = d[list(d.keys())[i]]
return list(d.keys())
def getValueByPath(complete, path):
d = complete
for i in path:
d = d[list(d.keys())[i]]
return d
c = [0,0,0]
2019-01-22 15:29:55 -05:00
2019-01-22 18:33:11 -05:00
getKeysByPath(v,c)
getValueByPath(v,c)
2019-01-22 15:29:55 -05:00
2019-01-29 17:19:49 -05:00
'''