예제 #1
0
 def test_broken_register_catalog_hook(self, tmp_path):
     pattern = "Expected an instance of `DataCatalog`, got `NoneType` instead."
     with KedroSession.create(MOCK_PACKAGE_NAME, tmp_path) as session:
         context = session.load_context()
         with pytest.raises(KedroContextError, match=re.escape(pattern)):
             _ = context.catalog
예제 #2
0
 def test_broken_register_config_loader_hook(self, tmp_path):
     pattern = "Expected an instance of `ConfigLoader`, got `NoneType` instead."
     with pytest.raises(KedroContextError, match=re.escape(pattern)):
         KedroSession.create(MOCK_PACKAGE_NAME, tmp_path)
예제 #3
0
def reload_kedro(path,
                 line=None,
                 env: str = None,
                 extra_params: Dict[str, Any] = None):
    """Line magic which reloads all Kedro default variables."""
    global startup_error
    global context
    global catalog
    global session

    try:
        import kedro.config.default_logger
        from kedro.framework.hooks import get_hook_manager
        from kedro.framework.session import KedroSession
        from kedro.framework.session.session import _activate_session
        from kedro.framework.cli.jupyter import collect_line_magic
    except ImportError:
        logging.error(
            "Kedro appears not to be installed in your current environment "
            "or your current IPython session was not started in a valid Kedro project."
        )
        raise

    try:
        path = path or project_path

        # clear hook manager
        hook_manager = get_hook_manager()
        name_plugin_pairs = hook_manager.list_name_plugin()
        for name, plugin in name_plugin_pairs:
            hook_manager.unregister(name=name, plugin=plugin)

        # remove cached user modules
        metadata = _get_project_metadata(path)
        to_remove = [
            mod for mod in sys.modules if mod.startswith(metadata.package_name)
        ]
        # `del` is used instead of `reload()` because: If the new version of a module does not
        # define a name that was defined by the old version, the old definition remains.
        for module in to_remove:
            del sys.modules[module]

        session = KedroSession.create(metadata.package_name,
                                      path,
                                      env=env,
                                      extra_params=extra_params)
        _activate_session(session, force=True)
        logging.debug("Loading the context from %s", str(path))
        context = session.load_context()
        catalog = context.catalog

        logging.info("** Kedro project %s", str(metadata.project_name))
        logging.info(
            "Defined global variable `context`, `session` and `catalog`")

        for line_magic in collect_line_magic():
            register_line_magic(needs_local_scope(line_magic))
            logging.info("Registered line magic `%s`", line_magic.__name__)
    except Exception as err:
        startup_error = err
        logging.exception("Kedro's ipython session startup script failed:\n%s",
                          str(err))
        raise err
예제 #4
0
def mock_session_with_hooks(tmp_path, mock_settings_import, logging_hooks):  # pylint: disable=unused-argument
    logging_hooks.queue_listener.start()
    yield KedroSession.create(MOCK_PACKAGE_NAME, tmp_path)
    logging_hooks.queue_listener.stop()
예제 #5
0
def run_package():
    # Entry point for running a Kedro project packaged with `kedro package`
    # using `python -m <project_package>.run` command.
    package_name = Path(__file__).resolve().parent.name
    with KedroSession.create(package_name) as session:
        session.run()