Skip to content
Snippets Groups Projects
Commit 24ff81ae authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[algorithm] Use error_on_duplicate_key_hook when loading data

parent 2b5b629b
No related branches found
No related tags found
1 merge request!51Handle duplicate key
...@@ -48,7 +48,7 @@ import logging ...@@ -48,7 +48,7 @@ import logging
import six import six
import numpy import numpy
import simplejson import simplejson as json
from . import dataformat from . import dataformat
from . import library from . import library
...@@ -433,7 +433,14 @@ class Algorithm(object): ...@@ -433,7 +433,14 @@ class Algorithm(object):
return return
with open(json_path, "rb") as f: with open(json_path, "rb") as f:
self.data = simplejson.loads(f.read().decode("utf-8")) try:
self.data = json.loads(
f.read().decode("utf-8"),
object_pairs_hook=utils.error_on_duplicate_key_hook,
)
except RuntimeError as error:
self.errors.append("Algorithm declaration file invalid: %s" % error)
return
self.code_path = self.storage.code.path self.code_path = self.storage.code.path
self.code = self.storage.code.load() self.code = self.storage.code.load()
...@@ -938,7 +945,7 @@ class Algorithm(object): ...@@ -938,7 +945,7 @@ class Algorithm(object):
""" """
return simplejson.dumps(self.data, indent=indent, cls=utils.NumpyJSONEncoder) return json.dumps(self.data, indent=indent, cls=utils.NumpyJSONEncoder)
def __str__(self): def __str__(self):
return self.json_dumps() return self.json_dumps()
......
{
"language": "python",
"splittable": false,
"splittable": true,
"groups": [
{
"inputs": {
"in": {
"type": "user/single_integer/1"
}
},
"outputs": {
"out": {
"type": "user/single_integer/1"
}
}
}
]
}
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
###################################################################################
# #
# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided that the following conditions are met: #
# #
# 1. Redistributions of source code must retain the above copyright notice, this #
# list of conditions and the following disclaimer. #
# #
# 2. Redistributions in binary form must reproduce the above copyright notice, #
# this list of conditions and the following disclaimer in the documentation #
# and/or other materials provided with the distribution. #
# #
# 3. Neither the name of the copyright holder nor the names of its contributors #
# may be used to endorse or promote products derived from this software without #
# specific prior written permission. #
# #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED #
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE #
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE #
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL #
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR #
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER #
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, #
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE #
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #
# #
###################################################################################
class Algorithm:
def process(self, inputs, outputs):
return True
Test documentation
...@@ -92,6 +92,13 @@ class TestLegacyAPI_Loading(unittest.TestCase): ...@@ -92,6 +92,13 @@ class TestLegacyAPI_Loading(unittest.TestCase):
with self.assertRaises(AttributeError): with self.assertRaises(AttributeError):
algorithm.runner() algorithm.runner()
def test_duplicate_key_error(self):
algorithm = Algorithm(prefix, "legacy/duplicate_key_error/1")
self.assertFalse(algorithm.valid)
self.assertNotEqual(
algorithm.errors[0].find("Algorithm declaration file invalid"), -1
)
def test_load_valid_algorithm(self): def test_load_valid_algorithm(self):
algorithm = Algorithm(prefix, "legacy/valid_algorithm/1") algorithm = Algorithm(prefix, "legacy/valid_algorithm/1")
self.assertEqual(algorithm.name, "legacy/valid_algorithm/1") self.assertEqual(algorithm.name, "legacy/valid_algorithm/1")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment