initial commit
[ebuildfind.git] / commands / lib / layman / config.py
blobba57c81b074c79f662c0060c1e455edab92a398a
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #################################################################################
4 # LAYMAN CONFIGURATION
5 #################################################################################
6 # File: config.py
8 # Handles layman configuration
10 # Copyright:
11 # (c) 2005 - 2008 Gunnar Wrobel
12 # Distributed under the terms of the GNU General Public License v2
14 # Author(s):
15 # Gunnar Wrobel <wrobel@gentoo.org>
17 '''Defines the configuration options and provides parsing functionality.'''
19 __version__ = "$Id: config.py 286 2007-01-09 17:48:23Z wrobel $"
21 #===============================================================================
23 # Dependencies
25 #-------------------------------------------------------------------------------
27 import sys, ConfigParser
29 from optparse import OptionParser, OptionGroup
30 from layman.debug import OUT
31 from layman.version import VERSION
33 #===============================================================================
35 # Class Config
37 #-------------------------------------------------------------------------------
39 class Config(object):
40 '''Handles the configuration.'''
42 def __init__(self):
43 '''
44 Creates and describes all possible polymeraZe options and creates
45 a debugging object.
47 >>> import os.path
48 >>> here = os.path.dirname(os.path.realpath(__file__))
49 >>> sys.argv.append('--config')
50 >>> sys.argv.append(here + '/../etc/layman.cfg')
51 >>> a = Config()
52 >>> a['overlays']
53 '\\nhttp://www.gentoo.org/proj/en/overlays/layman-global.txt'
54 >>> sorted(a.keys())
55 ['cache', 'config', 'local_list', 'make_conf', 'nocheck', 'overlays', 'proxy', 'quietness', 'storage', 'umask', 'width']
56 '''
58 self.defaults = {'config' : '/etc/layman/layman.cfg',
59 'storage' : '/usr/portage/local/layman',
60 'cache' : '%(storage)s/cache',
61 'local_list': '%(storage)s/overlays.xml',
62 'make_conf' : '%(storage)s/make.conf',
63 'nocheck' : 'yes',
64 'proxy' : '',
65 'umask' : '0022',
66 'overlays' :
67 'http://www.gentoo.org/proj/en/overlays/layman-global.'
68 'txt',}
71 self.parser = OptionParser(
72 usage = '\n\nlayman -a/-d/-S|overlay\nlayman -f [-o url]\nlayman' \
73 ' -l|-L',
74 version = VERSION)
76 #-----------------------------------------------------------------
77 # Main Options
79 group = OptionGroup(self.parser,
80 '<Actions>')
82 group.add_option('-a',
83 '--add',
84 action = 'append',
85 help = 'Add the given overlay from the cached remote li'
86 'st to your locally installed overlays.. Specify "ALL" '
87 'to add all overlays from the remote list.')
89 group.add_option('-d',
90 '--delete',
91 action = 'append',
92 help = 'Remove the given overlay from your locally inst'
93 'alled overlays. Specify "ALL" to remove all overlays')
95 group.add_option('-s',
96 '--sync',
97 action = 'append',
98 help = 'Update the specified overlay. Use "ALL" as para'
99 'meter to synchronize all overlays')
101 group.add_option('-i',
102 '--info',
103 action = 'append',
104 help = 'Display information about the specified overlay'
105 '.')
107 group.add_option('-S',
108 '--sync-all',
109 action = 'store_true',
110 help = 'Update all overlays.')
112 group.add_option('-L',
113 '--list',
114 action = 'store_true',
115 help = 'List the contents of the remote list.')
117 group.add_option('-l',
118 '--list-local',
119 action = 'store_true',
120 help = 'List the locally installed overlays.')
122 group.add_option('-f',
123 '--fetch',
124 action = 'store_true',
125 help = 'Fetch a remote list of overlays. This option is'
126 ' deprecated. The fetch operation will be performed by '
127 'default when you run sync, sync-all, or list.')
129 group.add_option('-n',
130 '--nofetch',
131 action = 'store_true',
132 help = 'Do not fetch a remote list of overlays.')
134 group.add_option('-p',
135 '--priority',
136 action = 'store',
137 help = 'Use this with the --add switch to set the prior'
138 'ity of the added overlay. This will influence the sort'
139 'ing order of the overlays in the PORTDIR_OVERLAY varia'
140 'ble.')
142 self.parser.add_option_group(group)
144 #-----------------------------------------------------------------
145 # Additional Options
147 group = OptionGroup(self.parser,
148 '<Path options>')
150 group.add_option('-c',
151 '--config',
152 action = 'store',
153 help = 'Path to the config file [default: ' \
154 + self.defaults['config'] + '].')
156 group.add_option('-o',
157 '--overlays',
158 action = 'append',
159 help = 'The list of overlays [default: ' \
160 + self.defaults['overlays'] + '].')
162 self.parser.add_option_group(group)
164 #-----------------------------------------------------------------
165 # Output Options
167 group = OptionGroup(self.parser,
168 '<Output options>')
170 group.add_option('-v',
171 '--verbose',
172 action = 'store_true',
173 help = 'Increase the amount of output and describe the '
174 'overlays.')
176 group.add_option('-q',
177 '--quiet',
178 action = 'store_true',
179 help = 'Yield no output. Please be careful with this op'
180 'tion: If the processes spawned by layman when adding o'
181 'r synchronizing overlays require any input layman will'
182 ' hang without telling you why. This might happen for e'
183 'xample if your overlay resides in subversion and the S'
184 'SL certificate of the server needs acceptance.')
186 group.add_option('-N',
187 '--nocolor',
188 action = 'store_true',
189 help = 'Remove color codes from the layman output.')
191 group.add_option('-Q',
192 '--quietness',
193 action = 'store',
194 type = 'int',
195 default = '4',
196 help = 'Set the level of output (0-4). Default: 4. Once'
197 ' you set this below 2 the same warning as given for --'
198 'quiet applies! ')
200 group.add_option('-W',
201 '--width',
202 action = 'store',
203 type = 'int',
204 default = '0',
205 help = 'Sets the screen width. This setting is usually '
206 'not required as layman is capable of detecting the ava'
207 'available number of columns automatically.')
209 group.add_option('-k',
210 '--nocheck',
211 action = 'store_true',
212 help = 'Do not check overlay definitions and do not i'
213 'ssue a warning if description or contact information'
214 ' are missing.')
216 self.parser.add_option_group(group)
218 #-----------------------------------------------------------------
219 # Debug Options
221 OUT.cli_opts(self.parser)
223 # Parse the command line first since we need to get the config
224 # file option.
225 (self.options, args) = self.parser.parse_args()
227 # handle debugging
228 OUT.cli_handle(self.options)
230 if self.options.__dict__['nocolor']:
231 OUT.color_off()
233 # Fetch only an alternate config setting from the options
234 if not self.options.__dict__['config'] is None:
235 self.defaults['config'] = self.options.__dict__['config']
237 OUT.debug('Got config file at ' + self.defaults['config'], 8)
239 # Now parse the config file
240 self.config = ConfigParser.ConfigParser(self.defaults)
241 self.config.add_section('MAIN')
243 # handle quietness
244 if self['quiet']:
245 OUT.set_info_level(1)
246 OUT.set_warn_level(1)
247 self.defaults['quietness'] = 0
249 elif 'quietness' in self.keys():
250 OUT.set_info_level(int(self['quietness']))
251 OUT.set_warn_level(int(self['quietness']))
253 OUT.debug('Reading config file at ' + self.defaults['config'], 8)
255 self.config.read(self.defaults['config'])
257 def __getitem__(self, key):
259 if key == 'overlays':
260 overlays = ''
261 if (key in self.options.__dict__.keys()
262 and not self.options.__dict__[key] is None):
263 overlays = '\n'.join(self.options.__dict__[key])
264 if self.config.has_option('MAIN', 'overlays'):
265 overlays += '\n' + self.config.get('MAIN', 'overlays')
266 if overlays:
267 return overlays
269 OUT.debug('Retrieving option', 8)
271 if (key in self.options.__dict__.keys()
272 and not self.options.__dict__[key] is None):
273 return self.options.__dict__[key]
275 OUT.debug('Retrieving option', 8)
277 if self.config.has_option('MAIN', key):
278 if key == 'nocheck':
279 if self.config.get('MAIN', key).lower() == 'yes':
280 return True
281 else:
282 return False
283 return self.config.get('MAIN', key)
285 OUT.debug('Retrieving option', 8)
287 if key in self.defaults.keys():
288 return self.defaults[key]
290 OUT.debug('Retrieving option', 8)
292 return None
294 def keys(self):
295 '''Special handler for the configuration keys.'''
297 OUT.debug('Retrieving keys', 8)
299 keys = [i for i in self.options.__dict__.keys()
300 if not self.options.__dict__[i] is None]
302 OUT.debug('Retrieving keys', 8)
304 keys += [name for name, value in self.config.items('MAIN')
305 if not name in keys]
307 OUT.debug('Retrieving keys', 8)
309 keys += [i for i in self.defaults.keys()
310 if not i in keys]
312 OUT.debug('Retrieving keys', 8)
314 return keys
317 #===============================================================================
319 # Testing
321 #-------------------------------------------------------------------------------
323 if __name__ == '__main__':
324 import doctest
325 doctest.testmod(sys.modules[__name__])