Skip to content

Commit

Permalink
Use type annotations and fix mypy errors (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
ftCLI authored Sep 27, 2023
1 parent 63d5bdc commit d0df437
Show file tree
Hide file tree
Showing 21 changed files with 258 additions and 242 deletions.
74 changes: 36 additions & 38 deletions foundryToolsCLI/CLI/ftcli_assistant.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import typing as t

from copy import copy, deepcopy
from pathlib import Path

Expand All @@ -16,6 +18,8 @@
add_file_or_path_argument,
add_common_options,
file_overwrite_prompt,
linked_styles_callback,
choice_to_int_callback,
)
from foundryToolsCLI.Lib.utils.logger import logger, Logs
from foundryToolsCLI.Lib.utils.timer import Timer
Expand Down Expand Up @@ -88,6 +92,7 @@ def init(input_path: Path, quiet: bool = False):
"-ls",
"--linked-styles",
type=(click.IntRange(1, 1000), click.IntRange(1, 1000)),
callback=linked_styles_callback,
help="""Use this option to activate linked styles. If this option is active, linked styles must be specified. For
example: -ls 400 700, or -ls 300 600.
""",
Expand All @@ -97,6 +102,7 @@ def init(input_path: Path, quiet: bool = False):
"--exclude-namerecords",
type=click.Choice(choices=["1", "2", "3", "4", "5", "6", "16", "17", "18"]),
multiple=True,
callback=choice_to_int_callback,
help="""
Name IDs to skip. The specified name IDs won't be recalculated. This option can be repeated (for example: -x 3 -x 5
-x 6...).
Expand All @@ -107,6 +113,7 @@ def init(input_path: Path, quiet: bool = False):
"--shorten-width",
type=click.Choice(choices=["1", "4", "6", "16", "17"]),
multiple=True,
callback=choice_to_int_callback,
help="""
Name IDs where to use the short word for width style name (for example, 'Cn' instead of 'Condensed'). This option
can be repeated (for example: -swdt 1 -swdt 5, -swdt 16...).
Expand All @@ -117,11 +124,23 @@ def init(input_path: Path, quiet: bool = False):
"--shorten-weight",
type=click.Choice(choices=["1", "4", "6", "17"]),
multiple=True,
callback=choice_to_int_callback,
help="""
Name IDs where to use the short word for weight style name (for example, 'Md' instead of 'Medium'). This option can
be repeated (for example: -swgt 1 -swgt 5 -swgt 6...).
""",
)
@click.option(
"-sslp",
"--shorten-slope",
type=click.Choice(choices=["4", "6", "16", "17"]),
multiple=True,
callback=choice_to_int_callback,
help="""
Name IDs where to use the short word for slope style name (for example, 'It' instead of 'Italic'). This option can
be repeated (for example: -sslp 3 -sslp 5 -sslp 6...).
""",
)
@click.option(
"-kwdt",
"--keep-width-elidable",
Expand All @@ -138,16 +157,6 @@ def init(input_path: Path, quiet: bool = False):
Doesn't remove the weight elidable words (by default, "Rg" and "Regular").
""",
)
@click.option(
"-sslp",
"--shorten-slope",
type=click.Choice(choices=["4", "6", "16", "17"]),
multiple=True,
help="""
Name IDs where to use the short word for slope style name (for example, 'It' instead of 'Italic'). This option can
be repeated (for example: -sslp 3 -sslp 5 -sslp 6...).
""",
)
@click.option(
"-sf",
"--super-family",
Expand Down Expand Up @@ -201,23 +210,23 @@ def init(input_path: Path, quiet: bool = False):
@Timer(logger=logger.info)
def commit(
input_path: Path,
width_elidable: str,
weight_elidable: str,
linked_styles: list = None,
exclude_namerecords=None,
shorten_width=None,
shorten_weight=None,
keep_width_elidable=False,
keep_weight_elidable=False,
shorten_slope=None,
super_family=False,
alt_uid=False,
oblique_not_italic=False,
auto_shorten=True,
cff=False,
output_dir=None,
recalc_timestamp=False,
overwrite=True,
linked_styles: t.Optional[t.Tuple[int, int]] = None,
exclude_namerecords: t.Union[t.Tuple[int], t.Tuple[()]] = (),
shorten_width: t.Union[t.Tuple[int], t.Tuple[()]] = (),
shorten_weight: t.Union[t.Tuple[int], t.Tuple[()]] = (),
shorten_slope: t.Union[t.Tuple[int], t.Tuple[()]] = (),
width_elidable: str = "Normal",
weight_elidable: str = "Regular",
keep_width_elidable: bool = False,
keep_weight_elidable: bool = False,
super_family: bool = False,
alt_uid: bool = False,
oblique_not_italic: bool = False,
auto_shorten: bool = True,
cff: bool = False,
output_dir: t.Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
):
"""
Writes data from CSV to fonts.
Expand All @@ -229,17 +238,6 @@ def commit(
except Exception as e:
logger.exception(e)

if linked_styles:
linked_styles = sorted(linked_styles)
if exclude_namerecords:
exclude_namerecords = sorted(set(int(i) for i in exclude_namerecords))
if shorten_width:
shorten_width = sorted(set(int(i) for i in shorten_width))
if shorten_weight:
shorten_weight = sorted(set(int(i) for i in shorten_weight))
if shorten_slope:
shorten_slope = sorted(set(int(i) for i in shorten_slope))

try:
fonts_data = FontsData(get_fonts_data_path(input_path))
data = fonts_data.get_data()
Expand Down
8 changes: 5 additions & 3 deletions foundryToolsCLI/CLI/ftcli_cff.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import typing as t

from copy import deepcopy
from pathlib import Path

Expand Down Expand Up @@ -32,7 +34,7 @@ def del_names(
input_path: Path,
recursive: bool = False,
recalc_timestamp: bool = False,
output_dir: Path = None,
output_dir: t.Optional[Path] = None,
overwrite: bool = True,
**kwargs
):
Expand Down Expand Up @@ -99,7 +101,7 @@ def set_names(
input_path: Path,
recursive: bool = False,
recalc_timestamp: bool = False,
output_dir: Path = None,
output_dir: t.Optional[Path] = None,
overwrite: bool = True,
**kwargs
):
Expand Down Expand Up @@ -163,7 +165,7 @@ def find_replace(
old_string: str,
new_string: str,
recursive: bool = False,
output_dir: Path = None,
output_dir: t.Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
):
Expand Down
25 changes: 17 additions & 8 deletions foundryToolsCLI/CLI/ftcli_converter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import typing as t

from pathlib import Path

import click
Expand Down Expand Up @@ -42,11 +44,11 @@
def ttf2otf(
input_path: Path,
tolerance: float = 1.0,
scale_upm: int = None,
scale_upm: t.Optional[int] = None,
subroutinize: bool = True,
recalc_timestamp: bool = False,
recursive: bool = False,
output_dir: Path = None,
output_dir: t.Optional[Path] = None,
overwrite: bool = True,
verbose: bool = True,
):
Expand Down Expand Up @@ -92,7 +94,7 @@ def otf2ttf(
max_err: float = 1.0,
recalc_timestamp: bool = False,
recursive: bool = False,
output_dir: Path = None,
output_dir: t.Optional[Path] = None,
overwrite: bool = True,
):
"""
Expand Down Expand Up @@ -157,7 +159,7 @@ def vf2i(
cleanup: bool = True,
update_name_table: bool = True,
recursive: bool = False,
output_dir: Path = None,
output_dir: t.Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
):
Expand Down Expand Up @@ -228,9 +230,9 @@ def vf2i(
@Timer(logger=logger.info)
def wf2ft(
input_path: Path,
flavor: str = None,
flavor: t.Optional[str] = None,
recursive: bool = False,
output_dir: Path = None,
output_dir: t.Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
):
Expand Down Expand Up @@ -282,7 +284,14 @@ def wf2ft(
@add_recursive_option()
@add_common_options()
@Timer(logger=logger.info)
def ft2wf(input_path, flavor=None, recursive: bool = False, output_dir=None, recalc_timestamp=False, overwrite=True):
def ft2wf(
input_path: Path,
flavor: t.Optional[str] = None,
recursive: bool = False,
output_dir: t.Optional[Path] = None,
recalc_timestamp=False,
overwrite=True,
):
"""
Converts SFNT fonts (TTF or OTF) to web fonts (WOFF and/or WOFF2)
"""
Expand Down Expand Up @@ -315,7 +324,7 @@ def ft2wf(input_path, flavor=None, recursive: bool = False, output_dir=None, rec
@Timer(logger=logger.info)
def ttc2sfnt(
input_path: Path,
output_dir: Path = None,
output_dir: t.Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
):
Expand Down
20 changes: 11 additions & 9 deletions foundryToolsCLI/CLI/ftcli_fix.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from copy import deepcopy
from pathlib import Path

Expand Down Expand Up @@ -28,7 +30,7 @@
def monospace(
input_path: Path,
recursive: bool = False,
output_dir: Path = None,
output_dir: Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
):
Expand Down Expand Up @@ -175,7 +177,7 @@ def monospace(
def nbsp_width(
input_path: Path,
recursive: bool = False,
output_dir: Path = None,
output_dir: Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
):
Expand Down Expand Up @@ -267,7 +269,7 @@ def italic_angle(
input_path: Path,
mode: int = 1,
recursive: bool = False,
output_dir: Path = None,
output_dir: Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
min_slant: float = 2.0,
Expand Down Expand Up @@ -442,7 +444,7 @@ def italic_angle(
def nbsp_missing(
input_path: Path,
recursive: bool = False,
output_dir: Path = None,
output_dir: Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
):
Expand Down Expand Up @@ -499,7 +501,7 @@ def nbsp_missing(
def decompose_transformed(
input_path: Path,
recursive: bool = False,
output_dir: Path = None,
output_dir: Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
):
Expand Down Expand Up @@ -605,7 +607,7 @@ def decompose_transformed(
def duplicate_components(
input_path: Path,
recursive: bool = False,
output_dir: Path = None,
output_dir: Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
):
Expand Down Expand Up @@ -681,7 +683,7 @@ def duplicate_components(
def kern_table(
input_path: Path,
recursive: bool = False,
output_dir: Path = None,
output_dir: Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
):
Expand Down Expand Up @@ -768,7 +770,7 @@ def kern_table(
def strip_names(
input_path: Path,
recursive: bool = False,
output_dir: Path = None,
output_dir: Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
):
Expand Down Expand Up @@ -821,7 +823,7 @@ def strip_names(
@Timer(logger=logger.info)
def empty_names(
input_path: Path,
output_dir: Path = None,
output_dir: Optional[Path] = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
):
Expand Down
Loading

0 comments on commit d0df437

Please sign in to comment.