|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +from dataclasses import dataclass |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +import yaml |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from pathlib import Path |
| 11 | + |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | +SUPPORTED_TEMPLATE_VERSION = 1 |
| 15 | + |
| 16 | + |
| 17 | +class TemplateLibraryVersionError(Exception): |
| 18 | + def __init__(self, version: str): |
| 19 | + super().__init__(f"Template library version {version} is not supported") |
| 20 | + |
| 21 | + |
| 22 | +class TemplateSyntaxError(Exception): |
| 23 | + def __init__(self, message: str): |
| 24 | + super().__init__(f"Invalid template syntax: {message}") |
| 25 | + |
| 26 | + |
| 27 | +@dataclass |
| 28 | +class TemplateLibraryConfig: |
| 29 | + """Describes the configuration of a template library""" |
| 30 | + |
| 31 | + version: str |
| 32 | + meta: TemplateLibraryMeta |
| 33 | + templates: list[FeatureTemplate] |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def from_dict(cls, data: dict) -> TemplateLibraryConfig: |
| 37 | + file_version = data["version"] |
| 38 | + if file_version != SUPPORTED_TEMPLATE_VERSION: |
| 39 | + raise TemplateLibraryVersionError(file_version) |
| 40 | + |
| 41 | + try: |
| 42 | + return cls( |
| 43 | + version=file_version, |
| 44 | + meta=TemplateLibraryMeta.from_dict(data["meta"]), |
| 45 | + templates=[FeatureTemplate.from_dict(template) for template in data["templates"]], |
| 46 | + ) |
| 47 | + except KeyError as e: |
| 48 | + raise TemplateSyntaxError(str(e)) from e |
| 49 | + |
| 50 | + |
| 51 | +@dataclass |
| 52 | +class TemplateLibraryMeta: |
| 53 | + """Describes the metadata of a template library""" |
| 54 | + |
| 55 | + name: str |
| 56 | + description: str | None |
| 57 | + version: str | None |
| 58 | + |
| 59 | + @classmethod |
| 60 | + def from_dict(cls, data: dict) -> TemplateLibraryMeta: |
| 61 | + return cls( |
| 62 | + name=data["name"], |
| 63 | + description=data.get("description"), |
| 64 | + version=data.get("version"), |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +@dataclass |
| 69 | +class FeatureTemplate: |
| 70 | + """Describes a feature template that can include nested features""" |
| 71 | + |
| 72 | + name: str |
| 73 | + description: str | None |
| 74 | + feature: Feature |
| 75 | + |
| 76 | + @classmethod |
| 77 | + def from_dict(cls, data: dict) -> FeatureTemplate: |
| 78 | + return cls( |
| 79 | + name=data["name"], |
| 80 | + description=data.get("description"), |
| 81 | + feature=Feature.from_dict(data["feature"]), |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +@dataclass |
| 86 | +class Feature: |
| 87 | + """Describes a feature to be inserted into a Vector layer""" |
| 88 | + |
| 89 | + layer: str |
| 90 | + attributes: list[Attribute] |
| 91 | + child_features: list[Feature] | None |
| 92 | + |
| 93 | + @classmethod |
| 94 | + def from_dict(cls, data: dict) -> Feature: |
| 95 | + return cls( |
| 96 | + layer=data["layer"], |
| 97 | + attributes=[Attribute.from_dict(attribute) for attribute in data["attributes"]], |
| 98 | + child_features=[Feature.from_dict(feature) for feature in data.get("child_features", [])], |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +@dataclass |
| 103 | +class Attribute: |
| 104 | + """Describes an attribute to be set on a feature""" |
| 105 | + |
| 106 | + attribute: str |
| 107 | + default: str | None |
| 108 | + |
| 109 | + @classmethod |
| 110 | + def from_dict(cls, data: dict) -> Attribute: |
| 111 | + return cls(attribute=data["attribute"], default=data.get("default")) |
| 112 | + |
| 113 | + |
| 114 | +def parse_template_library_config(template_library_config: Path) -> TemplateLibraryConfig: |
| 115 | + with template_library_config.open(encoding="utf-8") as f: |
| 116 | + data = yaml.safe_load(f) |
| 117 | + return TemplateLibraryConfig.from_dict(data) |
0 commit comments