String module conversion bug (#1452)

* Wiring up the sping test cases into RDKit's CTesting.

* Need to declare an encoding to adhere to PEP 263.

* Fixing string module deprecation conversion bugs introduced in these two commits:

7d3a8b940a
b640be11d9

* Python 3 compatibility.

* Python 3 has unicode and these tables don't include widths for all unicode characters, so just set it to the maximum width of any character.

* Open the file object in text mode to support unicode in Python 3.
This commit is contained in:
Brian Cole
2017-06-19 08:50:31 -04:00
committed by Greg Landrum
parent 66779dfc8d
commit e03397c664
10 changed files with 74 additions and 28 deletions

View File

@@ -24,4 +24,6 @@ add_pytest(pythonTestDirVLib
${CMAKE_CURRENT_SOURCE_DIR}/VLib/test_list.py --testDir ${CMAKE_CURRENT_SOURCE_DIR}/VLib )
add_pytest(pythonTestDirChem
${CMAKE_CURRENT_SOURCE_DIR}/Chem/test_list.py --testDir ${CMAKE_CURRENT_SOURCE_DIR}/Chem )
add_pytest(pythonTestSping
${CMAKE_CURRENT_SOURCE_DIR}/Chem/test_list.py --testDir ${CMAKE_CURRENT_SOURCE_DIR}/sping )

View File

@@ -427,7 +427,7 @@ class PDFPage(PDFObject):
def setStream(self, data):
if isinstance(data, (list, tuple)):
data = LINEEND.join(data, LINEEND)
data = LINEEND.join(data)
self.stream.setStream(data)

View File

@@ -456,8 +456,8 @@ translate
# save() will now become part of the spec.
file = file or self.filename
fileobj = getFileObject(file)
fileobj.write(self.code.join(linesep))
fileobj = getFileObject(file, 'w')
fileobj.write(linesep.join(self.code))
# here's a hack. we might want to be able to add more after saving so
# preserve the current code ???
preserveCode = self.code
@@ -475,7 +475,7 @@ translate
self.psEndDocument() # depends on _inDocumentFlag :(
fileobj.write(finalizationCode.join(linesep))
fileobj.write(linesep.join(finalizationCode))
# fileobj.close() ### avoid this for now
## clean up my mess: This is not a good way to do things FIXME!!! ???
self.code = preserveCode

View File

@@ -226,5 +226,10 @@ def stringwidth(text, font, encoding):
raise KeyError("Improper encoding {0} or font name {1}".format(encoding, font))
w = 0
for char in text:
w = w + widths[ord(char)]
chr_idx = ord(char)
if chr_idx < len(widths):
chr_width = widths[chr_idx]
else:
chr_width = max(widths)
w = w + chr_width
return w

View File

@@ -25,6 +25,9 @@ class Color:
def __rmul__(self, x):
return Color(self.red * x, self.green * x, self.blue * x)
def __truediv__(self, x):
return Color(self.red / x, self.green / x, self.blue / x)
def __div__(self, x):
return Color(self.red / x, self.green / x, self.blue / x)

12
rdkit/sping/test_list.py Executable file
View File

@@ -0,0 +1,12 @@
tests = [
("python", "test_list.py", {'dir': 'tests'}),
]
longTests = []
if __name__ == '__main__':
import sys
from rdkit import TestRunner
failed, tests = TestRunner.RunScript('test_list.py', 0, 1)
sys.exit(len(failed))

View File

@@ -1,8 +1,8 @@
# $Id$
# -*- coding: utf-8 -*-
from __future__ import print_function
import pidtest
def testLatin1Chars(can):
curx, cury = 10, 20
can.drawString("hola M<>laga amigos ni<6E>os", curx, cury)

View File

@@ -0,0 +1,12 @@
tests = [
("python", "testallps.py", {}),
]
longTests = []
if __name__ == '__main__':
import sys
from rdkit import TestRunner
failed, tests = TestRunner.RunScript('test_list.py', 0, 1)
sys.exit(len(failed))

View File

@@ -1,31 +1,42 @@
#!/usr/bin/env python
import unittest
import pidtest
import sping.PS.pidPS
from rdkit.sping.PS import pidPS
canvas = sping.PS.pidPS.PSCanvas(name="testallps.ps", size=(400, 500))
class TestCase(unittest.TestCase):
def test_all(self):
canvas = pidPS.PSCanvas(name="testallps.ps", size=(400, 500))
pidtest.drawStrings(canvas)
pidtest.drawStrings(canvas)
canvas.flush()
canvas.clear() # resets the canvas
canvas.flush()
canvas.clear() # resets the canvas
pidtest.drawRotstring(canvas)
canvas.flush()
canvas.clear()
pidtest.drawRotstring(canvas)
canvas.flush()
canvas.clear()
x1 = 100
y1 = 100
radius = 20
canvas.drawString("Test", 100, 250)
canvas.drawEllipse(x1, y1 - radius, x1 + 2 * radius, y1 + radius, edgeWidth=1)
canvas.flush()
canvas.clear()
x1 = 100
y1 = 100
radius = 20
canvas.drawString("Test", 100, 250)
canvas.drawEllipse(x1, y1 - radius, x1 + 2 * radius, y1 + radius, edgeWidth=1)
canvas.flush()
canvas.clear()
pidtest.drawBasics(canvas)
canvas.flush()
canvas.clear()
pidtest.drawBasics(canvas)
canvas.flush()
canvas.clear()
import pstests
pstests.testLatin1Chars(canvas)
import pstests
pstests.testLatin1Chars(canvas)
canvas.save()
canvas.save()
def test_pdf_canvas_works(self):
from rdkit.Chem.Draw.spingCanvas import Canvas
canvas = Canvas((200, 400), 'test.pdf', imageType="pdf")
canvas.canvas.showPage()
if __name__ == '__main__': # pragma: nocover
unittest.main()

View File

@@ -6,6 +6,7 @@ tests = [
("python", "test_list.py", {'dir': 'SimDivFilters'}),
("python", "test_list.py", {'dir': 'VLib'}),
("python", "test_list.py", {'dir': 'utils'}),
("python", "test_list.py", {'dir': 'sping'}),
]
longTests = []