コード例 #1
0
ファイル: test_yamlpath.py プロジェクト: wwkimball/yamlpath
 def test_strip_path_prefix(self, path, prefix, result):
     original = YAMLPath(path)
     remove = YAMLPath(prefix) if prefix is not None else None
     compare = YAMLPath(result)
     stripped = YAMLPath.strip_path_prefix(original, remove)
     assert compare == stripped
コード例 #2
0
    def _prepare_user_rules(self, proc: Processor, merge_path: YAMLPath,
                            section: str, collector: dict) -> None:
        """
        Identify DOM nodes matching user-defined merge rules.

        Parameters:
        1. proc (Processor) Reference to the DOM Processor.
        2. merge_path (YAMLPath) User-specified path within the DOM at which
           merging will take place.
        3. section (str) User-configuration file section defining the merge
           rules to apply.
        4. collector (dict) Storage collector for matching nodes.

        Returns:  N/A
        """
        if self.config is None or not section in self.config:
            self.log.warning(
                "User-specified configuration file has no {} section.".format(
                    section))
            return

        for rule_key in self.config[section]:
            rule_value = self.config[section][rule_key]

            if "=" in rule_value:
                # There were at least two = signs on the configuration line
                conf_line = rule_key + "=" + rule_value
                delim_pos = conf_line.rfind("=")
                rule_key = conf_line[0:delim_pos].strip()
                rule_value = conf_line[delim_pos + 1:].strip()
                self.log.debug(
                    "MergerConfig::_prepare_user_rules:  Reconstituted"
                    " configuration line '{}' to extract adjusted key '{}'"
                    " with value '{}'".format(conf_line, rule_key, rule_value))

            rule_path = YAMLPath(rule_key)
            yaml_path = YAMLPath.strip_path_prefix(rule_path, merge_path)
            self.log.debug(
                "MergerConfig::_prepare_user_rules:  Matching '{}' nodes to"
                " YAML Path '{}' from key, {}.".format(section, yaml_path,
                                                       rule_key))
            try:
                for node_coord in proc.get_nodes(yaml_path, mustexist=True):
                    self.log.debug(
                        "Node will have merging rule, {}:".format(rule_value),
                        prefix="MergerConfig::_prepare_user_rules:  ",
                        data=node_coord.node)
                    collector[node_coord] = rule_value

            except YAMLPathException:
                self.log.warning("{} YAML Path matches no nodes:  {}".format(
                    section, yaml_path))

        self.log.debug("Matched rules to nodes:",
                       prefix="MergerConfig::_prepare_user_rules:  ")
        for node_coord, merge_rule in collector.items():
            self.log.debug("... RULE:  {}".format(merge_rule),
                           prefix="MergerConfig::_prepare_user_rules:  ")
            self.log.debug("... NODE:",
                           data=node_coord,
                           prefix="MergerConfig::_prepare_user_rules:  ")