| 1 |
import logging |
| 2 |
import os |
| 3 |
|
| 4 |
import click |
| 5 |
|
| 6 |
from jupyter_cache.entry_points import ENTRY_POINT_GROUP_EXEC, list_group_names |
| 7 |
from jupyter_cache.readers import list_readers |
| 8 |
|
| 9 |
|
| 10 |
def callback_autocomplete(ctx, param, value): |
| 11 |
if value and not ctx.resilient_parsing: |
| 12 |
click.echo("Execute this in the terminal for auto-completion:") |
| 13 |
click.echo('eval "$(_JCACHE_COMPLETE=source jcache)"') |
| 14 |
ctx.exit() |
| 15 |
|
| 16 |
|
| 17 |
AUTOCOMPLETE = click.option( |
| 18 |
"-a", |
| 19 |
"--autocomplete", |
| 20 |
help="Print the autocompletion command and exit.", |
| 21 |
is_flag=True, |
| 22 |
expose_value=True, |
| 23 |
is_eager=True, |
| 24 |
callback=callback_autocomplete, |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
def default_cache_path(): |
| 29 |
return os.environ.get("JUPYTERCACHE", os.path.join(os.getcwd(), ".jupyter_cache")) |
| 30 |
|
| 31 |
|
| 32 |
def callback_print_cache_path(ctx, param, value): |
| 33 |
if value and not ctx.resilient_parsing: |
| 34 |
click.secho("Cache path: ", fg="green", nl=False) |
| 35 |
click.echo(default_cache_path()) |
| 36 |
ctx.exit() |
| 37 |
|
| 38 |
|
| 39 |
PRINT_CACHE_PATH = click.option( |
| 40 |
"-p", |
| 41 |
"--print-path", |
| 42 |
help="Print the current cache path and exit.", |
| 43 |
is_flag=True, |
| 44 |
expose_value=True, |
| 45 |
is_eager=True, |
| 46 |
callback=callback_print_cache_path, |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
CACHE_PATH = click.option( |
| 51 |
"-p", |
| 52 |
"--cache-path", |
| 53 |
help="Path to project cache.", |
| 54 |
default=default_cache_path, |
| 55 |
show_default=".jupyter_cache", |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
NB_PATH = click.option( |
| 60 |
"-nb", |
| 61 |
"--nbpath", |
| 62 |
required=True, |
| 63 |
help="The notebooks path.", |
| 64 |
type=click.Path(dir_okay=False, exists=True, readable=True, resolve_path=True), |
| 65 |
) |
| 66 |
|
| 67 |
READER_KEY = click.option( |
| 68 |
"-r", |
| 69 |
"--reader", |
| 70 |
help="The notebook reader to use.", |
| 71 |
default="nbformat", |
| 72 |
type=click.Choice(list_readers()), |
| 73 |
show_default=True, |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
EXECUTOR_KEY = click.option( |
| 78 |
"-e", |
| 79 |
"--executor", |
| 80 |
help="The executor to use.", |
| 81 |
default="local-serial", |
| 82 |
type=click.Choice(list_group_names(ENTRY_POINT_GROUP_EXEC)), |
| 83 |
show_default=True, |
| 84 |
) |
| 85 |
|
| 86 |
EXEC_TIMEOUT = click.option( |
| 87 |
"-t", |
| 88 |
"--timeout", |
| 89 |
help="Execution timeout value in seconds.", |
| 90 |
default=30, |
| 91 |
show_default=True, |
| 92 |
) |
| 93 |
|
| 94 |
|
| 95 |
def EXEC_FORCE(default=False): |
| 96 |
return click.option( |
| 97 |
"-f", |
| 98 |
"--force/--no-force", |
| 99 |
help="Execute a notebook even if it is cached.", |
| 100 |
is_flag=True, |
| 101 |
default=default, |
| 102 |
show_default=True, |
| 103 |
) |
| 104 |
|
| 105 |
|
| 106 |
PATH_LENGTH = click.option( |
| 107 |
"-l", "--path-length", default=3, show_default=True, help="Maximum URI path." |
| 108 |
) |
| 109 |
|
| 110 |
|
| 111 |
VALIDATE_NB = click.option( |
| 112 |
"--validate/--no-validate", |
| 113 |
default=True, |
| 114 |
show_default=True, |
| 115 |
help="Whether to validate that a notebook has been executed.", |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
OVERWRITE_CACHED = click.option( |
| 120 |
"--overwrite/--no-overwrite", |
| 121 |
default=True, |
| 122 |
show_default=True, |
| 123 |
help="Whether to overwrite an existing notebook with the same hash.", |
| 124 |
) |
| 125 |
|
| 126 |
FORCE = click.option( |
| 127 |
"-f", "--force", default=False, is_flag=True, help="Do not ask for confirmation." |
| 128 |
) |
| 129 |
|
| 130 |
|
| 131 |
def confirm_remove_all(ctx, param, remove_all): |
| 132 |
if remove_all and not click.confirm("Are you sure you want to remove all?"): |
| 133 |
click.secho("Aborted!", bold=True, fg="red") |
| 134 |
ctx.exit() |
| 135 |
return remove_all |
| 136 |
|
| 137 |
|
| 138 |
REMOVE_ALL = click.option( |
| 139 |
"-a", |
| 140 |
"--all", |
| 141 |
"remove_all", |
| 142 |
is_flag=True, |
| 143 |
help="Remove all notebooks.", |
| 144 |
callback=confirm_remove_all, |
| 145 |
) |
| 146 |
|
| 147 |
|
| 148 |
def confirm_invalidate_all(ctx, param, remove_all): |
| 149 |
if remove_all and not click.confirm("Are you sure you want to invalidate all?"): |
| 150 |
click.secho("Aborted!", bold=True, fg="red") |
| 151 |
ctx.exit() |
| 152 |
return remove_all |
| 153 |
|
| 154 |
|
| 155 |
INVALIDATE_ALL = click.option( |
| 156 |
"-a", |
| 157 |
"--all", |
| 158 |
"invalidate_all", |
| 159 |
is_flag=True, |
| 160 |
help="Invalidate all notebooks.", |
| 161 |
callback=confirm_invalidate_all, |
| 162 |
) |
| 163 |
|
| 164 |
|
| 165 |
def set_log_level(logger): |
| 166 |
"""Set the log level of the logger.""" |
| 167 |
|
| 168 |
def _callback(ctx, param, value): |
| 169 |
"""Set logging level.""" |
| 170 |
level = getattr(logging, value.upper(), None) |
| 171 |
if level is None: |
| 172 |
raise click.BadParameter(f"Unknown log level: {value.upper()}") |
| 173 |
logger.setLevel(level) |
| 174 |
|
| 175 |
return click.option( |
| 176 |
"-v", |
| 177 |
"--verbosity", |
| 178 |
type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]), |
| 179 |
default="INFO", |
| 180 |
show_default=True, |
| 181 |
expose_value=False, |
| 182 |
callback=_callback, |
| 183 |
help="Set logging verbosity.", |
| 184 |
) |