root / snf-common / synnefo / settings / setup / __init__.py @ 7877e6d3
History | View | Annotate | Download (5 kB)
1 |
# Copyright 2013 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 |
|
35 |
class Example(object): |
36 |
"""Example settings are mandatory to be configured with real values.
|
37 |
There are no defaults, and not giving a value is a fatal error.
|
38 |
|
39 |
"""
|
40 |
def __init__(self, example_value, description=None): |
41 |
self.example_value = example_value
|
42 |
self.default_value = None |
43 |
self.description = description
|
44 |
|
45 |
|
46 |
class Default(object): |
47 |
"""Default settings are not mandatory in nature.
|
48 |
There are default values that are meant to work well,
|
49 |
and also serve as an example.
|
50 |
|
51 |
"""
|
52 |
def __init__(self, default_value, example=None, description=None): |
53 |
self.default_value = default_value
|
54 |
if example is None: |
55 |
example = default_value |
56 |
self.example_value = example
|
57 |
self.description = description
|
58 |
|
59 |
|
60 |
class Deprecated(object): |
61 |
"""Deprecated settings must be removed, renamed, or otherwise fixed."""
|
62 |
def __init__(self, rename_to=None, description=None): |
63 |
self.rename_to = rename_to
|
64 |
self.description = description
|
65 |
|
66 |
|
67 |
def get_all_settings(settings): |
68 |
var_list = [] |
69 |
for name in dir(settings): |
70 |
if not name.isupper() or name.startswith('_'): |
71 |
continue
|
72 |
var_list.append((name, getattr(settings, name)))
|
73 |
return var_list
|
74 |
|
75 |
|
76 |
def preproc_settings(settings): |
77 |
other = {} |
78 |
defaults = {} |
79 |
mandatory = {} |
80 |
deprecated = {} |
81 |
|
82 |
for name, value in get_all_settings(settings): |
83 |
if isinstance(value, Example): |
84 |
mandatory[name] = value |
85 |
elif isinstance(value, Default): |
86 |
defaults[name] = value |
87 |
setattr(settings, name, value.default_value)
|
88 |
elif isinstance(value, Deprecated): |
89 |
deprecated[name] = value |
90 |
else:
|
91 |
other[name] = value |
92 |
|
93 |
settings._OTHER = other |
94 |
settings._DEFAULTS = defaults |
95 |
settings._MANDATORY = mandatory |
96 |
settings._DEPRECATED = deprecated |
97 |
|
98 |
|
99 |
def postproc_settings(settings): |
100 |
configured = {} |
101 |
defaults = settings._DEFAULTS |
102 |
failed = [] |
103 |
import os |
104 |
relax_mandatory = bool(os.environ.get('SYNNEFO_RELAX_MANDATORY_SETTINGS')) |
105 |
|
106 |
for name, value in get_all_settings(settings): |
107 |
if isinstance(value, Example): |
108 |
if relax_mandatory:
|
109 |
setattr(settings, name, value.example_value)
|
110 |
else:
|
111 |
m = ("Setting '{name}' is mandatory. "
|
112 |
"Please provide a real value. "
|
113 |
"Example value: '{example}'")
|
114 |
m = m.format(name=name, example=value.example_value) |
115 |
failed.append(m) |
116 |
elif isinstance(value, Default): |
117 |
m = "unprocessed default setting in post processing"
|
118 |
raise AssertionError(m) |
119 |
defaults[name] = value |
120 |
setattr(settings, name, value.default_value)
|
121 |
elif isinstance(value, Deprecated): |
122 |
m = "Setting '{name}' has been deprecated.".format(name=name)
|
123 |
if value.rename_to:
|
124 |
m += " Please rename it to '{rename}'.".format(
|
125 |
rename=value.rename_to) |
126 |
if value.description:
|
127 |
m += "details: {desc}".format(desc=value.description)
|
128 |
failed.append(m) |
129 |
elif name in defaults: |
130 |
if defaults[name].default_value is not value: |
131 |
configured[name] = value |
132 |
|
133 |
settings._CONFIGURED = configured |
134 |
if failed:
|
135 |
import sys |
136 |
sys.stderr.write('\n')
|
137 |
sys.stderr.write('\n'.join(failed))
|
138 |
sys.stderr.write('\n\n')
|
139 |
raise AssertionError("Failed to read settings.") |