Skip to content
This repository was archived by the owner on Jan 28, 2019. It is now read-only.

copy lxml-based plistlib module from fonttools/ufoLib2 #160

Merged
merged 16 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Lib/ufoLib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ufoLib.validators import *
from ufoLib.filenames import userNameToFileName
from ufoLib.converters import convertUFO1OrUFO2KerningToUFO3Kerning
from ufoLib.plistlib import readPlist, writePlist
from ufoLib import plistlib
"""
A library for importing .ufo files and their descendants.
Refer to http://unifiedfontobject.com for the UFO specification.
Expand Down Expand Up @@ -107,7 +107,7 @@ def _getPlist(self, fileName, default=None):
raise UFOLibError("%s is missing in %s. This file is required" % (fileName, self._path))
try:
with open(path, "rb") as f:
return readPlist(f)
return plistlib.load(f)
except:
raise UFOLibError("The file %s could not be read." % fileName)

Expand Down Expand Up @@ -1343,9 +1343,7 @@ def writePlistAtomically(obj, path):
If so, the file is not rewritten so that the modification date
is preserved.
"""
f = BytesIO()
writePlist(obj, f)
data = f.getvalue()
data = plistlib.dumps(obj)
writeDataFileAtomically(data, path)

def writeFileAtomically(text, path, encoding="utf-8"):
Expand Down Expand Up @@ -1427,7 +1425,7 @@ def convertUFOFormatVersion1ToFormatVersion2(inPath, outPath=None, validateRead=
infoData = {}
else:
with open(infoPath, "rb") as f:
infoData = readPlist(f)
infoData = plistlib.load(f)
infoData = _convertFontInfoDataVersion1ToVersion2(infoData)
# if the paths are the same, only need to change the
# fontinfo and meta info files.
Expand Down
31 changes: 13 additions & 18 deletions Lib/ufoLib/glifLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
from warnings import warn
from collections import OrderedDict
from fontTools.misc.py23 import basestring, unicode, tobytes
from ufoLib.plistlib import PlistWriter, readPlist, writePlist
from ufoLib.plistFromETree import readPlistFromTree
from ufoLib import plistlib
from ufoLib.pointPen import AbstractPointPen, PointToSegmentPen
from ufoLib.filenames import userNameToFileName
from ufoLib.validators import isDictEnough, genericTypeValidator, colorValidator,\
Expand Down Expand Up @@ -181,7 +180,7 @@ def writeContents(self):
"""
contentsPath = os.path.join(self.dirName, "contents.plist")
with open(contentsPath, "wb") as f:
writePlist(self.contents, f)
plistlib.dump(self.contents, f)

# layer info

Expand Down Expand Up @@ -234,7 +233,7 @@ def writeLayerInfo(self, info, validateWrite=None):
# write file
path = os.path.join(self.dirName, LAYERINFO_FILENAME)
with open(path, "wb") as f:
writePlist(infoData, f)
plistlib.dump(infoData, f)

# read caching

Expand Down Expand Up @@ -481,7 +480,7 @@ def getImageReferences(self, glyphNames=None):
def _readPlist(self, path):
try:
with open(path, "rb") as f:
data = readPlist(f)
data = plistlib.load(f)
return data
except Exception as e:
if isinstance(e, IOError) and e.errno == 2:
Expand Down Expand Up @@ -549,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(
Expand Down Expand Up @@ -598,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
Expand Down Expand Up @@ -771,19 +767,18 @@ def _writeAnchors(glyphObject, element, identifiers, validate):

def _writeLib(glyphObject, element, validate):
lib = getattr(glyphObject, "lib", None)
if not lib:
# don't write empty lib
return
if validate:
valid, message = glyphLibValidator(lib)
if not valid:
raise GlifLibError(message)
if not isinstance(lib, dict):
lib = dict(lib)
f = BytesIO()
plistWriter = PlistWriter(f, writeHeader=False) # TODO: fix indent
plistWriter.writeValue(lib)
text = f.getvalue()
text = etree.fromstring(text)
if len(text):
etree.SubElement(element, "lib").append(text)
# plist inside GLIF begins with 2 levels of indentation
e = plistlib.totree(lib, indent_level=2)
etree.SubElement(element, "lib").append(e)

# -----------------------
# layerinfo.plist Support
Expand Down Expand Up @@ -1036,7 +1031,7 @@ def _readNote(glyphObject, note):
def _readLib(glyphObject, lib, validate):
assert len(lib) == 1
child = lib[0]
plist = readPlistFromTree(child)
plist = plistlib.fromtree(child)
if validate:
valid, message = glyphLibValidator(plist)
if not valid:
Expand Down
30 changes: 0 additions & 30 deletions Lib/ufoLib/plistFromETree.py

This file was deleted.

Loading