Skip to content

Commit

Permalink
WIP: Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AAClause committed Oct 25, 2020
1 parent cd1ee8d commit 348a6f7
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 234 deletions.
74 changes: 37 additions & 37 deletions addon/globalPlugins/brailleExtender/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
from . import utils
from .updateCheck import *
from . import advancedInputMode
from . import brailleTablesExt
from . import dictionaries
from . import tabledictionaries
from . import tablegroups
from . import huc
from . import patches
from . import settings
Expand Down Expand Up @@ -204,7 +204,7 @@ def __init__(self):
if config.conf["brailleExtender"]["reverseScrollBtns"]: self.reverseScrollBtns()
self.createMenu()
advancedInputMode.initialize()
brailleTablesExt.initializeGroups()
tablegroups.initializeGroups()
log.info(f"{addonName} {addonVersion} loaded ({round(time.time()-startTime, 2)}s)")

def event_gainFocus(self, obj, nextHandler):
Expand Down Expand Up @@ -262,13 +262,13 @@ def createMenu(self):
lambda event: wx.CallAfter(gui.mainFrame._popupSettingsDialog, settings.AddonSettingsDialog),
item
)
dictionariesMenu = wx.Menu()
self.submenu.AppendSubMenu(dictionariesMenu, _("Table &dictionaries"), _("'Braille dictionaries' menu"))
item = dictionariesMenu.Append(wx.ID_ANY, _("&Global dictionary"), _("A dialog where you can set global dictionary by adding dictionary entries to the list."))
tabledictionariesMenu = wx.Menu()
self.submenu.AppendSubMenu(tabledictionariesMenu, _("Table &dictionaries"), _("'Braille dictionaries' menu"))
item = tabledictionariesMenu.Append(wx.ID_ANY, _("&Global dictionary"), _("A dialog where you can set global dictionary by adding dictionary entries to the list."))
gui.mainFrame.sysTrayIcon.Bind(wx.EVT_MENU, self.onDefaultDictionary, item)
item = dictionariesMenu.Append(wx.ID_ANY, _("&Table dictionary"), _("A dialog where you can set table-specific dictionary by adding dictionary entries to the list."))
item = tabledictionariesMenu.Append(wx.ID_ANY, _("&Table dictionary"), _("A dialog where you can set table-specific dictionary by adding dictionary entries to the list."))
gui.mainFrame.sysTrayIcon.Bind(wx.EVT_MENU, self.onTableDictionary, item)
item = dictionariesMenu.Append(wx.ID_ANY, _("Te&mporary dictionary"), _("A dialog where you can set temporary dictionary by adding dictionary entries to the list."))
item = tabledictionariesMenu.Append(wx.ID_ANY, _("Te&mporary dictionary"), _("A dialog where you can set temporary dictionary by adding dictionary entries to the list."))
gui.mainFrame.sysTrayIcon.Bind(wx.EVT_MENU, self.onTemporaryDictionary, item)

item = self.submenu.Append(wx.ID_ANY, _("Advanced &input mode dictionary..."), _("Advanced input mode configuration"))
Expand Down Expand Up @@ -296,25 +296,25 @@ def createMenu(self):
self.submenu_item = gui.mainFrame.sysTrayIcon.menu.InsertMenu(2, wx.ID_ANY, "%s (%s)" % (_("&Braille Extender"), addonVersion), self.submenu)

def reloadBrailleTables(self):
dictionaries.setDictTables()
dictionaries.notifyInvalidTables()
tabledictionaries.setDictTables()
tabledictionaries.notifyInvalidTables()
if config.conf["brailleExtender"]["tabSpace"]:
liblouisDef = r"always \t " + ("0-" * configBE.getTabSize()).strip('-')
patches.louis.compileString(patches.getCurrentBrailleTables(), bytes(liblouisDef, "ASCII"))
undefinedChars.setUndefinedChar()

