mirror of
https://github.com/wxWidgets/Phoenix.git
synced 2026-01-06 20:10:08 +01:00
Use codecs.open in textfile_open() for Py2.7 so it can work more like open() in Py3, auto-converting to/from utf-8 and unicode. Use textfile_open in parts of the sphinx command that were having encoding errors and also in writeIfChanged().
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@71564 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -651,15 +651,15 @@ def writeIfChanged(filename, text):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename):
|
||||||
fid = codecs.open(filename, 'r', 'utf-8')
|
f = textfile_open(filename, 'rt')
|
||||||
current = fid.read()
|
current = f.read()
|
||||||
fid.close()
|
f.close()
|
||||||
|
|
||||||
if current == text:
|
if current == text:
|
||||||
return
|
return
|
||||||
|
|
||||||
f = codecs.open(filename, 'w', 'utf-8')
|
f = textfile_open(filename, 'wt')
|
||||||
f.write(text.encode('utf-8'))
|
f.write(text)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
@@ -777,12 +777,13 @@ def myExecfile(filename, ns):
|
|||||||
|
|
||||||
def textfile_open(filename, mode='rt'):
|
def textfile_open(filename, mode='rt'):
|
||||||
"""
|
"""
|
||||||
Simple wrapper around open() that will open normally on Python 2.x and on
|
Simple wrapper around open() that will use codecs.open on Python 2.x and
|
||||||
Python 2.3 will add the encoding parameter. The mode parameter must
|
on Python 2.3 will add the encoding parameter to the normal open(). The
|
||||||
include the 't' to put the stream into text mode.
|
mode parameter must include the 't' to put the stream into text mode.
|
||||||
"""
|
"""
|
||||||
assert 't' in mode
|
assert 't' in mode
|
||||||
if sys.version_info < (3,):
|
if sys.version_info < (3,):
|
||||||
return open(filename, mode)
|
import codecs
|
||||||
|
return codecs.open(filename, mode, encoding='utf-8')
|
||||||
else:
|
else:
|
||||||
return open(filename, mode, encoding='utf-8')
|
return open(filename, mode, encoding='utf-8')
|
||||||
|
|||||||
@@ -80,35 +80,31 @@ def nci(text, numSpaces=0, stripLeading=True):
|
|||||||
|
|
||||||
#---------------------------------------------------------------------------
|
#---------------------------------------------------------------------------
|
||||||
|
|
||||||
if sys.version_info < (3,):
|
# io.StringIO reads/writes unicode objects for both Python 2.7 and 3.x. For
|
||||||
# For Python 2.x we'll convert any unicode text values to strings before
|
# 2.7 we'll convert any string values to unicode objects before storing them
|
||||||
# adding them to the buffer
|
# in the StringIO
|
||||||
from StringIO import StringIO
|
|
||||||
class Utf8EncodingStream(StringIO):
|
import io
|
||||||
def write(self, text):
|
class Utf8EncodingStream(io.StringIO):
|
||||||
if isinstance(text, unicode):
|
if sys.version_info < (3,):
|
||||||
text = text.encode('utf-8')
|
def write(self, text):
|
||||||
return StringIO.write(self, text)
|
if isinstance(text, str):
|
||||||
|
text = text.decode('utf-8')
|
||||||
|
return io.StringIO.write(self, text)
|
||||||
|
|
||||||
else:
|
|
||||||
# For Python 3.x we'll keep it all as str (unicode) objects and let the
|
|
||||||
# conversion to bytes happen when the text is written to the actual
|
|
||||||
# file.
|
|
||||||
from io import StringIO
|
|
||||||
class Utf8EncodingStream(StringIO):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def textfile_open(filename, mode='rt'):
|
def textfile_open(filename, mode='rt'):
|
||||||
"""
|
"""
|
||||||
Simple wrapper around open() that will open normally on Python 2.x and on
|
Simple wrapper around open() that will use codecs.open on Python 2.x and
|
||||||
Python 2.3 will add the encoding parameter. The mode parameter must
|
on Python 2.3 will add the encoding parameter to the normal open(). The
|
||||||
include the 't' to put the stream into text mode.
|
mode parameter must include the 't' to put the stream into text mode.
|
||||||
"""
|
"""
|
||||||
assert 't' in mode
|
assert 't' in mode
|
||||||
if sys.version_info < (3,):
|
if sys.version_info < (3,):
|
||||||
return open(filename, mode)
|
import codecs
|
||||||
|
return codecs.open(filename, mode, encoding='utf-8')
|
||||||
else:
|
else:
|
||||||
return open(filename, mode, encoding='utf-8')
|
return open(filename, mode, encoding='utf-8')
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,9 @@ class SipWrapperGenerator(generators.WrapperGeneratorBase):
|
|||||||
# Write the contents of the stream to the destination file
|
# Write the contents of the stream to the destination file
|
||||||
if not destFile:
|
if not destFile:
|
||||||
destFile = os.path.join(phoenixRoot, 'sip/gen', module.name + '.sip')
|
destFile = os.path.join(phoenixRoot, 'sip/gen', module.name + '.sip')
|
||||||
textfile_open(destFile, 'wt').write(stream.getvalue())
|
f = textfile_open(destFile, 'wt')
|
||||||
|
f.write(stream.getvalue())
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
#-----------------------------------------------------------------------
|
#-----------------------------------------------------------------------
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import subprocess
|
|||||||
|
|
||||||
# Phoenix-specific imports
|
# Phoenix-specific imports
|
||||||
import templates
|
import templates
|
||||||
from buildtools.config import copyIfNewer, writeIfChanged, newer, getSvnRev
|
from buildtools.config import copyIfNewer, writeIfChanged, newer, getSvnRev, textfile_open
|
||||||
|
|
||||||
from utilities import Wx2Sphinx
|
from utilities import Wx2Sphinx
|
||||||
from constants import HTML_REPLACE, TODAY, SPHINXROOT, SECTIONS_EXCLUDE
|
from constants import HTML_REPLACE, TODAY, SPHINXROOT, SECTIONS_EXCLUDE
|
||||||
@@ -112,7 +112,7 @@ def BuildEnumsAndMethods(sphinxDir):
|
|||||||
|
|
||||||
for input in textfiles:
|
for input in textfiles:
|
||||||
|
|
||||||
fid = open(input, 'rt')
|
fid = textfile_open(input, 'rt')
|
||||||
orig_text = text = fid.read()
|
orig_text = text = fid.read()
|
||||||
fid.close()
|
fid.close()
|
||||||
|
|
||||||
@@ -166,7 +166,7 @@ def BuildEnumsAndMethods(sphinxDir):
|
|||||||
text = TooltipsOnInheritance(text, class_summary)
|
text = TooltipsOnInheritance(text, class_summary)
|
||||||
|
|
||||||
if text != orig_text:
|
if text != orig_text:
|
||||||
fid = open(input, 'wt')
|
fid = textfile_open(input, 'wt')
|
||||||
fid.write(text)
|
fid.write(text)
|
||||||
fid.close()
|
fid.close()
|
||||||
|
|
||||||
@@ -185,7 +185,7 @@ def BuildEnumsAndMethods(sphinxDir):
|
|||||||
keys = unreferenced_classes.keys()
|
keys = unreferenced_classes.keys()
|
||||||
keys.sort()
|
keys.sort()
|
||||||
|
|
||||||
fid = open(os.path.join(SPHINXROOT, 'unreferenced_classes.inc'), 'wt')
|
fid = textfile_open(os.path.join(SPHINXROOT, 'unreferenced_classes.inc'), 'wt')
|
||||||
fid.write('\n')
|
fid.write('\n')
|
||||||
fid.write('='*50 + ' ' + '='*50 + '\n')
|
fid.write('='*50 + ' ' + '='*50 + '\n')
|
||||||
fid.write('%-50s %-50s\n'%('Reference', 'File Name(s)'))
|
fid.write('%-50s %-50s\n'%('Reference', 'File Name(s)'))
|
||||||
|
|||||||
Reference in New Issue
Block a user