Package src :: Module osad_config
[hide private]
[frames] | no frames]

Source Code for Module src.osad_config

  1  # 
  2  # Copyright (c) 2008--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 14  # 
 15   
 16  import os 
 17  import sys 
 18   
 19  try: # python 3 
 20      import configparser as ConfigParser 
 21  except ImportError: # python 2 
 22      import ConfigParser 
 23   
 24  InterpolationError = ConfigParser.InterpolationError 
 25   
 26   
27 -class ClientConfigParser(ConfigParser.ConfigParser):
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
37 - def read_config_files(self, overrides={}):
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
49 - def _get_config_files(self):
50 return [ 51 self._global_config_file, 52 ]
53
54 - def get_option(self, option, defval=None):
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
68 - def has_key(self, option):
69 return self.has_option(self.section, option)
70
71 - def keys(self):
72 return self.options(self.section)
73
74 - def __getitem__(self, item):
75 return self.get_option(item)
76
77 -class UserPassConfigParser(ClientConfigParser):
78 _global_config_file = "/etc/sysconfig/rhn/osad-auth.conf"
79
80 -def init(section, defaults=None, config_file=None, **overrides):
81 cp = ClientConfigParser._instance = ClientConfigParser(section, defaults) 82 # Allow for the config file to be changed, if necessary 83 if config_file is not None: 84 cp._global_config_file = config_file 85 cp.read_config_files(overrides) 86 return cp
87
88 -def get(var, defval=None):
89 return _get_config().get_option(var, defval=defval)
90
91 -def _get_config():
92 if ClientConfigParser._instance is None: 93 raise ValueError("Configuration not initialized") 94 return ClientConfigParser._instance
95
96 -def instance():
97 return _get_config()
98
99 -def keys():
100 return _get_config().keys()
101
102 -def get_auth_info(auth_file, section, force, **defaults):
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
124 -def main():
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