@staticmethod
def onDefaultDictionary(evt):
gui.mainFrame._popupSettingsDialog(dictionaries.DictionaryDlg, _("Global dictionary"), "default")
gui.mainFrame._popupSettingsDialog(tabledictionaries.DictionaryDlg, _("Global dictionary"), "default")

@staticmethod
def onTableDictionary(evt):
outTable = brailleTablesExt.listTablesDisplayName()[brailleTablesExt.listTablesFileName().index(config.conf["braille"]["translationTable"])]
gui.mainFrame._popupSettingsDialog(dictionaries.DictionaryDlg, _("Table dictionary ({})").format(outTable), "table")
outTable = tablegroups.listTablesDisplayName()[tablegroups.listTablesFileName().index(config.conf["braille"]["translationTable"])]
gui.mainFrame._popupSettingsDialog(tabledictionaries.DictionaryDlg, _("Table dictionary ({})").format(outTable), "table")

@staticmethod
def onTemporaryDictionary(evt):
gui.mainFrame._popupSettingsDialog(dictionaries.DictionaryDlg, _("Temporary dictionary"), "tmp")
gui.mainFrame._popupSettingsDialog(tabledictionaries.DictionaryDlg, _("Temporary dictionary"), "tmp")

def restorReviewCursorTethering(self):
if not self.switchedMode: return
Expand Down Expand Up @@ -618,7 +618,7 @@ def script_reportExtraInfos(self, gesture):

def script_getTableOverview(self, gesture):
inTable = brailleInput.handler.table.displayName
ouTable = brailleTablesExt.listTablesDisplayName()[brailleTablesExt.listTablesFileName().index(config.conf["braille"]["translationTable"])]
ouTable = tablegroups.listTablesDisplayName()[tablegroups.listTablesFileName().index(config.conf["braille"]["translationTable"])]
t = (_(" Input table")+": %s\n"+_("Output table")+": %s\n\n") % (inTable+' (%s)' % (brailleInput.handler.table.fileName), ouTable+' (%s)' % (config.conf["braille"]["translationTable"]))
t += utils.getTableOverview()
ui.browseableMessage("<pre>%s</pre>" % t, _("Table overview (%s)" % brailleInput.handler.table.displayName), True)
Expand Down Expand Up @@ -831,19 +831,19 @@ def script_decreaseDelayAutoScroll(self, gesture):
script_decreaseDelayAutoScroll.__doc__ = _("Decreases braille autoscroll delay")

