[orca] Braille: Attempt to be smarter about the default contraction table



commit 5a0f7d6b61ed12a816ea85c2b525e9c6f843a570
Author: Joanmarie Diggs <jdiggs igalia com>
Date:   Thu Jan 7 19:06:41 2021 +0100

    Braille: Attempt to be smarter about the default contraction table
    
    * Match the active locale
    * Don't prefer tables like interline and mathtext
    * Prefer g1 then g2 then comp6 then comp8 (here the goal is maximize
      saneness for a brand-new install for a user whose braille level is
      unknown and might be minimal)
    
    Note: This won't impact existing configurations.
    
    Fixes #150.

 src/orca/braille.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 48 insertions(+), 7 deletions(-)
---
diff --git a/src/orca/braille.py b/src/orca/braille.py
index af1bfef3f..772ca400e 100644
--- a/src/orca/braille.py
+++ b/src/orca/braille.py
@@ -31,6 +31,7 @@ __date__      = "$Date$"
 __copyright__ = "Copyright (c) 2005-2009 Sun Microsystems Inc."
 __license__   = "LGPL"
 
+import locale
 import signal
 import os
 import re
@@ -241,19 +242,59 @@ def listTables():
     return tables
 
 def getDefaultTable():
-    # TODO: Why aren't we using the default for the locale??
+    userLocale = locale.getlocale(locale.LC_MESSAGES)[0]
+    msg = "BRAILLE: User locale is %s" % userLocale
+    debug.println(debug.LEVEL_INFO, msg, True)
+
+    if userLocale in (None, "C"):
+        userLocale = locale.getdefaultlocale()[0]
+        msg = "BRAILLE: Default locale is %s" % userLocale
+        debug.println(debug.LEVEL_INFO, msg, True)
+
+    if userLocale in (None, "C"):
+        msg = "BRAILLE: Locale cannot be determined. Falling back on 'en-us'"
+        debug.println(debug.LEVEL_INFO, msg, True)
+        language = "en-us"
+    else:
+        language = "-".join(userLocale.split("_")).lower()
+
     try:
-        for fname in os.listdir(tablesdir):
-            if fname[-4:] in (".utb", ".ctb"):
-                if fname.startswith("en-us"):
-                    return os.path.join(tablesdir, fname)
+        tables = [x for x in os.listdir(tablesdir) if x[-4:] in (".utb", ".ctb")]
     except OSError:
-        pass
+        msg = "BRAILLE: Exception calling os.listdir for %s" % tablesdir
+        debug.println(debug.LEVEL_INFO, msg, True)
+        return ""
+
+    # Some of the tables are probably not a good choice for default table....
+    exclude = ["interline", "mathtext"]
 
-    return ""
+    # Some of the tables might be a better default than others. For instance, someone who
+    # can read grade 2 braille presumably can read grade 1; the reverse is not necessarily
+    # true. Literary braille might be easier for some users to read than computer braille.
+    # We can adjust this based on user feedback, but in general the goal is a sane default
+    # for the largest group of users; not the perfect default for all users.
+    prefer = ["g1", "g2", "comp6", "comp8"]
+
+    isCandidate = lambda t: t.startswith(language) and not any(e in t for e in exclude)
+    tables = list(filter(isCandidate, tables))
+    msg = "BRAILLE: %i candidate tables for locale found: %s" % (len(tables), ", ".join(tables))
+    debug.println(debug.LEVEL_INFO, msg, True)
+
+    if not tables:
+        return ""
+
+    for p in prefer:
+        for table in tables:
+            if p in table:
+                return os.path.join(tablesdir, table)
+
+    # If we couldn't find a preferred match, just go with the first match for the locale.
+    return tables[0]
 
 if louis:
     _defaultContractionTable = getDefaultTable()
+    msg = "BRAILLE: Default contraction table is: %s" % _defaultContractionTable
+    debug.println(debug.LEVEL_INFO, msg, True)
 
 def _printBrailleEvent(level, command):
     """Prints out a Braille event.  The given level may be overridden


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]