def get_conf(conf_file=None, config_file=None): """Get CONF object for specific project. """ conf = cfg.ConfigOpts() gn.register_cli_opts(conf) oslo_args = ['--config-file', conf_file] conf(oslo_args) groups = gn._get_groups(gn._list_opts(conf.namespace)) # Make new CONF new_conf = cfg.ConfigOpts() project_args = ['--config-file', config_file] new_conf(project_args) all_namespaces = [] for k, v in groups.items(): group = cfg.OptGroup(k) try: namespaces = v.get('namespaces', []) except Exception: namespaces = v list_opts = [] for namespace in namespaces: all_namespaces.append(namespace[0]) list_opts.extend(namespace[1]) new_conf.register_group(group) if k == 'DEFAULT': LOG.info("Force register DEFAULT group to easier access " "configuration values in DEFAULT group.") try: new_conf.register_opts(list_opts) except cfg.DuplicateOptError: pass LOG.info("Register group %s into new_conf.", group.name) new_conf.register_opts(list_opts, group=group) projects = [] for namespace in all_namespaces: sp = namespace.split('.') LOG.info("Get project name from namespaces.") if 'oslo' in namespace: LOG.info("Support getting project name for Oslo projects.") projects.append(".".join(sp[:2])) else: projects.append(sp[0]) for project in projects: LOG.info("Autoload all dynamic section.") try: get_name_module = DYNAMIC_SECTION_PROJECTS[project] except KeyError: LOG.info("%s project does not have dynamic section or " "not is not supported now.") continue dynamic = importlib.import_module(get_name_module) dynamic.register_dynamic_section(new_conf) return new_conf, set(projects)
def init(): try: cfg.CONF([], project='nectar-osc', default_config_files=['~/.nectar-osc.conf']) except cfg.ConfigFilesNotFoundError: print('generating config file ~/.nectar-osc.conf') conf = cfg.ConfigOpts() generator.register_cli_opts(conf) conf.namespace = ['nectar_osc'] with open(homedir + '/' + '.nectar-osc.conf', 'w') as conf_file: generator.generate(conf, conf_file)
def test_generate(self, mock_log, named_mgr): generator.register_cli_opts(self.conf) namespaces = [i[0] for i in self.opts] self.config(namespace=namespaces) for group in self.groups.values(): self.conf.register_group(group) wrap_width = getattr(self, "wrap_width", None) if wrap_width is not None: self.config(wrap_width=wrap_width) if self.stdout: stdout = self._capture_stdout() else: output_file = self.tempdir.join(self.output_file) self.config(output_file=output_file) mock_eps = [] for name, opts in self.opts: mock_ep = mock.Mock() mock_ep.configure_mock(name=name, obj=opts) mock_eps.append(mock_ep) named_mgr.return_value = mock_eps generator.generate(self.conf) if self.stdout: self.assertEqual(self.expected, stdout.getvalue()) else: content = open(output_file).read() self.assertEqual(self.expected, content) named_mgr.assert_called_once_with( "oslo.config.opts", names=namespaces, on_load_failure_callback=generator.on_load_failure_callback, invoke_on_load=True, ) log_warning = getattr(self, "log_warning", None) if log_warning is not None: mock_log.warning.assert_called_once_with(*log_warning) else: self.assertFalse(mock_log.warning.called)
def test_generate(self, mock_log, raw_opts_loader): generator.register_cli_opts(self.conf) namespaces = [i[0] for i in self.opts] self.config(namespace=namespaces) for group in self.groups.values(): self.conf.register_group(group) wrap_width = getattr(self, 'wrap_width', None) if wrap_width is not None: self.config(wrap_width=wrap_width) if self.stdout: stdout = self._capture_stdout() else: output_file = self.tempdir.join(self.output_file) self.config(output_file=output_file) # We have a static data structure matching what should be # returned by _list_opts() but we're mocking out a lower level # function that needs to return a namespace and a callable to # return options from that namespace. We have to pass opts to # the lambda to cache a reference to the name because the list # comprehension changes the thing pointed to by the name each # time through the loop. raw_opts_loader.return_value = [ (ns, lambda opts=opts: opts) for ns, opts in self.opts ] generator.generate(self.conf) if self.stdout: self.assertEqual(self.expected, stdout.getvalue()) else: with open(output_file, 'r') as f: actual = f.read() self.assertEqual(self.expected, actual) log_warning = getattr(self, 'log_warning', None) if log_warning is not None: mock_log.warning.assert_called_once_with(*log_warning) else: self.assertFalse(mock_log.warning.called)
def test_generate(self, mock_log, named_mgr): generator.register_cli_opts(self.conf) namespaces = [i[0] for i in self.opts] self.config(namespace=namespaces) for group in self.groups.values(): self.conf.register_group(group) wrap_width = getattr(self, 'wrap_width', None) if wrap_width is not None: self.config(wrap_width=wrap_width) if self.stdout: stdout = self._capture_stdout() else: output_file = self.tempdir.join(self.output_file) self.config(output_file=output_file) mock_eps = [] for name, opts in self.opts: mock_ep = mock.Mock() mock_ep.configure_mock(name=name, obj=opts) mock_eps.append(mock_ep) named_mgr.return_value = mock_eps generator.generate(self.conf) if self.stdout: self.assertEqual(self.expected, stdout.getvalue()) else: content = open(output_file).read() self.assertEqual(self.expected, content) named_mgr.assert_called_once_with( 'oslo.config.opts', names=namespaces, on_load_failure_callback=generator.on_load_failure_callback, invoke_on_load=True) log_warning = getattr(self, 'log_warning', None) if log_warning is not None: mock_log.warning.assert_called_once_with(*log_warning) else: self.assertFalse(mock_log.warning.called)