author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 23 Sep 2009 13:41:18 +0200 | |
changeset 3431 | 6944a92c16f2 |
parent 2476 | 1294a6bdf3bf |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
1 |
""" |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
2 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
3 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
7 |
""" |
0 | 8 |
import sys |
9 |
from cubicweb import warning |
|
10 |
||
11 |
def lines(path, comments=None): |
|
12 |
result = [] |
|
13 |
for line in open(path, 'U'): |
|
14 |
line = line.strip() |
|
15 |
if line and (comments is None or not line.startswith(comments)): |
|
16 |
result.append(line) |
|
17 |
return result |
|
18 |
||
19 |
def read_config(config_file): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2172
diff
changeset
|
20 |
"""read the instance configuration from a file and return it as a |
0 | 21 |
dictionnary |
22 |
||
23 |
:type config_file: str |
|
24 |
:param config_file: path to the configuration file |
|
25 |
||
26 |
:rtype: dict |
|
2172
cf8f9180e63e
delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
27 |
:return: a dictionary with specified values associated to option names |
0 | 28 |
""" |
29 |
config = current = {} |
|
30 |
try: |
|
31 |
for line in lines(config_file, comments='#'): |
|
32 |
try: |
|
33 |
option, value = line.split('=', 1) |
|
34 |
except ValueError: |
|
35 |
option = line.strip().lower() |
|
36 |
if option[0] == '[': |
|
37 |
# start a section |
|
38 |
section = option[1:-1] |
|
39 |
assert not config.has_key(section), \ |
|
40 |
'Section %s is defined more than once' % section |
|
41 |
config[section] = current = {} |
|
42 |
continue |
|
43 |
print >> sys.stderr, 'ignoring malformed line\n%r' % line |
|
44 |
continue |
|
45 |
option = option.strip().replace(' ', '_') |
|
46 |
value = value.strip() |
|
47 |
current[option] = value or None |
|
48 |
except IOError, ex: |
|
49 |
warning('missing or non readable configuration file %s (%s)', |
|
50 |
config_file, ex) |
|
51 |
return config |
|
52 |
||
53 |
def env_path(env_var, default, name): |
|
54 |
return default |
|
55 |
||
56 |
def create_dir(*args): |
|
57 |
raise RuntimeError() |