equal
deleted
inserted
replaced
|
1 import sys |
|
2 from cubicweb import warning |
|
3 |
|
4 def lines(path, comments=None): |
|
5 result = [] |
|
6 for line in open(path, 'U'): |
|
7 line = line.strip() |
|
8 if line and (comments is None or not line.startswith(comments)): |
|
9 result.append(line) |
|
10 return result |
|
11 |
|
12 def read_config(config_file): |
|
13 """read the application configuration from a file and return it as a |
|
14 dictionnary |
|
15 |
|
16 :type config_file: str |
|
17 :param config_file: path to the configuration file |
|
18 |
|
19 :rtype: dict |
|
20 :return: a dictionary with specified values associated to option names |
|
21 """ |
|
22 config = current = {} |
|
23 try: |
|
24 for line in lines(config_file, comments='#'): |
|
25 try: |
|
26 option, value = line.split('=', 1) |
|
27 except ValueError: |
|
28 option = line.strip().lower() |
|
29 if option[0] == '[': |
|
30 # start a section |
|
31 section = option[1:-1] |
|
32 assert not config.has_key(section), \ |
|
33 'Section %s is defined more than once' % section |
|
34 config[section] = current = {} |
|
35 continue |
|
36 print >> sys.stderr, 'ignoring malformed line\n%r' % line |
|
37 continue |
|
38 option = option.strip().replace(' ', '_') |
|
39 value = value.strip() |
|
40 current[option] = value or None |
|
41 except IOError, ex: |
|
42 warning('missing or non readable configuration file %s (%s)', |
|
43 config_file, ex) |
|
44 return config |
|
45 |
|
46 def env_path(env_var, default, name): |
|
47 return default |
|
48 |
|
49 def create_dir(*args): |
|
50 raise RuntimeError() |