Skip to content

Commit

Permalink
Merge pull request #50 from ayasyrev/dev
Browse files Browse the repository at this point in the history
ver 0.0.9
  • Loading branch information
ayasyrev authored Aug 15, 2024
2 parents dbd2271 + c9fc5e2 commit 76fd3bc
Show file tree
Hide file tree
Showing 19 changed files with 205 additions and 83 deletions.
3 changes: 0 additions & 3 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@ extend-ignore = W503
disable-noqa = True
application-import-names = nbmetaclean, tests
import-order-style = google
per-file-ignores =
# imported but unused
__init__.py: F401
17 changes: 17 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Lint
on:
push:
branches:
- dev
- main
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- uses: actions/setup-python@main
with:
python-version: "3.11"
architecture: x64
- run: pip install ruff
- run: ruff check .
35 changes: 35 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Tests
on:
push:
branches:
- dev
- main
jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
python: ["3.8", "3.9", "3.10", "3.11", "3.12"]
steps:
- name: Checkout
uses: actions/checkout@main
- name: Setup Python ${{ matrix.python }}
uses: actions/setup-python@main
with:
python-version: ${{ matrix.python }}
architecture: x64

- name: Install
run: |
pip install uv
uv pip install --system .[test] "coverage[toml]"
- name: Tests
run: pytest --cov

- name: CodeCov
if: ${{ matrix.python == '3.11' }}
uses: codecov/codecov-action@main
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: ayasyrev/nbmetaclean
15 changes: 9 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
repos:
- repo: https://github.com/ayasyrev/nbmetaclean
rev: 0.0.8
- repo: local

hooks:
- id: nbclean
name: nbclean
# local version for testing
- id: nbclean
name: nbclean local
entry: nbclean
language: system
files: \.ipynb

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
Expand Down Expand Up @@ -34,7 +37,8 @@ repos:
- id: requirements-txt-fixer
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.5.1
rev: v0.5.7

hooks:
# Run the linter.
- id: ruff
Expand All @@ -47,5 +51,4 @@ repos:
- id: python-check-mock-methods
- id: python-use-type-annotations
- id: python-check-blanket-noqa
- id: python-use-type-annotations
- id: text-unicode-replacement-char
5 changes: 2 additions & 3 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import nox


@nox.session(python=["3.8", "3.9", "3.10", "3.11", "3.12"])
@nox.session(python=["3.8", "3.9", "3.10", "3.11", "3.12"], venv_backend="uv")
def tests(session: nox.Session) -> None:
args = session.posargs or ["--cov"]
session.install("uv")
session.run("uv", "pip", "install", ".[test]")
session.install("-e .[test]")
session.run("pytest", *args)
2 changes: 1 addition & 1 deletion noxfile_conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
def conda_tests(session: nox.Session) -> None:
args = session.posargs or ["--cov"]
session.conda_install("uv")
session.run("uv", "pip", "install", ".[test]")
session.install("uv", "pip", "install", "-e", ".[test]")
session.run("pytest", *args)
2 changes: 1 addition & 1 deletion noxfile_conda_lint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nox

locations = "src/nbmetaclean", "tests", "noxfile.py"
locations = "."


@nox.session(python=["3.8", "3.9", "3.10", "3.11", "3.12"], venv_backend="mamba")
Expand Down
2 changes: 1 addition & 1 deletion noxfile_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
locations = "."


@nox.session(python=["3.8", "3.9", "3.10", "3.11", "3.12"])
@nox.session(python=["3.8", "3.9", "3.10", "3.11", "3.12"], venv_backend="uv")
def lint(session: nox.Session) -> None:
args = session.posargs or locations
session.install("ruff")
Expand Down
15 changes: 14 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
[tool.coverage.paths]
source = ["src", "*/site-packages"]
source = ["src"]

[tool.coverage.run]
branch = true
source = ["nbmetaclean"]

[tool.coverage.report]
show_missing = true

[tool.ruff]
extend-include = ["*.ipynb"]
indent-width = 4

[tool.ruff.lint]
explicit-preview-rules = true

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"
2 changes: 2 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
black
black[jupyter]
coverage[toml]
flake8
isort
mypy
nox
pre-commit
ruff
2 changes: 0 additions & 2 deletions requirements_test_extra.txt

This file was deleted.

30 changes: 18 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
from setuptools import setup


REQUIREMENTS_FILENAME = "requirements.txt"
REQUIREMENTS_TEST_FILENAME = "requirements_test.txt"
REQUIREMENTS_DEV_FILENAME = "requirements_dev.txt"


def load_requirements(filename: str) -> list[str]:
"""Load requirements from file"""
try:
with open(filename, encoding="utf-8") as fh:
return fh.read().splitlines()
except FileNotFoundError:
return []


