~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/config/__init__.py

Changes per review. this includes a mass renaming, rewriting, and deletiong of conf files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
88
88
        The config is will be loaded only when there is not a config.
89
89
        Repeated calls to this method will not cause the config to reload.
90
90
        """
91
 
        if self._config is None:
92
 
            config_dir = os.path.join(
93
 
                os.path.dirname(__file__), os.pardir, os.pardir, os.pardir,
94
 
                'configs')
 
91
        if self._config is not None:
 
92
            return
 
93
 
 
94
        if self._default_config_section != 'default':
 
95
            environ_dir = self._default_config_section
 
96
        else:
95
97
            environ_dir = os.environ.get(
96
98
                    CONFIG_ENVIRONMENT_VARIABLE, DEFAULT_CONFIG)
97
 
            role = self._default_config_section
98
 
            schema_file = os.path.join(config_dir, 'schema.lazr.conf')
99
 
            config_file = os.path.join(
100
 
                config_dir, environ_dir,'launchpad.%s.conf' % role)
101
 
 
102
 
            # Monkey patch Section to store it's ZConfig counterpart, and
103
 
            # use it when it cannot provide the data.
104
 
            from canonical.lazr.config import ImplicitTypeSection
105
 
            ImplicitTypeSection._zconfig = self.getConfig()
106
 
            ImplicitTypeSection.__getattr__ = failover_to_zconfig(
107
 
                ImplicitTypeSection.__getattr__)
108
 
 
109
 
            schema = ImplicitTypeSchema(schema_file)
110
 
            self._config = schema.load(config_file)
111
 
            try:
112
 
                self._config.validate()
113
 
            except ConfigErrors, error:
114
 
                message = '\n'.join([str(e) for e in error.errors])
115
 
                self.fail(message)
 
99
        here = os.path.dirname(__file__)
 
100
        config_dir = os.path.join(
 
101
            here, os.pardir, os.pardir, os.pardir, 'configs', environ_dir)
 
102
        schema_file = os.path.join(here, 'schema-lazr.conf')
 
103
        config_file = os.path.join(config_dir, 'launchpad-lazr.conf')
 
104
 
 
105
        # Monkey patch the Section class to store it's ZConfig counterpart.
 
106
        # The Section instance will failover to the ZConfig instance when
 
107
        # the key does not exist. This is for the transitionary period
 
108
        # where ZConfig and lazr.config are concurrently loaded.
 
109
        from canonical.lazr.config import ImplicitTypeSection
 
110
        ImplicitTypeSection._zconfig = self.getConfig()
 
111
        ImplicitTypeSection.__getattr__ = failover_to_zconfig(
 
112
            ImplicitTypeSection.__getattr__)
 
113
 
 
114
        schema = ImplicitTypeSchema(schema_file)
 
115
        self._config = schema.load(config_file)
 
116
        try:
 
117
            self._config.validate()
 
118
        except ConfigErrors, error:
 
119
            message = '\n'.join([str(e) for e in error.errors])
 
120
            self.fail(message)
116
121
 
117
122
    def _magic_settings(self, config, root_options):
118
123
        """Modify the config, adding automatically generated settings"""
164
169
        # This method may access protected members.
165
170
        # pylint: disable-msg=W0212
166
171
        try:
167
 
            section_value = getattr(self._zconfig, self.name)
168
 
            zconfig_value = getattr(section_value, name)
 
172
            # Try to get the value of the section key from ZConfig.
 
173
            # e.g. config.section.key
 
174
            z_section = getattr(self._zconfig, self.name)
 
175
            z_key_value = getattr(z_section, name)
169
176
            is_zconfig = True
170
177
        except AttributeError:
171
178
            is_zconfig = False
172
 
            pass
173
179
 
174
180
        try:
 
181
            # Try to get the value of the section key from lazr.config.
 
182
            # e.g. config.section.key
175
183
            lazr_value = func(self, name)
176
184
        except AttributeError:
177
 
            # Raise a warning that the callsite is using multisections.
178
185
            lazr_value = None
179
 
            pass
180
186
 
181
187
        if not is_zconfig and lazr_value is None:
182
188
            # The callsite was converted to lazr config, but has an error.
193
199
            raise_warning(
194
200
                "Callsite requests a nonexistent key: '%s.%s'." %
195
201
                (self.name, name))
196
 
            return zconfig_value
 
202
            return z_key_value
197
203
 
198
204
    return failover_getattr
199
205