compare: pass screenshot output directory as command option
This commit is contained in:
parent
566b9ae239
commit
350f3ede00
2
TODO
2
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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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<path>.*)"\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')
|
Loading…
Reference in New Issue