1 # Copyright (C) 2011 W. Trevor King <wking@drexel.edu>
3 # This file is part of h5config.
5 # h5config is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the
7 # Free Software Foundation, either version 3 of the License, or (at your
8 # option) any later version.
10 # h5config is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with h5config. If not, see <http://www.gnu.org/licenses/>.
18 """HDF5 backend implementation
21 from __future__ import absolute_import
23 import os.path as _os_path
24 import types as _types
26 import yaml as _yaml # global PyYAML module
28 from .. import LOG as _LOG
29 from .. import config as _config
30 from . import FileStorage as _FileStorage
33 class _YAMLDumper (_yaml.SafeDumper):
34 def represent_bool(self, data):
35 "Use yes/no instead of the default true/false"
40 return self.represent_scalar(u'tag:yaml.org,2002:bool', value)
43 _YAMLDumper.add_representer(bool, _YAMLDumper.represent_bool)
46 class YAML_Storage (_FileStorage):
47 """Back a `Config` class with a YAML file.
50 >>> from ..test import TestConfig
53 >>> fd,filename = tempfile.mkstemp(
54 ... suffix='.'+YAML_Storage.extension, prefix='pypiezo-')
57 >>> c = TestConfig(storage=YAML_Storage(filename=filename))
60 Saving writes all the config values to disk.
64 >>> print open(filename, 'r').read() # doctest: +REPORT_UDIFF
74 species: Norwegian Blue
78 Loading reads the config files from disk.
87 Cleanup our temporary config file.
89 >>> os.remove(filename)
94 def _load(self, config):
95 if not _os_path.exists(self._filename):
96 open(self._filename, 'a').close()
97 with open(self._filename, 'r') as f:
98 data = _yaml.safe_load(f)
101 return self._from_dict(config, data)
104 def _from_dict(config, data):
105 settings = dict([(s.name, s) for s in config.settings])
106 for key,value in data.iteritems():
107 setting = settings[key]
108 if isinstance(setting, (_config.BooleanSetting,
109 _config.NumericSetting,
110 _config.FloatListSetting)):
111 if isinstance(value, _types.StringTypes):
112 # older versions of h5config
113 value = s.convert_from_text(value)
115 elif isinstance(setting, _config.ConfigListSetting) and value:
118 values.append(YAML_Storage._from_dict(
119 setting.config_class(), v))
121 elif isinstance(setting, _config.ConfigSetting) and value:
122 v = YAML_Storage._from_dict(setting.config_class(), value)
124 v = setting.convert_from_text(value)
128 def _save(self, config):
129 data = self._to_dict(config)
130 with open(self._filename, 'w') as f:
131 _yaml.dump(data, stream=f, Dumper=self.dumper,
132 default_flow_style=False)
135 def _to_dict(config):
137 settings = dict([(s.name, s) for s in config.settings])
138 for key,value in config.iteritems():
140 setting = settings[key]
141 if isinstance(setting, (_config.BooleanSetting,
142 _config.NumericSetting,
143 _config.FloatListSetting)):
145 elif isinstance(setting, _config.ConfigListSetting) and value:
148 values.append(YAML_Storage._to_dict(v))
150 elif isinstance(setting, _config.ConfigSetting) and value:
151 v = YAML_Storage._to_dict(value)
153 v = setting.convert_to_text(value)