diff --git a/frontools/cli.py b/frontools/cli.py index 2cb87db..76c58d3 100644 --- a/frontools/cli.py +++ b/frontools/cli.py @@ -102,6 +102,7 @@ def prune_caches(cache_names: list[str]) -> None: @argument("source", type=str) @option("-o", "--output-directory", type=PathArgument(), default=None) @option("-w", "--screen-width", type=int, default=None) +@option("-c", "--continue", "continue_", type=bool, help="Don't reshot already existing screenshots", count=True) @_async_command @pass_obj async def screenshot_diff_cli( @@ -109,9 +110,10 @@ async def screenshot_diff_cli( source: str, output_directory: Optional[str], screen_width: Optional[int], + continue_: bool ) -> None: """Generate screenshot diffs""" - await screenshot_diff(config, source, output_directory, screen_width=screen_width) + await screenshot_diff(config, source, output_directory, screen_width=screen_width, continue_=continue_) if __name__ == "__main__": diff --git a/frontools/screenshot.py b/frontools/screenshot.py index 273e406..430632d 100644 --- a/frontools/screenshot.py +++ b/frontools/screenshot.py @@ -19,6 +19,7 @@ async def screenshot_diff( config: Config, right_source_name: str, output_directory: Optional[str], + continue_: bool = False, screen_width: Optional[int] = None, ) -> None: """Compare pages with or without local css""" @@ -28,7 +29,7 @@ async def screenshot_diff( else: output_path = Path(output_directory) - output_path.mkdir(parents=True) + output_path.mkdir(parents=True, exist_ok=True) left_source = config.source right_source = config.get_source(right_source_name) @@ -45,6 +46,7 @@ async def screenshot_diff( url, output_path, theme_name, + continue_ ), ) for (theme_name, url) in config.urls @@ -59,7 +61,16 @@ async def _diff_url( url: str, output_path: Path, theme_name: str, + continue_: bool ) -> None: + + url_slug = get_url_slug(url) + left_filename = output_path / f"{theme_name}_{url_slug}_left" + right_filename = output_path / f"{theme_name}_{url_slug}_right" + + if left_filename.is_file() and right_filename.is_file() and continue_: + return + left_bytes = await _screenshot_url(left, url) right_bytes = await _screenshot_url(right, url) @@ -76,14 +87,12 @@ async def _diff_url( if not diff.getbbox(): return - url_slug = get_url_slug(url) - if not output_path.is_dir(): - output_path.mkdir() + output_path.mkdir(parents=True, exist_ok=True) - with open(output_path / f"{theme_name}_{url_slug}_left", "wb") as screenshot_file: + with open(left_filename, "wb") as screenshot_file: screenshot_file.write(left_bytes) - with open(output_path / f"{theme_name}_{url_slug}_right", "wb") as screenshot_file: + with open(right_filename, "wb") as screenshot_file: screenshot_file.write(right_bytes)