2022-03-25 18:22:37 +01:00
|
|
|
"""frontools main module"""
|
2022-03-22 17:26:51 +01:00
|
|
|
from asyncio import run
|
|
|
|
from functools import update_wrapper
|
2022-03-25 18:47:36 +01:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any, Optional
|
2022-03-22 17:26:51 +01:00
|
|
|
|
2022-03-28 18:26:50 +02:00
|
|
|
from click import Context as ClickContext
|
2022-03-22 17:26:51 +01:00
|
|
|
from click import Path as PathArgument
|
2022-04-11 12:38:03 +02:00
|
|
|
from click import argument, group, option, pass_context, pass_obj
|
2022-03-22 17:26:51 +01:00
|
|
|
|
2022-04-11 13:01:45 +02:00
|
|
|
from frontools.cache import Cache
|
2022-04-11 14:29:50 +02:00
|
|
|
from frontools.config import Config
|
2022-03-25 18:47:36 +01:00
|
|
|
from frontools.css import css_diff
|
|
|
|
from frontools.screenshot import screenshot_diff
|
2022-03-22 17:26:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _async_command(function: Any) -> Any:
|
|
|
|
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
|
|
return run(function(*args, **kwargs))
|
2022-03-25 18:47:36 +01:00
|
|
|
|
2022-03-22 17:26:51 +01:00
|
|
|
return update_wrapper(wrapper, function)
|
|
|
|
|
|
|
|
|
2022-03-25 22:02:29 +01:00
|
|
|
@group()
|
2022-03-25 18:47:36 +01:00
|
|
|
@pass_context
|
|
|
|
@option(
|
|
|
|
"-c",
|
|
|
|
"--config-file",
|
|
|
|
type=PathArgument(exists=True, file_okay=True, dir_okay=False),
|
|
|
|
default=None,
|
2022-03-25 22:02:29 +01:00
|
|
|
metavar="FILE",
|
2022-03-28 16:22:18 +02:00
|
|
|
help="Configuration file to use",
|
2022-03-25 18:47:36 +01:00
|
|
|
)
|
2022-03-28 18:26:50 +02:00
|
|
|
@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)
|
2022-04-11 13:35:24 +02:00
|
|
|
@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",
|
|
|
|
)
|
2022-03-28 16:22:18 +02:00
|
|
|
@_async_command
|
2022-03-28 18:26:50 +02:00
|
|
|
async def main(
|
2022-04-11 13:35:24 +02:00
|
|
|
ctx: ClickContext,
|
|
|
|
config_file: Optional[Path],
|
|
|
|
source: str,
|
|
|
|
no_cache: bool,
|
|
|
|
include_urls: list[str],
|
|
|
|
exclude_urls: list[str],
|
2022-03-28 18:26:50 +02:00
|
|
|
) -> None:
|
2022-03-28 16:22:18 +02:00
|
|
|
"""Utilities for EO frontend development."""
|
2022-04-11 14:29:50 +02:00
|
|
|
ctx.obj = await Config.load(
|
2022-04-11 13:35:24 +02:00
|
|
|
config_file, source, not no_cache, include_urls, exclude_urls
|
|
|
|
)
|
2022-03-22 17:26:51 +01:00
|
|
|
|
2022-04-11 14:29:50 +02:00
|
|
|
def _on_close() -> None:
|
|
|
|
ctx.obj.echo_error_summary()
|
2022-04-11 13:56:57 +02:00
|
|
|
|
|
|
|
ctx.call_on_close(_on_close)
|
|
|
|
|
2022-03-25 18:47:36 +01:00
|
|
|
|
2022-04-11 13:01:45 +02:00
|
|
|
@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)
|
|
|
|
|
|
|
|
|
2022-03-25 22:02:29 +01:00
|
|
|
@main.command(name="css-diff")
|
2022-03-28 18:26:50 +02:00
|
|
|
@argument("right_source", type=str)
|
2022-04-11 12:38:03 +02:00
|
|
|
@pass_obj
|
2022-03-25 15:04:21 +01:00
|
|
|
@_async_command
|
2022-04-11 14:29:50 +02:00
|
|
|
async def css_diff_cli(config: Config, right_source: str) -> None:
|
2022-03-28 18:26:50 +02:00
|
|
|
"""Diff CSS"""
|
2022-04-11 14:29:50 +02:00
|
|
|
for _, site in config.sites:
|
2022-03-28 16:52:56 +02:00
|
|
|
for site_url in site.urls:
|
2022-03-29 11:03:48 +02:00
|
|
|
await css_diff(
|
2022-04-11 13:56:57 +02:00
|
|
|
site_url,
|
2022-04-11 14:29:50 +02:00
|
|
|
config.default_source,
|
|
|
|
config.get_source(right_source),
|
2022-03-29 11:03:48 +02:00
|
|
|
)
|
2022-03-25 15:04:21 +01:00
|
|
|
|
2022-03-22 17:26:51 +01:00
|
|
|
|
2022-03-25 22:02:29 +01:00
|
|
|
@main.command(name="screenshot-diff")
|
2022-04-05 16:34:45 +02:00
|
|
|
@argument("source", type=str)
|
2022-03-25 18:47:36 +01:00
|
|
|
@option("-o", "--output-directory", type=PathArgument(), default=None)
|
2022-04-08 14:07:42 +02:00
|
|
|
@option("-r", "--resolution", type=str, default=None)
|
2022-03-22 17:26:51 +01:00
|
|
|
@_async_command
|
2022-04-11 12:38:03 +02:00
|
|
|
@pass_obj
|
2022-03-25 18:47:36 +01:00
|
|
|
async def screenshot_diff_cli(
|
2022-04-11 14:29:50 +02:00
|
|
|
config: Config,
|
2022-04-08 14:07:42 +02:00
|
|
|
source: str,
|
|
|
|
output_directory: Optional[str],
|
|
|
|
resolution: Optional[str],
|
2022-03-25 18:47:36 +01:00
|
|
|
) -> None:
|
2022-03-28 16:52:56 +02:00
|
|
|
"""Generate screenshot diffs"""
|
2022-04-11 14:29:50 +02:00
|
|
|
await screenshot_diff(config, source, output_directory, resolution=resolution)
|
2022-03-22 17:26:51 +01:00
|
|
|
|
|
|
|
|
2022-03-25 18:47:36 +01:00
|
|
|
if __name__ == "__main__":
|
2022-03-28 16:52:56 +02:00
|
|
|
main() # pylint: disable=no-value-for-parameter
|