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.
kdev/config.py ADDED
@@ -0,0 +1,181 @@
1
+ """Settings and accounts, stored at ~/.config/kdev/config.json (mode 0600).
2
+
3
+ One entry per Kaggle account. Every account in the group runs the same shared
4
+ notebook on its own weekly quota; which one runs a given session is the only
5
+ thing that changes when you switch.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import json
11
+ import os
12
+ import re
13
+ from dataclasses import asdict, dataclass, field
14
+ from pathlib import Path
15
+
16
+ from .api import Creds
17
+ from .errors import KdevError
18
+
19
+ CONFIG_DIR = Path(os.environ.get("KDEV_CONFIG_DIR", Path.home() / ".config" / "kdev"))
20
+ CONFIG_PATH = CONFIG_DIR / "config.json"
21
+
22
+ #: Bumped when the stored shape changes in a way `load` has to migrate.
23
+ SCHEMA = 1
24
+
25
+
26
+ @dataclass
27
+ class Profile:
28
+ """One Kaggle account. Its OAuth token lives in creds/<name>.json."""
29
+
30
+ username: str
31
+ #: Set by Config so `creds` can find this account's token file.
32
+ name: str = ""
33
+
34
+ @property
35
+ def creds(self) -> Creds:
36
+ from . import auth
37
+
38
+ token = ""
39
+ if self.name and auth.signed_in(self.name):
40
+ try:
41
+ token = auth.access_token(self.name)
42
+ except auth.AuthError:
43
+ token = ""
44
+ return Creds(self.username, token)
45
+
46
+
47
+ @dataclass
48
+ class Config:
49
+ schema: int = SCHEMA
50
+ profiles: dict[str, Profile] = field(default_factory=dict)
51
+ active: str = ""
52
+ #: The one notebook the whole group runs, as "owner/slug". It belongs to the
53
+ #: workspace, not to any account: every member runs it on their own quota.
54
+ notebook: str = ""
55
+ #: Its numeric id, needed for sharing (IAM) calls.
56
+ notebook_id: int = 0
57
+ #: The Kaggle group it is shared with.
58
+ group_slug: str = ""
59
+ #: Public hostname of the Cloudflare named tunnel, e.g. kaggle.example.com.
60
+ tunnel_hostname: str = ""
61
+ #: Connector token, for a tunnel whose ingress lives in the CF dashboard.
62
+ tunnel_token: str = ""
63
+ #: Credentials JSON for a locally-managed tunnel -- what `kdev tunnel
64
+ #: setup` makes, because it needs no dashboard provisioning.
65
+ tunnel_credentials: str = ""
66
+ tunnel_id: str = ""
67
+ #: Tunnel name as it appears in `cloudflared tunnel list`.
68
+ tunnel_name: str = "kdev"
69
+ #: This machine's SSH public key, authorised on every box it starts.
70
+ ssh_public_key: str = ""
71
+ #: Host alias written into ~/.ssh/config.
72
+ ssh_host_alias: str = "kaggle"
73
+ #: The box `kdev up` starts when no flags say otherwise.
74
+ default_gpu: str = ""
75
+ default_hours: float = 0.0
76
+ #: Optional git remote: cloned on boot, pushed every 15 min and on teardown.
77
+ git_remote: str = ""
78
+ #: Private deploy key (contents) for that remote.
79
+ git_deploy_key: str = ""
80
+
81
+ def add(self, name: str, prof: Profile) -> Profile:
82
+ prof.name = name
83
+ self.profiles[name] = prof
84
+ self.active = self.active or name
85
+ return prof
86
+
87
+ def profile(self, name: str = "") -> Profile:
88
+ name = name or self.active
89
+ if not name:
90
+ raise KdevError("No account yet.", "Sign one in: kdev account add")
91
+ if name not in self.profiles:
92
+ have = ", ".join(self.profiles) or "none"
93
+ raise KdevError(
94
+ f"No account called {name!r} (have: {have}).", "See them with: kdev account"
95
+ )
96
+ prof = self.profiles[name]
97
+ prof.name = name
98
+ return prof
99
+
100
+
101
+ def load() -> Config:
102
+ if not CONFIG_PATH.exists():
103
+ return Config()
104
+ try:
105
+ raw = json.loads(CONFIG_PATH.read_text())
106
+ except (OSError, ValueError) as e:
107
+ raise KdevError(
108
+ f"{CONFIG_PATH} is unreadable ({e}).", "Move it aside and run: kdev setup"
109
+ ) from e
110
+ if not isinstance(raw, dict):
111
+ raise KdevError(f"{CONFIG_PATH} is not a kdev config.", "Move it aside and run: kdev setup")
112
+ # `name` is what Profile.creds uses to find the token file, so it is
113
+ # stamped here: callers that iterate `cfg.profiles` directly would
114
+ # otherwise authenticate as nobody and get a 401.
115
+ raw["profiles"] = {
116
+ k: Profile(
117
+ **{
118
+ **{f: v for f, v in (v or {}).items() if f in Profile.__dataclass_fields__},
119
+ "name": k,
120
+ }
121
+ )
122
+ for k, v in (raw.get("profiles") or {}).items()
123
+ }
124
+ # Unknown keys are dropped rather than rejected, so a config written by an
125
+ # older kdev (API keys, per-account notebooks) still loads.
126
+ cfg = Config(**{k: v for k, v in raw.items() if k in Config.__dataclass_fields__})
127
+ cfg.schema = SCHEMA
128
+ if cfg.active and cfg.active not in cfg.profiles:
129
+ cfg.active = next(iter(cfg.profiles), "")
130
+ return cfg
131
+
132
+
133
+ def save(cfg: Config) -> None:
134
+ # A Config with no accounts is almost always half-built (a test, a script),
135
+ # and writing it over a working one would destroy every account, the
136
+ # notebook and the tunnel at once. Refuse unless there is nothing to lose.
137
+ if not cfg.profiles and CONFIG_PATH.exists():
138
+ try:
139
+ existing = json.loads(CONFIG_PATH.read_text() or "{}")
140
+ except ValueError:
141
+ existing = {}
142
+ if existing.get("profiles"):
143
+ raise ValueError(
144
+ f"refusing to overwrite {CONFIG_PATH} with a config that has no "
145
+ "accounts; set KDEV_CONFIG_DIR to write somewhere else"
146
+ )
147
+ CONFIG_DIR.mkdir(parents=True, exist_ok=True)
148
+ data = asdict(cfg)
149
+ for prof in data["profiles"].values():
150
+ prof.pop("name", None) # derived from the key; storing it invites drift
151
+ # Written through a temp file and renamed: a crash mid-write must never
152
+ # leave a truncated config behind. Created 0600 before any secret lands.
153
+ tmp = CONFIG_PATH.with_name(CONFIG_PATH.name + ".tmp")
154
+ fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
155
+ with os.fdopen(fd, "w") as f:
156
+ json.dump(data, f, indent=2)
157
+ tmp.replace(CONFIG_PATH)
158
+
159
+
160
+ #: Account names become file names under ~/.config/kdev/creds/, so they cannot
161
+ #: be arbitrary text -- an unvalidated prompt once stored a whole Rich panel
162
+ #: border as one.
163
+ PROFILE_NAME_RE = re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]{0,31}$")
164
+
165
+
166
+ def valid_profile_name(name: str) -> str:
167
+ name = (name or "").strip()
168
+ if not PROFILE_NAME_RE.fullmatch(name):
169
+ raise ValueError(
170
+ "Account names are 1-32 letters, digits, dots, dashes or underscores, "
171
+ "starting with a letter or digit."
172
+ )
173
+ return name
174
+
175
+
176
+ def default_ssh_public_key() -> str:
177
+ for name in ("id_ed25519.pub", "id_ecdsa.pub", "id_rsa.pub"):
178
+ p = Path.home() / ".ssh" / name
179
+ if p.exists():
180
+ return p.read_text().strip()
181
+ return ""
kdev/errors.py ADDED
@@ -0,0 +1,25 @@
1
+ """One error type for everything a user can act on.
2
+
3
+ Every failure kdev can explain carries a message (what went wrong, one line)
4
+ and optionally a hint (what to run about it). `cli.main` renders all of them
5
+ the same way and exits 1 -- so no command prints a raw traceback, a bare
6
+ `SystemExit` string, or a JSON blob from an API.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+
12
+ class KdevError(Exception):
13
+ """A failure the user can do something about."""
14
+
15
+ def __init__(self, message: str, hint: str = "") -> None:
16
+ super().__init__(message)
17
+ self.message = message
18
+ self.hint = hint
19
+
20
+
21
+ class Cancelled(KdevError):
22
+ """The user backed out of a prompt. Not an error; exits quietly."""
23
+
24
+ def __init__(self) -> None:
25
+ super().__init__("cancelled")