From 6735c859b401e64e779c762a2a119053a1e01c5f Mon Sep 17 00:00:00 2001 From: Samuel Gaist <samuel.gaist@idiap.ch> Date: Fri, 3 May 2019 09:32:20 +0200 Subject: [PATCH] [protocoltemplate] Implement support for prototype --- beat/core/protocoltemplate.py | 35 ++++++++++++---------- beat/core/prototypes/protocoltemplate.json | 11 +++++++ beat/core/test/test_protocoltemplate.py | 6 ++++ 3 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 beat/core/prototypes/protocoltemplate.json diff --git a/beat/core/protocoltemplate.py b/beat/core/protocoltemplate.py index 11574f56..7b932c12 100644 --- a/beat/core/protocoltemplate.py +++ b/beat/core/protocoltemplate.py @@ -48,6 +48,7 @@ Forward importing from :py:mod:`beat.backend.python.protocoltemplate`: import six from . import schema +from . import prototypes from beat.backend.python.protocoltemplate import Storage from beat.backend.python.protocoltemplate import ( @@ -106,18 +107,22 @@ class ProtocolTemplate(BackendProtocolTemplate): self.storage = None self.dataformats = {} # preloaded dataformats - if isinstance(data, six.string_types): # user has passed a file pointer - - self._name = data - self.storage = Storage(self.prefix, self._name) - data = self.storage.json.path - if not self.storage.json.exists(): - self.errors.append( - "Protocol template declaration file not found: %s" % data - ) - return - - # this runs basic validation, including JSON loading if required - self.data, self.errors = schema.validate("protocoltemplate", data) - if self.errors: - return # don't proceed with the rest of validation + if data is None: # loads prototype and validates it + self.data, self.errors = prototypes.load("protocoltemplate") + assert not self.errors, "\n * %s" % "\n *".join(self.errors) # nosec + else: + if isinstance(data, six.string_types): # user has passed a file pointer + + self._name = data + self.storage = Storage(self.prefix, self._name) + data = self.storage.json.path + if not self.storage.json.exists(): + self.errors.append( + "Protocol template declaration file not found: %s" % data + ) + return + + # this runs basic validation, including JSON loading if required + self.data, self.errors = schema.validate("protocoltemplate", data) + if self.errors: + return # don't proceed with the rest of validation diff --git a/beat/core/prototypes/protocoltemplate.json b/beat/core/prototypes/protocoltemplate.json new file mode 100644 index 00000000..81cd94cd --- /dev/null +++ b/beat/core/prototypes/protocoltemplate.json @@ -0,0 +1,11 @@ +{ + "schema_version": 1, + "sets": [ + { + "name": "name", + "outputs": { + "out": "user/single_integer/1" + } + } + ] +} diff --git a/beat/core/test/test_protocoltemplate.py b/beat/core/test/test_protocoltemplate.py index 526a8560..dfc9c6fe 100644 --- a/beat/core/test/test_protocoltemplate.py +++ b/beat/core/test/test_protocoltemplate.py @@ -41,6 +41,12 @@ from . import prefix, tmp_prefix from .utils import cleanup +def test_default(): + + obj = ProtocolTemplate(prefix, data=None) + nose.tools.assert_true(obj.valid, "\n * %s" % "\n * ".join(obj.errors)) + + def test_export(): for protocol_name in [ "double", -- GitLab