Merge pull request #2891 from daltonmaag/dskub-add-getaxisbytag
[designspaceLib] Add DS.getAxisByTag and refactor getAxis
This commit is contained in:
commit
8e9bf42cb4
@ -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
|
||||
|
@ -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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user