diff options
author | Alexei Sorokin <[email protected]> | 2016-08-22 23:56:13 +0300 |
---|---|---|
committer | Alexei Sorokin <[email protected]> | 2016-08-23 18:01:15 +0300 |
commit | 1c253ed41ff1f6f36173707d89070d36054db9f0 (patch) | |
tree | b65b729ddb985d01999c26e1cf7e4afd3d5b3427 /Mozo/MenuEditor.py | |
parent | cc776e061996e9b803cb08638d615e05df1efcb8 (diff) | |
download | mozo-1c253ed41ff1f6f36173707d89070d36054db9f0.tar.bz2 mozo-1c253ed41ff1f6f36173707d89070d36054db9f0.tar.xz |
encode with utf-8 before writing XML
based on:
https://github.com/GNOME/alacarte/commit/f7835d7
https://github.com/GNOME/alacarte/commit/0d7f351
https://github.com/GNOME/alacarte/commit/23f0318
Diffstat (limited to 'Mozo/MenuEditor.py')
-rw-r--r-- | Mozo/MenuEditor.py | 47 |
1 files changed, 33 insertions, 14 deletions
diff --git a/Mozo/MenuEditor.py b/Mozo/MenuEditor.py index 0e9652f..512e7ef 100644 --- a/Mozo/MenuEditor.py +++ b/Mozo/MenuEditor.py @@ -16,6 +16,7 @@ # 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 codecs import os import re import xml.dom.minidom @@ -68,9 +69,9 @@ class MenuEditor: def save(self, from_loading=False): for menu in ('applications', 'settings'): - fd = open(getattr(self, menu).path, 'w') - fd.write(re.sub("\n[\s]*([^\n<]*)\n[\s]*</", "\\1</", getattr(self, menu).dom.toprettyxml().replace('<?xml version="1.0" ?>\n', ''))) - fd.close() + xml = getattr(self, menu).dom.toprettyxml().replace('<?xml version="1.0" ?>\n', '') + with codecs.open(getattr(self, menu).path, 'w', 'utf-8') as f: + f.write(re.sub("\n[\s]*([^\n<]*)\n[\s]*</", "\\1</", xml)) if not from_loading: self.__loadMenus() @@ -123,13 +124,23 @@ class MenuEditor: for file_path in files: new_path = file_path.rsplit('.', 1)[0] redo_path = util.getUniqueRedoFile(new_path) - data = open(new_path).read() - open(redo_path, 'w').write(data) - data = open(file_path).read() - open(new_path, 'w').write(data) + + f_file_path = codecs.open(new_path, 'r', 'utf-8') + f_new_path = codecs.open(new_path, 'rw', 'utf-8') + f_redo_path = codecs.open(redo_path, 'rw', 'utf-8') + + data = f_new_path.read() + f_redo_path.write(data) + data = f_file_path.read() + f_new_path.write(data) + + f_file_path.close() + f_new_path.close() + f_redo_path.close() + os.unlink(file_path) redo.append(redo_path) - #reload DOM to make changes stick + # reload DOM to make changes stick for name in ('applications', 'settings'): menu = getattr(self, name) try: @@ -147,12 +158,18 @@ class MenuEditor: for file_path in files: new_path = file_path.rsplit('.', 1)[0] undo_path = util.getUniqueUndoFile(new_path) - data = open(new_path).read() - open(undo_path, 'w').write(data) - data = open(file_path).read() - open(new_path, 'w').write(data) + f_file_path = codecs.open(new_path, 'r', 'utf-8') + f_new_path = codecs.open(new_path, 'rw', 'utf-8') + f_undo_path = codecs.open(undo_path, 'rw', 'utf-8') + data = f_new_path.read() + f_undo_path.write(data) + data = f_file_path.read() + f_new_path.write(data) os.unlink(file_path) undo.append(undo_path) + f_file_path.close() + f_new_path.close() + f_undo_path.close() #reload DOM to make changes stick for name in ('applications', 'settings'): menu = getattr(self, name) @@ -428,9 +445,11 @@ class MenuEditor: file_path = item.get_desktop_file_path() else: continue - data = open(file_path).read() + with codecs.open(file_path, 'r', 'utf-8') as f: + data = f.read() undo_path = util.getUniqueUndoFile(file_path) - open(undo_path, 'w').write(data) + with codecs.open(undo_path, 'w', 'utf-8') as f: + f.write(data) self.__undo[-1].append(undo_path) def __getMenu(self, item): |