コード例 #1
0
    def load_config(
        self,
        config_path: str,
        is_primary_config: bool,
        package_override: Optional[str] = None,
    ) -> ConfigResult:
        normalized_config_path = self._normalize_file_name(config_path)
        res = importlib_resources.files(self.path).joinpath(normalized_config_path)
        if not res.exists():
            raise ConfigLoadError(f"Config not found : {normalized_config_path}")

        with open(res, encoding="utf-8") as f:
            header_text = f.read(512)
            header = ConfigSource._get_header_dict(header_text)
            self._update_package_in_header(
                header=header,
                normalized_config_path=normalized_config_path,
                is_primary_config=is_primary_config,
                package_override=package_override,
            )
            f.seek(0)
            cfg = OmegaConf.load(f)
            defaults_list = self._extract_defaults_list(
                config_path=config_path, cfg=cfg
            )
            return ConfigResult(
                config=self._embed_config(cfg, header["package"]),
                path=f"{self.scheme()}://{self.path}",
                provider=self.provider,
                header=header,
                defaults_list=defaults_list,
            )
コード例 #2
0
    def load_config(
        self,
        config_path: str,
        is_primary_config: bool,
        package_override: Optional[str] = None,
    ) -> ConfigResult:
        normalized_config_path = self._normalize_file_name(
            filename=config_path)
        module_name, resource_name = PackageConfigSource._split_module_and_resource(
            self.concat(self.path, normalized_config_path))

        try:
            with resource_stream(module_name, resource_name) as stream:
                header_text = stream.read(512)
                header = ConfigSource._get_header_dict(header_text.decode())
                self._update_package_in_header(
                    header=header,
                    normalized_config_path=normalized_config_path,
                    is_primary_config=is_primary_config,
                    package_override=package_override,
                )

                stream.seek(0)
                cfg = OmegaConf.load(stream)
                return ConfigResult(
                    config=self._embed_config(cfg, header["package"]),
                    path=f"{self.scheme()}://{self.path}",
                    provider=self.provider,
                    header=header,
                )
        except FileNotFoundError:
            raise ConfigLoadError(
                f"Config not found: module={module_name}, resource_name={resource_name}"
            )
コード例 #3
0
ファイル: file_config_source.py プロジェクト: wpc/hydra
    def load_config(
        self,
        config_path: str,
        is_primary_config: bool,
        package_override: Optional[str] = None,
    ) -> ConfigResult:
        normalized_config_path = self._normalize_file_name(config_path)
        full_path = os.path.realpath(
            os.path.join(self.path, normalized_config_path))
        if not os.path.exists(full_path):
            raise ConfigLoadError(f"Config not found : {full_path}")

        with open(full_path) as f:
            header_text = f.read(512)
            header = ConfigSource._get_header_dict(header_text)
            self._update_package_in_header(
                header=header,
                normalized_config_path=normalized_config_path,
                is_primary_config=is_primary_config,
                package_override=package_override,
            )
            f.seek(0)
            cfg = OmegaConf.load(f)
            return ConfigResult(
                config=self._embed_config(cfg, header["package"]),
                path=f"{self.scheme()}://{self.path}",
                provider=self.provider,
                header=header,
            )
コード例 #4
0
    def load_config(
        self,
        config_path: str,
        is_primary_config: bool,
        package_override: Optional[str] = None,
    ) -> ConfigResult:
        config_path = self._normalize_file_name(config_path)
        if config_path not in self.configs:
            raise ConfigLoadError("Config not found : " + config_path)
        header = self.headers[config_path].copy(
        ) if config_path in self.headers else {}
        if "package" not in header:
            header["package"] = ""

        self._update_package_in_header(
            header,
            config_path,
            is_primary_config=is_primary_config,
            package_override=package_override,
        )
        cfg = OmegaConf.create(self.configs[config_path])
        return ConfigResult(
            config=self._embed_config(cfg, header["package"]),
            path=f"{self.scheme()}://{self.path}",
            provider=self.provider,
            header=header,
        )
コード例 #5
0
 def load_config(self, config_path: str) -> ConfigResult:
     if config_path not in self.configs:
         raise ConfigLoadError("Config not found : " + config_path)
     return ConfigResult(
         config=OmegaConf.create(self.configs[config_path]),
         path=f"{self.scheme()}://{self.path}",
         provider=self.provider,
     )
コード例 #6
0
    def load_config(self, config_path: str) -> ConfigResult:
        normalized_config_path = self._normalize_file_name(config_path)
        res = resources.files(self.path).joinpath(normalized_config_path)
        if not res.exists():
            raise ConfigLoadError(
                f"Config not found : {normalized_config_path}")

        return self._read_config(res)
コード例 #7
0
 def load_config(self, config_path: str) -> ConfigResult:
     config_path = self._normalize_file_name(config_path)
     full_path = os.path.realpath(os.path.join(self.path, config_path))
     if not os.path.exists(full_path):
         raise ConfigLoadError(
             f"FileConfigSource: Config not found : {full_path}")
     return ConfigResult(
         config=OmegaConf.load(full_path),
         path=f"{self.scheme()}://{self.path}",
         provider=self.provider,
     )
