Skip to content

Commit

Permalink
Merge pull request #698 from nsidc/validate-style-fonts
Browse files Browse the repository at this point in the history
Add a list of allowed fonts and corresponding style validator
  • Loading branch information
mfisher87 authored Jul 31, 2023
2 parents 9664b17 + b76dedc commit 0f239aa
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions qgreenland/models/config/layer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path
from typing import Any, Optional, Union
from xml.etree import ElementTree

from pydantic import Field, validator

Expand Down Expand Up @@ -57,6 +58,24 @@ class Layer(QgrBaseModel):

_validate_description = reusable_validator("description", validate_paragraph_text)

@validator("style")
@classmethod
def style_file_only_contains_allowed_fonts(cls, value):
allowed_fonts = ["Open Sans"]
if value:
style_filepath = _style_filepath(value)
tree = ElementTree.parse(style_filepath)
for elem in tree.getroot().iter():
if font_family := elem.attrib.get("fontFamily", False):
if font_family not in allowed_fonts:
raise exc.QgrInvalidConfigError(
f"Style {style_filepath} contains disallowed font:"
f" '{font_family}'."
f" Only the following fonts are allowed: {allowed_fonts}."
)

return value

@validator("style")
@classmethod
def style_file_exists(cls, value):
Expand Down

0 comments on commit 0f239aa

Please sign in to comment.