From 350f3ede006a1a67d071cd26bfcc8e6a31a7f850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20S=C3=A9chet?= Date: Mon, 21 Mar 2022 17:40:11 +0100 Subject: [PATCH] compare: pass screenshot output directory as command option --- TODO | 2 +- theme_check/main.py | 17 +++++++++++++++-- theme_check/utils.py | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 theme_check/utils.py diff --git a/TODO b/TODO index c1c55e8..543eb8d 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,4 @@ -* Pass screenshots directory as command arguments +* Arrange correctly code in subdirectories * See how to pass css overwrite options efficiently to command line * Take screenshot of page with and without overwritten CSS * Compare CSS for differences diff --git a/theme_check/main.py b/theme_check/main.py index b2e02ed..65179b8 100644 --- a/theme_check/main.py +++ b/theme_check/main.py @@ -1,6 +1,10 @@ +from typing import Optional +from click import Path as PathArgument +from urllib.parse import urlparse from asyncio import run from click import group from click import argument +from click import option from click import pass_context from functools import update_wrapper from playwright.async_api import Route @@ -9,6 +13,9 @@ from re import compile as re_compile from sys import exit from urllib.request import urlopen +from theme_check.utils import get_default_screenshot_directory +from theme_check.utils import get_url_slug + def async_command(f): def wrapper(*args, **kwargs): @@ -34,17 +41,23 @@ async def get_css(route: Route) -> None: @cli.command() @argument('url', type=str) +@option('-o', '--output-directory', type=PathArgument(), default=None) @pass_context @async_command -async def compare(ctx, url): +async def compare(ctx, url: str, output_directory: Optional[str]): + if output_directory is None: + output_directory = get_default_screenshot_directory() + output_directory.mkdir(parents=True) + async with async_playwright() as p: browser = await p.firefox.launch() context = await browser.new_context() await context.route(re_compile(r'.*\.css(.map)?.*'), get_css) page = await context.new_page() await page.goto(url) + url_slug = get_url_slug(url) screenshot_bytes = await page.screenshot() - with open('/home/csechet/screenshot.png', 'wb') as screenshot_file: + with open(output_directory / url_slug, 'wb') as screenshot_file: screenshot_file.write(screenshot_bytes) await browser.close() diff --git a/theme_check/utils.py b/theme_check/utils.py new file mode 100644 index 0000000..bfa91b6 --- /dev/null +++ b/theme_check/utils.py @@ -0,0 +1,40 @@ +from datetime import datetime +from os.path import expandvars +from pathlib import Path +from re import compile as re_compile +from urllib.parse import urlparse +from xdg.BaseDirectory import xdg_config_home + + +def get_url_slug(url: str, suffix=None) -> str: + parsed_url = urlparse(url) + url_slug = parsed_url.hostname + + url_path = parsed_url.path + if url_path is not None: + url_slug += url_path + + if suffix is not None: + url_slug += suffix + + return url_slug + + +def get_default_screenshot_directory() -> Path: + pictures_dir_re = re_compile(r'\w*XDG_PICTURES_DIR\w*\="(?P.*)"\w*$') + pictures_dir = None + with open(Path(xdg_config_home) / 'user-dirs.dirs') as user_dirs: + for line in user_dirs.readlines(): + match = pictures_dir_re.match(line) + if match is None: + continue + + path_string = match.group('path') + path_string = expandvars(path_string) + pictures_dir = Path(path_string) + break + + if pictures_dir is None: + pictures_dir = Path.home() + + return pictures_dir / datetime.now().strftime('%Y-%m-%d - %H-%m-%S')