Mercurial > treepkg > treepkg
comparison treepkg/readconfig.py @ 2:e6a9f4037f68
readconfig.py is smarter now about conversions and supports shlex
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Thu, 08 Mar 2007 19:55:44 +0100 |
parents | f78a02e79c84 |
children | fee641fec94e |
comparison
equal
deleted
inserted
replaced
1:02d5702314a3 | 2:e6a9f4037f68 |
---|---|
6 # Read the file COPYING coming with the software for details. | 6 # Read the file COPYING coming with the software for details. |
7 | 7 |
8 """Reads the configuration file""" | 8 """Reads the configuration file""" |
9 | 9 |
10 import sys | 10 import sys |
11 import shlex | |
11 from ConfigParser import SafeConfigParser, NoOptionError | 12 from ConfigParser import SafeConfigParser, NoOptionError |
12 | |
13 | |
14 assembly_line_keys = ["name", "base_dir", "svn_url", "root_cmd", | |
15 "deb_email", "deb_fullname"] | |
16 | 13 |
17 defaults = dict(root_cmd="sudo") | 14 defaults = dict(root_cmd="sudo") |
18 | 15 |
16 packager_desc = [ | |
17 "name", "base_dir", "svn_url", | |
18 ("root_cmd", shlex.split), | |
19 "deb_email", "deb_fullname", | |
20 ] | |
21 | |
22 treepkg_desc = [ | |
23 ("check_interval", int), | |
24 ] | |
25 | |
26 | |
27 def read_config_section(parser, section, item_desc, defaults=None): | |
28 if defaults is None: | |
29 defaults = dict() | |
30 options = dict() | |
31 for item in item_desc: | |
32 if isinstance(item, tuple): | |
33 key, converter = item | |
34 else: | |
35 key = item | |
36 converter = str | |
37 try: | |
38 value = parser.get(section, key, vars=defaults) | |
39 options[key] = converter(value) | |
40 except NoOptionError: | |
41 print >>sys.stderr, "%s: Missing option %r in section %r" \ | |
42 % (filename, key, section) | |
43 sys.exit(1) | |
44 return options | |
19 | 45 |
20 | 46 |
21 def read_config(filename): | 47 def read_config(filename): |
22 """Reads the packagemill configuration from filename. | 48 """Reads the tree packager configuration from the file given by filename. |
23 | 49 |
24 The return value is a list of (name, option) pairs, one for each | 50 The function returns a tuple with a ('treepkg') and a list of dicts |
25 assembly line of the mill. | 51 ('packagers'). The treepkg dict contains the main configuration of |
52 the tree packager. The packagers list contains one dict with the | |
53 configuratiin for each packager. | |
26 """ | 54 """ |
27 assembly_lines = [] | |
28 parser = SafeConfigParser(defaults) | 55 parser = SafeConfigParser(defaults) |
29 parser.read([filename]) | 56 parser.read([filename]) |
57 | |
58 # extract packager configurations | |
59 packagers = [] | |
30 for section in parser.sections(): | 60 for section in parser.sections(): |
31 if section.startswith("pkg_"): | 61 if section.startswith("pkg_"): |
32 pkg_defaults = dict(name=section[4:]) | 62 packagers.append(read_config_section(parser, section, packager_desc, |
33 options = {} | 63 dict(name=section[4:]))) |
34 for key in assembly_line_keys: | |
35 try: | |
36 options[key] = parser.get(section, key, vars=pkg_defaults) | |
37 except NoOptionError: | |
38 print >>sys.stderr, "%s: Missing option %r in section %r" \ | |
39 % (filename, key, section) | |
40 sys.exit(1) | |
41 assembly_lines.append(options) | |
42 | 64 |
43 treepkg_opts = dict(check_interval=parser.getint("treepkg", | 65 # main config |
44 "check_interval")) | 66 treepkg = read_config_section(parser, "treepkg", treepkg_desc) |
45 | 67 |
46 return treepkg_opts, assembly_lines | 68 return treepkg, packagers |
47 | 69 |
48 | 70 |
49 if __name__ == "__main__": | 71 if __name__ == "__main__": |
50 print read_config(sys.argv[1]) | 72 import pprint |
73 print pprint.pprint(read_config(sys.argv[1])) |