[orca] Fix for bug 658781 - Some text not spoken at all since gobject introspection changes in orca 3.1.9x.



commit afc8a0d2990c517aa1e5f7927194a96997d8da5a
Author: Joanmarie Diggs <joanmarie diggs gmail com>
Date:   Wed Sep 14 12:09:03 2011 -0400

    Fix for bug 658781 - Some text not spoken at all since gobject introspection changes in orca 3.1.9x.

 src/orca/debug.py                   |    6 ++++
 src/orca/generator.py               |    2 +-
 src/orca/pronunciation_dict.py      |   24 +++++++---------
 src/orca/script_utilities.py        |   52 ++++++++++++++++++++++++-----------
 src/orca/speechdispatcherfactory.py |    7 ++++-
 5 files changed, 59 insertions(+), 32 deletions(-)
---
diff --git a/src/orca/debug.py b/src/orca/debug.py
index 7eba99d..dd75aa8 100644
--- a/src/orca/debug.py
+++ b/src/orca/debug.py
@@ -202,12 +202,18 @@ def println(level, text = ""):
             except TypeError:
                 text = "TypeError when trying to write text"
                 debugFile.writelines([text, "\n"])
+            except UnicodeDecodeError:
+                text = "UnicodeDecodeError when trying to write text"
+                debugFile.writelines([text, "\n"])
         else:
             try:
                 sys.stderr.writelines([text, "\n"])
             except TypeError:
                 text = "TypeError when trying to write text"
                 sys.stderr.writelines([text, "\n"])
+            except UnicodeDecodeError:
+                text = "UnicodeDecodeError when trying to write text"
+                debugFile.writelines([text, "\n"])
 
 def printObjectEvent(level, event, sourceInfo=None):
     """Prints out an Python Event object.  The given level may be
diff --git a/src/orca/generator.py b/src/orca/generator.py
index 79ccee9..77e411a 100644
--- a/src/orca/generator.py
+++ b/src/orca/generator.py
@@ -264,7 +264,7 @@ class Generator:
         debug.println(debug.LEVEL_ALL, "COMPLETION  TIME: %s" % duration)
         debug.println(debug.LEVEL_ALL, "generate %s results:" % self._mode)
         for element in result:
-            debug.println(debug.LEVEL_ALL, "  " + str(element))
+            debug.println(debug.LEVEL_ALL, "  %s" % element)
 
         return result
 
diff --git a/src/orca/pronunciation_dict.py b/src/orca/pronunciation_dict.py
index 1b9d9d7..5ea735b 100644
--- a/src/orca/pronunciation_dict.py
+++ b/src/orca/pronunciation_dict.py
@@ -28,7 +28,11 @@ __license__   = "LGPL"
 
 def getPronunciation(word, pronunciations=None):
     """Given a word, return a string that represents what this word
-    sounds like.
+    sounds like. Note: This code does not handle the pronunciation
+    of character names. If you want a character name to be spoken,
+    treat it as a punctuation character at LEVEL_NONE in
+    puncutation_settings.py. See, for example, the left_arrow and
+    right_arrow characters.
 
     Arguments:
     - word: the word to get the "sounds like" representation for.
@@ -42,19 +46,11 @@ def getPronunciation(word, pronunciations=None):
     if isinstance(word, unicode):
         word = word.encode("UTF-8")
 
