Skip to content

Commit

Permalink
Improve the usage of the info class
Browse files Browse the repository at this point in the history
  • Loading branch information
gutzbenj committed Apr 27, 2024
1 parent 35d3609 commit b63c719
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 70 deletions.
24 changes: 23 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from wetterdienst import Settings
from wetterdienst import Info, Settings
from wetterdienst.util.eccodes import ensure_eccodes, ensure_pdbufr

IS_CI = os.environ.get("CI", False) and True
Expand All @@ -13,6 +13,8 @@
IS_WINDOWS = platform.system() == "Windows"
ENSURE_ECCODES_PDBUFR = ensure_eccodes() and ensure_pdbufr()

info = Info()


@pytest.fixture(scope="function")
def default_settings():
Expand Down Expand Up @@ -64,3 +66,23 @@ def settings_si_false_wide_shape():
@pytest.fixture
def settings_wide_shape():
return Settings(ts_shape="wide", ignore_env=True)


@pytest.fixture
def metadata():
return {
"producer": {
"doi": "10.5281/zenodo.3960624",
"name": "wetterdienst",
"version": info.version,
"repository": "https://github.com/earthobservations/wetterdienst",
"documentation": "https://wetterdienst.readthedocs.io",
},
"provider": {
"copyright": "© Deutscher Wetterdienst (DWD), Climate Data Center (CDC)",
"country": "Germany",
"name_english": "German Weather Service",
"name_local": "Deutscher Wetterdienst",
"url": "https://opendata.dwd.de/climate_environment/CDC/",
},
}
50 changes: 16 additions & 34 deletions tests/core/timeseries/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,6 @@ def dwd_climate_summary_tabular_columns():
]


@pytest.fixture
def dwd_metadata():
return {
"producer": {
"doi": "10.5281/zenodo.3960624",
"name": "Wetterdienst",
"url": "https://github.com/earthobservations/wetterdienst",
},
"provider": {
"copyright": "© Deutscher Wetterdienst (DWD), Climate Data Center (CDC)",
"country": "Germany",
"name_english": "German Weather Service",
"name_local": "Deutscher Wetterdienst",
"url": "https://opendata.dwd.de/climate_environment/CDC/",
},
}


@pytest.fixture
def df_stations():
return pl.DataFrame(
Expand Down Expand Up @@ -251,15 +233,15 @@ def test_stations_to_dict(df_stations):
]


def test_stations_to_dict_with_metadata(df_stations, stations_mock, dwd_metadata):
def test_stations_to_dict_with_metadata(df_stations, stations_mock, metadata):
data = StationsResult(
df=df_stations,
df_all=df_stations,
stations_filter=StationsFilter.ALL,
stations=stations_mock,
).to_dict(with_metadata=True)
assert data.keys() == {"stations", "metadata"}
assert data["metadata"] == dwd_metadata
assert data["metadata"] == metadata


def test_stations_to_ogc_feature_collection(df_stations):
Expand All @@ -283,15 +265,15 @@ def test_stations_to_ogc_feature_collection(df_stations):
}


def test_stations_to_ogc_feature_collection_with_metadata(df_stations, stations_mock, dwd_metadata):
def test_stations_to_ogc_feature_collection_with_metadata(df_stations, stations_mock, metadata):
data = StationsResult(
df=df_stations,
df_all=df_stations,
stations_filter=StationsFilter.ALL,
stations=stations_mock,
).to_ogc_feature_collection(with_metadata=True)
assert data.keys() == {"data", "metadata"}
assert data["metadata"] == dwd_metadata
assert data["metadata"] == metadata


def test_stations_format_json(df_stations):
Expand Down Expand Up @@ -357,10 +339,10 @@ def test_values_to_dict(df_values):
]


def test_values_to_dict_with_metadata(df_values, stations_result_mock, dwd_metadata):
def test_values_to_dict_with_metadata(df_values, stations_result_mock, metadata):
data = ValuesResult(stations=stations_result_mock, values=None, df=df_values[0, :]).to_dict(with_metadata=True)
assert data.keys() == {"values", "metadata"}
assert data["metadata"] == dwd_metadata
assert data["metadata"] == metadata


def test_values_to_ogc_feature_collection(df_values, stations_result_mock):
Expand Down Expand Up @@ -388,12 +370,12 @@ def test_values_to_ogc_feature_collection(df_values, stations_result_mock):
}


