Debian Codegraph @

jupyter_cache/cli/commands/cmd_project.py

raw
1 import logging
2
3 import click
4
5 from jupyter_cache.cli import options, pass_cache, utils
6 from jupyter_cache.cli.commands.cmd_main import jcache
7
8 logger = logging.getLogger(__name__)
9 utils.setup_logger(logger)
10
11
12 @jcache.group("project")
13 @options.CACHE_PATH
14 @pass_cache
15 def cmnd_project(cache, cache_path):
16 """Work with a project."""
17 cache.set_cache_path(cache_path)
18
19
20 @cmnd_project.command("version")
21 @pass_cache
22 def version(cache):
23 """Print the version of the cache."""
24 if not cache.cache_path.exists():
25 click.secho("No cache found.", fg="red")
26 raise click.Abort()
27 version = cache.get_cache().get_version()
28 if version is None:
29 click.secho("Cache version not found", fg="red")
30 raise click.Abort()
31 click.echo(version)
32
33
34 @cmnd_project.command("clear")
35 @options.FORCE
36 @pass_cache
37 def clear_cache(cache, force):
38 """Clear the project cache completely."""
39 if not cache.cache_path.exists():
40 click.secho("Cache does not exist", fg="green")
41 raise click.Abort()
42 if not force:
43 click.echo(f"Cache path: {cache.cache_path}")
44 click.confirm(
45 "Are you sure you want to permanently clear the cache!?",
46 abort=True,
47 )
48 cache.get_cache().clear_cache()
49 click.secho("Cache cleared!", fg="green")
50
51
52 @cmnd_project.command("cache-limit")
53 @click.argument("limit", metavar="CACHE_LIMIT", type=int, required=False)
54 @pass_cache
55 def change_cache_limit(cache, limit):
56 """Get/set maximum number of notebooks stored in the cache."""
57 db = cache.get_cache()
58 if limit is None:
59 limit = db.get_cache_limit()
60 click.echo(f"Current cache limit: {limit}")
61 else:
62 db.change_cache_limit(limit)
63 click.secho("Cache limit changed!", fg="green")
64
65
66 @cmnd_project.command("execute")
67 @options.EXECUTOR_KEY
68 @options.EXEC_TIMEOUT
69 @options.EXEC_FORCE(default=False)
70 @options.set_log_level(logger)
71 @pass_cache
72 def execute_nbs(cache, executor, timeout, force):
73 """Execute all outdated notebooks in the project."""
74 import yaml
75
76 from jupyter_cache.executors import load_executor
77
78 db = cache.get_cache()
79 try:
80 executor = load_executor(executor, db, logger=logger)
81 except ImportError as error:
82 logger.error(str(error))
83 return 1
84 result = executor.run_and_cache(timeout=timeout, force=force)
85 click.secho(
86 "Finished! Successfully executed notebooks have been cached.", fg="green"
87 )
88 click.echo(yaml.safe_dump(result.as_json(), sort_keys=False))