misc-csechet/frontools/cli.py

115 lines
2.8 KiB
Python

"""frontools main module"""
from asyncio import run
from functools import update_wrapper
from pathlib import Path
from typing import Any, Optional
from click import Context as ClickContext
from click import Path as PathArgument
from click import argument, group, option, pass_context, pass_obj
from frontools.cache import Cache
from frontools.config import Config
from frontools.css import css_diff
from frontools.screenshot import screenshot_diff
def _async_command(function: Any) -> Any:
def wrapper(*args: Any, **kwargs: Any) -> Any:
return run(function(*args, **kwargs))
return update_wrapper(wrapper, function)
@group()
@pass_context
@option(
"-c",
"--config-file",
type=PathArgument(exists=True, file_okay=True, dir_okay=False),
default=None,
metavar="FILE",
help="Configuration file to use",
)
@option(
"-s",
"source",
type=str,
required=True,
default="remote",
help="Source to use (configured in config file)",
)
@option("--no-cache", type=bool, help="Disable caching", count=True)
@option(
"--include-urls",
type=str,
multiple=True,
help="Take into account only urls matching these patterns",
)
@option(
"--exclude-urls",
type=str,
multiple=True,
help="Patterns of urls to ignore",
)
@_async_command
async def main(
ctx: ClickContext,
config_file: Optional[Path],
source: str,
no_cache: bool,
include_urls: list[str],
exclude_urls: list[str],
) -> None:
"""Utilities for EO frontend development."""
ctx.obj = await Config.load(
config_file, source, not no_cache, include_urls, exclude_urls
)
def _on_close() -> None:
ctx.obj.echo_error_summary()
ctx.call_on_close(_on_close)
@main.command(name="prune-caches")
@argument("cache_names", nargs=-1)
def prune_caches(cache_names: list[str]) -> None:
"""Prune frontools caches"""
Cache.prune(cache_names)
@main.command(name="css-diff")
@argument("right_source", type=str)
@pass_obj
@_async_command
async def css_diff_cli(config: Config, right_source: str) -> None:
"""Diff CSS"""
for _, site in config.sites:
for site_url in site.urls:
await css_diff(
site_url,
config.default_source,
config.get_source(right_source),
)
@main.command(name="screenshot-diff")
@argument("source", type=str)
@option("-o", "--output-directory", type=PathArgument(), default=None)
@option("-r", "--resolution", type=str, default=None)
@_async_command
@pass_obj
async def screenshot_diff_cli(
config: Config,
source: str,
output_directory: Optional[str],
resolution: Optional[str],
) -> None:
"""Generate screenshot diffs"""
await screenshot_diff(config, source, output_directory, resolution=resolution)
if __name__ == "__main__":
main() # pylint: disable=no-value-for-parameter