Statistics
| Branch: | Tag: | Revision:

root / kamaki / config.py @ f3ddb705

History | View | Annotate | Download (3.7 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from collections import defaultdict
35
from ConfigParser import RawConfigParser, NoOptionError, NoSectionError
36

    
37
from .utils import OrderedDict
38

    
39

    
40
HEADER = """
41
# Kamaki configuration file
42
"""
43

    
44
DEFAULTS = {
45
    'global': {
46
        'colors': 'on',
47
        'token': ''
48
    },
49
    'compute': {
50
        'enable': 'on',
51
        'cyclades_extensions': 'on',
52
        'url': 'https://okeanos.grnet.gr/api/v1.1',
53
        'token': ''
54
    },
55
    'image': {
56
        'enable': 'on',
57
        'url': 'https://okeanos.grnet.gr/plankton',
58
        'token': ''
59
    },
60
    'storage': {
61
        'enable': 'on',
62
        'pithos_extensions': 'on',
63
        'url': 'https://plus.pithos.grnet.gr/v1',
64
        'account': '',
65
        'container': '',
66
        'token': ''
67
    }
68
}
69

    
70

    
71
class Config(RawConfigParser):
72
    def __init__(self, path=None):
73
        RawConfigParser.__init__(self, dict_type=OrderedDict)
74
        self.path = path
75
        self._overrides = defaultdict(dict)
76
        self.read(path)
77
    
78
    def sections(self):
79
        return DEFAULTS.keys()
80
    
81
    def get(self, section, option):
82
        value = self._overrides.get(section, {}).get(option)
83
        if value is not None:
84
            return value
85
        
86
        try:
87
            return RawConfigParser.get(self, section, option)
88
        except (NoSectionError, NoOptionError) as e:
89
            return DEFAULTS.get(section, {}).get(option)
90
    
91
    def set(self, section, option, value):
92
        if section not in RawConfigParser.sections(self):
93
            self.add_section(section)
94
        RawConfigParser.set(self, section, option, value)
95
    
96
    def remove_option(self, section, option):
97
        try:
98
            RawConfigParser.remove_option(self, section, option)
99
        except NoSectionError:
100
            pass
101
    
102
    def items(self, section, include_defaults=False):
103
        d = dict(DEFAULTS[section]) if include_defaults else {}
104
        try:
105
            d.update(RawConfigParser.items(self, section))
106
        except NoSectionError:
107
            pass
108
        return d.items()
109
    
110
    def override(self, section, option, value):
111
        self._overrides[section][option] = value
112
    
113
    def write(self):
114
        with open(self.path, 'w') as f:
115
            f.write(HEADER.lstrip())
116
            RawConfigParser.write(self, f)