Mercurial > treepkg > treepkg
comparison treepkg/readconfig.py @ 258:bb98e728f25b
Allow default values for individual options to be passed to read_config_section.
The default value can now be passed as a third item in a tuple used in
the section description passed to read_config_section. The predefined
option descriptions have been updated to use this new mechanism and the
global defaults variable is not needed anymore. Also, indivdual
PackageTrack classes can now use this mechanism to specify defaults for
their additional configuration options.
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Fri, 17 Apr 2009 18:48:58 +0000 |
parents | eaa696629a91 |
children | faeeac2c4c71 |
comparison
equal
deleted
inserted
replaced
257:ab752c4d0786 | 258:bb98e728f25b |
---|---|
20 return True | 20 return True |
21 if s in ("false", "no", "0"): | 21 if s in ("false", "no", "0"): |
22 return False | 22 return False |
23 raise ValueError("cannot determine boolean value of %r" % (s,)) | 23 raise ValueError("cannot determine boolean value of %r" % (s,)) |
24 | 24 |
25 defaults = dict(root_cmd="sudo", | |
26 signing_key_id="", | |
27 rules_svn_url="") | |
28 | 25 |
29 packager_desc = [ | 26 packager_desc = [ |
30 "name", "base_dir", "svn_url", "rules_svn_url", "packager_class", | 27 "name", "base_dir", "svn_url", ("rules_svn_url", str, ""), "packager_class", |
31 ("root_cmd", shlex.split), "pbuilderrc", | 28 ("root_cmd", shlex.split, "sudo"), "pbuilderrc", |
32 "deb_email", "deb_fullname", "debrevision_prefix", | 29 "deb_email", "deb_fullname", "debrevision_prefix", |
33 ("handle_dependencies", convert_bool), | 30 ("handle_dependencies", convert_bool), |
34 "signing_key_id", | 31 ("signing_key_id", str, ""), |
35 ] | 32 ] |
36 | 33 |
37 treepkg_desc = [ | 34 treepkg_desc = [ |
38 ("check_interval", int), | 35 ("check_interval", int), |
39 "instructions_file", | 36 "instructions_file", |
43 def read_config_section(parser, section, item_desc, defaults=None): | 40 def read_config_section(parser, section, item_desc, defaults=None): |
44 if defaults is None: | 41 if defaults is None: |
45 defaults = dict() | 42 defaults = dict() |
46 options = dict() | 43 options = dict() |
47 for item in item_desc: | 44 for item in item_desc: |
45 has_default_value = False | |
48 if isinstance(item, tuple): | 46 if isinstance(item, tuple): |
49 key, converter = item | 47 key, converter = item[:2] |
48 if len(item) == 3: | |
49 default_value = item[-1] | |
50 has_default_value = True | |
50 else: | 51 else: |
51 key = item | 52 key = item |
52 converter = str | 53 converter = str |
53 try: | 54 try: |
54 value = parser.get(section, key, vars=defaults) | 55 value = parser.get(section, key, vars=defaults) |
55 options[key] = converter(value) | |
56 except NoOptionError: | 56 except NoOptionError: |
57 print >>sys.stderr, "Missing option %r in section %r" \ | 57 if has_default_value: |
58 % (key, section) | 58 value = default_value |
59 sys.exit(1) | 59 else: |
60 print >>sys.stderr, "Missing option %r in section %r" \ | |
61 % (key, section) | |
62 sys.exit(1) | |
63 options[key] = converter(value) | |
60 return options | 64 return options |
61 | 65 |
62 | 66 |
63 def read_config(filename): | 67 def read_config(filename): |
64 """Reads the tree packager configuration from the file given by filename. | 68 """Reads the tree packager configuration from the file given by filename. |
66 The function returns a tuple with a dict ('treepkg') and a list of | 70 The function returns a tuple with a dict ('treepkg') and a list of |
67 dicts ('packagers'). The treepkg dict contains the main | 71 dicts ('packagers'). The treepkg dict contains the main |
68 configuration of the tree packager. The packagers list contains one | 72 configuration of the tree packager. The packagers list contains one |
69 dict with the configuratiin for each packager. | 73 dict with the configuratiin for each packager. |
70 """ | 74 """ |
71 parser = SafeConfigParser(defaults) | 75 parser = SafeConfigParser() |
72 parser.read([filename]) | 76 parser.read([filename]) |
73 | 77 |
74 # extract packager configurations | 78 # extract packager configurations |
75 packagers = [] | 79 packagers = [] |
76 for section in parser.sections(): | 80 for section in parser.sections(): |