Skip to content

Commit

Permalink
refactor: use updated python typings
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyrt committed Jul 19, 2024
1 parent 5be772b commit db960ba
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 35 deletions.
12 changes: 6 additions & 6 deletions scripts/builder/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from shutil import rmtree
from tempfile import mkdtemp
from typing import Callable, List
from typing import Callable

from glyphsLib import (
GSClass,
Expand Down Expand Up @@ -33,15 +33,15 @@ def __init__(self, path: str):
def file(self) -> GSFont:
return self._font

def ligatures(self) -> List[str]:
def ligatures(self) -> list[str]:
glyphs = self.glyphs(lambda x: x.name.endswith(LIGATURE_SUFFIX))
ligatures = []
for glyph in glyphs:
if glyph.export:
ligatures.append(glyph.name)
return ligatures

def glyphs(self, _filter: GlyphFilter = lambda x: True) -> List[GSGlyph]:
def glyphs(self, _filter: GlyphFilter = lambda x: True) -> list[GSGlyph]:
"""Returns a list of glyphs that match filter"""
result = []
for glyph in self._font.glyphs:
Expand All @@ -57,15 +57,15 @@ def save_to(self, path: str) -> None:
"""Saves the file to the specified path"""
self._font.save(path)

def set_classes(self, classes: List[GSClass]):
def set_classes(self, classes: list[GSClass]):
"""Sets the font classes"""
for cls in classes:
if cls.name in self._font.classes:
self._font.classes[cls.name] = cls
else:
self._font.classes.append(cls)

def set_features(self, features: List[GSFeature]):
def set_features(self, features: list[GSFeature]):
"""Sets the font features"""
for fea in features:
if fea.name in self._font.features:
Expand All @@ -79,7 +79,7 @@ def set_version(self, version: str):
self._font.versionMajor = int(parts[0])
self._font.versionMinor = int(parts[1])

def build(self, formats: List[str], out_dir: str, store_temp=False) -> bool:
def build(self, formats: list[str], out_dir: str, store_temp=False) -> bool:
print("Preparing build environment")
temp_dir, ds_file = self._prepare_build()
success = True
Expand Down
3 changes: 1 addition & 2 deletions scripts/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
Usage: find.py ../features/calt/hyphen-arrows.fea missing
"""
from re import sub
from typing import List

from arrrgs import arg, command, run
from builder import GlyphsFont
Expand Down Expand Up @@ -85,7 +84,7 @@ def _report_progress(title: str, total: int, current: int):
def _is_glyph_name(word: str) -> bool:
return len(word) > 0 and word[0] != "@" and word not in KEYWORDS

def glyphs_from_fea(path: str) -> List[str]:
def glyphs_from_fea(path: str) -> list[str]:
with open(path, mode="r", encoding="utf-8") as file:
content = file.read()
# Remove comments
Expand Down
13 changes: 6 additions & 7 deletions scripts/generator/ligatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@
from __future__ import annotations

from re import sub
from typing import List

from .const import IGNORE_TPL, IGNORES, PRIORITIES, REPLACE_TPL, SKIP_IGNORES


def _populate_tpl(templates: List[str], prefix: str) -> str:
def _populate_tpl(templates: list[str], prefix: str) -> str:
"""Renders fea statements"""
result = ""
for value in templates:
normalized_value = sub(" +", " ", value)
result += f' {prefix} {normalized_value};\n'
return result

def _populate_ignore(templates: List[str]) -> str:
def _populate_ignore(templates: list[str]) -> str:
"""Renders ignore sub statements"""
return _populate_tpl(templates, "ignore sub")

def _populate_sub(templates: List[str]) -> str:
def _populate_sub(templates: list[str]) -> str:
"""Renders sub statements"""
return _populate_tpl(templates, "sub")

class LigatureLookup:
ignores: List[str] = []
subs: List[str] = []
ignores: list[str] = []
subs: list[str] = []
glyphs: tuple
name: str

Expand Down Expand Up @@ -63,7 +62,7 @@ def _hydrate(self, template: str) -> str:
result = result.replace(str(i + 1), glyph)
return result

def render_ligatures(items: List[str]) -> str:
def render_ligatures(items: list[str]) -> str:
"""Renders the list of ligatures in the OpenType feature"""
lookups = []
for name in items:
Expand Down
6 changes: 2 additions & 4 deletions scripts/generator/spacers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""Spacer generator"""
from typing import List

from glyphsLib import GSGlyph


def _required_spacers(name: str) -> List[str]:
def _required_spacers(name: str) -> list[str]:
return name.split("_")[0:-1]

def generate_spacers(ligatures: List[str], glyphs: List[GSGlyph]) -> List[str]:
def generate_spacers(ligatures: list[str], glyphs: list[GSGlyph]) -> list[str]:
"""Finds missing spacers"""
unique = []
for liga in ligatures:
Expand Down
3 changes: 1 addition & 2 deletions scripts/lilex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""Lilex helper entrypoint"""
import sys
from argparse import BooleanOptionalAction
from typing import List

from arrrgs import arg, command, global_args, run
from builder import DEFAULT_FORMATS, GlyphsFont
Expand Down Expand Up @@ -66,7 +65,7 @@ def generate_calt(font: GlyphsFont) -> GSFeature:
code = render_ligatures(glyphs) + read_files(f"{FEATURES_DIR}/calt")
return GSFeature("calt", code)

def move_to_calt(font: GSFont, features: List[str]):
def move_to_calt(font: GSFont, features: list[str]):
for fea in features:
if fea not in font.features:
print(f"Unknown feature: '{fea}'")
Expand Down
4 changes: 1 addition & 3 deletions scripts/preview/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Feature preview base"""
from typing import List

from colored import Style


Expand All @@ -14,7 +12,7 @@ def __init__(self) -> None:
def show(self):
"""Prints feature"""

def print_features(features: List[FeaturePreview]):
def print_features(features: list[FeaturePreview]):
for fea in features:
print(f'{Style.BOLD}{fea.name}{Style.reset}')
fea.show()
6 changes: 2 additions & 4 deletions scripts/preview/powerline.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""Powerline preview"""
from typing import List, Tuple

from colored import Style, back, fore

from .base import FeaturePreview

PowerlineEntry = Tuple[str, str]
PowerlineEntry = tuple[str, str]

VC_BRANCH_CHAR = "\uE0A0"
LN_CHAR = "\uE0A1"
Expand Down Expand Up @@ -39,7 +37,7 @@ def _print_examples(self):
])


def _print_line(self, entries: List[PowerlineEntry], right=True):
def _print_line(self, entries: list[PowerlineEntry], right=True):
line = ""
if right:
arrow = RIGHT_ARROW_CHAR
Expand Down
4 changes: 1 addition & 3 deletions scripts/utils/cli.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
"""CLI utilities"""
from typing import List

from glyphsLib import GSFeature

ORANGE = "\033[93m"
RESET = "\033[0m"

def print_gs(title: str, items: List[GSFeature]):
def print_gs(title: str, items: list[GSFeature]):
print(f"{title}:")
for item in items:
print(f" - {item.name}")
Expand Down
8 changes: 4 additions & 4 deletions scripts/utils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

from os import listdir
from os.path import basename, isfile, join, splitext
from typing import List, TypeVar
from typing import TypeVar

from glyphsLib import GSClass, GSFeature

T = TypeVar("T")

def list_files(dir_path: str) -> List[str]:
def list_files(dir_path: str) -> list[str]:
files = []
for file in listdir(dir_path):
name = splitext(file)[0]
Expand All @@ -21,14 +21,14 @@ def list_files(dir_path: str) -> List[str]:
files.append(file_path)
return sorted(files)

def read_classes(dir_path: str) -> List[GSClass]:
def read_classes(dir_path: str) -> list[GSClass]:
classes = []
for path in list_files(dir_path):
cls = _read_gs_file(path, GSClass)
classes.append(cls)
return classes

def read_features(dir_path: str) -> List[GSFeature]:
def read_features(dir_path: str) -> list[GSFeature]:
features = []
for path in list_files(dir_path):
fea = _read_gs_file(path, GSFeature)
Expand Down

0 comments on commit db960ba

Please sign in to comment.