def __init__( self, project_path: Union[Path, str], env: str = None, extra_params: Dict[str, Any] = None, ): """Create a context object by providing the root of a Kedro project and the environment configuration subfolders (see ``kedro.config.ConfigLoader``) Raises: KedroContextError: If there is a mismatch between Kedro project version and package version. Args: project_path: Project path to define the context for. env: Optional argument for configuration default environment to be used for running the pipeline. If not specified, it defaults to "local". extra_params: Optional dictionary containing extra project parameters. If specified, will update (and therefore take precedence over) the parameters retrieved from the project configuration. """ # check the match for major and minor version (skip patch version) if self.project_version.split(".")[:2] != __version__.split(".")[:2]: raise KedroContextError( _version_mismatch_error(self.project_version)) self._project_path = Path(project_path).expanduser().resolve() self.env = env or "local" self._extra_params = deepcopy(extra_params) self._setup_logging() # setup hooks self._register_hooks()
def __init__(self, project_path: Union[Path, str], env: str = None): """Create a context object by providing the root of a Kedro project and the environment configuration subfolders (see ``kedro.config.ConfigLoader``) Raises: KedroContextError: If there is a mismatch between Kedro project version and package version. Args: project_path: Project path to define the context for. env: Optional argument for configuration default environment to be used for running the pipeline. If not specified, it defaults to "local". """ def _version_mismatch_error(context_version): return ( "Your Kedro project version {} does not match Kedro package " "version {} you are running. Make sure to update your project template. " "See https://github.com/quantumblacklabs/kedro/blob/master/RELEASE.md for how to " "migrate your Kedro project." ).format(context_version, __version__) # check the match for major and minor version (skip patch version) if self.project_version.split(".")[:2] != __version__.split(".")[:2]: raise KedroContextError(_version_mismatch_error(self.project_version)) self._project_path = Path(project_path).expanduser().resolve() self.env = env or "local" self._setup_logging()
def _get_project_metadata(project_path: Union[str, Path]) -> ProjectMetadata: """Read project metadata from `<project_path>/pyproject.toml` config file, under the `[tool.kedro]` section. Args: project_path: Local path to project root directory to look up `pyproject.toml` in. Raises: RuntimeError: `pyproject.toml` was not found or the `[tool.kedro]` section is missing, or config file cannot be parsed. ValueError: If project version is different from Kedro package version. Note: Project version is the Kedro version the project was generated with. Returns: A named tuple that contains project metadata. """ project_path = Path(project_path).expanduser().resolve() pyproject_toml = project_path / _PYPROJECT if not pyproject_toml.is_file(): raise RuntimeError( f"Could not find the project configuration file '{_PYPROJECT}' in {project_path}. " f"If you have created your project with Kedro " f"version <0.17.0, make sure to update your project template. " f"See https://github.com/quantumblacklabs/kedro/blob/master/RELEASE.md" f"#migration-guide-from-kedro-016-to-kedro-0170 " f"for how to migrate your Kedro project.") try: metadata_dict = anyconfig.load(pyproject_toml) except Exception as exc: raise RuntimeError(f"Failed to parse '{_PYPROJECT}' file.") from exc try: metadata_dict = metadata_dict["tool"]["kedro"] except KeyError as exc: raise RuntimeError( f"There's no '[tool.kedro]' section in the '{_PYPROJECT}'. " f"Please add '[tool.kedro]' section to the file with appropriate " f"configuration parameters.") from exc mandatory_keys = ["package_name", "project_name", "project_version"] missing_keys = [key for key in mandatory_keys if key not in metadata_dict] if missing_keys: raise RuntimeError( f"Missing required keys {missing_keys} from '{_PYPROJECT}'.") # check the match for major and minor version (skip patch version) if metadata_dict["project_version"].split(".")[:2] != kedro_version.split( ".")[:2]: raise ValueError( _version_mismatch_error(metadata_dict["project_version"])) source_dir = Path(metadata_dict.get("source_dir", "src")).expanduser() source_dir = (project_path / source_dir).resolve() metadata_dict["source_dir"] = source_dir metadata_dict["config_file"] = pyproject_toml metadata_dict["project_path"] = project_path try: return ProjectMetadata(**metadata_dict) except TypeError as exc: expected_keys = mandatory_keys + ["source_dir"] raise RuntimeError( f"Found unexpected keys in '{_PYPROJECT}'. Make sure " f"it only contains the following keys: {expected_keys}.") from exc