diff --git a/frontools/screenshot.py b/frontools/screenshot.py index d0eb87b..2576e5b 100644 --- a/frontools/screenshot.py +++ b/frontools/screenshot.py @@ -1,18 +1,18 @@ """Pages screenshots""" +from datetime import datetime +from os.path import expandvars from pathlib import Path +from re import compile as re_compile from tempfile import NamedTemporaryFile from typing import Optional from PIL import Image, ImageChops from playwright.async_api import Error as PlaywrightError +from xdg import xdg_config_home from frontools.config import Config from frontools.sources import Browser -from frontools.utils import ( - get_default_screenshot_directory, - get_url_slug, - report_progress, -) +from frontools.utils import get_url_slug, report_progress async def screenshot_diff( @@ -24,7 +24,7 @@ async def screenshot_diff( """Compare pages with or without local css""" if output_directory is None: - output_path = get_default_screenshot_directory() + output_path = _get_default_screenshot_directory() else: output_path = Path(output_directory) @@ -96,3 +96,25 @@ async def _screenshot_url(browser: Browser, url: str) -> bytes: # Exception raisen when taking a screenshot of a to large page, retry without full_page return await page.screenshot() + + +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", encoding="utf-8" + ) 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") diff --git a/frontools/url_gather.py b/frontools/url_gather.py deleted file mode 100644 index d9587d9..0000000 --- a/frontools/url_gather.py +++ /dev/null @@ -1 +0,0 @@ -"""Gather theme urls from various sources""" diff --git a/frontools/utils.py b/frontools/utils.py index e143c19..a6b57ef 100644 --- a/frontools/utils.py +++ b/frontools/utils.py @@ -1,6 +1,5 @@ """Common utilities""" from asyncio import gather -from datetime import datetime from os.path import expandvars from pathlib import Path from re import compile as re_compile @@ -25,29 +24,6 @@ class ErrorSummary: echo(error, err=True) -def get_default_screenshot_directory() -> Path: - """Return the default folder where to store screenshot (XDG pictures dir by default)""" - 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", encoding="utf-8" - ) 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") - - TaskListType = list[tuple[str, Awaitable[None]]]