Skip to content

Commit

Permalink
Use loguru and add '--recursive' option (#105)
Browse files Browse the repository at this point in the history
* Add '--recursive' option to suitable commands to search for font files in subbolders

* Use loguru instead of click styled messages
  • Loading branch information
ftCLI authored Sep 22, 2023
1 parent 56aff29 commit 45189e4
Show file tree
Hide file tree
Showing 30 changed files with 1,196 additions and 767 deletions.
54 changes: 25 additions & 29 deletions foundryToolsCLI/CLI/ftcli_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@
from foundryToolsCLI.Lib.utils.cli_tools import (
get_style_mapping_path,
get_fonts_data_path,
get_output_dir,
)
from foundryToolsCLI.Lib.utils.click_tools import (
add_file_or_path_argument,
add_common_options,
file_overwrite_prompt,
generic_error_message,
file_saved_message,
file_not_changed_message,
generic_warning_message,
file_not_selected_message,
add_path_argument,
)
from foundryToolsCLI.Lib.utils.logger import logger, Logs

assistant = click.Group("subcommands")

Expand All @@ -39,12 +33,12 @@
Suppress the overwrite confirmation message if the fonts_data.csv and/or styles_mapping.json files already
exist in the ftCLI_files folder.""",
)
def init(input_path, quiet=False):
def init(input_path: Path, quiet: bool = False):
"""
Creates the 'styles_mapping.json' and the 'fonts_data.csv' files in the 'ftCLI_files' directory. If one or both
files already exist, user will be asked to overwrite.
Creates the ``styles_mapping.json`` and the ``fonts_data.csv`` files in the ``ftCLI_files`` directory. If one or
both files already exist, user will be prompted for overwrite.
Both files can be edited manually or using the 'ftcli assistant ui' command.
Both files can be edited manually or using the ``ftcli assistant ui`` command.
"""

try:
Expand All @@ -57,9 +51,9 @@ def init(input_path, quiet=False):

if styles_mapping_overwrite is True:
styles_mapping.reset_defaults()
file_saved_message(styles_mapping_file)
logger.success(Logs.file_saved, file=styles_mapping_file)
else:
file_not_changed_message(styles_mapping_file)
logger.skip(Logs.file_not_changed, file=styles_mapping_file)

fonts_data_file = get_fonts_data_path(input_path)
fonts_data = FontsData(fonts_data_file)
Expand All @@ -69,15 +63,15 @@ def init(input_path, quiet=False):
fonts_data_overwrite = True
if fonts_data_overwrite is True:
fonts_data.reset_data(styles_mapping=styles_mapping)
file_saved_message(fonts_data_file)
logger.success(Logs.file_saved, file=fonts_data_file)
else:
file_not_changed_message(fonts_data_file)
logger.skip(Logs.file_not_changed, file=fonts_data_file)
except Exception as e:
generic_error_message(e)
logger.exception(e)


@assistant.command()
@add_path_argument()
@add_file_or_path_argument(file_okay=False)
@click.option(
"--width-elidable",
default="Normal",
Expand Down Expand Up @@ -228,11 +222,11 @@ def commit(
Writes data from CSV to fonts.
"""

output_dir = get_output_dir(input_path=input_path, output_dir=output_dir)
try:
output_dir.mkdir(exist_ok=True)
except Exception as e:
generic_error_message(e)
if output_dir is not None:
try:
output_dir.mkdir(exist_ok=True, parents=True)
except Exception as e:
logger.exception(e)

if linked_styles:
linked_styles = sorted(linked_styles)
Expand All @@ -249,22 +243,25 @@ def commit(
fonts_data = FontsData(get_fonts_data_path(input_path))
data = fonts_data.get_data()
except Exception as e:
generic_error_message(e)
logger.exception(e)
return

for row in data:
file = Path(row["file_name"])
if not Path.exists(file):
generic_warning_message(f"{file} {click.style('file does not exist', fg='yellow')}")
logger.warning(Logs.file_not_exists, file)
continue

file_is_selected = bool(int(row["selected"]))
if not file_is_selected:
file_not_selected_message(file)
logger.warning(Logs.file_not_selected, file=file)
continue

try:
font = Font(file, recalcTimestamp=recalc_timestamp)
output_file = Path(makeOutputFileName(file, outputDir=output_dir, overWrite=overwrite))

logger.opt(colors=True).info(Logs.current_file, file=file)

name_table = font["name"]
name_table_copy = deepcopy(name_table)
Expand Down Expand Up @@ -309,14 +306,13 @@ def commit(
font_has_changed = True

if font_has_changed:
output_file = Path(makeOutputFileName(file, outputDir=output_dir, overWrite=overwrite))
font.save(output_file)
file_saved_message(output_file)
logger.success(Logs.file_saved, file=output_file)
else:
file_not_changed_message(file)
logger.skip(Logs.file_not_changed, file=file)

except Exception as e:
generic_error_message(e)
logger.exception(e)


@assistant.command()
Expand Down
76 changes: 48 additions & 28 deletions foundryToolsCLI/CLI/ftcli_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
from fontTools.misc.cliTools import makeOutputFileName

from foundryToolsCLI.Lib.tables.CFF_ import TableCFF
from foundryToolsCLI.Lib.utils.cli_tools import get_fonts_in_path, get_output_dir, initial_check_pass
from foundryToolsCLI.Lib.utils.cli_tools import get_fonts_in_path, initial_check_pass
from foundryToolsCLI.Lib.utils.click_tools import (
add_file_or_path_argument,
add_recursive_option,
add_common_options,
generic_error_message,
file_saved_message,
file_not_changed_message,
)
from foundryToolsCLI.Lib.utils.logger import logger, Logs

tbl_cff = click.Group("subcommands")

Expand All @@ -25,45 +24,52 @@
@click.option("--version", "version", is_flag=True, help="Deletes CFF.cff.topDictIndex[0] version")
@click.option("--copyright", "Copyright", is_flag=True, help="Deletes CFF.cff.topDictIndex[0] Copyright")
@click.option("--notice", "Notice", is_flag=True, help="Deletes CFF.cff.topDictIndex[0] Copyright")
@add_recursive_option()
@add_common_options()
def del_names(
input_path: Path, recalc_timestamp: bool = False, output_dir: Path = None, overwrite: bool = True, **kwargs
input_path: Path,
recursive: bool = False,
recalc_timestamp: bool = False,
output_dir: Path = None,
overwrite: bool = True,
**kwargs
):
"""
Deletes CFF names from topDict.
Deletes CFF names in topDict.
"""
params = {k: v for k, v in kwargs.items() if v}
if len(params) == 0:
generic_error_message("Please, pass at least a valid parameter.")
logger.error(Logs.no_parameter)
return

fonts = get_fonts_in_path(
input_path=input_path,
recursive=recursive,
recalc_timestamp=recalc_timestamp,
allow_ttf=False,
allow_variable=False,
)
output_dir = get_output_dir(input_path=input_path, output_dir=output_dir)
if not initial_check_pass(fonts=fonts, output_dir=output_dir):
return

for font in fonts:
try:
file = Path(font.reader.file.name)
output_file = Path(makeOutputFileName(file, outputDir=output_dir, overWrite=overwrite))

logger.opt(colors=True).info(Logs.current_file, file=file)

cff_table: TableCFF = font["CFF "]
cff_table_copy = deepcopy(cff_table)

cff_table.del_top_dict_names(list(params.keys()))

if cff_table.compile(font) != cff_table_copy.compile(font):
output_file = Path(makeOutputFileName(file, outputDir=output_dir, overWrite=overwrite))
font.save(output_file)
file_saved_message(output_file)
logger.success(Logs.file_saved, file=output_file)
else:
file_not_changed_message(file)

logger.skip(Logs.file_not_changed, file=file)
except Exception as e:
generic_error_message(e)
logger.exception(e)
finally:
font.close()

Expand All @@ -85,46 +91,55 @@ def del_names(
)
@click.option("--weight", "Weight", type=str, help="Sets CFF.cff.topDictIndex[0] Weight value")
@click.option("--version", "version", type=str, help="Sets CFF.cff.topDictIndex[0] version value")
@add_recursive_option()
@add_common_options()
def set_names(
input_path: Path, recalc_timestamp: bool = False, output_dir: Path = None, overwrite: bool = True, **kwargs
input_path: Path,
recursive: bool = False,
recalc_timestamp: bool = False,
output_dir: Path = None,
overwrite: bool = True,
**kwargs
):
"""
Sets CFF names in topDict.
Sets CFF names in topDict. If the name is not present, it will be added. If the name is present, it will be
replaced.
"""

params = {k: v for k, v in kwargs.items() if v is not None}
if len(params) == 0:
generic_error_message("Please, pass at least a valid parameter.")
logger.error(Logs.no_parameter)
return

fonts = get_fonts_in_path(
input_path=input_path,
recursive=recursive,
recalc_timestamp=recalc_timestamp,
allow_ttf=False,
allow_variable=False,
)
output_dir = get_output_dir(input_path=input_path, output_dir=output_dir)
if not initial_check_pass(fonts=fonts, output_dir=output_dir):
return

for font in fonts:
try:
file = Path(font.reader.file.name)
output_file = Path(makeOutputFileName(file, outputDir=output_dir, overWrite=overwrite))

logger.opt(colors=True).info(Logs.current_file, file=file)

cff_table: TableCFF = font["CFF "]
cff_table_copy = deepcopy(cff_table)

cff_table.set_top_dict_names(params)

if cff_table.compile(font) != cff_table_copy.compile(font):
output_file = Path(makeOutputFileName(file, outputDir=output_dir, overWrite=overwrite))
font.save(output_file)
file_saved_message(output_file)
logger.success(Logs.file_saved, file=output_file)
else:
file_not_changed_message(file)
logger.skip(Logs.file_not_changed, file=file)

except Exception as e:
generic_error_message(e)
logger.exception(e)
finally:
font.close()

Expand All @@ -139,11 +154,13 @@ def set_names(
help="The string to replace the old string with",
show_default=True,
)
@add_recursive_option()
@add_common_options()
def find_replace(
input_path: Path,
old_string: str,
new_string: str,
recursive: bool = False,
output_dir: Path = None,
recalc_timestamp: bool = False,
overwrite: bool = True,
Expand All @@ -155,31 +172,34 @@ def find_replace(

fonts = get_fonts_in_path(
input_path=input_path,
recursive=recursive,
recalc_timestamp=recalc_timestamp,
allow_ttf=False,
allow_variable=False,
)
output_dir = get_output_dir(input_path=input_path, output_dir=output_dir)
if not initial_check_pass(fonts=fonts, output_dir=output_dir):
return

for font in fonts:
try:
file = Path(font.reader.file.name)
output_file = Path(makeOutputFileName(file, outputDir=output_dir, overWrite=overwrite))

logger.opt(colors=True).info(Logs.current_file, file=file)

cff_table: TableCFF = font["CFF "]
cff_table_copy = deepcopy(cff_table)

cff_table.top_dict_find_replace(old_string=old_string, new_string=new_string)

if cff_table.compile(font) != cff_table_copy.compile(font):
output_file = Path(makeOutputFileName(file, outputDir=output_dir, overWrite=overwrite))
font.save(output_file)
file_saved_message(output_file)
logger.success(Logs.file_saved, file=output_file)
else:
file_not_changed_message(file)
logger.skip(Logs.file_not_changed, file=file)

except Exception as e:
generic_error_message(e)
logger.exception(e)
finally:
font.close()

Expand Down
Loading

0 comments on commit 45189e4

Please sign in to comment.