コード例 #8
0
    def _load(self, config_path: str) -> ConfigNode:
        idx = config_path.rfind("/")
        if idx == -1:
            ret = self._open(config_path)
            if ret is None:
                raise ConfigLoadError(f"Structured config not found {config_path}")
            assert isinstance(ret, ConfigNode)
            return ret
        else:
            path = config_path[0:idx]
            name = config_path[idx + 1 :]
            d = self._open(path)
            if d is None or not isinstance(d, dict):
                raise ConfigLoadError(f"Structured config not found {config_path}")

            if name not in d:
                raise ConfigLoadError(
                    f"Structured config {name} not found in {config_path}"
                )

            ret = d[name]
            assert isinstance(ret, ConfigNode)
            return ret
コード例 #9
0
    def load_config(self, config_path: str) -> ConfigResult:
        full_path = f"{self.path}/{config_path}"
        module_name, resource_name = PackageConfigSource._split_module_and_resource(
            full_path
        )

        try:
            with resource_stream(module_name, resource_name) as stream:
                return ConfigResult(
                    config=OmegaConf.load(stream),
                    path=f"{self.scheme()}://{self.path}",
                    provider=self.provider,
                )
        except IOError:
            raise ConfigLoadError(f"PackageConfigSource: Config not found: {full_path}")
コード例 #10
0
    def load_config(self, config_path: str) -> ConfigResult:
        config_path = self._normalize_file_name(filename=config_path)
        module_name, resource_name = PackageConfigSource._split_module_and_resource(
            self.concat(self.path, config_path))

        try:
            with resource_stream(module_name, resource_name) as stream:
                return ConfigResult(
                    config=OmegaConf.load(stream),
                    path=f"{self.scheme()}://{self.path}",
                    provider=self.provider,
                )
        except FileNotFoundError:
            raise ConfigLoadError(
                f"PackageConfigSource: Config not found: module={module_name}, resource_name={resource_name}"
            )
コード例 #11
0
    def load(self, config_path: str) -> DictConfig:
        idx = config_path.rfind("/")
        if idx == -1:
            ret = self._open(config_path)
            assert isinstance(ret, DictConfig)
            return ret
        else:
            path = config_path[0:idx]
            name = config_path[idx + 1:]
            d = self._open(path)
            if d is None or not isinstance(d, dict) or name not in d:
                raise ConfigLoadError(f"Error loading {config_path}")

            ret = d[name]
            assert isinstance(ret, DictConfig)
            return ret
コード例 #12
0
    def load_config(self, config_path: str) -> ConfigResult:
        normalized_config_path = self._normalize_file_name(config_path)
        res = resources.files(self.path).joinpath(normalized_config_path)  # type:ignore
        if not res.exists():
            raise ConfigLoadError(f"Config not found : {normalized_config_path}")

        with res.open(encoding="utf-8") as f:
            header_text = f.read(512)
            header = ConfigSource._get_header_dict(header_text)
            f.seek(0)
            cfg = OmegaConf.load(f)
            return ConfigResult(
                config=cfg,
                path=f"{self.scheme()}://{self.path}",
                provider=self.provider,
                header=header,
            )
コード例 #13
0
    def load_config(self, config_path: str) -> ConfigResult:
        normalized_config_path = self._normalize_file_name(config_path)
        full_path = os.path.realpath(os.path.join(self.path, normalized_config_path))
        if not os.path.exists(full_path):
            raise ConfigLoadError(f"Config not found : {full_path}")

        with open(full_path, encoding="utf-8") as f:
            header_text = f.read(512)
            header = ConfigSource._get_header_dict(header_text)
            f.seek(0)
            cfg = OmegaConf.load(f)
            return ConfigResult(
                config=cfg,
                path=f"{self.scheme()}://{self.path}",
                provider=self.provider,
                header=header,
            )
コード例 #14
0
    def load_config(self,
                    config_path: str,
                    package_override: Optional[str] = None) -> ConfigResult:
        name = self._normalize_file_name(config_path)

        if name not in self.configs:
            raise ConfigLoadError("Config not found : " + config_path)

        res_header: Dict[str, Optional[str]] = {"package": None}
        if name in self.headers:
            header = self.headers[name]
            res_header[
                "package"] = header["package"] if "package" in header else None

        cfg = OmegaConf.create(self.configs[name])
        return ConfigResult(
            config=cfg,
            path=f"{self.scheme()}://{self.path}",
            provider=self.provider,
            header=res_header,
        )
コード例 #15
0
    def add(self, path: str, name: str, node: DictConfig) -> None:
        d = self._open(path)
        if d is None or not isinstance(d, dict) or name in d:
            raise ConfigLoadError(f"Error adding {path}")

        d[name] = node