def test_values_to_ogc_feature_collection_with_metadata(df_values, stations_result_mock, dwd_metadata):
def test_values_to_ogc_feature_collection_with_metadata(df_values, stations_result_mock, metadata):
data = ValuesResult(stations=stations_result_mock, values=None, df=df_values[0, :]).to_ogc_feature_collection(
with_metadata=True,
)
assert data.keys() == {"data", "metadata"}
assert data["metadata"] == dwd_metadata
assert data["metadata"] == metadata


def test_values_format_json(df_values):
Expand Down Expand Up @@ -450,12 +432,12 @@ def test_interpolated_values_to_dict(df_interpolated_values):
]


def test_interpolated_values_to_dict_with_metadata(df_interpolated_values, stations_result_mock, dwd_metadata):
def test_interpolated_values_to_dict_with_metadata(df_interpolated_values, stations_result_mock, metadata):
data = InterpolatedValuesResult(stations=stations_result_mock, df=df_interpolated_values, latlon=(1, 2)).to_dict(
with_metadata=True,
)
assert data.keys() == {"values", "metadata"}
assert data["metadata"] == dwd_metadata
assert data["metadata"] == metadata


def test_interpolated_values_to_ogc_feature_collection(df_interpolated_values, stations_result_mock):
Expand Down Expand Up @@ -497,15 +479,15 @@ def test_interpolated_values_to_ogc_feature_collection(df_interpolated_values, s
def test_interpolated_values_to_ogc_feature_collection_with_metadata(
df_interpolated_values,
stations_result_mock,
dwd_metadata,
metadata,
):
data = InterpolatedValuesResult(
stations=stations_result_mock,
df=df_interpolated_values,
latlon=(1.2345, 2.3456),
).to_ogc_feature_collection(with_metadata=True)
assert data.keys() == {"data", "metadata"}
assert data["metadata"] == dwd_metadata
assert data["metadata"] == metadata


def test_summarized_values_to_dict(df_summarized_values):
Expand All @@ -523,14 +505,14 @@ def test_summarized_values_to_dict(df_summarized_values):
]


def test_summarized_values_to_dict_with_metadata(df_summarized_values, stations_result_mock, dwd_metadata):
def test_summarized_values_to_dict_with_metadata(df_summarized_values, stations_result_mock, metadata):
data = SummarizedValuesResult(
stations=stations_result_mock,
df=df_summarized_values,
latlon=(1.2345, 2.3456),
).to_dict(with_metadata=True)
assert data.keys() == {"values", "metadata"}
assert data["metadata"] == dwd_metadata
assert data["metadata"] == metadata