-    try:
-        lowerWord = word.decode("UTF-8").lower().encode("UTF-8")
-        if pronunciations != None:
-            return pronunciations[lowerWord][1]
-        else:
-            return pronunciation_dict[lowerWord][1]
-    except:
-        # If you want a character name to be spoken, treat it as a
-        # punctuation character at LEVEL_NONE in puncutation_settings.py.
-        # See, for example, the left_arrow and right_arrow characters.
-        #
-        #return chnames.getCharacterName(word)
-        return word
+    lowerWord = word.decode("UTF-8").lower().encode("UTF-8")
+    dictionary = pronunciations or pronunciation_dict
+    entry = dictionary.get(lowerWord, [word, word])
+
+    return entry[1]
 
 def setPronunciation(word, replacementString, pronunciations=None):
     """Given an actual word, and a replacement string, set a key/value
diff --git a/src/orca/script_utilities.py b/src/orca/script_utilities.py
index 3ff2675..64dfde7 100644
--- a/src/orca/script_utilities.py
+++ b/src/orca/script_utilities.py
@@ -2374,7 +2374,13 @@ class Utilities:
 
         newSegment = pronunciation_dict.getPronunciation(
             segment, self._script.app_pronunciation_dict)
-        if newSegment == segment:
+
+        try:
+            unchanged = newSegment == segment
+        except UnicodeEncodeError, UnicodeDecodeError:
+            unchanged = True
+
+        if unchanged:
             newSegment = pronunciation_dict.getPronunciation(segment)
 
         return newSegment
@@ -2393,7 +2399,11 @@ class Utilities:
 
         import punctuation_settings
 
-        line = line.decode("UTF-8")
+        try:
+            line = line.decode("UTF-8")
+        except UnicodeEncodeError:
+            pass
+
         endOffset = startOffset + len(line)
 
         try:
@@ -2453,18 +2463,14 @@ class Utilities:
             pass
 
         words = self.WORDS_RE.split(line)
-        for word in words:
-            if word.isalnum():
-                word = self._pronunciationForSegment(word)
-            newLine += word
+        newLine = ' '.join(map(self._pronunciationForSegment, words))
 
-        if line != newLine:
-            debug.println(debug.LEVEL_FINEST,
-                          "adjustForPronunciation: \n  From '%s'\n  To   '%s'" \
-                          % (line, newLine))
-            return newLine.encode("UTF-8")
-        else:
-            return line
+        try:
+            newLine = newLine.encode("UTF-8")
+        except UnicodeDecodeError:
+            pass
+
+        return newLine
 
     def adjustForRepeats(self, line):
         """Adjust line to include repeat character counts. As some people
@@ -2519,7 +2525,12 @@ class Utilities:
         #
         # pylint: disable-msg=E1103
 
-        return newLine.encode("UTF-8")
+        try:
+            newLine = newLine.encode("UTF-8")
+        except UnicodeDecodeError:
+            pass
+
+        return newLine
 
     def adjustForDigits(self, string):
         """Adjusts the string to convert digit-like text, such as subscript
@@ -2531,7 +2542,11 @@ class Utilities:
         Returns: a new string which contains actual digits.
         """
 
-        uString = string.decode("UTF-8")
+        try:
+            uString = string.decode("UTF-8")
+        except UnicodeEncodeError:
+            uString = string
+
         subscripted = set(re.findall(self.SUBSCRIPTS_RE, uString))
         superscripted = set(re.findall(self.SUPERSCRIPTS_RE, uString))
 
@@ -2555,7 +2570,12 @@ class Utilities:
             newString = _(" subscript %s") % "".join(new)
             uString = re.sub(number, newString, uString)
 
-        return uString.encode("UTF-8")
+        try:
+            uString = uString.encode("UTF-8")
+        except UnicodeDecodeError:
+            pass
+
+        return uString
 
     @staticmethod
     def absoluteMouseCoordinates():
diff --git a/src/orca/speechdispatcherfactory.py b/src/orca/speechdispatcherfactory.py
index 7373d9b..ae3fd83 100644
--- a/src/orca/speechdispatcherfactory.py
+++ b/src/orca/speechdispatcherfactory.py
@@ -311,7 +311,12 @@ class SpeechServer(speechserver.SpeechServer):
         if orca_state.activeScript:
             newText = orca_state.activeScript.utilities.adjustForDigits(newText)
 
-        return newText.encode("UTF-8")
+        try:
+            newText = newText.encode("UTF-8")
+        except UnicodeDecodeError:
+            pass
+
+        return newText
 
     def _speak(self, text, acss, **kwargs):
         if isinstance(text, ACSS):



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