Commit febdae5a authored by Matias Guijarro's avatar Matias Guijarro
Browse files

removed ruamel, using standard yaml with specific representer and

constructor as shown in 2nd answer from:
http://stackoverflow.com/questions/5121931/in-python-how-can-you-load-yaml-mappings-as-ordereddicts
added test suite and test file
removed !!omap from mockup.yml
parent 33008e8e
......@@ -56,36 +56,35 @@ if not hasattr(weakref, "WeakSet"):
from .conductor import client
try:
from ruamel import yaml as ordered_yaml
try:
from collections import OrderedDict as ordereddict
from collections import OrderedDict
except ImportError:
# Python 2.6 ?
from ordereddict import OrderedDict as ordereddict
NodeDict = ordereddict
from ordereddict import OrderedDict
except ImportError:
ordered_yaml = None
NodeDict = dict
else:
NodeDict = OrderedDict
CONFIG = None
def dict_representer(dumper, data):
return dumper.represent_dict(data.iteritems())
def dict_constructor(loader, node):
return OrderedDict(loader.construct_pairs(node))
if hasattr(yaml, "CLoader"):
yaml.add_representer(OrderedDict, dict_representer)
yaml.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, dict_constructor)
try:
yaml_load = functools.partial(yaml.load, Loader=yaml.CLoader)
else:
except AttributeError:
yaml_load = yaml.load
CONFIG = None
def load_cfg(filename):
cfg_string = client.get_config_file(filename)
if ordered_yaml:
return ordered_yaml.load(cfg_string,ordered_yaml.RoundTripLoader)
else:
return yaml_load(cfg_string)
def load_cfg_fromstring(cfg_string):
if ordered_yaml:
return ordered_yaml.load(cfg_string,ordered_yaml.RoundTripLoader)
else:
return yaml_load(cfg_string)
return yaml_load(cfg_string)
def get_config(base_path='', timeout=3.):
"""
......@@ -242,12 +241,8 @@ class Node(NodeDict):
save_nodes = self._get_save_dict(node,filename)
else:
save_nodes = self._get_save_list(nodes_2_save,filename)
if ordered_yaml:
file_content = ordered_yaml.dump(save_nodes,
Dumper=ordered_yaml.RoundTripDumper,
default_flow_style=False)
else:
file_content = yaml.dump(save_nodes,default_flow_style=False)
file_content = yaml.dump(save_nodes,default_flow_style=False)
self._config.set_config_db_file(filename,file_content)
def deep_copy(self):
......@@ -413,10 +408,7 @@ class Config(object):
base_path, file_name = os.path.split(path)
fs_node, fs_key = self._get_or_create_path_node(base_path)
if ordered_yaml:
d = ordered_yaml.load(file_content,ordered_yaml.RoundTripLoader)
else:
d = yaml_load(file_content)
d = yaml_load(file_content)
is_init_file = False
if file_name.startswith('__init__'):
......
import pytest
import os
test_cfg = """name: rw_test
test: this is just a test.
one:
- pink: martini
red: apples
blue: berry
two:
- a: true
b: false
c: null
"""
test_cfg2 = """name: rw_test
test: this is just a test.
one:
- pink: martini
red: strawberry
blue: berry
two:
- a: true
b: false
c: null
"""
TEST_FILE_PATH = os.path.join(os.path.dirname(__file__), "..", "test_configuration", "read_write.yml")
def test_config_save(beacon):
rw_cfg = beacon.get_config("rw_test")
# would be better to ask beacon to get file contents dump
with open(TEST_FILE_PATH, "r") as f:
assert f.read() == test_cfg
rw_cfg['one'][0]['red'] = 'strawberry'
rw_cfg.save()
beacon.reload()
rw_cfg2 = beacon.get_config("rw_test")
assert rw_cfg2['one'][0]['red'] == 'strawberry'
with open(TEST_FILE_PATH, "r") as f:
assert f.read() == test_cfg2
rw_cfg2['one'][0]['red'] = 'apples'
rw_cfg2.save()
!!omap
- controller:
- !!omap
- class: mockup
- name: test
- axes:
- !!omap
- name: robz
- steps_per_unit: 10000
- velocity: 100
- acceleration: 300
- low_limit: -1000
- high_limit: 1000000000.0
- !!omap
- name: robz2
- steps_per_unit: 10000
- velocity: 1000
- acceleration: 100
- !!omap
- class: mockup
- axes:
- !!omap
- class: MockupAxis
- name: roby
- backlash: 2
- steps_per_unit: 10000
- velocity: 2500.0
- acceleration: 1000.0
- low_limit:
- high_limit:
- default_cust_attr: 6.28
- !!omap
- name: m1
- steps_per_unit: 1000
- velocity: 50
- acceleration: 1
- encoder: $m1enc
- !!omap
- name: m0
- velocity: 1000
- acceleration: 100
- !!omap
- name: s1f
- steps_per_unit: -1000
- velocity: 500
- acceleration: 100
- low_limit: -10
- high_limit: 10
- !!omap
- name: s1b
- steps_per_unit: 1000
- velocity: 500
- acceleration: 100
- low_limit: -10
- high_limit: 10
- !!omap
- name: s1u
- steps_per_unit: -1000
- velocity: 500
- acceleration: 100
- low_limit: -10
- high_limit: 10
- !!omap
- name: s1d
- steps_per_unit: 1000
- velocity: 500
- acceleration: 100
- low_limit: -10
- high_limit: 10
- !!omap
- name: jogger
- steps_per_unit: -500
- velocity: 100
- acceleration: 300
- encoders:
- !!omap
- name: m1enc
- steps_per_unit: 50
- tolerance: 0.001
- !!omap
- class: slits
- axes:
- !!omap
- name: $s1f
- tags: real front
- !!omap
- name: $s1b
- tags: real back
- !!omap
- name: $s1u
- tags: real up
- !!omap
- name: $s1d
- tags: real down
- !!omap
- name: s1vg
- tags: vgap
- !!omap
- name: s1vo
- tags: voffset
- !!omap
- name: s1hg
- tags: hgap
- low_limit: -15
- !!omap
- name: s1ho
- tags: hoffset
controller:
- class: mockup
name: test
axes:
- name: robz
steps_per_unit: 10000
velocity: 100
acceleration: 300
low_limit: -1000
high_limit: 1000000000.0
- name: robz2
steps_per_unit: 10000
velocity: 1000
acceleration: 100
- class: mockup
axes:
- class: MockupAxis
name: roby
backlash: 2
steps_per_unit: 10000
velocity: 2500.0
acceleration: 1000.0
low_limit: null
high_limit: null
default_cust_attr: 6.28
- name: m1
steps_per_unit: 1000
velocity: 50
acceleration: 1
encoder: $m1enc
- name: m0
velocity: 1000
acceleration: 100
- name: s1f
steps_per_unit: -1000
velocity: 500
acceleration: 100
low_limit: -10
high_limit: 10
- name: s1b
steps_per_unit: 1000
velocity: 500
acceleration: 100
low_limit: -10
high_limit: 10
- name: s1u
steps_per_unit: -1000
velocity: 500
acceleration: 100
low_limit: -10
high_limit: 10
- name: s1d
steps_per_unit: 1000
velocity: 500
acceleration: 100
low_limit: -10
high_limit: 10
- name: jogger
steps_per_unit: -500
velocity: 100
acceleration: 300
encoders:
- name: m1enc
steps_per_unit: 50
tolerance: 0.001
- class: slits
axes:
- name: $s1f
tags: real front
- name: $s1b
tags: real back
- name: $s1u
tags: real up
- name: $s1d
tags: real down
- name: s1vg
tags: vgap
- name: s1vo
tags: voffset
- name: s1hg
tags: hgap
low_limit: -15
- name: s1ho
tags: hoffset
name: rw_test
test: this is just a test.
one:
- pink: martini
red: apples
blue: berry
two:
- a: true
b: false
c: null
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