Skip to content

Commit

Permalink
Use pluggy to hook into Xpublish
Browse files Browse the repository at this point in the history
  • Loading branch information
abkfenris committed Jan 16, 2023
1 parent 81a5d26 commit 27b678a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 91 deletions.
46 changes: 45 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
[build-system]
requires = ["setuptools>=42", "wheel", "setuptools_scm[toml]>=3.4"]
requires = ["setuptools>=61", "wheel", "setuptools_scm[toml]>=3.4"]
build-backend = "setuptools.build_meta"

[project]
name = "xpublish_edr"
description = ""
readme = "README.md"
requires-python = ">=3.7"
keywords = []
license = { file = "LICENSE.txt" }

classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Science/Research",
"Operating System :: OS Independent",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Topic :: Scientific/Engineering",
]

dynamic = ["version", "dependencies"]

[tool.setuptools]
packages = ["xpublish_edr"]

[tool.setuptools.dynamic]
dependencies = { file = ["requirements.txt"] }

[tool.setuptools_scm]
write_to = "xpublish_edr/_version.py"

[project.entry-points."xpublish.plugin"]
cf_edr = "xpublish_edr.plugin:CfEdrPlugin"

[project.entry-points.xpublish_edr_position_formats]
cf_covjson = "xpublish_edr.formats.to_covjson:to_cf_covjson"
csv = "xpublish_edr.formats.to_csv:to_csv"
nc = "xpublish_edr.formats.to_netcdf:to_netcdf"
netcdf = "xpublish_edr.formats.to_netcdf:to_netcdf"
nc4 = "xpublish_edr.formats.to_netcdf:to_netcdf"
netcdf4 = "xpublish_edr.formats.to_netcdf:to_netcdf"

[tool.interrogate]
ignore-init-method = true
ignore-init-module = false
Expand Down
54 changes: 0 additions & 54 deletions setup.cfg

This file was deleted.

22 changes: 0 additions & 22 deletions setup.py

This file was deleted.

46 changes: 32 additions & 14 deletions xpublish_edr/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
OGC EDR router for datasets with CF convention metadata
"""
import logging
from dataclasses import dataclass, field
from typing import List, Optional

import pkg_resources
import xarray as xr
from fastapi import Depends, HTTPException, Request
from xpublish.plugins import XpublishPluginFactory
from fastapi import APIRouter, Depends, HTTPException, Request
from xpublish import Plugin, hookimpl

from .formats.to_covjson import to_cf_covjson
from .query import EDRQuery, edr_query, edr_query_params
Expand All @@ -18,7 +17,8 @@

def position_formats():
"""
Return response format functions from registered `xpublish_edr_position_formats` entry_points
Return response format functions from registered
`xpublish_edr_position_formats` entry_points
"""
formats = {}

Expand All @@ -28,17 +28,25 @@ def position_formats():
return formats


@dataclass
class CfEdrPlugin(XpublishPluginFactory):
"""Provides access to data via a OGC EDR compatible API for datasets that are CF compliant"""
class CfEdrPlugin(Plugin):
"""
OGC EDR compatible endpoints for Xpublish datasets
"""

name = "cf_edr"

app_router_prefix: str = "/edr"
app_router_tags: List[str] = ["edr"]

dataset_router_prefix: str = "/edr"
dataset_router_tags: List[str] = field(default_factory=lambda: ["edr"])
dataset_router_tags: List[str] = ["edr"]

def register_routes(self):
"""Register EDR dataset routes"""
@hookimpl
def app_router(self):
"""Register an application level router for EDR format info"""
router = APIRouter(prefix=self.app_router_prefix, tags=self.app_router_tags)

@self.dataset_router.get(
@router.get(
"/position/formats",
summary="Position query response formats",
)
Expand All @@ -50,11 +58,18 @@ def get_position_formats():

return formats

@self.dataset_router.get("/position", summary="Position query")
return router

@hookimpl
def dataset_router(self):
"""Register dataset level router for EDR endpoints"""
router = APIRouter(prefix=self.app_router_prefix, tags=self.dataset_router_tags)

@router.get("/position", summary="Position query")
def get_position(
request: Request,
query: EDRQuery = Depends(edr_query),
dataset: xr.Dataset = Depends(self.dataset_dependency),
dataset: xr.Dataset = Depends(self.dependencies.dataset),
):
"""
Returns position data based on WKT `Point(lon lat)` coordinates
Expand Down Expand Up @@ -128,9 +143,12 @@ def get_position(
except KeyError:
raise HTTPException(
404,
f"{query.format} is not a valid format for EDR position queries. Get `./formats` for valid formats",
f"{query.format} is not a valid format for EDR position queries. "
"Get `./formats` for valid formats",
)

return format_fn(ds)

return to_cf_covjson(ds)

return router

0 comments on commit 27b678a

Please sign in to comment.