Skip to content

Commit dc7272d

Browse files
feat: add get_template function
1 parent 5b6135a commit dc7272d

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

ohsome_quality_api/indicators/base.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import json
2+
import os
23
from abc import ABCMeta, abstractmethod
34

45
import plotly.graph_objects as go
6+
import yaml
57
from geojson import Feature, Polygon
68

7-
from ohsome_quality_api.definitions import get_attribution, get_metadata
9+
from ohsome_quality_api.definitions import get_attribution
810
from ohsome_quality_api.indicators.models import IndicatorMetadata, Result
911
from ohsome_quality_api.topics.models import BaseTopic as Topic
10-
from ohsome_quality_api.utils.helper import json_serialize
12+
from ohsome_quality_api.utils.helper import (
13+
camel_to_hyphen,
14+
camel_to_snake,
15+
get_module_dir,
16+
json_serialize,
17+
)
1118

1219

1320
class BaseIndicator(metaclass=ABCMeta):
@@ -18,9 +25,7 @@ def __init__(
1825
topic: Topic,
1926
feature: Feature,
2027
) -> None:
21-
self.metadata: IndicatorMetadata = get_metadata(
22-
"indicators", type(self).__name__
23-
)
28+
self.metadata: IndicatorMetadata = self.get_template()
2429
self.topic: Topic = topic
2530
self.feature: Feature = feature
2631
self.result: Result = Result(
@@ -167,3 +172,15 @@ def _get_default_figure(self) -> None:
167172
raw = fig.to_dict()
168173
raw["layout"].pop("template") # remove boilerplate
169174
self.result.figure = raw
175+
176+
def get_template(self) -> dict:
177+
"""Get template for indicator."""
178+
indicator_key = camel_to_snake(type(self).__name__)
179+
dir = get_module_dir(f"ohsome_quality_api.indicators.{indicator_key}")
180+
file = os.path.join(dir, "metadata.yaml")
181+
with open(file, "r") as file:
182+
raw = yaml.safe_load(file)
183+
metadata = {}
184+
for k, v in raw.items():
185+
metadata[k] = IndicatorMetadata(**v)
186+
return metadata[camel_to_hyphen(type(self).__name__)]

tests/integrationtests/test_base_indicator.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from geojson import Feature
66

77
from ohsome_quality_api.indicators.minimal.indicator import Minimal
8-
from ohsome_quality_api.indicators.models import Result
8+
from ohsome_quality_api.indicators.models import IndicatorMetadata, Result
99

1010
from .utils import get_geojson_fixture, get_topic_fixture
1111

@@ -81,6 +81,12 @@ def test_figure(self, feature, topic):
8181
# comment out for manual test
8282
# pio.show(indicator.result.figure)
8383

84+
def test_get_template(self, feature, topic):
85+
indicator = Minimal(feature=feature, topic=topic)
86+
indicator.get_template()
87+
assert isinstance(indicator.metadata, IndicatorMetadata)
88+
assert isinstance(indicator.result, Result)
89+
8490

8591
class TestBaseResult:
8692
def test_label(self):

0 commit comments

Comments
 (0)