Merge "Remove Compass from genesis."
[genesis.git] / opensteak / tools / opensteak / conf.py
1 #!/usr/bin/python3
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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
13 # under the License.
14 #
15 # Authors:
16 # @author: David Blaisonneau <david.blaisonneau@orange.com>
17 # @author: Arnaud Morin <arnaud1.morin@orange.com>
18
19 from yaml import load, dump
20 try:
21     from yaml import CLoader as Loader, CDumper as Dumper
22 except ImportError:
23     from yaml import Loader, Dumper
24
25
26 class OpenSteakConfig:
27     """OpenSteak config class
28     Use this object as a dict
29     """
30
31     def __init__(self,
32                  config_file="/usr/local/opensteak/infra/config/common.yaml",
33                  autosave=False):
34         """ Function __init__
35         Load saved opensteak config.
36
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
42             default is False
43         """
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)
48
49     def __getitem__(self, index):
50         """Get an item of the configuration"""
51         return self._data[index]
52
53     def __setitem__(self, index, value):
54         """Set an item of the configuration"""
55         self._data[index] = value
56
57     def list(self):
58         """Set an item of the configuration"""
59         return self._data.keys()
60
61     def dump(self):
62         """Dump the configuration"""
63         return dump(self._data, Dumper=Dumper)
64
65     def save(self):
66         """Save the configuration to the file"""
67         with open(self.config_file, 'w') as f:
68             f.write(dump(self._data, Dumper=Dumper))
69
70     def __del__(self):
71         if self.autosave:
72             self.save()