# Requirements
try:
with open(REQUIREMENTS_FILENAME, encoding="utf-8") as fh:
REQUIRED = fh.read().split("\n")
except FileNotFoundError:
REQUIRED = []
REQUIRED = load_requirements(REQUIREMENTS_FILENAME)
TEST_REQUIRED = load_requirements(REQUIREMENTS_TEST_FILENAME)
DEV_REQUIRED = load_requirements(REQUIREMENTS_DEV_FILENAME)

try:
with open(REQUIREMENTS_TEST_FILENAME, encoding="utf-8") as fh:
TEST_REQUIRED = fh.read().split("\n")
except FileNotFoundError:
TEST_REQUIRED = []

# What packages are optional?
EXTRAS = {"test": TEST_REQUIRED}
EXTRAS = {
"test": TEST_REQUIRED,
"dev": DEV_REQUIRED + TEST_REQUIRED,
}


setup(
Expand Down
73 changes: 56 additions & 17 deletions src/nbmetaclean/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
action="store_true",
help="Silent mode.",
)
parser.add_argument(
"--not_ec",
action="store_false",
help="Do not clear execution_count.",
)
parser.add_argument(
"--not-pt",
action="store_true",
Expand Down Expand Up @@ -61,6 +66,18 @@
action="store_true",
help="Clean hidden notebooks.",
)
parser.add_argument(
"-D",
"--dry_run",
action="store_true",
help="perform a trial run, don't write results",
)
parser.add_argument(
"-V",
"--verbose",
action="store_true",
help="Verbose mode. Print extra information.",
)


def process_mask(mask: Union[list[str], None]) -> Union[tuple[TupleStr, ...], None]:
Expand All @@ -69,41 +86,63 @@ def process_mask(mask: Union[list[str], None]) -> Union[tuple[TupleStr, ...], No
return tuple(tuple(item.split(".")) for item in mask)


def print_result(
cleaned: list[Path],
errors: list[tuple[Path, Exception]],
clean_config: CleanConfig,
path: list[Path],
num_nbs: int,
) -> None:
if clean_config.verbose:
print(
f"Path: {', '.join(path)}, preserve timestamp: {clean_config.preserve_timestamp}"
)
print(f"checked: {num_nbs} notebooks")
if cleaned:
if len(cleaned) == 1:
print(f"cleaned: {cleaned[0]}")
else:
print(f"cleaned: {len(cleaned)} notebooks")
for nb in cleaned:
print("- ", nb)
if errors:
print(f"with errors: {len(errors)}")
for nb, exc in errors:
print(f"{nb}: {exc}")


def app() -> None:
"""Clean metadata and execution_count from Jupyter notebook."""
cfg = parser.parse_args()
path_list = cfg.path if isinstance(cfg.path, list) else [cfg.path]
nb_files: list[Path] = []
if not cfg.silent:
print(f"Path: {', '.join(cfg.path)}, preserve timestamp: {not cfg.not_pt}")
for path in path_list:
try:
nb_files.extend(get_nb_names(path, hidden=cfg.clean_hidden_nbs))
except FileNotFoundError:
print(f"{path} not exists!")
if not cfg.silent:
print(f"notebooks to check: {len(nb_files)} ")
clean_config = CleanConfig(
clear_nb_metadata=not cfg.dont_clear_nb_metadata,
clear_cell_metadata=cfg.clear_cell_metadata,
clear_execution_count=True,
clear_execution_count=cfg.not_ec,
clear_outputs=cfg.clear_outputs,
preserve_timestamp=not cfg.not_pt,
silent=cfg.silent,
nb_metadata_preserve_mask=process_mask(cfg.nb_metadata_preserve_mask),
cell_metadata_preserve_mask=process_mask(cfg.cell_metadata_preserve_mask),
mask_merge=not cfg.dont_merge_masks,
dry_run=cfg.dry_run,
verbose=cfg.verbose if not cfg.silent else False,
)
path_list = cfg.path if isinstance(cfg.path, list) else [cfg.path]
nb_files: list[Path] = []
for path in path_list:
path = Path(path)
if path.exists():
nb_files.extend(get_nb_names(path, hidden=cfg.clean_hidden_nbs))
else:
print(f"{path} not exists!")

cleaned, errors = clean_nb_file(
nb_files,
clean_config,
)

if not cfg.silent:
print(f"cleaned nbs: {len(cleaned)}")
if errors:
print(f"with errors: {len(errors)}")
for nb, exc in errors:
print(f"{nb}: {exc}")
print_result(cleaned, errors, clean_config, path_list, len(nb_files))


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 76fd3bc

Please sign in to comment.