From 8a25bedaf9323c6e1f62a9324b46344d9292ed24 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Tue, 25 Jun 2024 17:42:55 +1200 Subject: [PATCH] [external_files] Move common ``download_content`` function to ``external_files.py`` (#6982) --- esphome/components/font/__init__.py | 30 ++------------------------- esphome/components/image/__init__.py | 31 ++-------------------------- esphome/external_files.py | 26 +++++++++++++++++++++++ 3 files changed, 30 insertions(+), 57 deletions(-) diff --git a/esphome/components/font/__init__.py b/esphome/components/font/__init__.py index b3a5beb199..7e4674ffda 100644 --- a/esphome/components/font/__init__.py +++ b/esphome/components/font/__init__.py @@ -17,7 +17,6 @@ from esphome.helpers import ( cpp_string_escape, ) from esphome.const import ( - __version__, CONF_FAMILY, CONF_FILE, CONF_GLYPHS, @@ -185,31 +184,6 @@ def get_font_path(value, type) -> Path: return None -def download_content(url: str, path: Path) -> None: - if not external_files.has_remote_file_changed(url, path): - _LOGGER.debug("Remote file has not changed %s", url) - return - - _LOGGER.debug( - "Remote file has changed, downloading from %s to %s", - url, - path, - ) - - try: - req = requests.get( - url, - timeout=external_files.NETWORK_TIMEOUT, - headers={"User-agent": f"ESPHome/{__version__} (https://esphome.io)"}, - ) - req.raise_for_status() - except requests.exceptions.RequestException as e: - raise cv.Invalid(f"Could not download from {url}: {e}") - - path.parent.mkdir(parents=True, exist_ok=True) - path.write_bytes(req.content) - - def download_gfont(value): name = ( f"{value[CONF_FAMILY]}:ital,wght@{int(value[CONF_ITALIC])},{value[CONF_WEIGHT]}" @@ -236,7 +210,7 @@ def download_gfont(value): ttf_url = match.group(1) _LOGGER.debug("download_gfont: ttf_url=%s", ttf_url) - download_content(ttf_url, path) + external_files.download_content(ttf_url, path) return value @@ -244,7 +218,7 @@ def download_web_font(value): url = value[CONF_URL] path = get_font_path(value, TYPE_WEB) - download_content(url, path) + external_files.download_content(url, path) _LOGGER.debug("download_web_font: path=%s", path) return value diff --git a/esphome/components/image/__init__.py b/esphome/components/image/__init__.py index c275136427..e5a205f1e0 100644 --- a/esphome/components/image/__init__.py +++ b/esphome/components/image/__init__.py @@ -6,7 +6,6 @@ import hashlib import io from pathlib import Path import re -import requests from magic import Magic from esphome import core @@ -15,7 +14,6 @@ from esphome import external_files import esphome.config_validation as cv import esphome.codegen as cg from esphome.const import ( - __version__, CONF_DITHER, CONF_FILE, CONF_ICON, @@ -75,31 +73,6 @@ def compute_local_image_path(value: dict) -> Path: return base_dir / key -def download_content(url: str, path: Path) -> None: - if not external_files.has_remote_file_changed(url, path): - _LOGGER.debug("Remote file has not changed %s", url) - return - - _LOGGER.debug( - "Remote file has changed, downloading from %s to %s", - url, - path, - ) - - try: - req = requests.get( - url, - timeout=IMAGE_DOWNLOAD_TIMEOUT, - headers={"User-agent": f"ESPHome/{__version__} (https://esphome.io)"}, - ) - req.raise_for_status() - except requests.exceptions.RequestException as e: - raise cv.Invalid(f"Could not download from {url}: {e}") - - path.parent.mkdir(parents=True, exist_ok=True) - path.write_bytes(req.content) - - def download_mdi(value): validate_cairosvg_installed(value) @@ -108,7 +81,7 @@ def download_mdi(value): url = f"https://raw.githubusercontent.com/Templarian/MaterialDesign/master/svg/{mdi_id}.svg" - download_content(url, path) + external_files.download_content(url, path, IMAGE_DOWNLOAD_TIMEOUT) return value @@ -117,7 +90,7 @@ def download_image(value): url = value[CONF_URL] path = compute_local_image_path(value) - download_content(url, path) + external_files.download_content(url, path, IMAGE_DOWNLOAD_TIMEOUT) return value diff --git a/esphome/external_files.py b/esphome/external_files.py index a1422d02b1..f8eb1dcabe 100644 --- a/esphome/external_files.py +++ b/esphome/external_files.py @@ -7,6 +7,7 @@ from datetime import datetime import requests import esphome.config_validation as cv from esphome.core import CORE, TimePeriodSeconds +from esphome.const import __version__ _LOGGER = logging.getLogger(__name__) CODEOWNERS = ["@landonr"] @@ -75,3 +76,28 @@ def compute_local_file_dir(domain: str) -> Path: base_directory.mkdir(parents=True, exist_ok=True) return base_directory + + +def download_content(url: str, path: Path, timeout=NETWORK_TIMEOUT) -> None: + if not has_remote_file_changed(url, path): + _LOGGER.debug("Remote file has not changed %s", url) + return + + _LOGGER.debug( + "Remote file has changed, downloading from %s to %s", + url, + path, + ) + + try: + req = requests.get( + url, + timeout=timeout, + headers={"User-agent": f"ESPHome/{__version__} (https://esphome.io)"}, + ) + req.raise_for_status() + except requests.exceptions.RequestException as e: + raise cv.Invalid(f"Could not download from {url}: {e}") + + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(req.content)