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

34
35
import os

36
37
from collections import defaultdict
from ConfigParser import RawConfigParser, NoOptionError, NoSectionError
Giorgos Verigakis's avatar
Giorgos Verigakis committed
38

39
from .utils import OrderedDict
Giorgos Verigakis's avatar
Giorgos Verigakis committed
40
41


42
43
44
HEADER = """
# Kamaki configuration file
"""
Giorgos Verigakis's avatar
Giorgos Verigakis committed
45

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

Giorgos Verigakis's avatar
Giorgos Verigakis committed
77

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