2019-12-29 23:57:39 -07:00
|
|
|
"""Utility Functions for Pydantic Models."""
|
2019-09-11 01:35:31 -07:00
|
|
|
|
2020-02-03 02:35:11 -07:00
|
|
|
# Standard Library
|
2020-02-15 11:00:51 -07:00
|
|
|
import os
|
2020-04-12 10:16:22 -07:00
|
|
|
from pathlib import Path
|
2019-10-09 03:10:52 -07:00
|
|
|
|
2020-03-21 01:44:38 -07:00
|
|
|
# Project
|
2020-04-16 09:29:57 -07:00
|
|
|
from hyperglass.log import log
|
2020-03-21 01:44:38 -07:00
|
|
|
|
|
|
|
|
2020-02-15 11:00:51 -07:00
|
|
|
def validate_image(value):
|
|
|
|
"""Convert file path to URL path.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
value {FilePath} -- Path to logo file.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
{str} -- Formatted logo path
|
|
|
|
"""
|
2020-03-29 20:03:36 -07:00
|
|
|
config_path = Path(os.environ["hyperglass_directory"])
|
|
|
|
base_path = [v for v in value.split("/") if v != ""]
|
2020-02-15 11:00:51 -07:00
|
|
|
|
|
|
|
if base_path[0] not in ("images", "custom"):
|
|
|
|
raise ValueError(
|
2020-03-22 18:05:41 -07:00
|
|
|
f"Logo files must be in the 'custom/' directory of your hyperglass directory. Got: {value}"
|
2020-02-15 11:00:51 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
if base_path[0] == "custom":
|
2020-03-29 20:03:36 -07:00
|
|
|
file = config_path / "static" / "custom" / "/".join(base_path[1:])
|
|
|
|
|
|
|
|
else:
|
|
|
|
file = config_path / "static" / "images" / "/".join(base_path[1:])
|
|
|
|
|
|
|
|
log.error(file)
|
|
|
|
if not file.exists():
|
|
|
|
raise ValueError(f"'{str(file)}' does not exist")
|
2020-02-15 11:00:51 -07:00
|
|
|
|
2020-03-29 20:03:36 -07:00
|
|
|
base_index = file.parts.index(base_path[0])
|
2020-02-15 11:00:51 -07:00
|
|
|
|
2020-03-29 20:03:36 -07:00
|
|
|
return "/".join(file.parts[base_index:])
|