plistlib: use double-quotes in xml declaration

for consistency with glifLib
This commit is contained in:
Cosimo Lupo 2018-07-11 13:29:54 +01:00
parent 61c19acace
commit a8124a0eef
No known key found for this signature in database
GPG Key ID: 59D54DB0C9976482
3 changed files with 13 additions and 8 deletions

View File

@ -548,10 +548,7 @@ def readGlyphFromString(aString, glyphObject=None, pointPen=None, formatVersions
_readGlyphFromTree(tree, glyphObject, pointPen, formatVersions=formatVersions, validate=validate)
# we use a custom XML declaration for backward compatibility with older
# ufoLib versions which would write it using double quotes.
# https://github.com/unified-font-object/ufoLib/issues/158
XML_DECLARATION = b"""<?xml version="1.0" encoding="UTF-8"?>\n"""
_XML_DECLARATION = plistlib.XML_DECLARATION + b"\n"
def _writeGlyphToBytes(
@ -597,7 +594,7 @@ def _writeGlyphToBytes(
if getattr(glyphObject, "lib", None):
_writeLib(glyphObject, root, validate)
# return the text
data = XML_DECLARATION + etree.tostring(
data = _XML_DECLARATION + etree.tostring(
root, encoding="utf-8", xml_declaration=False, pretty_print=True
)
return data

View File

@ -13,6 +13,11 @@ from lxml import etree
from fontTools.misc.py23 import unicode, basestring, tounicode
# we use a custom XML declaration for backward compatibility with older
# ufoLib versions which would write it using double quotes.
# https://github.com/unified-font-object/ufoLib/issues/158
XML_DECLARATION = b"""<?xml version="1.0" encoding="UTF-8"?>"""
PLIST_DOCTYPE = (
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" '
'"http://www.apple.com/DTDs/PropertyList-1.0.dtd">'
@ -328,11 +333,14 @@ def dump(value, fp, sort_keys=True, skipkeys=False, _pretty_print=True):
root = etree.Element("plist", version="1.0")
root.append(totree(value, sort_keys=sort_keys, skipkeys=skipkeys))
tree = etree.ElementTree(root)
fp.write(XML_DECLARATION)
if _pretty_print:
fp.write(b"\n")
tree.write(
fp,
encoding="utf-8",
pretty_print=_pretty_print,
xml_declaration=True,
xml_declaration=False,
doctype=PLIST_DOCTYPE,
)

View File

@ -6,7 +6,7 @@ from io import open
from ufoLib.test.testSupport import getDemoFontGlyphSetPath
from ufoLib.glifLib import (
GlyphSet, glyphNameToFileName, readGlyphFromString, writeGlyphToString,
XML_DECLARATION,
_XML_DECLARATION,
)
GLYPHSETDIR = getDemoFontGlyphSetPath()
@ -160,7 +160,7 @@ class ReadWriteFuncTest(unittest.TestCase):
def testXmlDeclaration(self):
s = writeGlyphToString("a", _Glyph())
self.assertTrue(s.startswith(XML_DECLARATION.decode("utf-8")))
self.assertTrue(s.startswith(_XML_DECLARATION.decode("utf-8")))
if __name__ == "__main__":