Beispiel #1
0
    def load(self, conf_dir, conf_name, conf_dir_paths=None, opt_keys=None,
             no_ignore=False):
        """Load a (runtime) configuration directory with inheritance.

        Return a ConfigTree object that represents the result.

        conf_dir -- The path to the configuration directory to load.
        conf_name -- The (base) name of the configuration file.
                     E.g. "rose-suite.conf".
        conf_dir_paths -- A list of directories to locate relative paths to
                          configurations.
        opt_keys -- Optional configuration keys.

        """

        if not conf_dir_paths:
            conf_dir_paths = []
        conf_dir = self._search(conf_dir, [os.getcwd()] + conf_dir_paths)
        nodes = {} # {conf_dir: node, ...}
        conf_file_name = os.path.join(conf_dir, conf_name)
        used_keys = []
        nodes[conf_dir] = self.node_loader.load_with_opts(
                conf_file_name, more_keys=opt_keys, used_keys=used_keys)

        config_tree = ConfigTree()
        config_tree.conf_dirs = mro(conf_dir, self._get_base_names, conf_name,
                                    conf_dir_paths, opt_keys, used_keys, nodes)

        if opt_keys:
            bad_keys = []
            for opt_key in opt_keys:
                if opt_key not in used_keys:
                    bad_keys.append(opt_key)
            if bad_keys:
                raise BadOptionalConfigurationKeysError(bad_keys)

        config_tree.node = ConfigNode()
        for t_conf_dir in config_tree.conf_dirs:
            node = nodes[t_conf_dir]
            for keys, sub_node in node.walk(no_ignore=no_ignore):
                if keys == ["", "import"]:
                    continue
                if config_tree.node.get(keys) is None:
                    config_tree.node.set(keys, sub_node.value, sub_node.state,
                                         sub_node.comments)
            for dir_path, dir_names, file_names in os.walk(t_conf_dir):
                names = [d for d in dir_names if d.startswith(".")]
                for name in names:
                    dir_names.remove(name)
                for file_name in file_names:
                    if file_name == conf_name or file_name.startswith("."):
                        continue
                    path = os.path.join(dir_path, file_name)
                    rel_path = os.path.relpath(path, t_conf_dir)
                    if not config_tree.files.has_key(rel_path):
                        config_tree.files[rel_path] = t_conf_dir

        return config_tree
Beispiel #2
0
    def load(self, conf_dir, conf_name, conf_dir_paths=None, opt_keys=None,
             conf_node=None, no_ignore=False):
        """Load a (runtime) configuration directory with inheritance.

        Return a ConfigTree object that represents the result.

        conf_dir -- The path to the configuration directory to load.
        conf_name -- The (base) name of the configuration file.
                     E.g. "rose-suite.conf".
        conf_dir_paths -- A list of directories to locate relative paths to
                          configurations.
        opt_keys -- Optional configuration keys.
        conf_node -- A rose.config.ConfigNode to extend, or None to use a
                     fresh one.
        no_ignore -- If True, skip loading ignored config settings.

        """

        if not conf_dir_paths:
            conf_dir_paths = []
        conf_dir = self._search(conf_dir, [os.getcwd()] + conf_dir_paths)
        nodes = {}  # {conf_dir: node, ...}
        conf_file_name = os.path.join(conf_dir, conf_name)
        used_keys = []
        nodes[conf_dir] = self.node_loader.load_with_opts(
            conf_file_name, more_keys=opt_keys, used_keys=used_keys)

        conf_tree = ConfigTree()
        conf_tree.conf_dirs = mro(
            conf_dir, self._get_base_names, conf_name, conf_dir_paths,
            opt_keys, used_keys, nodes)

        if opt_keys:
            bad_keys = []
            for opt_key in opt_keys:
                if opt_key not in used_keys:
                    bad_keys.append(opt_key)
            if bad_keys:
                raise BadOptionalConfigurationKeysError(bad_keys)

        if conf_node is None:
            conf_tree.node = ConfigNode()
        else:
            conf_tree.node = conf_node
        for t_conf_dir in conf_tree.conf_dirs:
            node = nodes[t_conf_dir]
            for keys, sub_node in node.walk(no_ignore=no_ignore):
                if keys == ["", "import"]:
                    continue
                if conf_tree.node.get(keys) is None:
                    conf_tree.node.set(keys, sub_node.value, sub_node.state,
                                         sub_node.comments)
            for dir_path, dir_names, file_names in os.walk(t_conf_dir):
                names = [dir_ for dir_ in dir_names if dir_.startswith(".")]
                for name in names:
                    dir_names.remove(name)
                for file_name in file_names:
                    if file_name == conf_name or file_name.startswith("."):
                        continue
                    path = os.path.join(dir_path, file_name)
                    rel_path = os.path.relpath(path, t_conf_dir)
                    if rel_path not in conf_tree.files:
                        conf_tree.files[rel_path] = t_conf_dir
                    if rel_path not in conf_tree.file_locs:
                        conf_tree.file_locs[rel_path] = []
                    conf_tree.file_locs[rel_path].append(t_conf_dir)

        return conf_tree