1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import os
18 from iniparse import INIConfig as Base
19
20
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
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
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
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
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
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
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