2 # -*- coding: utf-8 -*-
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
16 # @author: David Blaisonneau <david.blaisonneau@orange.com>
17 # @author: Arnaud Morin <arnaud1.morin@orange.com>
19 from yaml import load, dump
21 from yaml import CLoader as Loader, CDumper as Dumper
23 from yaml import Loader, Dumper
26 class OpenSteakConfig:
27 """OpenSteak config class
28 Use this object as a dict
32 config_file="/usr/local/opensteak/infra/config/common.yaml",
35 Load saved opensteak config.
37 @param PARAM: DESCRIPTION
38 @return RETURN: DESCRIPTION
39 @param config_file: the yaml config file to read.
40 default is '/usr/local/opensteak/infra/config/common.yaml'
41 @param autosave: save automaticly the config at destroy
44 self.config_file = config_file
45 self.autosave = autosave
46 with open(self.config_file, 'r') as stream:
47 self._data = load(stream, Loader=Loader)
49 def __getitem__(self, index):
50 """Get an item of the configuration"""
51 return self._data[index]
53 def __setitem__(self, index, value):
54 """Set an item of the configuration"""
55 self._data[index] = value
58 """Set an item of the configuration"""
59 return self._data.keys()
62 """Dump the configuration"""
63 return dump(self._data, Dumper=Dumper)
66 """Save the configuration to the file"""
67 with open(self.config_file, 'w') as f:
68 f.write(dump(self._data, Dumper=Dumper))