Package Products :: Package ZenUtils :: Module GlobalConfig
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenUtils.GlobalConfig

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2010, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 or (at your 
  8  # option) any later version as published by the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13  import sys 
 14  from optparse import OptionValueError, BadOptionError 
 15   
 16  from Products.ZenUtils.Utils import zenPath 
 17  from Products.ZenUtils.config import Config, ConfigLoader 
 18   
 19  CONFIG_FILE = zenPath('etc', 'global.conf') 
 20   
21 -class GlobalConfig(Config):
22 """ 23 A method for retrieving the global configuration options 24 outside of a daemon. This is used to configure the 25 AMQP connection in Zope and zenhub 26 27 @todo Add validation for expected keys and values 28 """ 29 pass
30 31 _GLOBAL_CONFIG = ConfigLoader(CONFIG_FILE, GlobalConfig)
32 -def getGlobalConfiguration():
33 return _GLOBAL_CONFIG()
34 35
36 -class _GlobalConfParserAdapter(object):
37 - def __init__(self, parser):
38 self.parser = parser
39
40 - def apply(self):
41 self.parser.defaults = self._getGlobalConfigFileDefaults() 42 return self.parser
43
44 - def _getParametersFromConfig(self, lines):
45 args = [] 46 validkeys = self.parser.get_default_values().__dict__.keys() 47 48 for line in lines: 49 if line.get('type', None) == 'option' and line['key'] in validkeys: 50 args += ['--%s' % line['key'], line['value']] 51 52 return args
53
55 """ 56 Parse a config file which has key-value pairs delimited by white space, 57 and update the parser's option defaults with these values. 58 """ 59 options = self.parser.get_default_values() 60 lines = self._loadConfigFile(CONFIG_FILE) 61 if lines: 62 args = self._getParametersFromConfig(lines) 63 try: 64 self.parser._process_args([], args, options) 65 except (BadOptionError, OptionValueError) as err: 66 # Ignore it, we only care about our own options as defined in the parser 67 pass 68 return options.__dict__
69
70 - def _loadConfigFile(self, filename):
71 """ 72 Parse a config file which has key-value pairs delimited by white space. 73 74 @parameter filename: path to the configuration file 75 @type filename: string 76 """ 77 lines = [] 78 try: 79 with open(filename) as file: 80 for line in file: 81 if line.lstrip().startswith('#') or line.strip() == '': 82 lines.append(dict(type='comment', line=line)) 83 else: 84 try: 85 key, value = line.strip().split(None, 1) 86 except ValueError: 87 lines.append(dict(type='option', line=line, key=line.strip(), value=None, option=None)) 88 else: 89 option = self.parser.get_option('--%s' % key) 90 lines.append(dict(type='option', line=line, key=key, value=value, option=option)) 91 except IOError as e: 92 errorMessage = 'WARN: unable to read config file {filename} \ 93 -- skipping. ({exceptionName}: {exception})'.format( 94 filename=filename, 95 exceptionName=e.__class__.__name__, 96 exception=e 97 ) 98 print >>sys.stderr, errorMessage 99 return [] 100 101 return lines
102 103
104 -def applyGlobalConfToParser(parser):
105 return _GlobalConfParserAdapter(parser).apply()
106