screenshot: move get_default_screenshot_dir to screenshot module

This commit is contained in:
Corentin Sechet 2022-04-12 10:45:51 +02:00
parent 98fbfe63ab
commit 1be03e7067
3 changed files with 28 additions and 31 deletions

View File

@ -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<path>.*)"\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")

View File

@ -1 +0,0 @@
"""Gather theme urls from various sources"""

View File

@ -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<path>.*)"\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]]]