Add DS.getAxisByTag and refactor getAxis

This commit is contained in:
Nikolaus Waxweiler 2022-11-14 10:56:04 +00:00
parent a1fc9b1efc
commit 45c89c0700
2 changed files with 16 additions and 5 deletions

View File

@ -2691,12 +2691,13 @@ class DesignSpaceDocument(LogMixin, AsDictMixin):
names.append(axisDescriptor.name)
return names
def getAxis(self, name):
def getAxis(self, name: str) -> AxisDescriptor | DiscreteAxisDescriptor | None:
"""Return the axis with the given ``name``, or ``None`` if no such axis exists."""
for axisDescriptor in self.axes:
if axisDescriptor.name == name:
return axisDescriptor
return None
return next((axis for axis in self.axes if axis.name == name), None)
def getAxisByTag(self, tag: str) -> AxisDescriptor | DiscreteAxisDescriptor | None:
"""Return the axis with the given ``tag``, or ``None`` if no such axis exists."""
return next((axis for axis in self.axes if axis.tag == tag), None)
def getLocationLabel(self, name: str) -> Optional[LocationLabelDescriptor]:
"""Return the top-level location label with the given ``name``, or

View File

@ -1,6 +1,7 @@
# coding=utf-8
import os
from pathlib import Path
import re
import pytest
@ -21,6 +22,8 @@ from fontTools.designspaceLib import (
from fontTools.designspaceLib.types import Range
from fontTools.misc import plistlib
from .fixtures import datadir
def _axesAsDict(axes):
"""
@ -1064,3 +1067,10 @@ def test_Range_post_init():
assert r.minimum == -1
assert r.maximum == 2
assert r.default == -1
def test_get_axes(datadir: Path) -> None:
ds = DesignSpaceDocument.fromfile(datadir / "test_v5.designspace")
assert ds.getAxis("Width") is ds.getAxisByTag("wdth")
assert ds.getAxis("Italic") is ds.getAxisByTag("ital")