Skip to content

Commit a288a25

Browse files
authored
Merge pull request #26 from ayasyrev/dev_0.2
0.2
2 parents 4ff0eb3 + ab74883 commit a288a25

25 files changed

+641
-319
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.vscode/
22
tmp*/
3+
tmp*
34
# Byte-compiled / optimized / DLL files
45
__pycache__/
56
*.py[cod]

noxfile_conda.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
@nox.session(python=["3.8", "3.9", "3.10", "3.11"], venv_backend="mamba")
5-
def tests(session: nox.Session) -> None:
5+
def conda_tests(session: nox.Session) -> None:
66
args = session.posargs or ["--cov"]
77
session.install("pytest", "pytest-cov")
88
session.install("-e", ".")

noxfile_conda_lint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
@nox.session(python=["3.8", "3.9", "3.10", "3.11"], venv_backend="mamba")
7-
def lint(session: nox.Session) -> None:
7+
def conda_lint(session: nox.Session) -> None:
88
args = session.posargs or locations
99
session.conda_install("flake8")
1010
session.run("flake8", *args)

noxfile_cov.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
@nox.session(python=["3.10"])
5-
def tests(session: nox.Session) -> None:
5+
def cov_tests(session: nox.Session) -> None:
66
args = session.posargs or ["--cov"]
77
session.install(".", "pytest", "pytest-cov", "coverage[toml]")
88
session.run("pytest", *args)

requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
nbformat
22
nbconvert
3-
typer
43
rich
5-
pydantic
4+
argparsecfg

setup.cfg

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ where = src
2424

2525
[options.entry_points]
2626
console_scripts =
27-
nbdocs=nbdocs.apps.app_nbdocs:app
28-
nb2md=nbdocs.apps.app_nb2md:app
29-
nbclean=nbdocs.apps.app_nbclean:app
27+
nbdocs=nbdocs.apps.app_nbdocs:main
28+
nb2md=nbdocs.apps.app_nb2md:main
29+
nbclean=nbdocs.apps.app_nbclean:main
3030
pipx.run =
31-
nbdocs=nbdocs.apps.app_nbdocs:app
32-
nb2md=nbdocs.apps.app_nb2md:app
33-
nbclean=nbdocs.apps.app_nbclean:app
31+
nbdocs=nbdocs.apps.app_nbdocs:main
32+
nb2md=nbdocs.apps.app_nb2md:main
33+
nbclean=nbdocs.apps.app_nbclean:main

src/nbdocs/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from nbdocs.apps.app_nbdocs import app
1+
from nbdocs.apps.app_nbdocs import main
22

3-
app()
3+
main()

src/nbdocs/apps/app_nb2md.py

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,96 @@
1+
from dataclasses import dataclass
12
from pathlib import Path
2-
import typer
3+
import sys
4+
from typing import Optional, Sequence
5+
6+
from argparsecfg import ArgumentParserCfg, field_argument, parse_args
7+
from rich import print as rprint
8+
39
from nbdocs.convert import convert2md, filter_changed
410
from nbdocs.core import get_nb_names
511
from nbdocs.settings import get_config
612

7-
app = typer.Typer(rich_markup_mode="rich")
13+
14+
parser_cfg = ArgumentParserCfg(description="Nb2Md. Convert notebooks to Markdown.")
15+
16+
17+
@dataclass
18+
class AppConfig:
19+
nb_path: str = field_argument(
20+
"nb_path",
21+
help="Path to NB or folder with Nbs to convert",
22+
)
23+
dest_path: str = field_argument(
24+
default=None,
25+
flag="--dest",
26+
help="Docs path.",
27+
)
28+
images_path: str = field_argument(
29+
default=None,
30+
help="Image path at docs.",
31+
)
32+
force: bool = field_argument(
33+
"-F",
34+
default=False,
35+
action="store_true",
36+
help="Force convert all notebooks.",
37+
)
38+
silent_mode: bool = field_argument(
39+
"-s",
40+
default=False,
41+
action="store_true",
42+
help="Run in silent mode.",
43+
)
844

945

