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

@ -10,7 +10,7 @@ def parseStyleAttribute(value, asDict=False):
else: res = []
for attr in value.split(';'):
if not attr.strip(): continue
name, value = attr.split(':')
name, value = attr.split(':', 1)
if asDict: res[name.strip()] = value.strip()
else: res.append( (name.strip(), value.strip()) )
return res
@ -42,9 +42,9 @@ class CssStyles:
'''Analyses styles as found in p_attrs and sets, for every found style,
an attribute on self.'''
# First, parse the "style" attr if present
if attrs.has_key('style'):
if 'style' in attrs.keys():
styles = parseStyleAttribute(attrs['style'], asDict=True)
for name, value in styles.iteritems():
for name, value in styles.items():
if name in CssStyles.withUnit:
value = CssValue(value)
setattr(self, name.replace('-', ''), value)
@ -52,12 +52,12 @@ class CssStyles:
# override corresponding attributes from the "styles" attributes if
# found.
for name in ('width', 'height'):
if not hasattr(self, name) and attrs.has_key(name):
if not hasattr(self, name) and name in attrs.keys():
setattr(self, name, CssValue(attrs[name]))
def __repr__(self):
res = '<CSS'
for name, value in self.__dict__.iteritems():
for name, value in self.__dict__.items():
res += ' %s:%s' % (name, value)
return res + '>'
# ------------------------------------------------------------------------------

View file

@ -111,15 +111,15 @@ class CsvParser:
Python type specified in p_basicType (int, float, ...).'''
if (basicType != str) and (basicType != str):
try:
exec('res = %s' % str(value))
res = eval('%s' % str(value))
except SyntaxError as se:
res = None
else:
try:
exec('res = """%s"""' % str(value))
res = eval('"""%s"""' % str(value))
except SyntaxError as se:
try:
exec("res = '''%s'''" % str(value))
res = eval("res = '''%s'''" % str(value))
except SyntaxError as se:
res = None
return res

View file

@ -34,7 +34,7 @@ class FormDataEncoder:
def encode(self):
res = []
for name, value in self.data.iteritems():
for name, value in self.data.items():
res.append(self.marshalValue(name, value))
return '&'.join(res)

View file

@ -306,16 +306,16 @@ class HtmlDiff:
else: tag = 'span'
# What message will it show in its 'title' attribute?
if not msg:
exec('msg = self.%sMsg' % type)
msg = eval('self.%sMsg' % type)
# What CSS class (or, if none, tag-specific style) will be used ?
exec('cssClass = self.%sCss' % type)
cssClass = eval('self.%sCss' % type)
if cssClass:
style = 'class="%s"' % cssClass
else:
exec('style = self.%sStyle' % type)
style = eval('self.%sStyle' % type)
style = 'style="%s"' % style
# The 'name' attribute of the tag indicates the type of the update.
exec('tagName = self.%sName' % type)
tagName = eval('self.%sName' % type)
# The idea is: if there are several lines, every line must be surrounded
# by a tag. This way, we know that a surrounding tag can't span several
# lines, which is a prerequisite for managing cumulative diffs.

View file

@ -83,7 +83,7 @@ class LdapConfig:
dict of params usable for creating or updating the corresponding
Appy user.'''
res = {}
for name, appyName in self.ldapAttributes.iteritems():
for name, appyName in self.ldapAttributes.items():
if not appyName: continue
# Get the name of the attribute as known in the LDAP
ldapName = getattr(self, name)
@ -111,7 +111,7 @@ class LdapConfig:
user = tool.search1('User', noSecurity=True, login=login)
if user:
# Yes. Update it with info about him from the LDAP
for name, value in attrs.iteritems():
for name, value in attrs.items():
currentValue = getattr(user, name)
if value != currentValue:
setattr(user, name, value)

View file

@ -69,11 +69,9 @@ class TestReport:
TestReport.instance = self
else:
raise InternalError(TEST_REPORT_SINGLETON_ERROR)
def say(self, msg, force=False, encoding=None):
def say(self, msg, force=False):
if self.verbose or force:
print(msg)
if encoding:
self.report.write(msg.encode(encoding))
else:
self.report.write(msg)
self.report.write('\n')

