Statistics
| Branch: | Tag: | Revision:

root / kamaki / config.py @ 5afb968b

History | View | Annotate | Download (4.2 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
try:
40
    from collections import OrderedDict
41
except ImportError:
42
    from ordereddict import OrderedDict
43

    
44

    
45
# Path to the file that stores the configuration
46
CONFIG_PATH = os.path.expanduser('~/.kamakirc')
47

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

    
51
HEADER = """
52
# Kamaki configuration file
53
"""
54

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

    
86

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