Statistics
| Branch: | Tag: | Revision:

root / kamaki / config.py @ 49919699

History | View | Annotate | Download (3.9 kB)

1
# Copyright 2011-2012 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
import os
35

    
36
from collections import defaultdict
37
from ConfigParser import RawConfigParser, NoOptionError, NoSectionError
38

    
39
from .utils import OrderedDict
40

    
41

    
42
HEADER = """
43
# Kamaki configuration file
44
"""
45

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

    
77

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