From dddd2e37953d795e4004b1d2722c25ec652135ad Mon Sep 17 00:00:00 2001 From: Adam Coldrick Date: Thu, 6 Oct 2016 22:54:29 +0100 Subject: Add support for YAML lorries This commit makes lorry-controller understand YAML lorry files. If the file cannot be parsed as YAML then we fall back to attempting to load it as JSON, before giving up on loading it completely. The test for broken JSON is modified to use a string which is invalid for both YAML and JSON. Change-Id: If83e2e44b38e6fb63dbf0b857e143fdcabab78ac --- lorrycontroller/readconf.py | 8 ++++++-- yarns.webapp/060-validation.yarn | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lorrycontroller/readconf.py b/lorrycontroller/readconf.py index aee2462..162e116 100644 --- a/lorrycontroller/readconf.py +++ b/lorrycontroller/readconf.py @@ -23,6 +23,7 @@ import re import bottle import cliapp +import yaml import lorrycontroller @@ -238,9 +239,12 @@ class ReadConfiguration(lorrycontroller.LorryControllerRoute): try: with open(filename) as f: - obj = json.load(f) + try: + obj = yaml.safe_load(f) + except yaml.YAMLError: + obj = json.load(f) except ValueError as e: - logging.error('JSON problem in %s', filename) + logging.error('YAML and JSON problem in %s', filename) return [] if type(obj) != dict: diff --git a/yarns.webapp/060-validation.yarn b/yarns.webapp/060-validation.yarn index 076879b..29bc7ca 100644 --- a/yarns.webapp/060-validation.yarn +++ b/yarns.webapp/060-validation.yarn @@ -166,7 +166,7 @@ Make sure WEBAPP handles there not being any `.lorry` files. Add a `.lorry` file that contains broken JSON. - GIVEN Lorry file CONFGIT/notjson.lorry with THIS IS NOT JSON + GIVEN Lorry file CONFGIT/notjson.lorry with { THIS IS NOT JSON ] WHEN admin makes request POST /1.0/read-configuration THEN response matches "has been updated" AND STATEDB is empty -- cgit v1.2.1