Commit 61f96996 authored by blissadm_ID31@bibhelm's avatar blissadm_ID31@bibhelm
Browse files

config: .to_dict() resolves references by default

parent 293e31ff
......@@ -485,10 +485,11 @@ class ConfigNode(MutableMapping):
)
except RuntimeError:
# file does not exist
yaml_contents = self.to_dict()
yaml_contents = self.to_dict(resolve_references=False)
else:
prudent_update(
ConfigNode.goto_path(yaml_contents, self.path), self.to_dict()
ConfigNode.goto_path(yaml_contents, self.path),
self.to_dict(resolve_references=False),
)
string_stream = StringIO()
......@@ -504,13 +505,25 @@ class ConfigNode(MutableMapping):
# keep source node in case of saving
return node
def to_dict(self):
def to_dict(self, resolve_references=True):
"""
full copy and transform to dict object.
the return object is a simple dictionary
"""
return json.loads(json.dumps(self._data, cls=ConfigNodeDictEncoder))
if resolve_references:
def decoder_hook(d):
for k, v in d.items():
if isinstance(v, str) and ConfigReference.is_reference(v):
d[k] = ConfigReference(self.parent, v).dereference()
return d
return json.JSONDecoder(object_hook=decoder_hook).decode(
json.dumps(self._data, cls=ConfigNodeDictEncoder)
)
else:
return json.loads(json.dumps(self._data, cls=ConfigNodeDictEncoder))
@staticmethod
def _pprint(node, cur_indet, indent, cur_depth, depth):
......
......@@ -6,6 +6,7 @@
# Distributed under the GNU LGPLv3. See LICENSE for more info.
from bliss.config.conductor import client
from tests.motors.conftest import s1hg, s1vo, m0
import pytest
import sys, os
import ruamel
......@@ -290,3 +291,31 @@ def test_user_tags(beacon):
assert set(
[beacon.get_config(obj) for obj in obj_list]
) == beacon.get_user_tag_configs(tag)
def test_to_dict(beacon, s1hg, s1vo, m0):
"""
- name: refs_test
plugin: default
m0: $m0
slits:
- axis: $s1hg
position: 0
- axis: $s1vo
position: 1
scan:
axis: $m0
"""
refs_test = beacon.get("refs_test")
refs_test_dict = refs_test.to_dict(resolve_references=False)
assert refs_test_dict["m0"] == "$m0"
assert refs_test_dict["slits"][0]["axis"] == "$s1hg"
assert refs_test_dict["slits"][1]["axis"] == "$s1vo"
assert refs_test_dict["scan"]["axis"] == "$m0"
refs_test_dict = refs_test.to_dict()
assert refs_test_dict["m0"] is m0
assert refs_test_dict["slits"][0]["axis"] is s1hg
assert refs_test_dict["slits"][1]["axis"] is s1vo
assert refs_test_dict["scan"]["axis"] is m0
Supports Markdown
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