def test_summarized_values_to_ogc_feature_collection(df_summarized_values, stations_result_mock):
Expand Down Expand Up @@ -572,15 +554,15 @@ def test_summarized_values_to_ogc_feature_collection(df_summarized_values, stati
def test_summarized_values_to_ogc_feature_collection_with_metadata(
df_summarized_values,
stations_result_mock,
dwd_metadata,
metadata,
):
data = SummarizedValuesResult(
stations=stations_result_mock,
df=df_summarized_values,
latlon=(1.2345, 2.3456),
).to_ogc_feature_collection(with_metadata=True)
assert data.keys() == {"data", "metadata"}
assert data["metadata"] == dwd_metadata
assert data["metadata"] == metadata


def test_filter_by_date(df_values):
Expand Down
19 changes: 0 additions & 19 deletions tests/ui/cli/conftest.py

This file was deleted.

15 changes: 9 additions & 6 deletions wetterdienst/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@ class Author:

class Info:
def __init__(self):
self.__version__ = __version__
self.name = __appname__
self.version = __version__
self.authors = [
Author("Benjamin Gutzmann", "gutzemann@gmail.com", "gutzbenj"),
Author("Andreas Motl", "andreas.motl@panodata.org", "amotl"),
]
self.documentation = "https://wetterdienst.readthedocs.io"
self.repository = "https://github.com/earthobservations/wetterdienst"
self.documentation = "https://wetterdienst.readthedocs.io"
self.cache_dir = Settings().cache_dir

def __str__(self):
return dedent(f"""
===========================================
Wetterdienst - Open weather data for humans
{self.name} - open weather data for humans
===========================================
version: {self.__version__}
version: {self.version}
authors: {', '.join([f"{author.name} <{author.email}>" for author in self.authors])}
documentation: {self.documentation}
repository: {self.repository}
Expand All @@ -49,17 +50,19 @@ def __str__(self):

def to_dict(self):
return {
"version": self.__version__,
"name": self.name,
"version": self.version,
"authors": [asdict(author) for author in self.authors],
"documentation": self.documentation,
"repository": self.repository,
"documentation": self.documentation,
"cache_dir": self.cache_dir,
}


__all__ = [
"__appname__",
"__version__",
"Author",
"Info",
"Kind",
"Parameter",
Expand Down
14 changes: 9 additions & 5 deletions wetterdienst/core/timeseries/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import polars as pl
from typing_extensions import NotRequired, TypedDict

from wetterdienst import Info
from wetterdienst.core.process import filter_by_date
from wetterdienst.core.timeseries.export import ExportMixin
from wetterdienst.metadata.columns import Columns
Expand All @@ -25,8 +26,7 @@
from wetterdienst.provider.dwd.dmo import DwdDmoRequest
from wetterdienst.provider.dwd.mosmix import DwdMosmixRequest

PRODUCER_NAME = "Wetterdienst"
PRODUCER_LINK = "https://github.com/earthobservations/wetterdienst"
info = Info()


class StationsFilter:
Expand All @@ -50,7 +50,9 @@ class _Provider(TypedDict):

class _Producer(TypedDict):
name: str
url: str
version: str
repository: str
documentation: str
doi: str


Expand Down Expand Up @@ -236,8 +238,10 @@ def get_metadata(self) -> _Metadata:
"url": url,
},
"producer": {
"name": PRODUCER_NAME,
"url": PRODUCER_LINK,
"name": info.name,
"version": info.version,
"repository": info.repository,
"documentation": info.documentation,
"doi": "10.5281/zenodo.3960624",
},
}
Expand Down
11 changes: 6 additions & 5 deletions wetterdienst/ui/restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from fastapi import FastAPI, HTTPException, Query
from fastapi.responses import HTMLResponse, PlainTextResponse, Response

from wetterdienst import Author, Info, Period, Provider, Wetterdienst, __appname__
from wetterdienst import Author, Info, Period, Provider, Wetterdienst
from wetterdienst.core.timeseries.result import (
_InterpolatedValuesDict,
_InterpolatedValuesOgcFeatureCollection,
Expand All @@ -37,6 +37,8 @@
if TYPE_CHECKING:
from wetterdienst.core.timeseries.request import TimeseriesRequest

info = Info()

app = FastAPI(debug=False)

log = logging.getLogger(__name__)
Expand All @@ -50,7 +52,7 @@ def _create_author_entry(author: Author):
# create author string Max Mustermann (Github href, Mailto)
return f"{author.name} (<a href='https://github.com/{author.github_handle}' target='_blank' rel='noopener'>github</a>, <a href='mailto:{author.email}'>mail</a>)" # noqa:E501

title = f"{__appname__} restapi"
title = f"{info.name} restapi"
about = "Wetterdienst - Open weather data for humans."
sources = []
for provider in Provider:
Expand All @@ -60,7 +62,6 @@ def _create_author_entry(author: Author):
f"<li><a href={url} target='_blank' rel='noopener'>{shortname}</a> ({name}, {country}) - {copyright_}</li>",
)
sources = "\n".join(sources)
info = Info()
return f"""
<html lang="en">
<head>
Expand Down Expand Up @@ -151,10 +152,10 @@ def _create_author_entry(author: Author):
</div>
<h2>Producer</h2>
<div class="List">
<li>Version: {info.__version__}</li>
<li>Version: {info.version}</li>
<li>Authors: {', w'.join(_create_author_entry(author) for author in info.authors)}</li>
<li>Documentation: <a href="{info.documentation}" target="_blank" rel="noopener">{info.documentation}</a></li>
<li>Repository: <a href="{info.repository}" target="_blank" rel="noopener">{info.repository}</a></li>
<li>Documentation: <a href="{info.documentation}" target="_blank" rel="noopener">{info.documentation}</a></li>
</div>
<h2>Providers</h2>
<div class="list">
Expand Down

0 comments on commit b63c719

Please sign in to comment.