676333ddf2a7e9931caaf143623b0e2776425cf9
[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     daisies: 13
73     name: ''
74     species: Norwegian Blue
75     spouse: ''
76     <BLANKLINE>
77
78     Loading reads the config files from disk.
79
80     >>> c.clear()
81     >>> c['alive']
82     False
83     >>> c.load()
84     >>> c['alive']
85     True
86
87     Cleanup our temporary config file.
88
89     >>> os.remove(filename)
90     """
91     extension = 'yaml'
92     dumper = _YAMLDumper
93
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)
99         if data == None:
100             return  # empty file
101         return self._from_dict(config, data)
102
103     @staticmethod
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)
114                 v = value
115             elif isinstance(setting, _config.ConfigListSetting) and value:
116                 values = []
117                 for v in value:
118                     values.append(YAML_Storage._from_dict(
119                             setting.config_class(), v))
120                 v = values
121             elif isinstance(setting, _config.ConfigSetting) and value:
122                 v = YAML_Storage._from_dict(setting.config_class(), value)
123             else:
124                 v = setting.convert_from_text(value)
125             config[key] = v
126         return config
127
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)
133
134     @staticmethod
135     def _to_dict(config):
136         data = {}
137         settings = dict([(s.name, s) for s in config.settings])
138         for key,value in config.iteritems():
139             if key in settings:
140                 setting = settings[key]
141                 if isinstance(setting, (_config.BooleanSetting,
142                                         _config.NumericSetting,
143                                         _config.FloatListSetting)):
144                     v = value
145                 elif isinstance(setting, _config.ConfigListSetting) and value:
146                     values = []
147                     for v in value:
148                         values.append(YAML_Storage._to_dict(v))
149                     v = values
150                 elif isinstance(setting, _config.ConfigSetting) and value:
151                     v = YAML_Storage._to_dict(value)
152                 else:
153                     v = setting.convert_to_text(value)
154                 data[key] = v
155         return data