Statistics
| Branch: | Tag: | Revision:

root / kamaki / config.py @ d83a2834

History | View | Annotate | Download (4.1 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
# Path to the file that stores the configuration
43
CONFIG_PATH = os.path.expanduser('~/.kamakirc')
44

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

    
48
HEADER = """
49
# Kamaki configuration file
50
"""
51

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

    
83

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