common: rename sites to theme

This commit is contained in:
Corentin Sechet 2022-04-12 11:14:46 +02:00
parent 1be03e7067
commit 5125e02d3c
3 changed files with 26 additions and 28 deletions

View File

@ -1,7 +1,5 @@
""" Core module for Frontools"""
from .config import Config, SiteConfig
from .sources import get_page_stylesheets
from .utils import report_progress
from .config import Config
__all__ = ["Config", "SiteConfig", "report_progress", "get_page_stylesheets"]
__all__ = ["Config"]

View File

@ -28,8 +28,8 @@ class UrlConfig:
self.tags: set[str] = set()
class SiteConfig:
"""Configuration object for a particular website"""
class ThemeConfig:
"""Configuration object for a particular webtheme"""
def __init__(self) -> None:
self.urls: dict[str, UrlConfig] = {}
@ -49,7 +49,7 @@ class Config:
):
self._use_cache = use_cache
self._sources: dict[str, Source] = {}
self._sites: dict[str, SiteConfig] = {}
self._themes: dict[str, ThemeConfig] = {}
self._block_urls: list[Pattern[str]] = []
if default_source_name is None:
@ -123,41 +123,41 @@ class Config:
@property
def urls(self) -> Iterable[tuple[str, str]]:
"""Return sites configured for this context"""
for site_name, site in self._sites.items():
for url, config in site.urls.items():
"""Return themes configured for this context"""
for theme_name, theme in self._themes.items():
for url, config in theme.urls.items():
if self._filter(url, config.tags):
yield site_name, url
yield theme_name, url
def add_site_url(
def add_theme_url(
self, name: str, url: str, tags: Optional[Iterable[str]] = None
) -> None:
"""Add an url for a site"""
"""Add an url for a theme"""
site = self._sites.get(name, None)
if site is None:
site = SiteConfig()
self._sites[name] = site
theme = self._themes.get(name, None)
if theme is None:
theme = ThemeConfig()
self._themes[name] = theme
if tags is None:
new_tags = set()
else:
new_tags = set(tags)
url_config = site.urls.get(url, None)
url_config = theme.urls.get(url, None)
if url_config is None:
url_config = UrlConfig()
site.urls[url] = url_config
theme.urls[url] = url_config
url_config.tags.update(new_tags)
def load_sites_from_yaml(self, yaml_path: str) -> None:
"""Load a yaml file containing dictionnary of urls to add as sites."""
def load_urls(self, yaml_path: str) -> None:
"""Load a yaml file containing dictionnary of urls to add as themes."""
with open(yaml_path, "r", encoding="utf-8") as yaml_file:
yaml_document = load_yaml(yaml_file, Loader)
for site_name, urls in yaml_document.items():
for theme_name, urls in yaml_document.items():
for url, tags in urls.items():
self.add_site_url(site_name, url, tags)
self.add_theme_url(theme_name, url, tags)
def block_url_patterns(self, *patterns: str) -> None:
"""Will return 500 error for urls matching this pattern."""

View File

@ -44,10 +44,10 @@ async def screenshot_diff(
right_browser,
url,
output_path,
site_name,
theme_name,
),
)
for (site_name, url) in config.urls
for (theme_name, url) in config.urls
],
nb_workers=3,
)
@ -58,7 +58,7 @@ async def _diff_url(
right: Browser,
url: str,
output_path: Path,
site_name: str,
theme_name: str,
) -> None:
left_bytes = await _screenshot_url(left, url)
right_bytes = await _screenshot_url(right, url)
@ -80,10 +80,10 @@ async def _diff_url(
if not output_path.is_dir():
output_path.mkdir()
with open(output_path / f"{site_name}_{url_slug}_left", "wb") as screenshot_file:
with open(output_path / f"{theme_name}_{url_slug}_left", "wb") as screenshot_file:
screenshot_file.write(left_bytes)
with open(output_path / f"{site_name}_{url_slug}_right", "wb") as screenshot_file:
with open(output_path / f"{theme_name}_{url_slug}_right", "wb") as screenshot_file:
screenshot_file.write(right_bytes)