diff --git a/hyperglass/configuration/models/_utils.py b/hyperglass/configuration/models/_utils.py index 9dc2579..bd83f10 100644 --- a/hyperglass/configuration/models/_utils.py +++ b/hyperglass/configuration/models/_utils.py @@ -1,7 +1,9 @@ """Utility Functions for Pydantic Models.""" # Standard Library +import os import re +from pathlib import Path # Third Party from pydantic import BaseModel @@ -77,3 +79,33 @@ class AnyUri(str): def __repr__(self): """Stringify custom field representation.""" return f"AnyUri({super().__repr__()})" + + +def validate_image(value): + """Convert file path to URL path. + + Arguments: + value {FilePath} -- Path to logo file. + + Returns: + {str} -- Formatted logo path + """ + base_path = value.split("/") + + if base_path[0] == "/": + value = "/".join(base_path[1:]) + + if base_path[0] not in ("images", "custom"): + raise ValueError( + "Logo files must be in the 'custom/' directory of your hyperglass directory. Got: {f}", + f=value, + ) + + if base_path[0] == "custom": + config_path = Path(os.environ["hyperglass_directory"]) + custom_file = config_path / "static" / value + + if not custom_file.exists(): + raise ValueError("'{f}' does not exist", f=str(custom_file)) + + return value diff --git a/hyperglass/configuration/models/opengraph.py b/hyperglass/configuration/models/opengraph.py index 23c38bb..42671ac 100644 --- a/hyperglass/configuration/models/opengraph.py +++ b/hyperglass/configuration/models/opengraph.py @@ -1,15 +1,18 @@ """Validate OpenGraph Configuration Parameters.""" # Standard Library +import os from typing import Optional from pathlib import Path # Third Party import PIL.Image as PilImage -from pydantic import FilePath, StrictInt, root_validator +from pydantic import StrictInt, StrictStr, root_validator # Project -from hyperglass.configuration.models._utils import HyperglassModel +from hyperglass.configuration.models._utils import HyperglassModel, validate_image + +CONFIG_PATH = Path(os.environ["hyperglass_directory"]) class OpenGraph(HyperglassModel): @@ -17,10 +20,10 @@ class OpenGraph(HyperglassModel): width: Optional[StrictInt] height: Optional[StrictInt] - image: Optional[FilePath] + image: Optional[StrictStr] @root_validator - def validate_image(cls, values): + def validate_opengraph(cls, values): """Set default opengraph image location. Arguments: @@ -40,13 +43,13 @@ class OpenGraph(HyperglassModel): ) ) if values["image"] is None: - image = ( - Path(__file__).parent.parent.parent - / "static/images/hyperglass-opengraph.png" - ) - values["image"] = "".join(str(image).split("static")[1::]) + values["image"] = "images/hyperglass-opengraph.png" - with PilImage.open(image) as img: + values["image"] = validate_image(values["image"]) + + image_file = CONFIG_PATH / "static" / values["image"] + + with PilImage.open(image_file) as img: width, height = img.size if values["width"] is None: values["width"] = width diff --git a/hyperglass/configuration/models/web.py b/hyperglass/configuration/models/web.py index 5d2b4fa..7581807 100644 --- a/hyperglass/configuration/models/web.py +++ b/hyperglass/configuration/models/web.py @@ -1,9 +1,7 @@ """Validate branding configuration variables.""" # Standard Library -import os from typing import Optional -from pathlib import Path # Third Party from pydantic import ( @@ -20,7 +18,7 @@ from pydantic.color import Color # Project from hyperglass.constants import DNS_OVER_HTTPS, FUNC_COLOR_MAP -from hyperglass.configuration.models._utils import HyperglassModel +from hyperglass.configuration.models._utils import HyperglassModel, validate_image from hyperglass.configuration.models.opengraph import OpenGraph @@ -116,23 +114,7 @@ class Logo(HyperglassLevel3): Returns: {str} -- Formatted logo path """ - base_path = value.split("/") - - if base_path[0] == "/": - value = "/".join(base_path[1:]) - - if base_path[0] not in ("images", "custom"): - raise ValueError( - "Logo files must be in the 'custom/' directory of your hyperglass directory. Got: {f}", - f=value, - ) - if base_path[0] == "custom": - config_path = Path(os.environ["hyperglass_directory"]) - custom_file = config_path / "static" / value - if not custom_file.exists(): - raise ValueError("'{f}' does not exist", f=str(custom_file)) - - return value + return validate_image(value) class Config: """Override pydantic config."""