Commit 997cf059 authored by Samuel GAIST's avatar Samuel GAIST

[plotterparameter] Fix writing

The write method was wrong as well as there were missing
pieces in the class for it to work properly.
parent 8a79b938
Pipeline #29919 passed with stage
in 15 minutes and 57 seconds
...@@ -42,6 +42,8 @@ plotterparameter ...@@ -42,6 +42,8 @@ plotterparameter
Validation for plotterparameters Validation for plotterparameters
""" """
import simplejson as json
from . import schema from . import schema
from . import prototypes from . import prototypes
from . import utils from . import utils
...@@ -241,6 +243,16 @@ class Plotterparameter(object): ...@@ -241,6 +243,16 @@ class Plotterparameter(object):
self._name = value self._name = value
self.storage = Storage(self.prefix, value) self.storage = Storage(self.prefix, value)
@property
def description(self):
"""The short description for this object"""
return self.data.get("description", None)
@description.setter
def description(self, value):
"""Sets the short description for this object"""
self.data["description"] = value
@property @property
def documentation(self): def documentation(self):
"""The full-length description for this object""" """The full-length description for this object"""
...@@ -272,6 +284,27 @@ class Plotterparameter(object): ...@@ -272,6 +284,27 @@ class Plotterparameter(object):
return self.storage.hash() return self.storage.hash()
def json_dumps(self, indent=4):
"""Dumps the JSON declaration of this object in a string
Parameters:
indent (int): The number of indentation spaces at every indentation
level
Returns:
str: The JSON representation for this object
"""
return json.dumps(self.data, indent=indent, cls=utils.NumpyJSONEncoder)
def __str__(self):
return self.json_dumps()
def write(self, storage=None): def write(self, storage=None):
"""Writes contents to prefix location """Writes contents to prefix location
...@@ -287,4 +320,4 @@ class Plotterparameter(object): ...@@ -287,4 +320,4 @@ class Plotterparameter(object):
raise RuntimeError("plotterparameter has no name") raise RuntimeError("plotterparameter has no name")
storage = self.storage # overwrite storage = self.storage # overwrite
storage.save(str(self), self.code, self.description) storage.save(str(self), self.description)
{ {
"plotter": "plot/unknown/1", "plotter": "plot/unknown/1",
"description": "", "description": "",
"data": {} "data": {}
} }
...@@ -35,27 +35,52 @@ ...@@ -35,27 +35,52 @@
import nose.tools import nose.tools
from . import prefix
from ..plotterparameter import Plotterparameter from ..plotterparameter import Plotterparameter
from ..plotterparameter import Storage
from . import prefix
def test_default(): def test_default():
# test for the "dummy" plotterparameter # test for the "dummy" plotterparameter
p = Plotterparameter(prefix, data=None) p = Plotterparameter(prefix, data=None)
nose.tools.assert_false(p.valid) nose.tools.assert_false(p.valid)
def test_plot_config_1(): def test_plot_config_1():
# test for a simple plotterparameter for a simple plotter # test for a simple plotterparameter for a simple plotter
p = Plotterparameter(prefix, "plot/config/1") p = Plotterparameter(prefix, "plot/config/1")
nose.tools.assert_true(p.valid, "\n * %s" % "\n * ".join(p.errors)) nose.tools.assert_true(p.valid, "\n * %s" % "\n * ".join(p.errors))
def test_plot_invalid_1(): def test_plot_invalid_1():
# test for invalid parameter name # test for invalid parameter name
p = Plotterparameter(prefix, "plot/invalid/1") p = Plotterparameter(prefix, "plot/invalid/1")
nose.tools.assert_false(p.valid) nose.tools.assert_false(p.valid)
nose.tools.assert_true(p.errors[0] == "'not_an_option' isn't a parameter for plotter user/scatter/1") nose.tools.assert_true(
p.errors[0] == "'not_an_option' isn't a parameter for plotter user/scatter/1"
)
def test_plot_invalid_2(): def test_plot_invalid_2():
# test for invalid "plotter" field # test for invalid "plotter" field
p = Plotterparameter(prefix, "plot/invalid/2") p = Plotterparameter(prefix, "plot/invalid/2")
nose.tools.assert_false(p.valid) nose.tools.assert_false(p.valid)
nose.tools.assert_true(p.errors[0] == "Plotter declaration file not found: user/not_a_plotter/1") nose.tools.assert_true(
p.errors[0] == "Plotter declaration file not found: user/not_a_plotter/1"
)
def test_export():
plotterparameter_name = "plot/config/1"
target_name = "plot/generated/1"
obj = Plotterparameter(prefix, plotterparameter_name)
nose.tools.assert_true(obj.valid, "\n * %s" % "\n * ".join(obj.errors))
pp_storage = Storage(prefix, target_name)
obj.write(pp_storage)
# load from tmp_prefix and validates
exported = Plotterparameter(prefix, target_name)
nose.tools.assert_true(exported.valid, "\n * %s" % "\n * ".join(exported.errors))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment