1
2
3
4
5
6
7
8
9
10
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
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)
34
35
39
43
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
67 pass
68 return options.__dict__
69
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
106