View file

@ -194,7 +194,7 @@ def flipDict(d):
'''Flips dict p_d: keys become values, values become keys. p_d is left
untouched: a new, flipped, dict is returned.'''
res = {}
for k, v in d.iteritems(): res[v] = k
for k, v in d.items(): res[v] = k
return res
# ------------------------------------------------------------------------------
@ -291,7 +291,7 @@ def normalizeString(s, usage='fileName'):
for char in s:
if char not in fileNameIgnore: res += char
elif usage.startswith('alpha'):
exec('rex = %sRex' % usage)
rex = eval('%sRex' % usage)
res = ''
for char in s:
if rex.match(char): res += char

View file

@ -222,7 +222,7 @@ class XmlParser(ContentHandler, ErrorHandler):
'''This method is called every time expat does not recognize an entity.
We provide here support for HTML entities.'''
if name in HTML_ENTITIES:
self.characters(HTML_ENTITIES[name].decode('utf-8'))
self.characters(HTML_ENTITIES[name])
else:
# Put a question mark instead of raising an exception.
self.characters('?')
@ -245,23 +245,20 @@ class XmlParser(ContentHandler, ErrorHandler):
- a file instance opened for reading. Note that in this case, this
method will close it.
'''
try:
from io import StringIO
except ImportError:
from io import StringIO
from io import BytesIO
self._xml = xml
self.parser.setContentHandler(self)
self.parser.setErrorHandler(self)
self.parser.setFeature(feature_external_ges, False)
inputSource = InputSource()
if source == 'string':
inputSource.setByteStream(StringIO(xml))
inputSource.setByteStream(BytesIO(xml.encode('utf-8')))
else:
if not isinstance(xml, file):
xml = file(xml)
if isinstance(xml, str):
xml = open(xml,'rb')
inputSource.setByteStream(xml)
self.parser.parse(inputSource)
if isinstance(xml, file): xml.close()
if hasattr(xml, 'close'): xml.close()
return self.res
# ------------------------------------------------------------------------------
@ -464,7 +461,7 @@ class XmlUnmarshaller(XmlParser):
# If not, try a standard conversion
elif e.currentBasicType in self.numericTypes:
try:
exec('value = %s' % value)
value = eval('%s' % value)
except SyntaxError:
raise AppyError(CONVERSION_ERROR % (
e.currentBasicType, value))
@ -945,10 +942,10 @@ class XmlComparator:
# Perform the comparison
differ = difflib.Differ()
if self.areXml:
f = file(self.fileNameA)
f = open(self.fileNameA, 'rb')
contentA = f.read()
f.close()
f = file(self.fileNameB)
f = open(self.fileNameB, 'rb')
contentB = f.read()
f.close()
xmlHandler = XmlHandler(self.xmlTagsToIgnore, self.xmlAttrsToIgnore)
@ -958,10 +955,10 @@ class XmlComparator:
xml.sax.parseString(contentB, xmlHandler)
contentB = xmlHandler.res.split('\n')
else:
f = file(self.fileNameA)
f = open(self.fileNameA, 'r', encoding=encoding)
contentA = f.readlines()
f.close()
f = file(self.fileNameB)
f = open(self.fileNameB, 'r', encoding=encoding)
contentB = f.readlines()
f.close()
diffResult = list(differ.compare(contentA, contentB))
@ -975,17 +972,17 @@ class XmlComparator:
if not atLeastOneDiff:
msg = 'Difference(s) detected between files %s and %s:' % \
(self.fileNameA, self.fileNameB)
if report: report.say(msg, encoding='utf-8')
if report: report.say(msg)
else: print(msg)
atLeastOneDiff = True
if not lastLinePrinted:
if report: report.say('...')
else: print('...')
if self.areXml:
if report: report.say(line, encoding=encoding)
if report: report.say(line)
else: print(line)
else:
if report: report.say(line[:-1], encoding=encoding)
if report: report.say(line[:-1])
else: print((line[:-1]))
lastLinePrinted = True
else: