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-01-22 15:29:55 -05:00
|
|
|
import os
|
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
|
|
|
|
def shutdown(self):
|
2019-01-21 18:37:07 -05:00
|
|
|
pass
|
2019-01-28 17:29:49 -05:00
|
|
|
def setCurrMenu(self, currMenu = ''):
|
|
|
|
try:
|
|
|
|
t = self.menuDict[currMenu]
|
|
|
|
l = list(menuDict.keys())
|
|
|
|
self.currIndex = [l.index(currMenu)]
|
|
|
|
self.currMenu = currMenu
|
|
|
|
except:
|
|
|
|
self.currIndex = None
|
|
|
|
self.currMenu = ''
|
|
|
|
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
|
|
|
|
if active:
|
2019-01-28 18:03:31 -05:00
|
|
|
self.createMenuTree()
|
2019-01-28 17:29:49 -05:00
|
|
|
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'
|
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 = ''
|
|
|
|
self.active = False
|
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']])])
|
|
|
|
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:
|
|
|
|
pass
|
2019-01-21 18:37:07 -05:00
|
|
|
|
2019-01-22 18:33:11 -05:00
|
|
|
|
|
|
|
def createMenuTree(self):
|
|
|
|
self.currIndex = None
|
|
|
|
self.menuDict = fs_tree_to_dict( '/home/chrys/Projekte/fenrir/src/fenrirscreenreader/commands/vmenu/KEY')
|
2019-01-21 18:37:07 -05:00
|
|
|
if len(self.menuDict) > 0:
|
2019-01-28 18:03:31 -05:00
|
|
|
self.currIndex = [0]
|
2019-01-22 18:33:11 -05:00
|
|
|
def executeMenu(self):
|
|
|
|
if self.currIndex == None:
|
|
|
|
return
|
|
|
|
def incLevel(self):
|
|
|
|
if self.currIndex == None:
|
|
|
|
return
|
|
|
|
try:
|
2019-01-29 17:19:49 -05:00
|
|
|
r = self.getValueByPath(self.menuDict, self.currIndex +[0])
|
2019-01-22 18:33:11 -05:00
|
|
|
if not r:
|
|
|
|
return
|
|
|
|
if not isinstance(r, dict):
|
|
|
|
return
|
|
|
|
if r == {}:
|
|
|
|
return
|
|
|
|
except:
|
|
|
|
return
|
|
|
|
self.currIndex.append(0)
|
|
|
|
def decLevel(self):
|
2019-01-21 18:37:07 -05:00
|
|
|
if self.currIndex == None:
|
2019-01-22 18:33:11 -05:00
|
|
|
return
|
|
|
|
if len(self.currIndex) == 1:
|
|
|
|
return
|
|
|
|
self.currIndex.remove(len(self.currIndex) - 1)
|
2019-01-21 18:16:41 -05:00
|
|
|
def nextIndex(self):
|
2019-01-21 18:37:07 -05:00
|
|
|
if self.currIndex == None:
|
2019-01-21 18:16:41 -05:00
|
|
|
return
|
2019-01-22 18:33:11 -05:00
|
|
|
self.currIndex[len(self.currIndex) - 1] += 1
|
|
|
|
if self.currIndex[len(self.currIndex) - 1] >= len(self.getNestedByPath(self.menuDict, self.currIndex[:-1])):
|
|
|
|
self.currIndex[len(self.currIndex) - 1] = 0
|
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-21 18:16:41 -05:00
|
|
|
return
|
2019-01-22 18:33:11 -05:00
|
|
|
if len(self.currIndex) - 1 < self.currLevel:
|
|
|
|
return
|
|
|
|
self.currIndex[len(self.currIndex) - 1] -= 1
|
|
|
|
if self.currIndex[len(self.currIndex) - 1] < 0:
|
|
|
|
self.currIndex[len(self.currIndex) - 1] = len(self.getNestedByPath(self.menuDict, self.currIndex[:-1])) - 1
|
2019-01-28 18:03:31 -05:00
|
|
|
def getCurrentEntry(self):
|
|
|
|
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_):
|
|
|
|
tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in 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-28 18:03:31 -05:00
|
|
|
|
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):
|
|
|
|
return None
|
|
|
|
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
|
|
|
'''
|