aboutsummaryrefslogtreecommitdiffstats
path: root/nbs/config.ipynb
blob: 9b0be3497d08d643246ff784797e670251c4cf75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0b47680",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "989dbd8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| default_exp: config"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "983c3e1a",
   "metadata": {},
   "source": [
    "# Config\n",
    "\n",
    "> Reading configuration for `nb`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5006bc03",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "import dataclasses\n",
    "import os\n",
    "import tomllib\n",
    "from pathlib import Path\n",
    "\n",
    "from nb.core import patch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "645d4349",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tempfile import TemporaryDirectory\n",
    "from nb import test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c282bbbc",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "def config_path(curdir:str|Path='.')->Path|None:\n",
    "    \"Find the pyproject.toml configuration file by traversing dirs until the root of the file system.\"\n",
    "    path = os.path.abspath(curdir)\n",
    "    while path != os.path.dirname(path):\n",
    "        if 'pyproject.toml' in os.listdir(path):\n",
    "            return Path(path + '/pyproject.toml')\n",
    "        path = os.path.dirname(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c5c3460",
   "metadata": {},
   "outputs": [],
   "source": [
    "with TemporaryDirectory() as tmp:\n",
    "    test.equal(config_path(tmp), None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7363f5ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "with TemporaryDirectory() as tmp:\n",
    "    p = Path(tmp)/'pyproject.toml'\n",
    "    p.touch()\n",
    "    test.equal(config_path(tmp), p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89f6f072",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "def config_dir(curdir:str|Path='.')->Path|None:\n",
    "    return p.parent if config_path(curdir) is not None else None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73f36540",
   "metadata": {},
   "outputs": [],
   "source": [
    "with TemporaryDirectory() as tmp:\n",
    "    p = Path(tmp)/'pyproject.toml'\n",
    "    p.touch()\n",
    "    test.equal(config_dir(tmp), Path(tmp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0948469f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "@dataclasses.dataclass\n",
    "class Config:\n",
    "    nb_dir:str|Path|None\n",
    "    export_dir:str|Path|None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "311895a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "def config_defaults(base_dir:Path)->dict:\n",
    "    return {'nb_dir': base_dir/Path('nbs'), 'export_dir': base_dir/Path('.')}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2c35e58",
   "metadata": {},
   "outputs": [],
   "source": [
    "with TemporaryDirectory() as tmp:\n",
    "    test.equal(config_defaults(Path(tmp)), {'nb_dir': Path(tmp)/Path('nbs'), 'export_dir': Path(tmp)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b811084",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "@patch\n",
    "def default(cls:Config, base_dir:Path)->Config:\n",
    "    return cls(**config_defaults(base_dir))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "667948b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "@patch\n",
    "def empty(cls:Config)->Config:\n",
    "    return cls(**{f.name:None for f in dataclasses.fields(cls)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97fc77c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "empty = Config.empty()\n",
    "test.equal(empty, Config(nb_dir=None, export_dir=None))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d95dafba",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "@patch\n",
    "def from_dict(cls:Config, d:dict, base_dir:Path)->Config:\n",
    "    d = config_defaults(base_dir) | d\n",
    "    fields = {f.name for f in dataclasses.fields(cls)}\n",
    "    extra = d.keys() - fields\n",
    "    if extra: raise ValueError(f\"Unknown config keys: {extra}\")\n",
    "    return cls(**d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2217ca78",
   "metadata": {},
   "outputs": [],
   "source": [
    "with test.raises(exc=ValueError, contains=\"Unknown config keys: {'abc'}\"):\n",
    "    Config.from_dict({'abc': 123}, base_dir=Path('.'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed87dda5",
   "metadata": {},
   "outputs": [],
   "source": [
    "test.equal(Config.from_dict({}, base_dir=Path('.')), Config.default(base_dir=Path('.')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b2800d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "_d = {'nb_dir': 'abc'}\n",
    "test.equal(Config.from_dict(_d, base_dir=Path('.')), Config(**(config_defaults(Path('.')) | _d)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00ceaa67",
   "metadata": {},
   "outputs": [],
   "source": [
    "#| export\n",
    "def get_config(curdir:str|Path='.')->Config:\n",
    "    p = config_path(curdir)\n",
    "    if not p: return Config.empty()\n",
    "    nb = tomllib.loads(p.read_text()).get('tool', {}).get('nb', {})\n",
    "    return Config.from_dict(nb, base_dir=p.parent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bee6bc66",
   "metadata": {},
   "outputs": [],
   "source": [
    "with TemporaryDirectory() as tmp:\n",
    "    test.equal(get_config(tmp), Config.empty())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0084313",
   "metadata": {},
   "outputs": [],
   "source": [
    "with TemporaryDirectory() as tmp:\n",
    "    p = Path(tmp)/'pyproject.toml'\n",
    "    p.touch()\n",
    "    test.equal(get_config(tmp), Config.default(tmp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "502bec22",
   "metadata": {},
   "outputs": [],
   "source": [
    "with TemporaryDirectory() as tmp:\n",
    "    p = Path(tmp)/'pyproject.toml'\n",
    "    p.write_text('[tool.nb]')\n",
    "    test.equal(get_config(tmp), Config.default(tmp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a47c918",
   "metadata": {},
   "outputs": [],
   "source": [
    "with (\n",
    "    test.raises(exc=ValueError, contains=\"Unknown config keys: {'dir'}\"),\n",
    "    TemporaryDirectory() as tmp\n",
    "):\n",
    "    p = Path(tmp)/'pyproject.toml'\n",
    "    p.write_text('[tool.nb]\\ndir=1')\n",
    "    get_config(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "552b4e70",
   "metadata": {},
   "outputs": [],
   "source": [
    "with TemporaryDirectory() as tmp:\n",
    "    p = Path(tmp)/'pyproject.toml'\n",
    "    p.write_text('[tool.nb]\\nnb_dir=\"abc/test\"')\n",
    "    test.equal(get_config(tmp), Config.from_dict({'nb_dir': 'abc/test'}, tmp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7d6ee6fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import nb; nb.export_nb('config.ipynb', src_dir='../nb')"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}