|
6 | 6 | import warnings |
7 | 7 | from hashlib import sha256 |
8 | 8 | from itertools import product |
9 | | -from pathlib import Path |
| 9 | +from pathlib import Path, PurePosixPath |
10 | 10 | from typing import Any, Dict, Generator, List, Optional, Sequence, Tuple, Union |
| 11 | +from urllib.parse import urlsplit |
11 | 12 |
|
12 | 13 | import numpy |
13 | 14 | import requests |
@@ -124,7 +125,7 @@ def resolve_partners( |
124 | 125 | assert isinstance(partner_collection, Collection) |
125 | 126 | except Exception as e: |
126 | 127 | warnings.warn( |
127 | | - f"Invalid partner source {partner['source']} (Cannot update to format {current_format}): {e}" |
| 128 | + f"Invalid partner source {partner.get('source')} (Cannot update to format {current_format}): {e}" |
128 | 129 | ) |
129 | 130 | ignored_partners.add(f"partner[{idx}]") |
130 | 131 | continue |
@@ -385,43 +386,42 @@ def load_yaml_dict(path: Path, raise_missing_keys: Sequence[str]) -> Optional[Di |
385 | 386 | return data |
386 | 387 |
|
387 | 388 |
|
388 | | -def downsize_image(image_path: Path, dist: Path, size: Tuple[int, int]): |
| 389 | +def downsize_image(image_path: Path, output_path: Path, size: Tuple[int, int]): |
389 | 390 | """downsize or copy an image""" |
390 | 391 | from PIL import Image |
391 | 392 |
|
392 | | - output_path = dist / f"{image_path.stem}.png" |
393 | 393 | try: |
394 | 394 | with Image.open(image_path) as img: |
395 | 395 | img.thumbnail(size) |
396 | 396 | img.save(output_path, "PNG") |
397 | 397 | except Exception as e: |
398 | 398 | warnings.warn(str(e)) |
399 | | - output_path = output_path.with_name(image_path.name) |
400 | 399 | shutil.copy(image_path, output_path) |
401 | 400 |
|
402 | | - return output_path |
403 | 401 |
|
404 | | - |
405 | | -def deploy_thumbnails(rdf_like: Dict[str, Any], dist: Path, resource_id: str, version_id: str) -> None: |
| 402 | +def deploy_thumbnails(rdf_like: Dict[str, Any], dist: Path, gh_pages: Path, resource_id: str, version_id: str) -> None: |
406 | 403 | import pooch |
407 | 404 |
|
408 | 405 | dist /= f"rdfs/{resource_id}/{version_id}" |
| 406 | + gh_pages /= f"rdfs/{resource_id}/{version_id}" |
409 | 407 | dist.mkdir(exist_ok=True, parents=True) |
410 | 408 | covers: Union[Any, List[Any]] = rdf_like.get("covers") |
411 | 409 | if isinstance(covers, list): |
412 | 410 | for i, cover_url in enumerate(covers): |
413 | 411 | if not isinstance(cover_url, str) or cover_url.startswith(DEPLOYED_BASE_URL): |
414 | 412 | continue # invalid or already cached |
415 | 413 |
|
416 | | - try: |
417 | | - downloaded_cover = Path(pooch.retrieve(cover_url, None)) # type: ignore |
418 | | - except Exception as e: |
419 | | - warnings.warn(str(e)) |
420 | | - continue |
| 414 | + cover_file_name = PurePosixPath(urlsplit(cover_url).path).name |
| 415 | + if not (gh_pages / cover_file_name).exists(): |
| 416 | + try: |
| 417 | + downloaded_cover = Path(pooch.retrieve(cover_url, None)) # type: ignore |
| 418 | + except Exception as e: |
| 419 | + warnings.warn(str(e)) |
| 420 | + continue |
421 | 421 |
|
422 | | - resized_cover = downsize_image(downloaded_cover, dist, size=(600, 340)) |
| 422 | + downsize_image(downloaded_cover, dist / cover_file_name, size=(600, 340)) |
423 | 423 |
|
424 | | - rdf_like["covers"][i] = f"{DEPLOYED_BASE_URL}/rdfs/{resource_id}/{version_id}/{resized_cover.name}" |
| 424 | + rdf_like["covers"][i] = f"{DEPLOYED_BASE_URL}/rdfs/{resource_id}/{version_id}/{cover_file_name}" |
425 | 425 |
|
426 | 426 | badges: Union[Any, List[Union[Any, Dict[Any, Any]]]] = rdf_like.get("badges") |
427 | 427 | if isinstance(badges, list): |
|
0 commit comments