Package pulp :: Package client :: Module config
[hide private]
[frames] | no frames]

Source Code for Module pulp.client.config

  1  # 
  2  # Copyright (c) 2010 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   
 17  import os 
 18  from iniparse import INIConfig as Base 
 19   
 20   
21 -class Config(Base):
22 """ 23 The pulp client configuration. 24 @cvar PATH: The absolute path to the config directory. 25 @type PATH: str 26 @cvar USER: The path to an alternate configuration file 27 within the user's home. 28 @type USER: str 29 @cvar ALT: The environment variable with a path to an alternate 30 configuration file. 31 @type ALT: str 32 """ 33 34 FILE = 'client.conf' 35 PATH = os.path.join('/etc/pulp', FILE) 36 USER = os.path.join('~/.pulp', FILE) 37 ALT = 'PULP_CLIENT_OVERRIDE' 38
39 - def __init__(self):
40 """ 41 Open the configuration. 42 Merge (in) alternate configuration file when specified 43 by environment variable. 44 """ 45 fp = open(self.PATH) 46 try: 47 Base.__init__(self, fp) 48 altpath = self.__altpath() 49 if altpath: 50 alt = self.__read(altpath) 51 self.__mergeIn(alt) 52 finally: 53 fp.close()
54
55 - def write(self):
56 """ 57 Write the configuration. 58 """ 59 altpath = self.__altpath() 60 if altpath: 61 alt = self.__read(altpath) 62 self.__mergeOut(alt) 63 path = altpath 64 s = str(alt) 65 else: 66 path = self.PATH 67 s = str(self) 68 fp = open(path, 'w') 69 try: 70 fp.write(s) 71 finally: 72 fp.close()
73
74 - def __mergeIn(self, other):
75 """ 76 Merge (in) the specified I{other} configuration. 77 @param other: The conf to merge in. 78 @type other: Base 79 @return: self 80 @rtype: L{Config} 81 """ 82 for section in other: 83 if section not in self: 84 continue 85 sA = self[section] 86 sB = other[section] 87 for key in sB: 88 value = sB[key] 89 setattr(sA, key, value) 90 return self
91
92 - def __mergeOut(self, other):
93 """ 94 Merge (out) to the specified I{other} configuration. 95 @param other: The conf to merge out. 96 @type other: Base 97 @return: self 98 @rtype: L{Config} 99 """ 100 for section in other: 101 if section not in self: 102 continue 103 sA = self[section] 104 sB = other[section] 105 for key in sB: 106 value = sA[key] 107 setattr(sB, key, value) 108 return self
109
110 - def __read(self, path):
111 """ 112 Read the configuration at the specified path. 113 @param path: The fully qualified path. 114 @type path: str 115 @return: The configuration object. 116 @rtype: Base 117 """ 118 fp = open(path) 119 try: 120 return Base(fp) 121 finally: 122 fp.close()
123 124
125 - def __altpath(self):
126 """ 127 Get the I{alternate} configuration path. 128 Resolution order: ALT, USER 129 @return: The path to the alternate configuration file. 130 @rtype: str 131 """ 132 path = os.environ.get(self.ALT) 133 if path: 134 return path 135 path = os.path.expanduser(self.USER) 136 if os.path.exists(path): 137 return path 138 else: 139 None
140