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
25 import yaml as _yaml # global PyYAML module
27 from .. import LOG as _LOG
28 from .. import config as _config
29 from . import FileStorage as _FileStorage
30 from . import is_string as _is_string
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('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
77 species: Norwegian Blue
84 Loading reads the config files from disk.
93 Cleanup our temporary config file.
95 >>> os.remove(filename)
100 def _load(self, config):
101 if not _os_path.exists(self._filename):
102 open(self._filename, 'a').close()
103 with open(self._filename, 'r') as f:
104 data = _yaml.safe_load(f)
107 return self._from_dict(config, data)
110 def _from_dict(config, data):
111 settings = dict([(s.name, s) for s in config.settings])
112 for key,value in data.items():
113 setting = settings[key]
114 if isinstance(setting, (_config.BooleanSetting,
115 _config.NumericSetting,
117 _config.IntegerListSetting,
118 _config.FloatListSetting)):
119 if _is_string(value):
120 # older versions of h5config
121 value = s.convert_from_text(value)
123 elif isinstance(setting, _config.ConfigListSetting) and value:
126 values.append(YAML_Storage._from_dict(
127 setting.config_class(), v))
129 elif isinstance(setting, _config.ConfigSetting) and value:
130 v = YAML_Storage._from_dict(setting.config_class(), value)
132 v = setting.convert_from_text(value)
136 def _save(self, config):
137 self._create_basedir(filename=self._filename)
138 data = self._to_dict(config)
139 with open(self._filename, 'w') as f:
140 _yaml.dump(data, stream=f, Dumper=self.dumper,
141 default_flow_style=False)
144 def _to_dict(config):
146 settings = dict([(s.name, s) for s in config.settings])
147 for key,value in config.items():
149 setting = settings[key]
150 if isinstance(setting, (_config.BooleanSetting,
151 _config.NumericSetting,
153 _config.IntegerListSetting,
154 _config.FloatListSetting)):
156 elif isinstance(setting, _config.ConfigListSetting) and value:
159 values.append(YAML_Storage._to_dict(v))
161 elif isinstance(setting, _config.ConfigSetting) and value:
162 v = YAML_Storage._to_dict(value)
164 v = setting.convert_to_text(value)