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 0d8185e commit fdd56a9
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 143 deletions.
37 changes: 36 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
[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_opendap"
description = ""
readme = "README.md"
requires-python = ">=3.9"
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",
"Topic :: Scientific/Engineering",
]

dynamic = ["version", "dependencies"]

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

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

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

[project.entry-points."xpublish.plugin"]
opendap = "xpublish_opendap.plugin:OpenDapPlugin"

[tool.interrogate]
ignore-init-method = true
ignore-init-module = false
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
opendap-protocol<1.2.0
xarray
xpublish<=0.1.0
xpublish
50 changes: 0 additions & 50 deletions setup.cfg

This file was deleted.

12 changes: 0 additions & 12 deletions setup.py

This file was deleted.

4 changes: 2 additions & 2 deletions xpublish_opendap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
xpublish_opendap provides an OpenDAP router for Xpublish
"""

from xpublish_opendap.router import dap_router
from xpublish_opendap.plugin import OpenDapPlugin

__all__ = ["dap_router"]
__all__ = ["OpenDapPlugin"]

try:
from ._version import __version__
Expand Down
85 changes: 85 additions & 0 deletions xpublish_opendap/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
xpublish_opendap
OpenDAP router for Xpublish
"""
import logging
from typing import List
from urllib import parse

import cachey
import opendap_protocol as dap
import xarray as xr
from fastapi import APIRouter, Depends, Request
from fastapi.responses import StreamingResponse
from xpublish import Plugin, hookimpl

from .dap_xarray import dap_dataset

logger = logging.getLogger("uvicorn")



class OpenDapPlugin(Plugin):
name = "opendap"

dataset_router_prefix = "/opendap"
dataset_router_tags: List[str] = ["opendap"]

@hookimpl
def dataset_router(self):
router = APIRouter(prefix=self.dataset_router_prefix, tags=self.dataset_router_tags)

def get_dap_dataset(
dataset_id: str,
ds: xr.Dataset = Depends(self.dependencies.dataset),
cache: cachey.Cache = Depends(self.dependencies.cache),
):
"""
Get a dataset that has been translated to opendap
"""
cache_key = f"opendap_dataset_{dataset_id}"
dataset = cache.get(cache_key)

if dataset is None:
dataset = dap_dataset(ds, dataset_id)

cache.put(cache_key, dataset, 99999)

return dataset

def dap_constraint(request: Request) -> str:
"""Parse DAP constraints from request"""
constraint = parse.unquote(request.url.components[3])

return constraint

@router.get(".dds")
def dds_response(
constraint=Depends(dap_constraint), dataset: dap.Dataset = Depends(get_dap_dataset),
):
"""OpenDAP DDS response (types and dimension metadata)"""
return StreamingResponse(
dataset.dds(constraint=constraint), media_type="text/plain",
)

@router.get(".das")
def das_response(
constraint=Depends(dap_constraint), dataset: dap.Dataset = Depends(get_dap_dataset),
):
"""OpenDAP DAS response (attribute metadata)"""
return StreamingResponse(
dataset.das(constraint=constraint), media_type="text/plain",
)


@router.get(".dods")
def dods_response(
constraint=Depends(dap_constraint), dataset: dap.Dataset = Depends(get_dap_dataset),
):
"""OpenDAP dods response (data access)"""
return StreamingResponse(
dataset.dods(constraint=constraint), media_type="application/octet-stream",
)

return router
77 changes: 0 additions & 77 deletions xpublish_opendap/router.py

This file was deleted.

0 comments on commit fdd56a9

Please sign in to comment.