diff options
Diffstat (limited to 'Lib/fontTools/ufoLib')
-rwxr-xr-x | Lib/fontTools/ufoLib/__init__.py | 34 | ||||
-rwxr-xr-x | Lib/fontTools/ufoLib/glifLib.py | 6 |
2 files changed, 37 insertions, 3 deletions
diff --git a/Lib/fontTools/ufoLib/__init__.py b/Lib/fontTools/ufoLib/__init__.py index 4b92102d..e846d085 100755 --- a/Lib/fontTools/ufoLib/__init__.py +++ b/Lib/fontTools/ufoLib/__init__.py @@ -723,6 +723,7 @@ class UFOReader(_UFOBaseIO): ufoFormatVersion=self._formatVersion, validateRead=validateRead, validateWrite=validateWrite, + expectContentsFile=True ) def getCharacterMapping(self, layerName=None, validate=None): @@ -1422,7 +1423,15 @@ class UFOWriter(UFOReader): raise UFOLibError("Could not locate a glyph set directory for the layer named %s." % layerName) return foundDirectory - def getGlyphSet(self, layerName=None, defaultLayer=True, glyphNameToFileNameFunc=None, validateRead=None, validateWrite=None): + def getGlyphSet( + self, + layerName=None, + defaultLayer=True, + glyphNameToFileNameFunc=None, + validateRead=None, + validateWrite=None, + expectContentsFile=False, + ): """ Return the GlyphSet object associated with the appropriate glyph directory in the .ufo. @@ -1435,6 +1444,10 @@ class UFOWriter(UFOReader): class's validate value, can be overridden. ``validateWrte`` will validate the written data, by default it is set to the class's validate value, can be overridden. + ``expectContentsFile`` will raise a GlifLibError if a contents.plist file is + not found on the glyph set file system. This should be set to ``True`` if you + are reading an existing UFO and ``False`` if you use ``getGlyphSet`` to create + a fresh glyph set. """ if validateRead is None: validateRead = self._validate @@ -1459,7 +1472,12 @@ class UFOWriter(UFOReader): raise UFOLibError("A layer name must be provided for non-default layers.") # move along to format specific writing if self._formatVersion < UFOFormatVersion.FORMAT_3_0: - return self._getDefaultGlyphSet(validateRead, validateWrite, glyphNameToFileNameFunc=glyphNameToFileNameFunc) + return self._getDefaultGlyphSet( + validateRead, + validateWrite, + glyphNameToFileNameFunc=glyphNameToFileNameFunc, + expectContentsFile=expectContentsFile + ) elif self._formatVersion.major == UFOFormatVersion.FORMAT_3_0.major: return self._getGlyphSetFormatVersion3( validateRead, @@ -1467,11 +1485,18 @@ class UFOWriter(UFOReader): layerName=layerName, defaultLayer=defaultLayer, glyphNameToFileNameFunc=glyphNameToFileNameFunc, + expectContentsFile=expectContentsFile, ) else: raise NotImplementedError(self._formatVersion) - def _getDefaultGlyphSet(self, validateRead, validateWrite, glyphNameToFileNameFunc=None): + def _getDefaultGlyphSet( + self, + validateRead, + validateWrite, + glyphNameToFileNameFunc=None, + expectContentsFile=False, + ): from fontTools.ufoLib.glifLib import GlyphSet glyphSubFS = self.fs.makedir(DEFAULT_GLYPHS_DIRNAME, recreate=True) @@ -1481,6 +1506,7 @@ class UFOWriter(UFOReader): ufoFormatVersion=self._formatVersion, validateRead=validateRead, validateWrite=validateWrite, + expectContentsFile=expectContentsFile, ) def _getGlyphSetFormatVersion3( @@ -1490,6 +1516,7 @@ class UFOWriter(UFOReader): layerName=None, defaultLayer=True, glyphNameToFileNameFunc=None, + expectContentsFile=False, ): from fontTools.ufoLib.glifLib import GlyphSet @@ -1529,6 +1556,7 @@ class UFOWriter(UFOReader): ufoFormatVersion=self._formatVersion, validateRead=validateRead, validateWrite=validateWrite, + expectContentsFile=expectContentsFile, ) def renameGlyphSet(self, layerName, newLayerName, defaultLayer=False): diff --git a/Lib/fontTools/ufoLib/glifLib.py b/Lib/fontTools/ufoLib/glifLib.py index 385f54bf..3003110e 100755 --- a/Lib/fontTools/ufoLib/glifLib.py +++ b/Lib/fontTools/ufoLib/glifLib.py @@ -135,6 +135,7 @@ class GlyphSet(_UFOBaseIO): ufoFormatVersion=None, validateRead=True, validateWrite=True, + expectContentsFile=False, ): """ 'path' should be a path (string) to an existing local directory, or @@ -148,6 +149,9 @@ class GlyphSet(_UFOBaseIO): ``validateRead`` will validate read operations. Its default is ``True``. ``validateWrite`` will validate write operations. Its default is ``True``. + ``expectContentsFile`` will raise a GlifLibError if a contents.plist file is + not found on the glyph set file system. This should be set to ``True`` if you + are reading an existing UFO and ``False`` if you create a fresh glyph set. """ try: ufoFormatVersion = UFOFormatVersion(ufoFormatVersion) @@ -191,6 +195,8 @@ class GlyphSet(_UFOBaseIO): self.fs = filesystem # if glyphSet contains no 'contents.plist', we consider it empty self._havePreviousFile = filesystem.exists(CONTENTS_FILENAME) + if expectContentsFile and not self._havePreviousFile: + raise GlifLibError(f"{CONTENTS_FILENAME} is missing.") # attribute kept for backward compatibility self.ufoFormatVersion = ufoFormatVersion.major self.ufoFormatVersionTuple = ufoFormatVersion |