diff options
author | Jasper St. Pierre <[email protected]> | 2012-06-01 01:30:55 -0400 |
---|---|---|
committer | Alexei Sorokin <[email protected]> | 2016-08-23 18:01:15 +0300 |
commit | cc776e061996e9b803cb08638d615e05df1efcb8 (patch) | |
tree | 304a9b445231f1672e212276624470f7639c1d1a | |
parent | 6c6595ddd7460a47771ac68ebb45a5c60022001b (diff) | |
download | mozo-cc776e061996e9b803cb08638d615e05df1efcb8.tar.bz2 mozo-cc776e061996e9b803cb08638d615e05df1efcb8.tar.xz |
MenuEditor: be resilient to bad XML
https://bugzilla.gnome.org/show_bug.cgi?id=654167
-rw-r--r-- | Mozo/MenuEditor.py | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/Mozo/MenuEditor.py b/Mozo/MenuEditor.py index 66e4d74..0e9652f 100644 --- a/Mozo/MenuEditor.py +++ b/Mozo/MenuEditor.py @@ -16,7 +16,11 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -import os, re, xml.dom.minidom, locale +import os +import re +import xml.dom.minidom +import xml.parsers.expat +import locale import matemenu from Mozo import util @@ -42,10 +46,10 @@ class MenuEditor: self.applications.tree.sort_key = matemenu.SORT_DISPLAY_NAME self.applications.visible_tree.sort_key = matemenu.SORT_DISPLAY_NAME self.applications.path = os.path.join(util.getUserMenuPath(), self.applications.tree.get_menu_file()) - if not os.path.isfile(self.applications.path): - self.applications.dom = xml.dom.minidom.parseString(util.getUserMenuXml(self.applications.tree)) - else: + try: self.applications.dom = xml.dom.minidom.parse(self.applications.path) + except (IOError, xml.parsers.expat.ExpatError): + self.applications.dom = xml.dom.minidom.parseString(util.getUserMenuXml(self.applications.tree)) self.__remove_whilespace_nodes(self.applications.dom) self.settings = Menu() @@ -54,10 +58,10 @@ class MenuEditor: self.settings.tree.sort_key = matemenu.SORT_DISPLAY_NAME self.settings.visible_tree.sort_key = matemenu.SORT_DISPLAY_NAME self.settings.path = os.path.join(util.getUserMenuPath(), self.settings.tree.get_menu_file()) - if not os.path.isfile(self.settings.path): - self.settings.dom = xml.dom.minidom.parseString(util.getUserMenuXml(self.settings.tree)) - else: + try: self.settings.dom = xml.dom.minidom.parse(self.settings.path) + except (IOError, xml.parsers.expat.ExpatError): + self.settings.dom = xml.dom.minidom.parseString(util.getUserMenuXml(self.settings.tree)) self.__remove_whilespace_nodes(self.settings.dom) self.save(True) @@ -94,10 +98,10 @@ class MenuEditor: except OSError: pass #reload DOM for each menu - if not os.path.isfile(menu.path): - menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) - else: + try: menu.dom = xml.dom.minidom.parse(menu.path) + except (IOError, xml.parsers.expat.ExpatError): + menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) self.__remove_whilespace_nodes(menu.dom) #reset undo/redo, no way to recover from this self.__undo, self.__redo = [], [] @@ -128,10 +132,10 @@ class MenuEditor: #reload DOM to make changes stick for name in ('applications', 'settings'): menu = getattr(self, name) - if not os.path.isfile(menu.path): - menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) - else: + try: menu.dom = xml.dom.minidom.parse(menu.path) + except (IOError, xml.parsers.expat.ExpatError): + menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) self.__remove_whilespace_nodes(menu.dom) self.__redo.append(redo) @@ -152,10 +156,10 @@ class MenuEditor: #reload DOM to make changes stick for name in ('applications', 'settings'): menu = getattr(self, name) - if not os.path.isfile(menu.path): - menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) - else: + try: menu.dom = xml.dom.minidom.parse(menu.path) + except (IOError, xml.parsers.expat.ExpatError): + menu.dom = xml.dom.minidom.parseString(util.getUserMenuXml(menu.tree)) self.__remove_whilespace_nodes(menu.dom) self.__undo.append(undo) |