Example #1
0
def test_install_hook_replace_hook_older_version(tmp_path, monkeypatch):
    def mock_jupyter_config_dir():
        return str(tmp_path)

    monkeypatch.setattr(jupyter_config, "jupyter_config_dir",
                        mock_jupyter_config_dir)

    config_path = tmp_path / "jupyter_notebook_config.py"

    old_config_text = """\
        print('hello world!')

        # >>> nbautoexport initialize, version=[0] >>>
        old_and_tired()
        # <<< nbautoexport initialize <<<

        print('good night world!')
    """

    with config_path.open("w", encoding="utf-8") as fp:
        fp.write(textwrap.dedent(old_config_text))

    jupyter_config.install_post_save_hook()

    assert config_path.exists()
    with config_path.open("r", encoding="utf-8") as fp:
        config = fp.read()
    assert config == ("print('hello world!')\n\n" +
                      jupyter_config.post_save_hook_initialize_block +
                      "\nprint('good night world!')\n")
    assert "old_and_tired()" not in config
    assert f"version=[{__version__}]" in config
Example #2
0
def install(jupyter_config: Optional[Path] = typer.Option(
    None,
    exists=False,
    file_okay=True,
    dir_okay=False,
    writable=True,
    help=
    ("Path to config file. If not specified (default), will determine appropriate path "
     "used by Jupyter. You should only specify this option if you use a nonstandard config "
     "file path that you explicitly pass to Jupyter with the --config option at startup."
     ),
)):
    """Register nbautoexport post-save hook with Jupyter. Note that if you already have a Jupyter
    server running, you will need to restart in order for it to take effect. This is a one-time
    installation.

    This works by adding an initialization block in your Jupyter config file that will register
    nbautoexport's post-save function. If an nbautoexport initialization block already exists and
    is from an older version of nbautoexport, this command will replace it with an updated version.
    """
    install_post_save_hook(config_path=jupyter_config)

    typer.echo(
        "nbautoexport post-save hook successfully installed with Jupyter.")
    typer.echo(
        "If a Jupyter server is already running, you will need to restart it for nbautoexport "
        "to work.")
def test_configure_no_warning(tmp_path, monkeypatch):
    monkeypatch.setenv("JUPYTER_CONFIG_DIR", str(tmp_path))

    jupyter_config.install_post_save_hook(tmp_path /
                                          "jupyter_notebook_config.py")

    result = CliRunner().invoke(app, ["configure", str(tmp_path)])
    assert result.exit_code == 0
    assert "Warning:" not in result.output
Example #4
0
def test_configure_no_warning(tmp_path, monkeypatch):
    monkeypatch.setenv("JUPYTER_CONFIG_DIR", str(tmp_path))

    jupyter_config_path = tmp_path / "jupyter_notebook_config.py"
    jupyter_config.install_post_save_hook(jupyter_config_path)
    # Print to assist with debugging
    with jupyter_config_path.open("r") as fp:
        print(fp.read())

    result = CliRunner().invoke(app, ["configure", str(tmp_path)])
    assert result.exit_code == 0
    assert "Warning:" not in result.output
Example #5
0
def test_install_hook_no_config(tmp_path, monkeypatch):
    def mock_jupyter_config_dir():
        return str(tmp_path)

    monkeypatch.setattr(jupyter_config, "jupyter_config_dir",
                        mock_jupyter_config_dir)

    config_path = tmp_path / "jupyter_notebook_config.py"

    assert not config_path.exists()

    jupyter_config.install_post_save_hook()

    assert config_path.exists()

    with config_path.open("r", encoding="utf-8") as fp:
        config = fp.read()
    assert config == jupyter_config.post_save_hook_initialize_block
Example #6
0
def test_install_hook_existing_config_no_hook(tmp_path, monkeypatch):
    def mock_jupyter_config_dir():
        return str(tmp_path)

    monkeypatch.setattr(jupyter_config, "jupyter_config_dir",
                        mock_jupyter_config_dir)

    config_path = tmp_path / "jupyter_notebook_config.py"

    with config_path.open("w", encoding="utf-8") as fp:
        fp.write("print('hello world!')")

    jupyter_config.install_post_save_hook()

    assert config_path.exists()

    with config_path.open("r", encoding="utf-8") as fp:
        config = fp.read()
    assert config == ("print('hello world!')" + "\n" +
                      jupyter_config.post_save_hook_initialize_block)
def test_install_hook_missing_config_dir(tmp_path, monkeypatch):
    config_dir = tmp_path / "not_yet_a_real_dir"

    def mock_jupyter_config_dir():
        return str(config_dir)

    monkeypatch.setattr(jupyter_config, "jupyter_config_dir",
                        mock_jupyter_config_dir)

    config_path = config_dir / "jupyter_notebook_config.py"

    assert not config_dir.exists()
    assert not config_path.exists()

    jupyter_config.install_post_save_hook()

    assert config_dir.exists()
    assert config_path.exists()

    with config_path.open("r") as fp:
        config = fp.read()
    assert config == jupyter_config.post_save_hook_initialize_block