python3 compatibility fixes

This commit is contained in:
Stefan Klug 2015-11-03 20:55:21 +01:00
parent 65565c7b16
commit 07adc8ce4c
16 changed files with 52 additions and 62 deletions

View file

@ -69,7 +69,7 @@ class PodError(Exception):
'<%s:date>%s</%s:date><%s:p>' % \
(officeNs, dcNs, dcNs, dcNs,
time.strftime('%Y-%m-%dT%H:%M:%S'), dcNs, textNs))
buffer.dumpContent(message)
buffer.dumpContent(str(message))
buffer.write('</%s:p>' % textNs)
if dumpTb:
# We don't dump the traceback if it is an expression error (it is

View file

@ -222,7 +222,7 @@ class FileBuffer(Buffer):
def __init__(self, env, result):
Buffer.__init__(self, env, None)
self.result = result
self.content = file(result, 'w')
self.content = open(result, 'w', encoding='utf-8')
self.content.write(xmlPrologue)
# getLength is used to manage insertions into sub-buffers. But in the case
@ -231,10 +231,7 @@ class FileBuffer(Buffer):
def getLength(self): return 0
def write(self, something):
try:
self.content.write(something.encode('utf-8'))
except UnicodeDecodeError:
self.content.write(something)
self.content.write(something)
def addExpression(self, expression, tiedHook=None):
# At 2013-02-06, this method was not called within the whole test suite.
@ -674,7 +671,7 @@ class MemoryBuffer(Buffer):
# Find the start position of the deepest element to remove
deepestElem = self.action.elem.DEEPEST_TO_REMOVE
pos = self.content.find('<%s' % deepestElem.elem)
for index in self.elements.keys():
for index in list(self.elements.keys()):
if index < pos: del self.elements[index]
reTagContent = re.compile('<(?P<p>[\w-]+):(?P<f>[\w-]+)(.*?)>.*</(?P=p):' \

View file

@ -150,7 +150,7 @@ class Expression(PodElement):
if resultType == 'NoneType':
res = ''
elif resultType == 'str':
res = res.decode('utf-8')
pass
elif resultType == 'unicode':
pass # Don't perform any conversion, unicode is the target type.
elif resultType == 'Px':

View file

@ -38,7 +38,7 @@ class OdInsert:
styles). OdInsert instances define such 'inserts' (what to insert and
when).'''
def __init__(self, odtChunk, elem, nsUris={}):
self.odtChunk = odtChunk.decode('utf-8') # The odt chunk to insert
self.odtChunk = odtChunk # The odt chunk to insert
self.elem = elem # The p_odtChunk will be inserted just after the p_elem
# start, which must be an XmlElement instance. If more than one p_elem
# is present in the odt file, the p_odtChunk will be inserted only at

View file

@ -177,8 +177,8 @@ class Renderer:
self.unzipFolder = os.path.join(self.tempFolder, 'unzip')
os.mkdir(self.unzipFolder)
info = unzip(template, self.unzipFolder, odf=True)
self.contentXml = info['content.xml']
self.stylesXml = info['styles.xml']
self.contentXml = info['content.xml'].decode('utf-8')
self.stylesXml = info['styles.xml'].decode('utf-8')
self.stylesManager = StylesManager(self.stylesXml)
# From LibreOffice 3.5, it is not possible anymore to dump errors into
# the resulting ods as annotations. Indeed, annotations can't reside
@ -524,11 +524,11 @@ class Renderer:
os.path.join(self.unzipFolder, innerFile))
# Insert dynamic styles
contentXml = os.path.join(self.unzipFolder, 'content.xml')
f = file(contentXml)
f = open(contentXml, 'r+', encoding='utf-8')
dynamicStyles = ''.join(self.dynamicStyles)
content = f.read().replace('<!DYNAMIC_STYLES!>', dynamicStyles)
f.close()
f = file(contentXml, 'w')
f.seek(0)
f.truncate(0)
f.write(content)
f.close()
# Call the user-defined "finalize" function when present

View file

@ -94,7 +94,7 @@ class Test(appy.shared.test.Test):
raise TesterError(CONTEXT_NOT_FOUND % contextPy)
contextPkg = 'appy.pod.test.contexts.%s' % contextName
exec('import %s' % contextPkg)
exec('context = dir(%s)' % contextPkg)
context = eval('dir(%s)' % contextPkg)
res = {}
for elem in context:
if not elem.startswith('__'):
@ -149,7 +149,7 @@ class Test(appy.shared.test.Test):
zipFile = zipfile.ZipFile(odtFile)
for zippedFile in zipFile.namelist():
if zippedFile in self.interestingOdtContent:
f = file(os.path.join(self.tempFolder,
f = open(os.path.join(self.tempFolder,
'%s.%s' % (filePrefix, zippedFile)), 'wb')
fileContent = zipFile.read(zippedFile)
if zippedFile == 'content.xml':
@ -159,12 +159,10 @@ class Test(appy.shared.test.Test):
# to the other. So we remove those paths.
annotationsRemover = AnnotationsRemover(
OdfEnvironment(), self)
annotationsRemover.parse(fileContent)
fileContent = annotationsRemover.getResult()
try:
f.write(fileContent.encode('utf-8'))
except UnicodeDecodeError:
f.write(fileContent)
annotationsRemover.parse(fileContent.decode('utf-8'))
fileContent = annotationsRemover.getResult().encode('utf-8')
f.write(fileContent)
f.close()
zipFile.close()

View file

@ -282,7 +282,7 @@ class HtmlTable:
decl = '<%s:style %s:name="%s.%d" %s:family="table-column">' \
'<%s:table-column-properties %s:rel-column-width="%d*"' \
'/></%s:style>' % (s, s, self.name, i, s, s, s, width, s)
renderer.dynamicStyles.append(decl.encode('utf-8'))
renderer.dynamicStyles.append(decl)
# ------------------------------------------------------------------------------
class XhtmlEnvironment(XmlEnvironment):
@ -512,7 +512,7 @@ class XhtmlEnvironment(XmlEnvironment):
sizes = table.columnContentSizes
# Insert None values if the list is too small
while (len(sizes)-1) < table.cellIndex: sizes.append(None)
highest = max(sizes[table.cellIndex], table.cellContentSize, 5)
highest = max(sizes[table.cellIndex] or 0, table.cellContentSize, 5)
# Put a maximum
highest = min(highest, 100)
sizes[table.cellIndex] = highest