2020-02-03 02:35:11 -07:00
|
|
|
"""Validate OpenGraph Configuration Parameters."""
|
|
|
|
|
|
|
|
# Standard Library
|
2020-02-15 11:00:51 -07:00
|
|
|
import os
|
2020-02-03 02:35:11 -07:00
|
|
|
from pathlib import Path
|
2020-01-16 02:52:00 -07:00
|
|
|
|
2020-02-03 02:35:11 -07:00
|
|
|
# Third Party
|
2020-07-04 15:18:53 -07:00
|
|
|
from pydantic import FilePath, validator
|
2020-01-16 02:52:00 -07:00
|
|
|
|
2020-02-03 02:35:11 -07:00
|
|
|
# Project
|
2020-04-16 09:29:57 -07:00
|
|
|
from hyperglass.models import HyperglassModel
|
2020-02-15 11:00:51 -07:00
|
|
|
|
|
|
|
CONFIG_PATH = Path(os.environ["hyperglass_directory"])
|
2020-06-21 14:11:23 -07:00
|
|
|
DEFAULT_IMAGES = Path(__file__).parent.parent.parent / "images"
|
2020-01-16 02:52:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
class OpenGraph(HyperglassModel):
|
2020-01-28 08:59:27 -07:00
|
|
|
"""Validation model for params.opengraph."""
|
2020-01-16 02:52:00 -07:00
|
|
|
|
2020-07-04 14:57:47 -07:00
|
|
|
image: FilePath = DEFAULT_IMAGES / "hyperglass-opengraph.jpg"
|
2020-01-16 02:52:00 -07:00
|
|
|
|
2020-07-04 14:57:47 -07:00
|
|
|
@validator("image")
|
|
|
|
def validate_opengraph(cls, value):
|
|
|
|
"""Ensure the opengraph image is a supported format.
|
2020-01-16 02:52:00 -07:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
value {FilePath} -- Path to opengraph image file.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
{Path} -- Opengraph image file path object
|
|
|
|
"""
|
2020-02-01 16:11:01 -10:00
|
|
|
supported_extensions = (".jpg", ".jpeg", ".png")
|
2020-07-04 14:57:47 -07:00
|
|
|
if value is not None and value.suffix not in supported_extensions:
|
2020-02-01 16:11:01 -10:00
|
|
|
raise ValueError(
|
|
|
|
"OpenGraph image must be one of {e}".format(
|
|
|
|
e=", ".join(supported_extensions)
|
|
|
|
)
|
|
|
|
)
|
2020-02-15 11:00:51 -07:00
|
|
|
|
2020-07-04 14:57:47 -07:00
|
|
|
return value
|