1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import os
17 import sys
18
19 try:
20 import configparser as ConfigParser
21 except ImportError:
22 import ConfigParser
23
24 InterpolationError = ConfigParser.InterpolationError
25
26
28 _instance = None
29 _global_config_file = "/etc/sysconfig/rhn/osad.conf"
30
31 - def __init__(self, section, defaults=None):
32 """defaults is either None, or a dictionary of default values which can be overridden"""
33 ConfigParser.ConfigParser.__init__(self, defaults)
34 self.section = section
35 self.overrides = {}
36
38 self.overrides.clear()
39 self.overrides.update(overrides)
40
41 try:
42 self.read(self._get_config_files())
43 except ConfigParser.MissingSectionHeaderError:
44 e = sys.exc_info()[1]
45 print("Config error: line %s, file %s: %s" % (e.lineno,
46 e.filename, e))
47 sys.exit(1)
48
53
55 try:
56 return self.get(self.section, option, vars=self.overrides)
57 except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
58 e = sys.exc_info()[1]
59 pass
60
61 defaults = self.defaults()
62
63 if option in defaults:
64 return defaults[option]
65
66 return defval
67
69 return self.has_option(self.section, option)
70
72 return self.options(self.section)
73
76
79
80 -def init(section, defaults=None, config_file=None, **overrides):
87
88 -def get(var, defval=None):
90
95
98
101
103 _modified = 0
104 c = UserPassConfigParser(section)
105 if auth_file is not None:
106 c._global_config_file = auth_file
107 c.read_config_files()
108 if not c.has_section(section):
109 _modified = 1
110 c.add_section(section)
111 for k, v in defaults.items():
112 if not c.has_option(section, k) or force:
113 c.set(section, k, v)
114 _modified = 1
115 if _modified:
116 fd = os.open(c._global_config_file,
117 os.O_CREAT | os.O_TRUNC | os.O_WRONLY, int("0600", 8))
118 f = os.fdopen(fd, "w")
119 f.write("# Automatically generated. Do not edit!\n\n")
120 c.write(f)
121 f.close()
122 return c
123
125 init('osad')
126 print("server_url: %s" % get("server_url"))
127
128 auth_file = "/tmp/osad-auth.conf"
129 section = "osad-auth"
130 c = get_auth_info(auth_file, section, username="aaa", password="bbb")
131 print(c.keys())
132
133 if __name__ == '__main__':
134 main()
135