Lightly validate the lib.
git-svn-id: http://svn.robofab.com/branches/ufo3k@364 b5fa9d6c-a76f-4ffd-b3cb-f825fc41095c
This commit is contained in:
parent
6c9707f49d
commit
c8cad30c69
@ -248,7 +248,7 @@ class UFOReader(object):
|
||||
# normal
|
||||
else:
|
||||
groups = self._readGroups()
|
||||
valid, message = validateGroups(groups)
|
||||
valid, message = groupsValidator(groups)
|
||||
if not valid:
|
||||
raise UFOLibError(message)
|
||||
return groups
|
||||
@ -365,9 +365,10 @@ class UFOReader(object):
|
||||
if not self._checkForFile(path):
|
||||
return {}
|
||||
data = self._readPlist(path)
|
||||
if not isinstance(data, dict):
|
||||
raise UFOLibError("lib.plist is not properly formatted.")
|
||||
return readPlist(path)
|
||||
valid, message = validateLib(data)
|
||||
if not valid:
|
||||
raise UFOLibError(message)
|
||||
return data
|
||||
|
||||
# features.fea
|
||||
|
||||
@ -719,7 +720,7 @@ class UFOWriter(object):
|
||||
Write groups.plist. This method requires a
|
||||
dict of glyph groups as an argument.
|
||||
"""
|
||||
valid, message = validateGroups(groups)
|
||||
valid, message = groupsValidator(groups)
|
||||
if not valid:
|
||||
raise UFOLibError(message)
|
||||
self._makeDirectory()
|
||||
@ -809,8 +810,9 @@ class UFOWriter(object):
|
||||
Write lib.plist. This method requires a
|
||||
lib dict as an argument.
|
||||
"""
|
||||
if not isinstance(libDict, dict):
|
||||
raise UFOLibError("The libDict must be a dict or dict like object.")
|
||||
valid, message = validateLib(libDict)
|
||||
if not valid:
|
||||
raise UFOLibError(message)
|
||||
self._makeDirectory()
|
||||
path = os.path.join(self._path, LIB_FILENAME)
|
||||
if libDict:
|
||||
|
@ -17,7 +17,7 @@ from xmlTreeBuilder import buildTree, stripCharacterData
|
||||
from robofab.pens.pointPen import AbstractPointPen
|
||||
from plistlib import readPlist, writePlistToString
|
||||
from filenames import userNameToFileName
|
||||
from validators import genericTypeValidator, colorValidator, guidelinesValidator, identifierValidator, imageValidator
|
||||
from validators import genericTypeValidator, colorValidator, guidelinesValidator, identifierValidator, imageValidator, libValidator
|
||||
|
||||
try:
|
||||
set
|
||||
@ -799,7 +799,6 @@ def _readGlyphFromTree(tree, glyphObject=None, pointPen=None):
|
||||
if haveSeenNote:
|
||||
raise GlifLibError("The note element occurs more than once.")
|
||||
haveSeenNote = True
|
||||
# XXX validate?
|
||||
rawNote = "\n".join(children)
|
||||
lines = rawNote.split("\n")
|
||||
lines = [line.strip() for line in lines]
|
||||
@ -809,10 +808,12 @@ def _readGlyphFromTree(tree, glyphObject=None, pointPen=None):
|
||||
if haveSeenLib:
|
||||
raise GlifLibError("The lib element occurs more than once.")
|
||||
haveSeenLib = True
|
||||
# XXX validate?
|
||||
from plistFromTree import readPlistFromTree
|
||||
assert len(children) == 1
|
||||
lib = readPlistFromTree(children[0])
|
||||
valid, message = libValidator(lib)
|
||||
if not valid:
|
||||
raise GlifLibError(message)
|
||||
_relaxedSetattr(glyphObject, "lib", lib)
|
||||
else:
|
||||
raise GlifLibError("Unknown element in GLIF: %s" % element)
|
||||
|
@ -735,33 +735,33 @@ def layerContentsValidator(value, ufoPath):
|
||||
# groups.plist
|
||||
# ------------
|
||||
|
||||
def validateGroups(value):
|
||||
def groupsValidator(value):
|
||||
"""
|
||||
>>> groups = {"A" : ["A", "A"], "A2" : ["A"]}
|
||||
>>> validateGroups(groups)
|
||||
>>> groupsValidator(groups)
|
||||
(True, None)
|
||||
|
||||
>>> groups = {"" : ["A"]}
|
||||
>>> validateGroups(groups)
|
||||
>>> groupsValidator(groups)
|
||||
(False, 'A group has an empty name.')
|
||||
|
||||
>>> groups = {"public.awesome" : ["A"]}
|
||||
>>> validateGroups(groups)
|
||||
>>> groupsValidator(groups)
|
||||
(True, None)
|
||||
|
||||
>>> groups = {"public.kern1." : ["A"]}
|
||||
>>> validateGroups(groups)
|
||||
>>> groupsValidator(groups)
|
||||
(False, 'The group data contains a kerning group with an incomplete name.')
|
||||
>>> groups = {"public.kern2." : ["A"]}
|
||||
>>> validateGroups(groups)
|
||||
>>> groupsValidator(groups)
|
||||
(False, 'The group data contains a kerning group with an incomplete name.')
|
||||
|
||||
>>> groups = {"public.kern1.A" : ["A"], "public.kern2.A" : ["A"]}
|
||||
>>> validateGroups(groups)
|
||||
>>> groupsValidator(groups)
|
||||
(True, None)
|
||||
|
||||
>>> groups = {"public.kern1.A1" : ["A"], "public.kern1.A2" : ["A"]}
|
||||
>>> validateGroups(groups)
|
||||
>>> groupsValidator(groups)
|
||||
(False, 'The glyph "A" occurs in too many kerning groups.')
|
||||
"""
|
||||
bogusFormatMessage = "The group data is not in the correct format."
|
||||
@ -778,7 +778,7 @@ def validateGroups(value):
|
||||
return False, "A group has an empty name."
|
||||
if groupName.startswith("public."):
|
||||
if not groupName.startswith("public.kern1.") and not groupName.startswith("public.kern2."):
|
||||
# uknown pubic.* name, silently skip.
|
||||
# unknown pubic.* name. silently skip.
|
||||
continue
|
||||
else:
|
||||
if len("public.kernN.") == len(groupName):
|
||||
@ -799,6 +799,43 @@ def validateGroups(value):
|
||||
# lib.plist/lib
|
||||
# -------------
|
||||
|
||||
def libValidator(lib):
|
||||
"""
|
||||
>>> lib = {"foo" : "bar"}
|
||||
>>> libValidator(lib)
|
||||
(True, None)
|
||||
|
||||
>>> lib = {"public.awesome" : "hello"}
|
||||
>>> libValidator(lib)
|
||||
(True, None)
|
||||
|
||||
>>> lib = {"public.glyphOrder" : ["A", "C", "B"]}
|
||||
>>> libValidator(lib)
|
||||
(True, None)
|
||||
|
||||
>>> lib = {"public.glyphOrder" : "hello"}
|
||||
>>> libValidator(lib)
|
||||
(False, 'public.glyphOrder is not properly formatted.')
|
||||
|
||||
>>> lib = {"public.glyphOrder" : ["A", 1, "B"]}
|
||||
>>> libValidator(lib)
|
||||
(False, 'public.glyphOrder is not properly formatted.')
|
||||
"""
|
||||
bogusFormatMessage = "The lib data is not in the correct format."
|
||||
if not isinstance(lib, dict):
|
||||
return False, bogusFormatMessage
|
||||
for key, value in lib.items():
|
||||
if not isinstance(key, basestring):
|
||||
return False, bogusFormatMessage
|
||||
# public.glyphOrder
|
||||
if key == "public.glyphOrder":
|
||||
bogusGlyphOrderMessage = "public.glyphOrder is not properly formatted."
|
||||
if not isinstance(value, (list, tuple)):
|
||||
return False, bogusGlyphOrderMessage
|
||||
for glyphName in value:
|
||||
if not isinstance(glyphName, basestring):
|
||||
return False, bogusGlyphOrderMessage
|
||||
return True, None
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
Loading…
x
Reference in New Issue
Block a user