Upgrade syntax to Python 3.6+

This commit is contained in:
Nikolaus Waxweiler 2019-09-09 21:28:51 +01:00 committed by Nikolaus Waxweiler
parent 7aa18f3cd8
commit bc344380e8
3 changed files with 49 additions and 50 deletions

View File

@ -106,7 +106,7 @@ class UFOFileStructure(enum.Enum):
# --------------
class _UFOBaseIO(object):
class _UFOBaseIO:
def getFileModificationTime(self, path):
"""
@ -146,7 +146,7 @@ class _UFOBaseIO(object):
except Exception as e:
# TODO(anthrotype): try to narrow this down a little
raise UFOLibError(
"'%s' could not be read on %s: %s" % (fileName, self.fs, e)
f"'{fileName}' could not be read on {self.fs}: {e}"
)
def _writePlist(self, fileName, obj):
@ -212,7 +212,7 @@ class UFOReader(_UFOBaseIO):
else:
parentFS = fs.osfs.OSFS(path)
except fs.errors.CreateFailed as e:
raise UFOLibError("unable to open '%s': %s" % (path, e))
raise UFOLibError(f"unable to open '{path}': {e}")
if structure is UFOFileStructure.ZIP:
# .ufoz zip files must contain a single root directory, with arbitrary
@ -660,7 +660,7 @@ class UFOReader(_UFOBaseIO):
glyphSubFS = self.fs.opendir(directory)
except fs.errors.ResourceNotFound:
raise UFOLibError(
"No '%s' directory for layer '%s'" % (directory, layerName)
f"No '{directory}' directory for layer '{layerName}'"
)
return GlyphSet(
glyphSubFS,
@ -759,7 +759,7 @@ class UFOReader(_UFOBaseIO):
dataFS = self.fs.opendir(DATA_DIRNAME)
data = dataFS.readbytes(fileName)
except fs.errors.ResourceNotFound:
raise UFOLibError("No data file named '%s' on %s" % (fileName, self.fs))
raise UFOLibError(f"No data file named '{fileName}' on {self.fs}")
return data
def readImage(self, fileName, validate=None):
@ -782,7 +782,7 @@ class UFOReader(_UFOBaseIO):
imagesFS = self.fs.opendir(IMAGES_DIRNAME)
data = imagesFS.readbytes(fileName)
except fs.errors.ResourceNotFound:
raise UFOLibError("No image file named '%s' on %s" % (fileName, self.fs))
raise UFOLibError(f"No image file named '{fileName}' on {self.fs}")
if validate:
valid, error = pngValidator(data=data)
if not valid:
@ -1039,7 +1039,7 @@ class UFOWriter(UFOReader):
return self.fs.open(path, mode=mode, encoding=encoding)
except fs.errors.ResourceError as e:
return UFOLibError(
"unable to open '%s' on %s: %s" % (path, self.fs, e)
f"unable to open '{path}' on {self.fs}: {e}"
)
def removePath(self, path, force=False, removeEmptyParents=True):
@ -1059,7 +1059,7 @@ class UFOWriter(UFOReader):
except fs.errors.ResourceNotFound:
if not force:
raise UFOLibError(
"'%s' does not exist on %s" % (path, self.fs)
f"'{path}' does not exist on {self.fs}"
)
if removeEmptyParents:
parent = fs.path.dirname(path)
@ -1517,13 +1517,13 @@ class UFOWriter(UFOReader):
Write data to fileName in the 'data' directory.
The data must be a bytes string.
"""
self.writeBytesToPath("%s/%s" % (DATA_DIRNAME, fsdecode(fileName)), data)
self.writeBytesToPath(f"{DATA_DIRNAME}/{fsdecode(fileName)}", data)
def removeData(self, fileName):
"""
Remove the file named fileName from the data directory.
"""
self.removePath("%s/%s" % (DATA_DIRNAME, fsdecode(fileName)))
self.removePath(f"{DATA_DIRNAME}/{fsdecode(fileName)}")
# /images
@ -1541,7 +1541,7 @@ class UFOWriter(UFOReader):
valid, error = pngValidator(data=data)
if not valid:
raise UFOLibError(error)
self.writeBytesToPath("%s/%s" % (IMAGES_DIRNAME, fileName), data)
self.writeBytesToPath(f"{IMAGES_DIRNAME}/{fileName}", data)
def removeImage(self, fileName, validate=None): # XXX remove unused 'validate'?
"""
@ -1550,7 +1550,7 @@ class UFOWriter(UFOReader):
"""
if self._formatVersion < 3:
raise UFOLibError("Images are not allowed in UFO %d." % self._formatVersion)
self.removePath("%s/%s" % (IMAGES_DIRNAME, fsdecode(fileName)))
self.removePath(f"{IMAGES_DIRNAME}/{fsdecode(fileName)}")
def copyImageFromReader(self, reader, sourceFileName, destFileName, validate=None):
"""
@ -1562,8 +1562,8 @@ class UFOWriter(UFOReader):
validate = self._validate
if self._formatVersion < 3:
raise UFOLibError("Images are not allowed in UFO %d." % self._formatVersion)
sourcePath = "%s/%s" % (IMAGES_DIRNAME, fsdecode(sourceFileName))
destPath = "%s/%s" % (IMAGES_DIRNAME, fsdecode(destFileName))
sourcePath = f"{IMAGES_DIRNAME}/{fsdecode(sourceFileName)}"
destPath = f"{IMAGES_DIRNAME}/{fsdecode(destFileName)}"
self.copyFromReader(reader, sourcePath, destPath)
def close(self):
@ -1573,7 +1573,7 @@ class UFOWriter(UFOReader):
rootDir = os.path.splitext(os.path.basename(self._path))[0] + ".ufo"
with fs.zipfs.ZipFS(self._path, write=True, encoding="utf-8") as destFS:
fs.copy.copy_fs(self.fs, destFS.makedir(rootDir))
super(UFOWriter, self).close()
super().close()
# just an alias, makes it more explicit
@ -1668,7 +1668,7 @@ def validateInfoVersion2Data(infoData):
for attr, value in list(infoData.items()):
isValidValue = validateFontInfoVersion2ValueForAttribute(attr, value)
if not isValidValue:
raise UFOLibError("Invalid value for attribute %s (%s)." % (attr, repr(value)))
raise UFOLibError(f"Invalid value for attribute {attr} ({repr(value)}).")
else:
validInfoData[attr] = value
return validInfoData
@ -1712,7 +1712,7 @@ def validateInfoVersion3Data(infoData):
for attr, value in list(infoData.items()):
isValidValue = validateFontInfoVersion3ValueForAttribute(attr, value)
if not isValidValue:
raise UFOLibError("Invalid value for attribute %s (%s)." % (attr, repr(value)))
raise UFOLibError(f"Invalid value for attribute {attr} ({repr(value)}).")
else:
validInfoData[attr] = value
return validInfoData
@ -1730,7 +1730,7 @@ fontInfoOpenTypeOS2TypeOptions = [0, 1, 2, 3, 8, 9]
# cases the possible values, that can exist is
# fontinfo.plist.
fontInfoAttributesVersion1 = set([
fontInfoAttributesVersion1 = {
"familyName",
"styleName",
"fullName",
@ -1771,7 +1771,7 @@ fontInfoAttributesVersion1 = set([
"ttVendor",
"ttUniqueID",
"ttVersion",
])
}
fontInfoAttributesVersion2ValueData = {
"familyName" : dict(type=basestring),
@ -2042,17 +2042,17 @@ def convertFontInfoValueForAttributeFromVersion1ToVersion2(attr, value):
if attr == "fontStyle":
v = _fontStyle1To2.get(value)
if v is None:
raise UFOLibError("Cannot convert value (%s) for attribute %s." % (repr(value), attr))
raise UFOLibError(f"Cannot convert value ({repr(value)}) for attribute {attr}.")
value = v
elif attr == "widthName":
v = _widthName1To2.get(value)
if v is None:
raise UFOLibError("Cannot convert value (%s) for attribute %s." % (repr(value), attr))
raise UFOLibError(f"Cannot convert value ({repr(value)}) for attribute {attr}.")
value = v
elif attr == "msCharSet":
v = _msCharSet1To2.get(value)
if v is None:
raise UFOLibError("Cannot convert value (%s) for attribute %s." % (repr(value), attr))
raise UFOLibError(f"Cannot convert value ({repr(value)}) for attribute {attr}.")
value = v
attr = fontInfoAttributesVersion1To2.get(attr, attr)
return attr, value
@ -2087,7 +2087,7 @@ def _convertFontInfoDataVersion1ToVersion2(data):
continue
# catch values that can't be converted
if value is None:
raise UFOLibError("Cannot convert value (%s) for attribute %s." % (repr(value), newAttr))
raise UFOLibError(f"Cannot convert value ({repr(value)}) for attribute {newAttr}.")
# store
converted[newAttr] = newValue
return converted
@ -2101,23 +2101,23 @@ def _convertFontInfoDataVersion2ToVersion1(data):
continue
# catch values that can't be converted
if value is None:
raise UFOLibError("Cannot convert value (%s) for attribute %s." % (repr(value), newAttr))
raise UFOLibError(f"Cannot convert value ({repr(value)}) for attribute {newAttr}.")
# store
converted[newAttr] = newValue
return converted
# 2 <-> 3
_ufo2To3NonNegativeInt = set((
_ufo2To3NonNegativeInt = {
"versionMinor",
"openTypeHeadLowestRecPPEM",
"openTypeOS2WinAscent",
"openTypeOS2WinDescent"
))
_ufo2To3NonNegativeIntOrFloat = set((
"unitsPerEm"
))
_ufo2To3FloatToInt = set(((
}
_ufo2To3NonNegativeIntOrFloat = {
"unitsPerEm",
}
_ufo2To3FloatToInt = {
"openTypeHeadLowestRecPPEM",
"openTypeHheaAscender",
"openTypeHheaDescender",
@ -2142,7 +2142,7 @@ _ufo2To3FloatToInt = set(((
"openTypeVheaVertTypoDescender",
"openTypeVheaVertTypoLineGap",
"openTypeVheaCaretOffset"
)))
}
def convertFontInfoValueForAttributeFromVersion2ToVersion3(attr, value):
"""

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
glifLib.py -- Generic module for reading and writing the .glif format.
@ -59,7 +58,7 @@ supportedGLIFFormatVersions = [1, 2]
# Simple Glyph
# ------------
class Glyph(object):
class Glyph:
"""
Minimal glyph object. It has no glyph attributes until either
@ -852,7 +851,7 @@ def validateLayerInfoVersion3Data(infoData):
raise GlifLibError("Unknown attribute %s." % attr)
isValidValue = validateLayerInfoVersion3ValueForAttribute(attr, value)
if not isValidValue:
raise GlifLibError("Invalid value for attribute %s (%s)." % (attr, repr(value)))
raise GlifLibError(f"Invalid value for attribute {attr} ({repr(value)}).")
return infoData
# -----------------
@ -1072,13 +1071,13 @@ def _readImage(glyphObject, image, validate):
# GLIF to PointPen
# ----------------
contourAttributesFormat2 = set(["identifier"])
componentAttributesFormat1 = set(["base", "xScale", "xyScale", "yxScale", "yScale", "xOffset", "yOffset"])
componentAttributesFormat2 = componentAttributesFormat1 | set(["identifier"])
pointAttributesFormat1 = set(["x", "y", "type", "smooth", "name"])
pointAttributesFormat2 = pointAttributesFormat1 | set(["identifier"])
pointSmoothOptions = set(("no", "yes"))
pointTypeOptions = set(["move", "line", "offcurve", "curve", "qcurve"])
contourAttributesFormat2 = {"identifier"}
componentAttributesFormat1 = {"base", "xScale", "xyScale", "yxScale", "yScale", "xOffset", "yOffset"}
componentAttributesFormat2 = componentAttributesFormat1 | {"identifier"}
pointAttributesFormat1 = {"x", "y", "type", "smooth", "name"}
pointAttributesFormat2 = pointAttributesFormat1 | {"identifier"}
pointSmoothOptions = {"no", "yes"}
pointTypeOptions = {"move", "line", "offcurve", "curve", "qcurve"}
# format 1
@ -1388,7 +1387,7 @@ def _number(s):
class _DoneParsing(Exception): pass
class _BaseParser(object):
class _BaseParser:
def __init__(self):
self._elementStack = []
@ -1422,7 +1421,7 @@ class _FetchUnicodesParser(_BaseParser):
def __init__(self):
self.unicodes = []
super(_FetchUnicodesParser, self).__init__()
super().__init__()
def startElementHandler(self, name, attrs):
if name == "unicode" and self._elementStack and self._elementStack[-1] == "glyph":
@ -1434,7 +1433,7 @@ class _FetchUnicodesParser(_BaseParser):
self.unicodes.append(value)
except ValueError:
pass
super(_FetchUnicodesParser, self).startElementHandler(name, attrs)
super().startElementHandler(name, attrs)
# image
@ -1453,13 +1452,13 @@ class _FetchImageFileNameParser(_BaseParser):
def __init__(self):
self.fileName = None
super(_FetchImageFileNameParser, self).__init__()
super().__init__()
def startElementHandler(self, name, attrs):
if name == "image" and self._elementStack and self._elementStack[-1] == "glyph":
self.fileName = attrs.get("fileName")
raise _DoneParsing
super(_FetchImageFileNameParser, self).startElementHandler(name, attrs)
super().startElementHandler(name, attrs)
# component references
@ -1478,19 +1477,19 @@ class _FetchComponentBasesParser(_BaseParser):
def __init__(self):
self.bases = []
super(_FetchComponentBasesParser, self).__init__()
super().__init__()
def startElementHandler(self, name, attrs):
if name == "component" and self._elementStack and self._elementStack[-1] == "outline":
base = attrs.get("base")
if base is not None:
self.bases.append(base)
super(_FetchComponentBasesParser, self).startElementHandler(name, attrs)
super().startElementHandler(name, attrs)
def endElementHandler(self, name):
if name == "outline":
raise _DoneParsing
super(_FetchComponentBasesParser, self).endElementHandler(name)
super().endElementHandler(name)
# --------------
# GLIF Point Pen

View File

@ -64,7 +64,7 @@ def deprecated(msg=""):
@functools.wraps(func)
def wrapper(*args, **kwargs):
warnings.warn(
"{} function is a deprecated. {}".format(func.__name__, msg),
f"{func.__name__} function is a deprecated. {msg}",
category=DeprecationWarning,
stacklevel=2,
)