def script_switchInputBrailleTable(self, gesture):
usableIn = brailleTablesExt.USABLE_INPUT
choices = brailleTablesExt.getPreferredTables()[0] + brailleTablesExt.getGroups(0)[0]
usageIn = tablegroups.USAGE_INPUT
choices = tablegroups.getPreferredTables()[0] + tablegroups.getGroups(0)[0]
if len(choices) < 2:
return ui.message(_("Please fill at least two tables and/or groups of tables for this feature first"))
newGroup = brailleTablesExt.getGroup(
position=brailleTablesExt.POSITION_NEXT,
usableIn=usableIn
newGroup = tablegroups.getGroup(
position=tablegroups.POSITION_NEXT,
usageIn=usageIn
)
res = brailleTablesExt.setTableOrGroup(
usableIn=usableIn,
res = tablegroups.setTableOrGroup(
usageIn=usageIn,
e=newGroup
)
table = brailleTablesExt.getTable(newGroup.members[0] if newGroup else config.conf["braille"]["inputTable"])
table = tablegroups.get_table_by_file_name(newGroup.members[0] if newGroup else config.conf["braille"]["inputTable"])
if table: brailleInput.handler._table = table
if not res: raise RuntimeError("error")
self.reloadBrailleTables()
Expand All @@ -854,37 +854,37 @@ def script_switchInputBrailleTable(self, gesture):
script_switchInputBrailleTable.__doc__ = _("Switch between your favorite input braille tables including groups")

def script_switchOutputBrailleTable(self, gesture):
usableIn = brailleTablesExt.USABLE_OUTPUT
choices = brailleTablesExt.getPreferredTables()[1] + brailleTablesExt.getGroups(0)[1]
usageIn = tablegroups.USAGE_OUTPUT
choices = tablegroups.getPreferredTables()[1] + tablegroups.getGroups(0)[1]
if len(choices) < 2:
return ui.message(_("Please fill at least two tables and/or groups of tables for this feature first"))
newGroup = brailleTablesExt.getGroup(
position=brailleTablesExt.POSITION_NEXT,
usableIn=usableIn
newGroup = tablegroups.getGroup(
position=tablegroups.POSITION_NEXT,
usageIn=usageIn
)
res = brailleTablesExt.setTableOrGroup(
usableIn=usableIn,
res = tablegroups.setTableOrGroup(
usageIn=usageIn,
e=newGroup
)
if not res: raise RuntimeError("error")
self.reloadBrailleTables()
utils.refreshBD()
dictionaries.setDictTables()
desc = (newGroup.name + (" (%s)" % _("group") if len(newGroup.members) > 1 else '') if newGroup else (_("Default") + " (%s)" % brailleTablesExt.fileName2displayName([config.conf["braille"]["translationTable"]])[0]))
desc = (newGroup.name + (" (%s)" % _("group") if len(newGroup.members) > 1 else '') if newGroup else (_("Default") + " (%s)" % tablegroups.fileName2displayName([config.conf["braille"]["translationTable"]])[0]))
ui.message(_("Output: %s") % desc)

script_switchOutputBrailleTable.__doc__ = _("Switch between your favorite output braille tables including groups")

def script_currentBrailleTable(self, gesture):
inTable = None
ouTable = None
if brailleTablesExt.groupEnabled():
i = brailleTablesExt.getGroup(brailleTablesExt.USABLE_INPUT)
o = brailleTablesExt.getGroup(brailleTablesExt.USABLE_OUTPUT)
if tablegroups.groupEnabled():
i = tablegroups.getGroup(tablegroups.USAGE_INPUT)
o = tablegroups.getGroup(tablegroups.USAGE_OUTPUT)
if i: inTable = i.name
if o: ouTable = o.name
if not inTable: inTable = brailleInput.handler.table.displayName
if not ouTable: ouTable = brailleTablesExt.listTablesDisplayName()[brailleTablesExt.listTablesFileName().index(config.conf["braille"]["translationTable"])]
if not ouTable: ouTable = tablegroups.listTablesDisplayName()[tablegroups.listTablesFileName().index(config.conf["braille"]["translationTable"])]
if ouTable == inTable:
braille.handler.message(_("I⣿O:{I}").format(I=inTable, O=ouTable))
speech.speakMessage(_("Input and output: {I}.").format(I=inTable, O=ouTable))
Expand Down Expand Up @@ -1268,7 +1268,7 @@ def script_autoTest(self, gesture):

def script_addDictionaryEntry(self, gesture):
curChar = utils.getCurrentChar()
gui.mainFrame._popupSettingsDialog(dictionaries.DictionaryEntryDlg, title=_("Add dictionary entry or see a dictionary"), textPattern=curChar, specifyDict=True)
gui.mainFrame._popupSettingsDialog(tabledictionaries.DictionaryEntryDlg, title=_("Add dictionary entry or see a dictionary"), textPattern=curChar, specifyDict=True)
script_addDictionaryEntry.__doc__ = _("Adds an entry in braille dictionary")

def script_toggle_blank_line_scroll(self, gesture):
Expand Down Expand Up @@ -1318,7 +1318,7 @@ def terminate(self):
self.autoScrollTimer.Stop()
config.conf["braille"]["showCursor"] = self.backupShowCursor
if self.autoTestPlayed: self.autoTestTimer.Stop()
dictionaries.removeTmpDict()
tabledictionaries.removeTmpDict()
advancedInputMode.terminate()
super().terminate()

Expand Down
1 change: 0 additions & 1 deletion addon/globalPlugins/brailleExtender/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
from . import advancedInputMode
from . import brailleRegionHelper
from . import configBE
from . import dictionaries
from . import huc
from . import undefinedChars
from .oneHandMode import process as processOneHandMode
Expand Down
4 changes: 2 additions & 2 deletions addon/globalPlugins/brailleExtender/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import ui
addonHandler.initTranslation()

from .brailleTablesExt import SettingsDlg as BrailleTablesDlg
from .tablegroups import SettingsDlg as BrailleTablesDlg
from . import configBE
from . import utils
from .advancedInputMode import SettingsDlg as AdvancedInputModeDlg
Expand Down Expand Up @@ -481,7 +481,7 @@ class AddonSettingsDialog(gui.settingsDialogs.MultiCategorySettingsDialog):
categoryClasses=[
GeneralDlg,
AttribraDlg,
BrailleTablesDlg,
#BrailleTablesDlg,
UndefinedCharsDlg,
AdvancedInputModeDlg,
OneHandModeDlg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from logHandler import log
from . import huc
from . import brailleTablesExt
from . import tablegroups
from .common import configDir
from collections import namedtuple
import louis
Expand Down Expand Up @@ -62,19 +62,19 @@ def checkTable(path):
return True


def getValidPathsDict(usableIn):
def getValidPathsDict(usageIn):
types = ["tmp", "table", "default"]
paths = [getPathDict(type_, usableIn) for type_ in types]
paths = [getPathDict(type_, usageIn) for type_ in types]
def valid(path): return os.path.exists(
path) and os.path.isfile(path) and checkTable(path)
return [path for path in paths if valid(path)]


def getPathDict(type_, usableIn="io"):
groupEnabled = brailleTablesExt.groupEnabled()
g = brailleTablesExt.getGroup(usableIn=usableIn)
def getPathDict(type_, usageIn="io"):
groupEnabled = tablegroups.groupEnabled()
g = tablegroups.getGroup(usageIn=usageIn)
table = os.path.join(configDir, "brailleDicts", config.conf["braille"]["inputTable"]
) if usableIn == brailleTablesExt.USABLE_INPUT else config.conf["braille"]["translationTable"]
) if usageIn == tablegroups.USAGE_INPUT else config.conf["braille"]["translationTable"]
if type_ == "table":
if groupEnabled and g and g.members:
if len(g.members) == 1:
Expand Down Expand Up @@ -135,8 +135,8 @@ def saveDict(type_, dict_):

def setDictTables():
global inputTables, outTable
inputTables = getValidPathsDict(brailleTablesExt.USABLE_INPUT)
outputTables = getValidPathsDict(brailleTablesExt.USABLE_OUTPUT)
inputTables = getValidPathsDict(tablegroups.USAGE_INPUT)
outputTables = getValidPathsDict(tablegroups.USAGE_OUTPUT)
invalidTables.clear()


Expand Down Expand Up @@ -362,7 +362,7 @@ def __init__(self, parent=None, title=_("Edit Dictionary Entry"), textPattern=''
if specifyDict:
# Translators: This is a label for an edit field in add dictionary entry dialog.
dictText = _("Dictionary")
outTable = brailleTablesExt.fileName2displayName(
outTable = tablegroups.fileName2displayName(
config.conf["braille"]["translationTable"])
dictChoices = [_("Global"), _(
"Table ({})").format(outTable), _("Temporary")]
Expand Down Expand Up @@ -418,7 +418,7 @@ def __init__(self, parent=None, title=_("Edit Dictionary Entry"), textPattern=''
self.Bind(wx.EVT_BUTTON, self.onOk, id=wx.ID_OK)

def onSeeEntriesClick(self, evt):
outTable = brailleTablesExt.fileName2displayName(
outTable = tablegroups.fileName2displayName(
config.conf["braille"]["translationTable"])
label = [_("Global dictionary"), _("Table dictionary ({})").format(
outTable), _("Temporary dictionary")][self.dictRadioBox.GetSelection()]
Expand Down
Loading

0 comments on commit 348a6f7

Please sign in to comment.