10-
@app.command()
1146
def convert(
12-
nb_path: str = typer.Argument(..., help="Path to NB or folder with Nbs to convert"),
13-
dest_path: str = typer.Option(None, "--dest", "--dest-path", help="Docs path."),
14-
images_path: str = typer.Option(None, help="Image path at docs."),
15-
force: bool = typer.Option(
16-
False, "-F", "--force", help="Force convert all notebooks."
17-
),
18-
silent_mode: bool = typer.Option(False, "-s", help="Run in silent mode."),
47+
app_cfg: AppConfig,
1948
) -> None:
2049
"""Nb2Md. Convert notebooks to Markdown."""
21-
nb_names = get_nb_names(nb_path)
22-
if len(nb_names) == 0:
23-
typer.echo("No files to convert!")
24-
raise typer.Exit()
50+
nb_names = get_nb_names(app_cfg.nb_path)
51+
nbs_number = len(nb_names)
52+
if nbs_number == 0:
53+
rprint("No files to convert!")
54+
sys.exit()
55+
rprint(f"Found {nbs_number} notebooks.")
2556

2657
cfg = get_config(
27-
notebooks_path=nb_path, docs_path=dest_path, images_path=images_path
58+
notebooks_path=app_cfg.nb_path, docs_path=app_cfg.dest_path, images_path=app_cfg.images_path
2859
)
2960

3061
# check logic -> do we need subdir and how to check modified Nbs
3162
# if convert whole directory, put result to docs subdir.
32-
if (path := Path(nb_path)).is_dir():
63+
if (path := Path(app_cfg.nb_path)).is_dir():
3364
cfg.docs_path = f"{cfg.docs_path}/{path.name}"
3465
Path(cfg.docs_path).mkdir(parents=True, exist_ok=True)
3566
(Path(cfg.docs_path) / cfg.images_path).mkdir(exist_ok=True)
3667

37-
if not force:
68+
if not app_cfg.force:
69+
message = "Filtering notebooks with changes... "
3870
nb_names = filter_changed(nb_names, cfg)
71+
if len(nb_names) == nbs_number:
72+
message += "No changes."
73+
rprint(message)
3974

4075
if len(nb_names) == 0:
41-
typer.echo("No files with changes to convert!")
42-
raise typer.Exit()
76+
rprint("No files with changes to convert!")
77+
sys.exit()
4378

44-
if not silent_mode:
79+
if not app_cfg.silent_mode:
4580
print(f"Files to convert from {nb_names[0].parent}:")
4681
for fn in nb_names:
4782
print(f" {fn.name}")
4883
print(
49-
f"Destination directory: {dest_path},\nImage directory: {cfg.images_path}"
84+
f"Destination directory: {app_cfg.dest_path},\nImage directory: {cfg.images_path}"
5085
)
5186

5287
convert2md(nb_names, cfg)
5388

5489

90+
def main(args: Optional[Sequence[str]] = None) -> None:
91+
app_cfg = parse_args(AppConfig, parser_cfg, args)
92+
convert(app_cfg)
93+
94+
5595
if __name__ == "__main__": # pragma: no cover
56-
app()
96+
main()

src/nbdocs/apps/app_nbclean.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,51 @@
1-
import typer
1+
import sys
2+
from dataclasses import dataclass
3+
from typing import Optional, Sequence
4+
5+
from argparsecfg import field_argument, parse_args, ArgumentParserCfg
6+
from rich import print as rprint
7+
28
from nbdocs.clean import clean_nb_file
39
from nbdocs.core import get_nb_names
410
from nbdocs.settings import get_config
511

6-
app = typer.Typer(rich_markup_mode="rich")
712

13+
parser_cfg = ArgumentParserCfg(
14+
description="Clean Nb or notebooks at `nb_path` - metadata and execution counts from nbs."
15+
)
16+
17+
18+
@dataclass
19+
class AppConfig:
20+
"""Config for `app_nbclean`."""
821

