|
1 | 1 | # -*- coding: UTF-8 -*- |
2 | 2 | # A part of NonVisual Desktop Access (NVDA) |
3 | | -# Copyright (C) 2007-2022 NV Access Limited, Peter Vágner, Mesar Hameed, Joseph Lee, |
| 3 | +# Copyright (C) 2007-2023 NV Access Limited, Peter Vágner, Mesar Hameed, Joseph Lee, |
4 | 4 | # Aaron Cannon, Ethan Holliger, Julien Cochuyt, Thomas Stivers, Cyrille Bougot, Aleksey Sadovoy |
5 | 5 | # This file is covered by the GNU General Public License. |
6 | 6 | # See the file COPYING for more details. |
7 | 7 |
|
| 8 | +from re import error as RegexpError |
| 9 | + |
8 | 10 | from abc import abstractmethod |
9 | 11 | import wx |
10 | 12 |
|
@@ -94,25 +96,44 @@ def onOk(self, evt): |
94 | 96 | ) |
95 | 97 | self.patternTextCtrl.SetFocus() |
96 | 98 | return |
| 99 | + entryType = self.getType() |
97 | 100 | try: |
98 | 101 | dictEntry = self.dictEntry = speechDictHandler.SpeechDictEntry( |
99 | 102 | self.patternTextCtrl.GetValue(), |
100 | 103 | self.replacementTextCtrl.GetValue(), |
101 | 104 | self.commentTextCtrl.GetValue(), |
102 | 105 | bool(self.caseSensitiveCheckBox.GetValue()), |
103 | | - self.getType() |
| 106 | + entryType, |
104 | 107 | ) |
| 108 | + except RegexpError as e: |
| 109 | + log.debugWarning(f"Could not add dictionary entry due to regex error in the pattern field : {e}") |
| 110 | + if entryType != speechDictHandler.ENTRY_TYPE_REGEXP: |
| 111 | + raise e |
| 112 | + gui.messageBox( |
| 113 | + # Translators: This is an error message to let the user know that the dictionary entry is not valid. |
| 114 | + _("Regular Expression error in the pattern field: \"{error}\".").format(error=e), |
| 115 | + # Translators: The title of an error message raised by the Dictionary Entry dialog |
| 116 | + _("Dictionary Entry Error"), |
| 117 | + wx.OK | wx.ICON_WARNING, |
| 118 | + self |
| 119 | + ) |
| 120 | + self.patternTextCtrl.SetFocus() |
| 121 | + return |
| 122 | + try: |
105 | 123 | dictEntry.sub("test") # Ensure there are no grouping error (#11407) |
106 | | - except Exception as e: |
107 | | - log.debugWarning("Could not add dictionary entry due to (regex error) : %s" % e) |
| 124 | + except RegexpError as e: |
| 125 | + log.debugWarning(f"Could not add dictionary entry due to regex error in the replacement field : {e}") |
| 126 | + if entryType != speechDictHandler.ENTRY_TYPE_REGEXP: |
| 127 | + raise e |
108 | 128 | gui.messageBox( |
109 | 129 | # Translators: This is an error message to let the user know that the dictionary entry is not valid. |
110 | | - _("Regular Expression error: \"%s\".") % e, |
| 130 | + _("Regular Expression error in the replacement field: \"{error}\".").format(error=e), |
111 | 131 | # Translators: The title of an error message raised by the Dictionary Entry dialog |
112 | 132 | _("Dictionary Entry Error"), |
113 | 133 | wx.OK | wx.ICON_WARNING, |
114 | 134 | self |
115 | 135 | ) |
| 136 | + self.replacementTextCtrl.SetFocus() |
116 | 137 | return |
117 | 138 | evt.Skip() |
118 | 139 |
|
|
0 commit comments