9b61c6ecec157eb693513a784c62536bfa56327b
[h5config.git] / h5config / storage / yaml.py
1 # Copyright (C) 2011 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of h5config.
4 #
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.
9 #
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.
14 #
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/>.
17
18 """HDF5 backend implementation
19 """
20
21 from __future__ import absolute_import
22
23 import os.path as _os_path
24 import types as _types
25
26 import yaml as _yaml  # global PyYAML module
27
28 from .. import LOG as _LOG
29 from .. import config as _config
30 from . import FileStorage as _FileStorage
31
32
33 class _YAMLDumper (_yaml.SafeDumper):
34     def represent_bool(self, data):
35         "Use yes/no instead of the default true/false"
36         if data:
37             value = u'yes'
38         else:
39             value = u'no'
40         return self.represent_scalar(u'tag:yaml.org,2002:bool', value)
41
42
43 _YAMLDumper.add_representer(bool, _YAMLDumper.represent_bool)
44
45
46 class YAML_Storage (_FileStorage):
47     """Back a `Config` class with a YAML file.
48
49     >>> import os
50     >>> from ..test import TestConfig
51     >>> import os.path
52     >>> import tempfile
53     >>> fd,filename = tempfile.mkstemp(
54     ...     suffix='.'+YAML_Storage.extension, prefix='pypiezo-')
55     >>> os.close(fd)
56
57     >>> c = TestConfig(storage=YAML_Storage(filename=filename))
58     >>> c.load()
59
60     Saving writes all the config values to disk.
61
62     >>> c['alive'] = True
63     >>> c.save()
64     >>> print(open(filename, 'r').read())  # doctest: +REPORT_UDIFF
65     age: 1.3
66     alive: yes
67     bids:
68     - 5.4
69     - 3.2
70     - 1
71     children: ''
72     claws:
73     - 1
74     - 2
75     daisies: 13
76     name: ''
77     species: Norwegian Blue
78     spouse: ''
79     words:
80     - cracker
81     - wants
82     <BLANKLINE>
83
84     Loading reads the config files from disk.
85
86     >>> c.clear()
87     >>> c['alive']
88     False
89     >>> c.load()
90     >>> c['alive']
91     True
92
93     Cleanup our temporary config file.
94
95     >>> os.remove(filename)
96     """
97     extension = 'yaml'
98     dumper = _YAMLDumper
99
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)
105         if data == None:
106             return  # empty file
107         return self._from_dict(config, data)
108
109     @staticmethod
110     def _from_dict(config, data):
111         settings = dict([(s.name, s) for s in config.settings])
112         for key,value in data.iteritems():
113             setting = settings[key]
114             if isinstance(setting, (_config.BooleanSetting,
115                                     _config.NumericSetting,
116                                     _config.ListSetting,
117                                     _config.IntegerListSetting,
118                                     _config.FloatListSetting)):
119                 if isinstance(value, _types.StringTypes):
120                     # older versions of h5config
121                     value = s.convert_from_text(value)
122                 v = value
123             elif isinstance(setting, _config.ConfigListSetting) and value:
124                 values = []
125                 for v in value:
126                     values.append(YAML_Storage._from_dict(
127                             setting.config_class(), v))
128                 v = values
129             elif isinstance(setting, _config.ConfigSetting) and value:
130                 v = YAML_Storage._from_dict(setting.config_class(), value)
131             else:
132                 v = setting.convert_from_text(value)
133             config[key] = v
134         return config
135
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)
142
143     @staticmethod
144     def _to_dict(config):
145         data = {}
146         settings = dict([(s.name, s) for s in config.settings])
147         for key,value in config.iteritems():
148             if key in settings:
149                 setting = settings[key]
150                 if isinstance(setting, (_config.BooleanSetting,
151                                         _config.NumericSetting,
152                                         _config.ListSetting,
153                                         _config.IntegerListSetting,
154                                         _config.FloatListSetting)):
155                     v = value
156                 elif isinstance(setting, _config.ConfigListSetting) and value:
157                     values = []
158                     for v in value:
159                         values.append(YAML_Storage._to_dict(v))
160                     v = values
161                 elif isinstance(setting, _config.ConfigSetting) and value:
162                     v = YAML_Storage._to_dict(value)
163                 else:
164                     v = setting.convert_to_text(value)
165                 data[key] = v
166         return data