9-
@app.command()
10-
def nbclean(
11-
nb_path: str = typer.Argument(None, help="Path to NB or folder with Nbs to clean"),
12-
clear_execution_count: bool = typer.Option(
13-
True, "--ec", help="Clean execution counts."
14-
),
15-
) -> None:
22+
nb_path: str = field_argument(
23+
"nb_path", help="Path to NB or folder with Nbs to clean"
24+
)
25+
clear_execution_count: bool = field_argument(
26+
flag="--no-ec", default=True, action="store_false", help="Clean execution counts."
27+
)
28+
29+
30+
def nbclean(app_cfg: AppConfig) -> None:
1631
"""Clean Nb or notebooks at `nb_path` - metadata and execution counts from nbs."""
17-
cfg = get_config(notebooks_path=nb_path)
32+
cfg = get_config(notebooks_path=app_cfg.nb_path)
1833

1934
nb_names = get_nb_names(cfg.notebooks_path)
2035

2136
if (num_nbs := len(nb_names)) == 0:
22-
typer.echo("No files to clean!")
23-
raise typer.Abort()
37+
rprint("No files to clean!")
38+
sys.exit()
39+
40+
rprint(f"Clean: {cfg.notebooks_path}, found {num_nbs} notebooks.")
41+
42+
clean_nb_file(nb_names, app_cfg.clear_execution_count)
2443

25-
typer.echo(f"Clean: {cfg.notebooks_path}, found {num_nbs} notebooks.")
2644

27-
clean_nb_file(nb_names, clear_execution_count)
45+
def main(args: Optional[Sequence[str]] = None) -> None:
46+
app_cfg = parse_args(AppConfig, parser_cfg, args)
47+
nbclean(app_cfg)
2848

2949

3050
if __name__ == "__main__": # pragma: no cover
31-
app()
51+
main()

src/nbdocs/apps/app_nbdocs.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,54 @@
1-
import typer
1+
import sys
2+
from dataclasses import dataclass
3+
from typing import Optional, Sequence
4+
5+
from argparsecfg import ArgumentParserCfg, field_argument, parse_args
6+
from rich import print as rprint
27

38
from nbdocs.convert import convert2md, filter_changed
49
from nbdocs.core import get_nb_names
510
from nbdocs.settings import get_config
611

7-
app = typer.Typer(rich_markup_mode="rich")
12+
parser_cfg = ArgumentParserCfg(description="NbDocs. Convert notebooks to docs. Default to .md")
13+
14+
15+
@dataclass
16+
class AppConfig:
17+
force: bool = field_argument(
18+
"-F",
19+
default=False,
20+
help="Force convert all notebooks.",
21+
)
822

923

10-
@app.command()
11-
def nbdocs(
12-
force: bool = typer.Option(
13-
False, "-F", "--force", help="Force convert all notebooks."
14-
),
15-
) -> None:
24+
def nbdocs(app_cfg: AppConfig,) -> None:
1625
"""NbDocs. Convert notebooks to docs. Default to .md"""
1726
cfg = get_config()
1827
nb_names = get_nb_names(cfg.notebooks_path)
19-
if not force:
28+
nbs_number = len(nb_names)
29+
if nbs_number == 0:
30+
rprint("No files to convert!")
31+
sys.exit()
32+
rprint(f"Found {nbs_number} notebooks.")
33+
if not app_cfg.force:
34+
message = "Filtering notebooks with changes... "
2035
nb_names = filter_changed(nb_names, cfg)
36+
if len(nb_names) == nbs_number:
37+
message += "No changes."
38+
rprint(message)
2139

2240
if len(nb_names) == 0:
23-
typer.echo("No files to convert!")
24-
raise typer.Exit()
41+
rprint("No files to convert!")
42+
sys.exit()
2543

44+
rprint(f"To convert: {len(nb_names)} notebooks.")
2645
convert2md(nb_names, cfg)
2746

2847

48+
def main(args: Optional[Sequence[str]] = None) -> None:
49+
app_cfg = parse_args(AppConfig, parser_cfg, args)
50+
nbdocs(app_cfg)
51+
52+
2953
if __name__ == "__main__":
30-
app()
54+
main()

0 commit comments

Comments
 (0)