Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log-verbosity #4

Merged
merged 3 commits into from Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion databooks/common.py
@@ -1,6 +1,7 @@
"""Common set of miscellaneous functions"""
import json
import logging
import os
from itertools import chain
from pathlib import Path
from typing import List
Expand All @@ -10,8 +11,10 @@
from databooks import JupyterNotebook


def get_logger(name: str, level: str = "INFO") -> logging.Logger:
def get_logger(name: str) -> logging.Logger:
"""Get logger with rich configuration."""
level = os.getenv("LOG_LEVEL", logging.INFO)

logging.basicConfig(
level=level,
format="%(message)s",
Expand All @@ -21,6 +24,15 @@ def get_logger(name: str, level: str = "INFO") -> logging.Logger:
return logging.getLogger(name)


def set_verbose(logger: logging.Logger) -> None:
bart6114 marked this conversation as resolved.
Show resolved Hide resolved
"""Set logger to DEBUG level when user requests verbosity."""
verbose_level = logging.DEBUG
logger.setLevel(verbose_level)
logger.debug(
f"Verbose mode: setting log level to {logging.getLevelName(verbose_level)}"
)


def write_notebook(nb: JupyterNotebook, path: Path) -> None:
"""Write notebook to a path"""
with path.open("w") as f:
Expand Down
12 changes: 7 additions & 5 deletions databooks/conflicts.py
Expand Up @@ -7,7 +7,7 @@

from git import Repo

from databooks.common import get_logger, write_notebook
from databooks.common import get_logger, set_verbose, write_notebook
from databooks.data_models.base import BaseCells, DiffModel
from databooks.data_models.notebook import Cell, Cells, JupyterNotebook
from databooks.git_utils import ConflictFile, get_conflict_blobs, get_repo
Expand Down Expand Up @@ -64,16 +64,19 @@ def conflict2nb(
:return: Resolved conflicts as a `databooks.data_models.notebook.JupyterNotebook`
model
"""
if verbose:
set_verbose(logger)

nb_1 = JupyterNotebook.parse_raw(conflict_file.first_contents)
nb_2 = JupyterNotebook.parse_raw(conflict_file.last_contents)
if nb_1.metadata != nb_2.metadata and verbose:
if nb_1.metadata != nb_2.metadata:
msg = (
f"Notebook metadata conflict for {conflict_file.filename}. Keeping "
+ "first."
if keep_first
else "last."
)
logger.info(msg)
logger.debug(msg)

diff_nb = cast(DiffModel, nb_1 - nb_2)
nb = cast(
Expand All @@ -86,8 +89,7 @@ def conflict2nb(
last_id=conflict_file.last_log,
),
)
if verbose:
logger.info(f"Resolved conflicts in {conflict_file.filename}.")
logger.debug(f"Resolved conflicts in {conflict_file.filename}.")
return nb


Expand Down
26 changes: 13 additions & 13 deletions databooks/metadata.py
Expand Up @@ -3,7 +3,7 @@
from typing import Any, Callable, List, Optional, Sequence

from databooks import JupyterNotebook
from databooks.common import get_logger, write_notebook
from databooks.common import get_logger, set_verbose, write_notebook

logger = get_logger(__file__)

Expand All @@ -30,6 +30,8 @@ def clear(
`databooks.data_models.JupyterNotebook.clear_metadata`
:return: Whether notebooks are equal
"""
if verbose:
set_verbose(logger)

if write_path is None:
write_path = read_path
Expand All @@ -41,20 +43,18 @@ def clear(
**kwargs,
)
nb_equals = notebook == JupyterNotebook.parse_file(read_path)
if verbose:
if nb_equals or check:
msg = (
"only check (unwanted metadata found)."
if not nb_equals
else "no metadata to remove."
)
logger.info(f"No action taken for {read_path} - " + msg)
else:
write_notebook(nb=notebook, path=write_path)
logger.info(f"Removed metadata from {read_path}, saved as {write_path}")

elif not nb_equals and not check:
if nb_equals or check:
msg = (
"only check (unwanted metadata found)."
if not nb_equals
else "no metadata to remove."
)
logger.debug(f"No action taken for {read_path} - " + msg)
else:
write_notebook(nb=notebook, path=write_path)
logger.debug(f"Removed metadata from {read_path}, saved as {write_path}")

return nb_equals


Expand Down
6 changes: 3 additions & 3 deletions tests/test_metadata.py
Expand Up @@ -15,7 +15,7 @@ def test_metadata_clear__check_verbose(
tmpdir: LocalPath, caplog: LogCaptureFixture
) -> None:
"""Clear metadata from a notebook and write clean notebook"""
caplog.set_level(logging.INFO)
caplog.set_level(logging.DEBUG)
read_path = Path(tmpdir.mkdir("notebooks") / "test_nb.ipynb") # type: ignore
write_notebook(nb=TestJupyterNotebook().jupyter_notebook, path=read_path)
write_path = read_path.parent / ("clean_" + read_path.name)
Expand All @@ -35,8 +35,8 @@ def test_metadata_clear__check_verbose(
)

assert not write_path.exists()
assert len(logs) == 1
assert logs[0].message == (
assert len(logs) == 2
assert logs[1].message == (
f"No action taken for {read_path} - only check (unwanted metadata found)."
)

Expand Down