kdev-cli 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,127 @@
1
+ """`kdev doctor`: check every piece kdev depends on, and fix what it can."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ import shutil
7
+ import sys
8
+ from dataclasses import dataclass
9
+
10
+ import typer
11
+
12
+ from .. import api, auth, cloudflared, config, sshcfg, ui
13
+ from .. import tunnel as cftunnel
14
+
15
+
16
+ @dataclass
17
+ class Check:
18
+ name: str
19
+ ok: bool
20
+ detail: str
21
+ fix: str = ""
22
+ warn_only: bool = False
23
+
24
+
25
+ def _checks(cfg: config.Config) -> list[Check]:
26
+ out: list[Check] = []
27
+ py = sys.version_info
28
+ out.append(
29
+ Check(
30
+ "python",
31
+ py >= (3, 11),
32
+ f"{py.major}.{py.minor}.{py.micro}",
33
+ "kdev needs Python 3.11 or newer",
34
+ )
35
+ )
36
+ for tool, why in (("ssh", "to reach the box"), ("rsync", "for kdev backup")):
37
+ path = shutil.which(tool)
38
+ out.append(
39
+ Check(tool, bool(path), path or f"not found; needed {why}", warn_only=tool == "rsync")
40
+ )
41
+ cf = cloudflared.find()
42
+ if cf:
43
+ found = re.search(r"\d{4}\.\d+\.\d+", cloudflared.version(cf))
44
+ detail = found.group(0) if found else cloudflared.version(cf)
45
+ else:
46
+ detail = "missing"
47
+ out.append(Check("cloudflared", bool(cf), detail, "kdev doctor --fix"))
48
+ out.append(
49
+ Check(
50
+ "ssh key",
51
+ bool(cfg.ssh_public_key),
52
+ (cfg.ssh_public_key.split()[-1] if " " in cfg.ssh_public_key else "set")
53
+ if cfg.ssh_public_key
54
+ else "none",
55
+ "kdev setup",
56
+ )
57
+ )
58
+ if not cfg.profiles:
59
+ out.append(Check("accounts", False, "none", "kdev account add"))
60
+ for name, prof in cfg.profiles.items():
61
+ if not auth.signed_in(name):
62
+ out.append(Check(f"account {name}", False, "not signed in", f"kdev account add {name}"))
63
+ continue
64
+ try:
65
+ api.quota(prof.creds)
66
+ out.append(Check(f"account {name}", True, prof.username))
67
+ except api.KaggleError as e:
68
+ out.append(Check(f"account {name}", False, str(e)[:60], f"kdev account add {name}"))
69
+ if cfg.notebook and cfg.profiles:
70
+ try:
71
+ meta = api.get_kernel(cfg.profile().creds, cfg.notebook)
72
+ out.append(Check("workspace", bool(meta), cfg.notebook))
73
+ except api.KaggleError as e:
74
+ out.append(
75
+ Check("workspace", False, f"{cfg.notebook}: {str(e)[:50]}", "kdev workspace join")
76
+ )
77
+ else:
78
+ out.append(Check("workspace", False, "none", "kdev workspace join"))
79
+ if cfg.tunnel_credentials or cfg.tunnel_token:
80
+ detail = cfg.tunnel_hostname or "token"
81
+ good = True
82
+ if cfg.tunnel_credentials and cftunnel.logged_in():
83
+ rec = cftunnel.dns_record(cfg.tunnel_hostname)
84
+ good = bool(rec and rec.get("content") == cftunnel.tunnel_target(cfg.tunnel_id))
85
+ detail += "" if good else " (DNS does not point at it)"
86
+ out.append(Check("tunnel", good, detail, "kdev tunnel setup --force"))
87
+ else:
88
+ out.append(Check("tunnel", True, "quick (new hostname each session)", warn_only=True))
89
+ out.append(
90
+ Check(
91
+ "ssh config",
92
+ True,
93
+ f"{cfg.ssh_host_alias} "
94
+ + ("present" if sshcfg.has_block() else "written on the next kdev up"),
95
+ )
96
+ )
97
+ return out
98
+
99
+
100
+ def doctor(
101
+ fix: bool = typer.Option(False, "--fix", help="Install what is missing where kdev can."),
102
+ upgrade: bool = typer.Option(
103
+ False, "--upgrade-cloudflared", help="Re-download kdev's cloudflared."
104
+ ),
105
+ ) -> None:
106
+ """Check everything kdev depends on. Exits 1 if something is broken."""
107
+ cfg = config.load()
108
+ if upgrade or (fix and not cloudflared.find()):
109
+ path = ui.download("cloudflared", cloudflared.install)
110
+ ui.ok(f"installed cloudflared {cloudflared.version(path)}")
111
+ with ui.spinner("checking…"):
112
+ checks = _checks(cfg)
113
+ width = max(len(c.name) for c in checks)
114
+ for c in checks:
115
+ label = f"{c.name:<{width}} {c.detail}"
116
+ if c.ok and not (c.warn_only and "quick" in c.detail):
117
+ ui.ok(label)
118
+ elif c.warn_only:
119
+ ui.warn(label)
120
+ else:
121
+ ui.err(label, c.fix)
122
+ broken = [c for c in checks if not c.ok and not c.warn_only]
123
+ ui.blank()
124
+ if broken:
125
+ ui.err(f"{len(broken)} problem(s)")
126
+ raise typer.Exit(1)
127
+ ui.ok("everything kdev needs is in place")
kdev/commands/home.py ADDED
@@ -0,0 +1,92 @@
1
+ """Bare `kdev`: everything at a glance, then the next thing to do."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import typer
6
+ from rich.text import Text
7
+
8
+ from .. import api, config, quota, session, ui, wizard
9
+ from ._common import box_label
10
+
11
+
12
+ def setup() -> None:
13
+ """Get this machine ready. Safe to re-run: only does what is missing."""
14
+ session.ssh_ready(wizard.run())
15
+
16
+
17
+ def overview(run_command) -> None:
18
+ """The home screen. `run_command(argv)` dispatches a picked action."""
19
+ cfg = config.load()
20
+ if not cfg.profiles:
21
+ if ui.interactive():
22
+ wizard.run()
23
+ return
24
+ ui.err("kdev is not set up on this machine.", "Run `kdev setup` in a terminal.")
25
+ raise typer.Exit(1)
26
+
27
+ ui.header()
28
+ with ui.spinner("looking around…"):
29
+ state = {}
30
+ if cfg.notebook:
31
+ try:
32
+ state = api.session_status(cfg.profile().creds, cfg.notebook)
33
+ except api.KaggleError:
34
+ state = {}
35
+ running = state.get("status") in api.LIVE_STATES
36
+ reachable = running and session.reachable(cfg.ssh_host_alias)
37
+ rows = quota.rows(cfg)
38
+
39
+ box = Text.assemble(ui.live_dot(running), " ")
40
+ if not cfg.notebook:
41
+ box.append("no workspace yet", style="kdev.warn")
42
+ elif running:
43
+ box.append("running", style="bold kdev.ok")
44
+ box.append(" reachable" if reachable else " starting or unreachable", style="kdev.muted")
45
+ else:
46
+ box.append("stopped", style="bold")
47
+ box.append(f" files saved in {cfg.notebook}", style="kdev.muted")
48
+ ui.console.print(
49
+ ui.facts(
50
+ [
51
+ ("box", box),
52
+ ("workspace", cfg.notebook or "—"),
53
+ ("next box", box_label(cfg)),
54
+ ("tunnel", cfg.tunnel_hostname or "quick"),
55
+ ]
56
+ )
57
+ )
58
+ ui.blank()
59
+ ui.console.print(ui.accounts_table(rows))
60
+ ui.blank()
61
+
62
+ if not ui.interactive():
63
+ steps = (
64
+ [("kdev ssh", "open a shell"), ("kdev down", "stop it")]
65
+ if running
66
+ else [("kdev up", "start the box"), ("kdev account", "accounts and quota")]
67
+ )
68
+ ui.next_steps(steps)
69
+ return
70
+
71
+ actions: list[tuple[str, str]] = []
72
+ if not cfg.notebook:
73
+ actions.append(("workspace join", "Join your group's workspace"))
74
+ elif running:
75
+ actions += [
76
+ ("up --open code", "Open it in VS Code"),
77
+ ("ssh", "Open a shell"),
78
+ ("status", "Details"),
79
+ ("down", "Stop it"),
80
+ ]
81
+ else:
82
+ actions += [
83
+ ("up", f"Start the box {box_label(cfg)}"),
84
+ ("up --gpu none --hours 2", "Start a CPU box uses no GPU quota"),
85
+ ]
86
+ if len(cfg.profiles) > 1:
87
+ actions.append(("account use", "Switch account"))
88
+ actions += [("account add", "Add an account"), ("doctor", "Check setup"), ("", "Quit")]
89
+ picked = ui.choose("What now?", [(a, label) for a, label in actions])
90
+ if picked:
91
+ ui.blank()
92
+ run_command(picked.split())
@@ -0,0 +1,164 @@
1
+ """`kdev config`: see and change this machine's settings, one at a time."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ from collections.abc import Callable
7
+ from dataclasses import dataclass
8
+ from pathlib import Path
9
+
10
+ import typer
11
+ from rich.text import Text
12
+
13
+ from .. import api, config, tunnel, ui
14
+ from ..errors import KdevError
15
+ from ._common import cap_for
16
+
17
+ app = typer.Typer(
18
+ help="See and change settings.", no_args_is_help=False, invoke_without_command=True
19
+ )
20
+
21
+
22
+ def _gpu(value: str, _cfg: config.Config) -> str:
23
+ if value not in api.SHAPES:
24
+ raise KdevError(f"gpu must be one of: {', '.join(api.SHAPES)}.")
25
+ return value
26
+
27
+
28
+ def _hours(value: str, cfg: config.Config) -> float:
29
+ try:
30
+ hours = float(value)
31
+ except ValueError as e:
32
+ raise KdevError(f"{value!r} is not a number of hours.") from e
33
+ cap = cap_for(cfg.default_gpu)
34
+ if not 0 < hours <= cap:
35
+ raise KdevError(f"hours must be more than 0 and at most {cap:g}.")
36
+ return hours
37
+
38
+
39
+ def _file(kind: str) -> Callable[[str, config.Config], str]:
40
+ def read(value: str, _cfg: config.Config) -> str:
41
+ path = Path(value).expanduser()
42
+ if not path.is_file():
43
+ raise KdevError(f"{value} is not a file.", f"Pass the path to the {kind}.")
44
+ return path.read_text().strip()
45
+
46
+ return read
47
+
48
+
49
+ def _public_key(value: str, cfg: config.Config) -> str:
50
+ key = _file("public key (.pub)")(value, cfg)
51
+ if not re.match(r"(ssh-|ecdsa-|sk-)", key):
52
+ raise KdevError(
53
+ f"{value} does not look like an SSH public key.",
54
+ "It should be the .pub file, e.g. ~/.ssh/id_ed25519.pub",
55
+ )
56
+ return key
57
+
58
+
59
+ def _alias(value: str, _cfg: config.Config) -> str:
60
+ if not re.fullmatch(r"[A-Za-z0-9][A-Za-z0-9._-]*", value):
61
+ raise KdevError("ssh-alias is a single word: letters, digits, dot, dash, underscore.")
62
+ return value
63
+
64
+
65
+ def _hostname(value: str, _cfg: config.Config) -> str:
66
+ try:
67
+ return tunnel.validate_hostname(value)
68
+ except tunnel.TunnelError as e:
69
+ raise KdevError(str(e)) from e
70
+
71
+
72
+ def _text(value: str, _cfg: config.Config) -> str:
73
+ return value.strip()
74
+
75
+
76
+ @dataclass(frozen=True)
77
+ class Setting:
78
+ attr: str
79
+ parse: Callable[[str, config.Config], object]
80
+ help: str
81
+ secret: bool = False
82
+
83
+
84
+ SETTINGS: dict[str, Setting] = {
85
+ "gpu": Setting("default_gpu", _gpu, "accelerator for kdev up"),
86
+ "hours": Setting("default_hours", _hours, "session length for kdev up"),
87
+ "git-remote": Setting("git_remote", _text, "git repo synced with the box"),
88
+ "git-deploy-key": Setting(
89
+ "git_deploy_key", _file("private deploy key"), "its deploy key (a file path)", secret=True
90
+ ),
91
+ "ssh-key": Setting("ssh_public_key", _public_key, "this machine's key (.pub path)"),
92
+ "ssh-alias": Setting("ssh_host_alias", _alias, "name in ~/.ssh/config"),
93
+ "tunnel-hostname": Setting("tunnel_hostname", _hostname, "the box's hostname"),
94
+ "tunnel-token": Setting("tunnel_token", _text, "dashboard tunnel token", secret=True),
95
+ }
96
+
97
+
98
+ def _shown(cfg: config.Config, key: str) -> Text:
99
+ s = SETTINGS[key]
100
+ value = getattr(cfg, s.attr)
101
+ if value in ("", 0, 0.0, None):
102
+ return Text("not set", style="kdev.muted")
103
+ if s.secret:
104
+ return Text("set (hidden)", style="kdev.muted")
105
+ if key == "ssh-key":
106
+ return Text(str(value).split()[-1] if " " in str(value) else "set")
107
+ if key == "hours":
108
+ return Text(f"{value:g}h")
109
+ if key == "gpu":
110
+ return Text(ui.gpu_label(value))
111
+ return Text(str(value))
112
+
113
+
114
+ def _key(name: str) -> Setting:
115
+ if name not in SETTINGS:
116
+ raise KdevError(f"No setting called {name!r}.", "Settings: " + ", ".join(SETTINGS))
117
+ return SETTINGS[name]
118
+
119
+
120
+ @app.callback()
121
+ def main(ctx: typer.Context) -> None:
122
+ """Show every setting."""
123
+ if ctx.invoked_subcommand:
124
+ return
125
+ cfg = config.load()
126
+ t = ui.table(
127
+ [("setting", "left"), ("value", "left"), ("what it is", "left")],
128
+ [
129
+ (Text(k, style="bold"), _shown(cfg, k), Text(s.help, style="kdev.muted"))
130
+ for k, s in SETTINGS.items()
131
+ ],
132
+ )
133
+ ui.console.print(t)
134
+ ui.blank()
135
+ ui.hint(f"stored in {config.CONFIG_PATH}")
136
+ ui.next_steps(
137
+ [
138
+ ("kdev config set <setting> <value>", "change one"),
139
+ ("kdev config unset <setting>", "back to the default"),
140
+ ]
141
+ )
142
+
143
+
144
+ @app.command("set")
145
+ def set_(
146
+ name: str = typer.Argument(..., help="The setting, e.g. gpu."),
147
+ value: str = typer.Argument(..., help="Its new value (a path, for keys)."),
148
+ ) -> None:
149
+ """Change one setting."""
150
+ cfg = config.load()
151
+ setting = _key(name)
152
+ setattr(cfg, setting.attr, setting.parse(value, cfg))
153
+ config.save(cfg)
154
+ ui.ok(Text.assemble((name, "bold"), " is now ", _shown(cfg, name)))
155
+
156
+
157
+ @app.command("unset")
158
+ def unset(name: str = typer.Argument(..., help="The setting to reset.")) -> None:
159
+ """Put one setting back to its default."""
160
+ cfg = config.load()
161
+ setting = _key(name)
162
+ setattr(cfg, setting.attr, config.Config.__dataclass_fields__[setting.attr].default)
163
+ config.save(cfg)
164
+ ui.ok(Text.assemble((name, "bold"), " is back to ", _shown(cfg, name)))
@@ -0,0 +1,78 @@
1
+ """`kdev tunnel`: the Cloudflare tunnel that makes the box reachable."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import typer
6
+
7
+ from .. import config, ui, wizard
8
+ from .. import tunnel as cf
9
+
10
+ app = typer.Typer(
11
+ help="The Cloudflare tunnel the box is reached through.",
12
+ no_args_is_help=False,
13
+ invoke_without_command=True,
14
+ )
15
+
16
+
17
+ @app.callback()
18
+ def main(ctx: typer.Context) -> None:
19
+ """How the box is reached, and whether its DNS points the right way."""
20
+ if ctx.invoked_subcommand:
21
+ return
22
+ cfg = config.load()
23
+ from rich.text import Text
24
+
25
+ if cfg.tunnel_credentials:
26
+ rows: list[tuple[str, object]] = [
27
+ ("kind", "named, managed by kdev"),
28
+ ("name", cfg.tunnel_name),
29
+ ("host", Text(cfg.tunnel_hostname, style="bold")),
30
+ ]
31
+ fix = ""
32
+ if not cf.logged_in():
33
+ rows.append(
34
+ (
35
+ "dns",
36
+ Text("not checked: no Cloudflare login on this machine", style="kdev.muted"),
37
+ )
38
+ )
39
+ else:
40
+ with ui.spinner("checking DNS…"):
41
+ rec = cf.dns_record(cfg.tunnel_hostname)
42
+ want = cf.tunnel_target(cfg.tunnel_id)
43
+ if rec and rec.get("content") == want:
44
+ rows.append(("dns", Text(f"{ui.g('ok')} points at this tunnel", style="kdev.ok")))
45
+ else:
46
+ where = f"points at {rec.get('content')}" if rec else "has no record"
47
+ rows.append(("dns", Text(f"{ui.g('err')} {where}", style="kdev.err")))
48
+ fix = "kdev tunnel setup --force"
49
+ ui.console.print(ui.facts(rows))
50
+ if fix:
51
+ ui.blank()
52
+ ui.next_steps([(fix, "point the hostname back at the tunnel")])
53
+ elif cfg.tunnel_token:
54
+ ui.console.print(
55
+ ui.facts(
56
+ [
57
+ ("kind", "managed in the Cloudflare dashboard"),
58
+ ("host", cfg.tunnel_hostname or Text("not set", style="kdev.warn")),
59
+ ]
60
+ )
61
+ )
62
+ else:
63
+ ui.console.print(
64
+ ui.facts([("kind", Text("quick: a new hostname every session", style="kdev.warn"))])
65
+ )
66
+ ui.blank()
67
+ ui.next_steps([("kdev tunnel setup", "one hostname that survives drops")])
68
+
69
+
70
+ @app.command("setup")
71
+ def setup(
72
+ hostname: str = typer.Option("", "--hostname", help="Full hostname, instead of <name>.<zone>."),
73
+ force: bool = typer.Option(False, "--force", help="Rebuild even if it already works."),
74
+ ) -> None:
75
+ """Create the named tunnel and its DNS record (opens a browser once)."""
76
+ cfg = config.load()
77
+ if wizard.setup_named_tunnel(cfg, hostname=hostname, force=force):
78
+ config.save(cfg)
@@ -0,0 +1,210 @@
1
+ """The shared workspace: one notebook, shared with your Kaggle group."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+
7
+ import typer
8
+ from rich.text import Text
9
+
10
+ from .. import api, config, kdev_box, persistence, ui
11
+ from .. import notebook as nb
12
+ from ..errors import KdevError
13
+ from ._common import need_notebook
14
+
15
+ app = typer.Typer(
16
+ help="The shared notebook every account in your group runs.",
17
+ no_args_is_help=False,
18
+ invoke_without_command=True,
19
+ )
20
+
21
+
22
+ def _adopt(cfg: config.Config, creds: api.Creds) -> None:
23
+ taken = nb.adopt_config(cfg, creds)
24
+ if any(k.startswith("tunnel_") for k in taken):
25
+ ui.ok(f"tunnel {cfg.tunnel_hostname or '(token)'}, taken from the notebook")
26
+ if "git_remote" in taken:
27
+ ui.ok(f"git remote {cfg.git_remote}, taken from the notebook")
28
+
29
+
30
+ @app.callback()
31
+ def main(
32
+ ctx: typer.Context,
33
+ account: str = typer.Option("", "--account", "-a", help="Look as this account."),
34
+ ) -> None:
35
+ """The notebook, its group and sharing, members and saved versions."""
36
+ if ctx.invoked_subcommand:
37
+ return
38
+ cfg = config.load()
39
+ target = need_notebook(cfg)
40
+ with ui.spinner("reading the workspace…"):
41
+ info = nb.describe(cfg, cfg.profile(account).creds)
42
+ role = info["role"]
43
+ sharing: object
44
+ if not cfg.group_slug:
45
+ sharing = Text("not shared with a group", style="kdev.warn")
46
+ elif role == api.ROLE_EDITOR:
47
+ sharing = Text(f"{cfg.group_slug} can edit", style="kdev.ok")
48
+ elif role:
49
+ sharing = Text(
50
+ f"{cfg.group_slug} is {role.rsplit('_', 1)[-1].lower()} only; members cannot run it",
51
+ style="kdev.warn",
52
+ )
53
+ else:
54
+ sharing = Text(f"{cfg.group_slug} is not on the notebook", style="kdev.err")
55
+ files = info["files"]
56
+ rows: list[tuple[str, object]] = [
57
+ ("notebook", Text(target, style="bold")),
58
+ ("owner", target.split("/")[0]),
59
+ ("sharing", sharing),
60
+ ]
61
+ if info["members"]:
62
+ rows.append(("members", ", ".join(info["members"])))
63
+ rows.append(("saved", f"{info['saved']}, {files} file(s)" if info["saved"] else "nothing yet"))
64
+ rows.append(("url", Text(nb.notebook_url(target), style="kdev.muted")))
65
+ ui.card(Text("workspace", style="bold"), rows)
66
+ for e in info["errors"]:
67
+ ui.warn(e)
68
+ fixes = []
69
+ if cfg.group_slug and role != api.ROLE_EDITOR:
70
+ fixes.append(("kdev workspace share", "give the group Can Edit again"))
71
+ fixes.append(("kdev workspace files", "what the latest saved version holds"))
72
+ ui.next_steps(fixes)
73
+
74
+
75
+ @app.command("files")
76
+ def files(
77
+ version: str = typer.Option(
78
+ "", "--version", help="A saved version, e.g. v12 (default: the newest with files)."
79
+ ),
80
+ account: str = typer.Option("", "--account", "-a", help="Read as this account."),
81
+ as_json: bool = typer.Option(False, "--json", help="Print JSON instead."),
82
+ ) -> None:
83
+ """What a saved version holds -- what `kdev up` restores."""
84
+ cfg = config.load()
85
+ target = need_notebook(cfg)
86
+ if version and not re.fullmatch(r"v\d+", version):
87
+ raise KdevError("--version takes a label like v12.")
88
+ creds = cfg.profile(account).creds
89
+ with ui.spinner("reading the saved version…"):
90
+ try:
91
+ if version:
92
+ listing = [f["name"] for f in api.session_output(creds, target, version)]
93
+ else:
94
+ # The newest version with files: a running box's own has none.
95
+ latest = int(api.get_kernel(creds, target).get("currentVersionNumber") or 0)
96
+ version, found = persistence.newest_saved(creds, target, latest)
97
+ listing = list(found)
98
+ except api.KaggleError as e:
99
+ raise KdevError(
100
+ f"Could not read {version or 'the saved files'} of {target}.", str(e)
101
+ ) from e
102
+ # Exactly what a restore would bring back: not kdev's own state, not the
103
+ # box's runtime files, not a download a killed restore left half-done.
104
+ names = sorted(
105
+ name
106
+ for name in listing
107
+ if not name.startswith(".kdev/")
108
+ and name not in kdev_box.RUNTIME
109
+ and not name.endswith(kdev_box.PART)
110
+ )
111
+ if as_json:
112
+ ui.emit_json({"notebook": target, "version": version or None, "files": names})
113
+ return
114
+ ui.console.print(
115
+ Text.assemble((target, "bold"), (" " + (version or "no saved version"), "kdev.muted"))
116
+ )
117
+ for name in names[:50]:
118
+ ui.hint(name)
119
+ if len(names) > 50:
120
+ ui.hint(f"… and {len(names) - 50} more (--json lists them all)")
121
+ if not names:
122
+ ui.hint("nothing saved yet")
123
+
124
+
125
+ @app.command("join")
126
+ def join(account: str = typer.Option("", "--account", "-a", help="Look as this account.")) -> None:
127
+ """Pick your group's notebook from the ones shared with you."""
128
+ if not ui.interactive():
129
+ raise KdevError(
130
+ "join asks which notebook to use.",
131
+ "Without a terminal, name it: kdev workspace use owner/slug",
132
+ )
133
+ cfg = config.load()
134
+ prof = cfg.profile(account)
135
+ if nb.join(cfg, prof.creds, prof.username):
136
+ _adopt(cfg, prof.creds)
137
+ config.save(cfg)
138
+ ui.blank()
139
+ ui.next_steps([("kdev up", "start the box")])
140
+
141
+
142
+ @app.command("use")
143
+ def use(
144
+ ref: str = typer.Argument(..., help="owner/notebook-slug"),
145
+ account: str = typer.Option("", "--account", "-a", help="Look as this account."),
146
+ ) -> None:
147
+ """Point this machine at a notebook by name."""
148
+ cfg = config.load()
149
+ creds = cfg.profile(account).creds
150
+ with ui.spinner("opening the notebook…"):
151
+ nb.use(cfg, creds, ref)
152
+ ui.ok(
153
+ f"workspace {cfg.notebook}"
154
+ + (f", shared with {cfg.group_slug}" if cfg.group_slug else ", not shared with a group")
155
+ )
156
+ _adopt(cfg, creds)
157
+ config.save(cfg)
158
+
159
+
160
+ @app.command("create")
161
+ def create(
162
+ group: str = typer.Option("", "--group", "-g", help="Your Kaggle group's slug."),
163
+ name: str = typer.Option("kdev-box", "--name", help="Title of the new notebook."),
164
+ account: str = typer.Option("", "--account", "-a", help="The account that will own it."),
165
+ ) -> None:
166
+ """Make a new notebook and share it with your group."""
167
+ cfg = config.load()
168
+ if not group:
169
+ if not ui.interactive():
170
+ raise KdevError("Name the group.", "kdev workspace create --group <slug>")
171
+ ui.hint("A group's slug is the end of its address: kaggle.com/groups/<slug>")
172
+ group = ui.ask("Group slug")
173
+ prof = cfg.profile(account)
174
+ with ui.spinner("creating the notebook…"):
175
+ slug, kid = nb.create(prof.creds, prof.username, name)
176
+ ui.ok(f"created {slug}")
177
+ with ui.spinner(f"sharing with {group}…"):
178
+ nb.share(prof.creds, kid, group)
179
+ ui.ok(f"shared with {group} (Can Edit)")
180
+ cfg.notebook, cfg.notebook_id, cfg.group_slug = slug, kid, group
181
+ config.save(cfg)
182
+ ui.blank()
183
+ ui.next_steps(
184
+ [
185
+ ("kdev up", "start the box"),
186
+ ("kdev workspace join", "what everyone else in the group runs"),
187
+ ]
188
+ )
189
+
190
+
191
+ @app.command("share")
192
+ def share(
193
+ group: str = typer.Option("", "--group", "-g", help="Group slug (default: the workspace's)."),
194
+ account: str = typer.Option("", "--account", "-a", help="The account that owns it."),
195
+ ) -> None:
196
+ """Give the group Can Edit on the notebook again."""
197
+ cfg = config.load()
198
+ target = need_notebook(cfg)
199
+ slug = group or cfg.group_slug
200
+ if not slug:
201
+ raise KdevError("Which group?", "kdev workspace share --group <slug>")
202
+ creds = cfg.profile(account).creds
203
+ kid = cfg.notebook_id or api.kernel_id(creds, target)
204
+ if not kid:
205
+ raise KdevError(f"Could not open {target}.")
206
+ with ui.spinner(f"sharing with {slug}…"):
207
+ nb.share(creds, kid, slug)
208
+ cfg.group_slug, cfg.notebook_id = slug, kid
209
+ config.save(cfg)
210
+ ui.ok(f"{target} is shared with {slug} (Can Edit)")