config.py 4.14 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
45
46
47
# Path to the file that stores the configuration
CONFIG_PATH = os.path.expanduser('~/.kamakirc')

# Name of a shell variable to bypass the CONFIG_PATH value
CONFIG_ENV = 'KAMAKI_CONFIG'

48
49
50
HEADER = """
# Kamaki configuration file
"""
Giorgos Verigakis's avatar
Giorgos Verigakis committed
51

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

Giorgos Verigakis's avatar
Giorgos Verigakis committed
83

84
85
86
class Config(RawConfigParser):
    def __init__(self, path=None):
        RawConfigParser.__init__(self, dict_type=OrderedDict)
87
        self.path = path or os.environ.get(CONFIG_ENV, CONFIG_PATH)
88
        self._overrides = defaultdict(dict)
89
        self.read(self.path)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
90
    
91
92
93
94
95
96
97
    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
98
99
        
        try:
100
            return RawConfigParser.get(self, section, option)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
101
        except (NoSectionError, NoOptionError):
102
            return DEFAULTS.get(section, {}).get(option)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
103
    
104
105
106
107
    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
108
    
109
110
111
112
113
    def remove_option(self, section, option):
        try:
            RawConfigParser.remove_option(self, section, option)
        except NoSectionError:
            pass
Giorgos Verigakis's avatar
Giorgos Verigakis committed
114
    
115
116
117
118
119
120
121
    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
122
    
123
124
    def override(self, section, option, value):
        self._overrides[section][option] = value
Giorgos Verigakis's avatar
Giorgos Verigakis committed
125
    
126
127
    def write(self):
        with open(self.path, 'w') as f:
128
            os.chmod(self.path, 0600)
129
130
            f.write(HEADER.lstrip())
            RawConfigParser.write(self, f)