Package config_common :: Module local_config
[hide private]
[frames] | no frames]

Source Code for Module config_common.local_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  # python2 
 20  try: 
 21      import ConfigParser 
 22  except: #python3 
 23      import configparser as ConfigParser 
 24   
 25   
 26  from config_common import utils 
 27   
28 -class rhncfgConfigParser(ConfigParser.ConfigParser):
29 _local_config_file_name = '.rhncfgrc' 30 _instance = None 31
32 - def __init__(self, section, defaults=None, config_file_override=None):
33 """defaults is either None, or a dictionary of default values which can be overridden""" 34 if defaults: 35 for (k, v) in defaults.items(): 36 if type(v) == int: 37 defaults[k] = str(v) 38 ConfigParser.ConfigParser.__init__(self, defaults) 39 self.section = section 40 self.overrides = {} 41 self.mydefaults = self.defaults() 42 self.config_file_override = config_file_override
43
44 - def read_config_files(self, overrides={}):
45 self.overrides.clear() 46 self.overrides.update(overrides) 47 48 try: 49 self.read(self._get_config_files()) 50 except ConfigParser.MissingSectionHeaderError: 51 e = sys.exc_info()[1] 52 print("Config error: line %s, file %s: %s" % (e.lineno, 53 e.filename, e)) 54 sys.exit(1)
55
56 - def _get_config_files(self):
57 if sys.platform.find('sunos') > -1: 58 default_config_file = "/opt/redhat/rhn/solaris/etc/sysconfig/rhn/%s.conf" % self.section 59 else: 60 default_config_file = "/etc/sysconfig/rhn/%s.conf" % self.section 61 62 config_file_list = [ 63 default_config_file, 64 os.path.join(utils.get_home_dir(), self._local_config_file_name), 65 self._local_config_file_name, 66 ] 67 68 if self.config_file_override: 69 config_file_list.append(self.config_file_override) 70 71 return config_file_list
72
73 - def get_option(self, option):
74 #6/29/05 wregglej 152388 75 # server_list is always in the defaults, never in the rhncfg config file. It's formed when there 76 # are more than one server in up2date's serverURL setting. 77 if option == 'server_list': 78 if 'server_list' in self.mydefaults: 79 if type(self.mydefaults['server_list']) is type([]): 80 return self.mydefaults['server_list'] 81 82 try: 83 ret = self.get(self.section, option, vars=self.overrides) 84 85 #5/25/05 wregglej - 158694 86 #Move the cast to an int to here from the up2date_config_parser, that way the stuff that needs to get interpolated 87 #gets interpolated, the stuff that should be an int ends up and int, and the stuff that's neither doesn't get 88 #messed with. 89 try: 90 if type(ret) != type([]): 91 ret = int(ret) 92 except ValueError: 93 pass 94 return ret 95 except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): 96 pass 97 98 defaults = self.defaults() 99 100 return defaults.get(option)
101
102 - def keys(self):
103 return self.options(self.section)
104
105 - def __getitem__(self, item):
106 return self.get_option(item)
107
108 -def init(section, defaults=None, config_file_override=None, **overrides):
109 cp = rhncfgConfigParser._instance = rhncfgConfigParser(section, defaults, config_file_override=config_file_override) 110 cp.read_config_files(overrides)
111
112 -def get(var):
113 return _get_config().get_option(var)
114
115 -def _get_config():
116 if rhncfgConfigParser._instance is None: 117 raise ValueError("Configuration not initialized") 118 return rhncfgConfigParser._instance
119
120 -def instance():
121 return _get_config()
122
123 -def keys():
124 return list(_get_config().keys())
125
126 -def main():
127 init('rhncfgcli') 128 print("repository: %s" % get("repository")) 129 print("useGPG: %s" % get("useGPG")) 130 print("serverURL: %s" % get("serverURL"))
131 132 if __name__ == '__main__': 133 main() 134