Package rhnpush :: Module rhnpush_config
[hide private]
[frames] | no frames]

Source Code for Module rhnpush.rhnpush_config

  1  # 
  2  # Copyright (c) 2008--2017 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  # The configuration file parser for the rhnpush utility. 
 17  # The majority of this code is taken from rhncfg/config_common/local_config.py 
 18  # 
 19  # 11/11/2004 John Wregglesworth 
 20  # 
 21   
 22  import sys 
 23   
 24  # pylint: disable=F0401 
 25  if sys.version_info[0] == 3: 
 26      import configparser as ConfigParser 
 27  else: 
 28      import ConfigParser 
 29   
 30  # Class that contains the options read in from the config file. 
 31  # Uses a ConfigParser to create a dictionary of the configuration options. 
 32  # That dictionary is then used to add instance variables to the object dynamically. 
 33   
 34   
35 -class rhnpushConfigParser:
36 # pylint: disable=W0201 37 _instance = None 38
39 - def __init__(self, filename=None, ensure_consistency=False):
40 41 # Defaults that are used if the ensure_consistency parameter of the constructor is true 42 # and the config file that is being read is missing some values. 43 self.options_defaults = { 44 'newest': '0', 45 'usage': '0', 46 'header': '0', 47 'test': '0', 48 'nullorg': '0', 49 'source': '0', 50 'stdin': '0', 51 'verbose': '0', 52 'force': '0', 53 'nosig': '0', 54 'list': '0', 55 'exclude': '', 56 'files': '', 57 'orgid': '', 58 'reldir': '', 59 'count': '', 60 'dir': '', 61 'server': 'http://rhn.redhat.com/APP', 62 'channel': '', 63 'cache_lifetime': '600', 64 'new_cache': '0', 65 'extended_test': '0', 66 'no_session_caching': '0', 67 'proxy': '', 68 'tolerant': '0', 69 'ca_chain': '/usr/share/rhn/RHN-ORG-TRUSTED-SSL-CERT', 70 'timeout': None 71 } 72 73 # Used to parse the config file. 74 self.settings = ConfigParser.ConfigParser() 75 76 # use options from the rhnpush section. 77 self.section = "rhnpush" 78 79 self.username = None 80 self.password = None 81 82 if filename: 83 self.filename = filename 84 self._read_config_files() 85 86 # Take all of the options read from the configuration file and add them as attributes 87 #(instance variables, member variables, whatever) of this object. 88 self._add_config_as_attr(ensure_consistency=ensure_consistency)
89 90 # Use the ConfigParser to read in the configuration file.
91 - def _read_config_files(self):
92 try: 93 self.settings.read([self.filename]) 94 except IOError: 95 e = sys.exc_info()[1] 96 print(("Config File Error: line %s, file %s: %s" % (e.lineno, e.filename, e))) 97 sys.exit(1)
98
99 - def write(self, fileobj):
100 try: 101 self.settings.write(fileobj) 102 except IOError: 103 e = sys.exc_info()[1] 104 print(("Config File Error: line %s, file %s: %s" % (e.lineno, e.filename, e))) 105 sys.exit(1)
106 107 # Returns an option read in from the configuration files and specified by the string variable option. 108 # This function can probably be safely removed, since all configuration options become attributes 109 # of an instantiation of this class.
110 - def get_option(self, option):
111 try: 112 return self.settings.get(self.section, option) 113 except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): 114 e = sys.exc_info()[1] 115 print("Option/Section Error: line %s, file %s: %s" % (e.lineno, e.filename, e)) 116 sys.exit(1)
117 118 # Returns the keys of the attributes of the object.
119 - def keys(self):
120 return list(self.__dict__.keys())
121 122 # Returns the keys of the options read in from the configuration files.
123 - def _keys(self):
124 if self.settings.has_section(self.section): 125 return self.settings.options(self.section) 126 127 return ()
128 129 # Returns an option read in from the configuration files.
130 - def __getitem__(self, item):
131 return self.get_option(item)
132
133 - def __delitem__(self, item):
134 pass
135
136 - def __len__(self):
137 pass
138
139 - def __setitem__(self, key, value):
140 pass
141 142 # Takes all of the configuration options read in by the ConfigParser and makes them attributes of the object.
143 - def _add_config_as_attr(self, ensure_consistency=False):
144 for k in self._keys(): 145 self.__dict__[k] = self.settings.get(self.section, k) 146 147 # ensuring consistency only checks for missing configuration option. 148 if ensure_consistency: 149 for thiskey in self.options_defaults: 150 if thiskey not in self.__dict__: 151 self.__dict__[thiskey] = self.options_defaults[thiskey]
152