def test_read_sub_file(self): # Verifies that sub-file content is able to be read to a table. expectedContent = 'testContent' expectedNameID = '1' expectedPlatform = '3' expectedLangId = '0x409' with tempfile.NamedTemporaryFile(delete=False) as tmp: subFileData = ( '<ttFont ttLibVersion="3.15">' '<name>' '<namerecord nameID="%s" platformID="%s" platEncID="1" langID="%s">' '%s' '</namerecord>' '</name>' '</ttFont>' ) % (expectedNameID, expectedPlatform, expectedLangId, expectedContent) tmp.write(subFileData.encode("utf-8")) with tempfile.NamedTemporaryFile(delete=False) as tmp2: fileData = ( '<ttFont ttLibVersion="3.15">' '<name>' '<namerecord src="%s"/>' '</name>' '</ttFont>' ) % tmp.name tmp2.write(fileData.encode('utf-8')) ttf = TTFont() with open(tmp2.name, "rb") as f: reader = XMLReader(f, ttf) reader.read() reader.close() nameTable = ttf['name'] self.assertTrue(int(expectedNameID) == nameTable.names[0].nameID) self.assertTrue(int(expectedLangId, 16) == nameTable.names[0].langID) self.assertTrue(int(expectedPlatform) == nameTable.names[0].platformID) self.assertEqual(expectedContent, nameTable.names[0].string.decode(nameTable.names[0].getEncoding())) os.remove(tmp.name) os.remove(tmp2.name)
def test_close_file_path(self): with tempfile.NamedTemporaryFile(delete=False) as tmp: tmp.write(b'<ttFont></ttFont>') reader = XMLReader(tmp.name, TTFont()) reader.read() # when reading from path, the file is closed automatically at the end self.assertTrue(reader.file.closed) # this does nothing reader.close() self.assertTrue(reader.file.closed) os.remove(tmp.name)
def test_progress(self): class DummyProgressPrinter(ProgressPrinter): def __init__(self, title, maxval=100): self.label = title self.maxval = maxval self.pos = 0 def set(self, val, maxval=None): if maxval is not None: self.maxval = maxval self.pos = val def increment(self, val=1): self.pos += val def setLabel(self, text): self.label = text data = ( '<ttFont>\n' ' <test>\n' ' %s\n' ' </test>\n' '</ttFont>\n' % ("z" * 2 * BUFSIZE) ).encode('utf-8') dataSize = len(data) progressBar = DummyProgressPrinter('test') with BytesIO(data) as tmp: reader = XMLReader(tmp, TTFont(), progress=progressBar) self.assertEqual(progressBar.pos, 0) reader.read() self.assertEqual(progressBar.pos, dataSize // 100) self.assertEqual(progressBar.maxval, dataSize // 100) self.assertTrue('test' in progressBar.label) with BytesIO(b"<ttFont></ttFont>") as tmp: reader = XMLReader(tmp, TTFont(), progress=progressBar) reader.read() # when data size is less than 100 bytes, 'maxval' is 1 self.assertEqual(progressBar.maxval, 1)
def test_close_file_obj(self): with tempfile.NamedTemporaryFile(delete=False) as tmp: tmp.write(b'<ttFont>"hello"</ttFont>') with open(tmp.name, "rb") as f: reader = XMLReader(f, TTFont()) reader.read() # when reading from a file or file-like object, the latter is kept open self.assertFalse(reader.file.closed) # ... until the user explicitly closes it reader.close() self.assertTrue(reader.file.closed) os.remove(tmp.name)
def test_read_sub_file(self): # Verifies that sub-file content is able to be read to a table. expectedContent = 'testContent' expectedNameID = '1' expectedPlatform = '3' expectedLangId = '0x409' with tempfile.NamedTemporaryFile(delete=False) as tmp: subFileData = ( '<ttFont ttLibVersion="3.15">' '<name>' '<namerecord nameID="%s" platformID="%s" platEncID="1" langID="%s">' '%s' '</namerecord>' '</name>' '</ttFont>') % (expectedNameID, expectedPlatform, expectedLangId, expectedContent) tmp.write(subFileData.encode("utf-8")) with tempfile.NamedTemporaryFile(delete=False) as tmp2: fileData = ('<ttFont ttLibVersion="3.15">' '<name>' '<namerecord src="%s"/>' '</name>' '</ttFont>') % tmp.name tmp2.write(fileData.encode('utf-8')) ttf = TTFont() with open(tmp2.name, "rb") as f: reader = XMLReader(f, ttf) reader.read() reader.close() nameTable = ttf['name'] self.assertTrue(int(expectedNameID) == nameTable.names[0].nameID) self.assertTrue( int(expectedLangId, 16) == nameTable.names[0].langID) self.assertTrue( int(expectedPlatform) == nameTable.names[0].platformID) self.assertEqual( expectedContent, nameTable.names[0].string.decode( nameTable.names[0].getEncoding())) os.remove(tmp.name) os.remove(tmp2.name)