ticket-1249
Last change
on this file since 0dde203 was
0dde203,
checked in by Paul Kienzle <pkienzle@…>, 5 years ago
|
Fix model editor load/save for wx4. Refs #1260, #1273.
|
-
Property mode set to
100644
|
File size:
872 bytes
|
Rev | Line | |
---|
[0dde203] | 1 | import os |
---|
| 2 | import sys |
---|
| 3 | import io |
---|
| 4 | |
---|
| 5 | import wx.py.document |
---|
| 6 | |
---|
| 7 | def patch_py_editor(): |
---|
| 8 | """ |
---|
| 9 | Monkeypatch wx.py.document.Document with py2/3 compatible file I/O. |
---|
| 10 | |
---|
| 11 | Tested on py2 and py3 with wx 4.0.1. It works well enough for loading |
---|
| 12 | and saving from wx.py.editor, but it changes the interface to Document |
---|
| 13 | to only support unicode text, whereas the old version of Document in |
---|
| 14 | python 2.7 only supports bytes. |
---|
| 15 | """ |
---|
| 16 | wx.py.document.Document.read = read |
---|
| 17 | wx.py.document.Document.write = write |
---|
| 18 | |
---|
| 19 | def read(self): |
---|
| 20 | """Return contents of file.""" |
---|
| 21 | if self.filepath and os.path.exists(self.filepath): |
---|
| 22 | with io.open(self.filepath, 'r', encoding='utf8') as f: |
---|
| 23 | return f.read() |
---|
| 24 | else: |
---|
| 25 | return '' |
---|
| 26 | |
---|
| 27 | def write(self, text): |
---|
| 28 | """Write text to file.""" |
---|
| 29 | with io.open(self.filepath, 'w', encoding='utf8') as f: |
---|
| 30 | f.write(text) |
---|
Note: See
TracBrowser
for help on using the repository browser.