Skip to content

Commit

Permalink
Merge pull request #523 from davidcaron/allow-custom-uom-reference
Browse files Browse the repository at this point in the history
allow custom UOM reference for literal IO
  • Loading branch information
cehbrecht authored Feb 25, 2020
2 parents 7126011 + d11ce24 commit 61e03fe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
8 changes: 6 additions & 2 deletions pywps/inout/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@ class UOM(object):
:param uom: unit of measure
"""

def __init__(self, uom=''):
def __init__(self, uom='', reference=None):
self.uom = uom
self.reference = reference

if self.reference is None:
self.reference = OGCUNIT[self.uom]

@property
def json(self):
return {"reference": OGCUNIT[self.uom],
return {"reference": self.reference,
"uom": self.uom}

def __eq__(self, other):
Expand Down
7 changes: 5 additions & 2 deletions pywps/inout/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,10 @@ def from_json(cls, json_input):

json_input_copy = deepcopy(json_input)
json_input_copy['allowed_values'] = allowed_values
json_input_copy['uoms'] = [basic.UOM(uom.get('uom')) for uom in json_input.get('uoms', [])]
json_input_copy['uoms'] = [
basic.UOM(uom['uom'], uom['reference'])
for uom in json_input.get('uoms', [])
]

data = json_input_copy.pop('data', None)
uom = json_input_copy.pop('uom', None)
Expand All @@ -352,7 +355,7 @@ def from_json(cls, json_input):
instance.metadata = [Metadata.from_json(d) for d in metadata]
instance.data = data
if uom:
instance.uom = basic.UOM(uom['uom'])
instance.uom = basic.UOM(uom['uom'], uom['reference'])

return instance

Expand Down
7 changes: 5 additions & 2 deletions pywps/inout/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ def json(self):

@classmethod
def from_json(cls, json_output):
uoms = [basic.UOM(uom.get('uom')) for uom in json_output.get('uoms', [])]
uoms = [
basic.UOM(uom['uom'], uom['reference'])
for uom in json_output.get('uoms', [])
]
uom = json_output.get('uom')

instance = cls(
Expand All @@ -296,7 +299,7 @@ def from_json(cls, json_output):

instance.data = json_output.get('data')
if uom:
instance.uom = basic.UOM(uom['uom'])
instance.uom = basic.UOM(uom['uom'], uom['reference'])

return instance

Expand Down
11 changes: 8 additions & 3 deletions tests/test_inout.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def setUp(self):
mode=2,
allowed_values=(1, 2, (3, 3, 12)),
default=6,
uoms=(UOM("metre"),),
uoms=(UOM("metre"), UOM("km / h", "custom reference")),
translations={"fr-CA": {"title": "Mon input", "abstract": "Une description"}},
)

Expand Down Expand Up @@ -603,8 +603,10 @@ def test_json_out(self):
self.literal_input.data = 9
out = self.literal_input.json

self.assertTrue('uoms' in out, 'UOMs does not exist')
self.assertTrue('uom' in out, 'uom does not exist')
self.assertEqual(out['uoms'][0]["uom"], "metre")
self.assertEqual(out['uoms'][1]["uom"], "km / h")
self.assertEqual(out['uoms'][1]["reference"], "custom reference")
self.assertEqual(out['uom']["uom"], "metre")
self.assertFalse(out['workdir'], 'Workdir exist')
self.assertEqual(out['data_type'], 'integer', 'Data type is integer')
self.assertFalse(out['abstract'], 'abstract exist')
Expand Down Expand Up @@ -669,6 +671,7 @@ def setUp(self):
"literaloutput",
data_type="integer",
title="Literal Output",
uoms=[UOM("km / h", "custom reference"), UOM("m / s", "other reference")],
translations={"fr-CA": {"title": "Mon output", "abstract": "Une description"}},
)

Expand All @@ -685,6 +688,8 @@ class Storage(object):
def test_json(self):
new_output = inout.outputs.LiteralOutput.from_json(self.literal_output.json)
self.assertEqual(new_output.identifier, 'literaloutput')
self.assertEqual(new_output.uom, self.literal_output.uom)
self.assertEqual(new_output.uoms, self.literal_output.uoms)
self.assertEqual(
new_output.translations,
{"fr-ca": {"title": "Mon output", "abstract": "Une description"}},
Expand Down

0 comments on commit 61e03fe

Please sign in to comment.