def annotate_config_with_metadata(config, meta_config, ignore_regexes=None, metadata_properties=None): """Add metadata to the rose.config.ConfigNode.comments attribute. config -- a rose.config.ConfigNode instance, containing app or suite data. meta_config -- a rose.config.ConfigNode instance, containing metadata for config. ignore_regexes -- (default None) a list of uncompiled regular expressions - if a setting contains any of these, don't include it in the annotated output. """ if ignore_regexes is None: ignore_regexes = [] ignore_recs = [re.compile(_) for _ in ignore_regexes] unset_keys = [] for keylist, node in config.walk(): section = keylist[0] option = None if len(keylist) > 1: option = keylist[1] id_ = rose.macro.get_id_from_section_option(section, option) if any(_.search(id_) for _ in ignore_recs): unset_keys.append(keylist) continue metadata = rose.macro.get_metadata_for_config_id(id_, meta_config) metadata_text = format_metadata_as_text( metadata, only_these_options=metadata_properties) metadata_lines = [" " + line for line in metadata_text.splitlines()] node.comments = metadata_lines + node.comments for keylist in unset_keys: config.unset(keylist) return config
def pretty_format_config(config): """Improve configuration prettiness.""" for section in config.value.keys(): keylist = [section] scheme = keylist[0] if ":" in scheme: scheme = scheme.split(":", 1)[0] try: scheme_module = getattr(rose.formats, scheme) pretty_format_keys = getattr(scheme_module, "pretty_format_keys") pretty_format_value = getattr(scheme_module, "pretty_format_value") except AttributeError: continue new_keylist = pretty_format_keys(keylist) if new_keylist != keylist: node = config.get(keylist) config.unset(keylist) config.set(new_keylist, node.value, node.state, node.comments) section = new_keylist[0] for keylist, node in list(config.walk([section])): values = rose.variable.array_split(node.value, ",") node.value = pretty_format_value(values) new_keylist = pretty_format_keys(keylist) if new_keylist != keylist: config.unset(keylist) config.set(new_keylist, node.value, node.state, node.comments)
def _remove_setting(self, config, keys, info=None): """Remove a setting from the configuration, if it exists.""" section, option = self._get_section_option_from_keys(keys) id_ = self._get_id_from_section_option(section, option) if config.get([section, option]) is None: return False if info is None: info = self.INFO_REMOVED config.unset([section, option]) self.add_report(section, option, None, info)
def _remove_setting(self, config, keys, info=None): """Remove a setting from the configuration, if it exists.""" section, option = self._get_section_option_from_keys(keys) if config.get([section, option]) is None: return False if info is None: info = self.INFO_REMOVED node = config.unset([section, option]) value = "" if node.value: value = node.value self.add_report(section, option, value, info)
def annotate_config_with_metadata(config, meta_config, ignore_regexes=None, metadata_properties=None): """Add metadata to the rose.config.ConfigNode.comments attribute. config -- a rose.config.ConfigNode instance, containing app or suite data. meta_config -- a rose.config.ConfigNode instance, containing metadata for config. ignore_regexes -- (default None) a list of uncompiled regular expressions - if a setting contains any of these, don't include it in the annotated output. """ if ignore_regexes is None: ignore_regexes = [] ignore_recs = [re.compile(_) for _ in ignore_regexes] unset_keys = [] for keylist, node in config.walk(): section = keylist[0] option = None if len(keylist) > 1: option = keylist[1] id_ = rose.macro.get_id_from_section_option(section, option) if any([_.search(id_) for _ in ignore_recs]): unset_keys.append(keylist) continue metadata = rose.macro.get_metadata_for_config_id(id_, meta_config) metadata_text = format_metadata_as_text( metadata, only_these_options=metadata_properties) metadata_lines = [" " + line for line in metadata_text.splitlines()] node.comments = metadata_lines + node.comments for keylist in unset_keys: config.unset(keylist) return config