Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Commit

Permalink
added Output parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
cehbrecht committed Jul 5, 2019
1 parent 8e68602 commit 3fa6ebc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
72 changes: 72 additions & 0 deletions owslib_esgfwps.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,78 @@ def __repr__(self):
return "{}({})".format(self.__class__.__name__, params.strip(","))


class Output(Parameter):
def __init__(self, id=None, uri=None, domain=None, mimetype=None):
super(Output, self).__init__()
self._id = id
self._uri = uri
self._domain = domain
self._mimetype = mimetype

@property
def id(self):
return self._id

@property
def uri(self):
return self._uri

@property
def domain(self):
return self._domain

@property
def mimetype(self):
return self._mimetype

@property
def json(self):
data = dict(uri=self.uri)
if self.id:
data['id'] = self.id
if self.domain:
data['domain'] = self.domain
if self.mimetype:
data['mime-type'] = self.mimetype
return data

@classmethod
def from_json(cls, data):
# TODO: use mimetype as key
new_data = data.copy()
if 'mime-type' in data:
new_data['mimetype'] = data['mime-type']
del new_data['mime-type']
return cls(**new_data)

@property
def params(self):
return self.json


class Outputs(Parameter):
def __init__(self, outputs=None):
super(Outputs, self).__init__()
self._outputs = outputs or []

@property
def outputs(self):
return self._outputs

@property
def json(self):
return [output.json for output in self.outputs]

@classmethod
def from_json(cls, data):
outputs = [Output.from_json(output) for output in data]
return cls(outputs=outputs)

@property
def params(self):
return dict(outputs=[output.id for output in self.outputs])


class Variable(Parameter):
def __init__(self, uri, var_name=None, id=None, domain=None):
super(Variable, self).__init__()
Expand Down
29 changes: 29 additions & 0 deletions tests/test_espfwps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"""

from owslib_esgfwps import (
Output,
Outputs,
Domain,
Domains,
Dimension,
Expand All @@ -15,6 +17,33 @@
)


def test_output_compat():
data = {
"uri": "http://test.org/output.nc",
"id": "tas_avg_mon",
"domain": {"id": "d0"},
"mime-type": "x-application/netcdf",
}
# from json
output = Output.from_json(data)
assert output.uri == data['uri']
# json
assert output.json['uri'] == data['uri']


def test_outputs():
data = [{
"uri": "http://test.org/output.nc",
"id": "tas_avg_mon",
"domain": {"id": "d0"},
"mime-type": "x-application/netcdf",
}]
output = Output(id="tas_avg_mon", uri="http://test.org/output.nc")
outputs = Outputs([output])
assert Outputs.from_json(outputs.json).outputs[0].uri == output.uri
assert output.id in str(outputs)


def test_dimension_compat():
dim_data = {"start": 0.0, "end": 90.0, "step": 1, "crs": "indices"}
# from json
Expand Down

0 comments on commit 3fa6ebc

Please sign in to comment.