Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions src/napari_resources/generate_logos.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,9 @@
}


TEMPLATE_DIR = resources.files("napari_resources.resources.logos.templates")
VARIANT_DIR = resources.files("napari_resources.resources.logos.variants")

TEMPLATE_FILES = {
template_path.stem: template_path # type: ignore
for template_path in sorted(TEMPLATE_DIR.iterdir(), key=lambda p: p.name)
if template_path.suffix == ".svg"
}
VARIANT_FILES = {
variant_path.stem: variant_path # type: ignore
for variant_path in sorted(VARIANT_DIR.iterdir(), key=lambda p: p.name)
if variant_path.suffix == ".svg"
}
def _dict_from_svgs(directory):
"""Return a dictionary mapping svg base names (stems) to their full paths."""
return {svg.stem: svg for svg in sorted(directory.iterdir(), key=lambda p: p.name) if svg.suffix == ".svg"}


def _change_border_color(root, color):
Expand Down Expand Up @@ -93,22 +83,28 @@ def _copy_defs(orig, dest):
dest_defs.append(copy.deepcopy(el))


def generate_single_logo(variant, template, mode, output_dir, png=False, icons=False):
def generate_single_logo(
variant, template, mode, output_dir, png=False, icons=False, variant_files=None, template_files=None
):
"""Generate a single logo combination."""
if variant in VARIANT_FILES:
variant_path = VARIANT_FILES[variant]
if variant_files is None:
variant_files = _dict_from_svgs(resources.files("napari_resources.resources.logos.variants"))
if template_files is None:
template_files = _dict_from_svgs(resources.files("napari_resources.resources.logos.templates"))
if variant in variant_files:
variant_path = variant_files[variant]
else:
variant_path = Path(variant)
if not variant_path.is_file() or variant_path.suffix != ".svg":
raise ValueError(f"variant must be either one of {set(VARIANT_FILES)} or a valid svg file. Got {variant}")
raise ValueError(f"variant must be either one of {set(variant_files)} or a valid svg file. Got {variant}")
variant = variant_path.stem
if template in TEMPLATE_FILES:
template_path = TEMPLATE_FILES[template]
if template in template_files:
template_path = template_files[template]
else:
template_path = Path(template)
if not template_path.is_file() or template_path.suffix != ".svg":
raise ValueError(
f"template must be either one of {set(TEMPLATE_FILES)} or a valid svg file. Got {template}"
f"template must be either one of {set(template_files)} or a valid svg file. Got {template}"
)
template = template_path.stem

Expand Down Expand Up @@ -219,15 +215,23 @@ def generate_logos(
icons=False,
montage=False,
quiet=True,
variants_dir=None,
templates_dir=None,
):
"""Generate logos based on variants, template and theme.

Template, variant and mode options may be passed more than once.
An empty option means all.
"""
output_dir = Path(output_dir)
selected_templates = selected_templates or list(TEMPLATE_FILES)
selected_variants = selected_variants or list(VARIANT_FILES)
if variants_dir is None:
variants_dir = resources.files("napari_resources.resources.logos.variants")
if templates_dir is None:
templates_dir = resources.files("napari_resources.resources.logos.templates")
available_templates = _dict_from_svgs(templates_dir)
available_variants = _dict_from_svgs(variants_dir)
selected_templates = selected_templates or list(available_templates)
selected_variants = selected_variants or list(available_variants)
selected_modes = selected_modes or ("light", "dark")

for variant, template, mode in product(selected_variants, selected_templates, selected_modes):
Expand All @@ -238,6 +242,8 @@ def generate_logos(
output_dir=output_dir,
png=png,
icons=icons,
variant_files=available_variants,
template_files=available_templates,
)
if not quiet:
print(f"Generated {generated_svg.stem}")
Expand Down Expand Up @@ -276,6 +282,9 @@ def main() -> None:
"""
import click

templates_dir = resources.files("napari_resources.resources.logos.templates")
variants_dir = resources.files("napari_resources.resources.logos.variants")

@click.command(
context_settings={"help_option_names": ["-h", "--help"], "show_default": True},
)
Expand All @@ -289,15 +298,15 @@ def main() -> None:
"selected_variants",
type=str,
multiple=True,
help=f"Logo variant to use. Can be either a custom svg path, or one of: {set(VARIANT_FILES)}",
help=f"Logo variant to use. Can be either a custom svg path, or one of: {set(_dict_from_svgs(variants_dir))}",
)
@click.option(
"-t",
"--template",
"selected_templates",
type=str,
multiple=True,
help=f"Logo template to use. Can be either a custom svg path, or one of: {set(TEMPLATE_FILES)}",
help=f"Logo template to use. Can be either a custom svg path, or one of: {set(_dict_from_svgs(templates_dir))}",
)
@click.option(
"-m",
Expand All @@ -318,7 +327,7 @@ def main() -> None:
@click.option("-q", "--quiet", is_flag=True, help="Do not print progress.")
def cli(**kwargs):
"""Generate logos based on variants, template and theme."""
generate_logos(**kwargs)
generate_logos(**kwargs, templates_dir=templates_dir, variants_dir=variants_dir)

cli()

Expand Down