|
17 | 17 | "metadata": {}, |
18 | 18 | "outputs": [], |
19 | 19 | "source": [ |
20 | | - "#|default_exp config" |
| 20 | + "#| default_exp config" |
21 | 21 | ] |
22 | 22 | }, |
23 | 23 | { |
|
26 | 26 | "metadata": {}, |
27 | 27 | "outputs": [], |
28 | 28 | "source": [ |
29 | | - "#|export\n", |
| 29 | + "#| export\n", |
30 | 30 | "from datetime import datetime\n", |
31 | 31 | "from fastcore.docments import *\n", |
32 | 32 | "from fastcore.utils import *\n", |
|
47 | 47 | "metadata": {}, |
48 | 48 | "outputs": [], |
49 | 49 | "source": [ |
50 | | - "#|hide\n", |
| 50 | + "#| hide\n", |
51 | 51 | "from fastcore.test import *\n", |
52 | 52 | "import tempfile" |
53 | 53 | ] |
|
84 | 84 | "metadata": {}, |
85 | 85 | "outputs": [], |
86 | 86 | "source": [ |
87 | | - "#|export\n", |
| 87 | + "#| export\n", |
88 | 88 | "_nbdev_home_dir = 'nbdev' # sub-directory of xdg base dir\n", |
89 | 89 | "_nbdev_cfg_name = 'settings.ini'" |
90 | 90 | ] |
|
95 | 95 | "metadata": {}, |
96 | 96 | "outputs": [], |
97 | 97 | "source": [ |
98 | | - "#|export\n", |
| 98 | + "#| export\n", |
99 | 99 | "def _git_repo():\n", |
100 | 100 | " try: return repo_details(run('git config --get remote.origin.url'))[1]\n", |
101 | 101 | " except OSError: return" |
|
107 | 107 | "metadata": {}, |
108 | 108 | "outputs": [], |
109 | 109 | "source": [ |
110 | | - "#|hide\n", |
| 110 | + "#| hide\n", |
111 | 111 | "test_eq(_git_repo(), 'nbdev')\n", |
112 | 112 | "with tempfile.TemporaryDirectory() as d, working_directory(d): test_is(_git_repo(), None)" |
113 | 113 | ] |
|
118 | 118 | "metadata": {}, |
119 | 119 | "outputs": [], |
120 | 120 | "source": [ |
121 | | - "#|export\n", |
| 121 | + "#| export\n", |
122 | 122 | "\n", |
123 | 123 | "# When adding a named default to the list below, be sure that that name\n", |
124 | 124 | "# is also added to one of the sections in `_nbdev_cfg_sections` as well,\n", |
|
176 | 176 | "metadata": {}, |
177 | 177 | "outputs": [], |
178 | 178 | "source": [ |
179 | | - "#|export\n", |
| 179 | + "#| export\n", |
180 | 180 | "def _get_info(owner, repo, default_branch='main', default_kw='nbdev'):\n", |
181 | 181 | " from ghapi.all import GhApi\n", |
182 | 182 | " api = GhApi(owner=owner, repo=repo, token=os.getenv('GITHUB_TOKEN'))\n", |
|
199 | 199 | "metadata": {}, |
200 | 200 | "outputs": [], |
201 | 201 | "source": [ |
202 | | - "#|hide\n", |
| 202 | + "#| hide\n", |
203 | 203 | "if os.getenv('GITHUB_ACTIONS') != 'true': # GITHUB_TOKEN in actions has limited scope.\n", |
204 | 204 | " _branch, _tags, _descrip = _get_info('fastai', 'fastai')\n", |
205 | 205 | " test_eq(_tags, 'colab deep-learning fastai gpu machine-learning notebooks python pytorch')\n", |
|
213 | 213 | "metadata": {}, |
214 | 214 | "outputs": [], |
215 | 215 | "source": [ |
216 | | - "#|export\n", |
| 216 | + "#| export\n", |
217 | 217 | "def _fetch_from_git(raise_err=False):\n", |
218 | 218 | " \"Get information for settings.ini from the user.\"\n", |
219 | 219 | " res={}\n", |
|
236 | 236 | "metadata": {}, |
237 | 237 | "outputs": [], |
238 | 238 | "source": [ |
239 | | - "#|hide\n", |
| 239 | + "#| hide\n", |
240 | 240 | "#test_eq(_fetch_from_git(raise_err=True)['lib_name'], 'nbdev')" |
241 | 241 | ] |
242 | 242 | }, |
|
246 | 246 | "metadata": {}, |
247 | 247 | "outputs": [], |
248 | 248 | "source": [ |
249 | | - "#|export\n", |
| 249 | + "#| export\n", |
250 | 250 | "def _prompt_user(cfg, inferred):\n", |
251 | 251 | " \"Let user input values not in `cfg` or `inferred`.\"\n", |
252 | 252 | " res = cfg.copy()\n", |
|
267 | 267 | "metadata": {}, |
268 | 268 | "outputs": [], |
269 | 269 | "source": [ |
270 | | - "#|hide\n", |
| 270 | + "#| hide\n", |
271 | 271 | "# `repo` not printed - already exists in `cfg`\n", |
272 | 272 | "# `user` printed - in `inferred` and not `cfg`\n", |
273 | 273 | "test_stdout(lambda: _prompt_user({'user':None,'repo':'nbdev'},{'user':'fastai'}),\n", |
|
280 | 280 | "metadata": {}, |
281 | 281 | "outputs": [], |
282 | 282 | "source": [ |
283 | | - "#|export\n", |
| 283 | + "#| export\n", |
284 | 284 | "def _cfg2txt(cfg, head, sections, tail=''):\n", |
285 | 285 | " \"Render `cfg` with commented sections.\"\n", |
286 | 286 | " nm = cfg.d.name\n", |
|
299 | 299 | "metadata": {}, |
300 | 300 | "outputs": [], |
301 | 301 | "source": [ |
302 | | - "#|hide\n", |
| 302 | + "#| hide\n", |
303 | 303 | "d = {'user': 'fastai', 'repo': 'nbdev', 'doc_path': '_docs'}\n", |
304 | 304 | "c = Config('.', 'test_settings.ini', d, save=False)\n", |
305 | 305 | "txt = '''[DEFAULT]\n", |
|
321 | 321 | "metadata": {}, |
322 | 322 | "outputs": [], |
323 | 323 | "source": [ |
324 | | - "#|export\n", |
| 324 | + "#| export\n", |
325 | 325 | "_nbdev_cfg_head = '''# All sections below are required unless otherwise specified.\n", |
326 | 326 | "# See https://github.com/AnswerDotAI/nbdev/blob/main/settings.ini for examples.\n", |
327 | 327 | "\n", |
|
345 | 345 | "metadata": {}, |
346 | 346 | "outputs": [], |
347 | 347 | "source": [ |
348 | | - "#|export\n", |
| 348 | + "#| export\n", |
349 | 349 | "@call_parse\n", |
350 | 350 | "@delegates(_apply_defaults, but='cfg')\n", |
351 | 351 | "def nbdev_create_config(\n", |
|
401 | 401 | "metadata": {}, |
402 | 402 | "outputs": [], |
403 | 403 | "source": [ |
404 | | - "#|export\n", |
| 404 | + "#| export\n", |
405 | 405 | "def _nbdev_config_file(cfg_name=_nbdev_cfg_name, path=None):\n", |
406 | 406 | " cfg_path = Path.cwd() if path is None else Path(path)\n", |
407 | 407 | " return getattr(Config.find(cfg_name), 'config_file', cfg_path/cfg_name)" |
|
413 | 413 | "metadata": {}, |
414 | 414 | "outputs": [], |
415 | 415 | "source": [ |
416 | | - "#|hide\n", |
| 416 | + "#| hide\n", |
417 | 417 | "test_eq(_nbdev_config_file(), Path.cwd().parent.parent/'settings.ini')" |
418 | 418 | ] |
419 | 419 | }, |
|
423 | 423 | "metadata": {}, |
424 | 424 | "outputs": [], |
425 | 425 | "source": [ |
426 | | - "#|export\n", |
| 426 | + "#| export\n", |
427 | 427 | "def _xdg_config_paths(cfg_name=_nbdev_cfg_name):\n", |
428 | 428 | " xdg_config_paths = reversed([xdg_config_home()]+xdg_config_dirs())\n", |
429 | 429 | " return [o/_nbdev_home_dir/cfg_name for o in xdg_config_paths]" |
|
435 | 435 | "metadata": {}, |
436 | 436 | "outputs": [], |
437 | 437 | "source": [ |
438 | | - "#|export\n", |
| 438 | + "#| export\n", |
439 | 439 | "def _type(t): return bool if t==bool_arg else t\n", |
440 | 440 | "_types = {k:_type(v['anno']) for k,v in docments(_apply_defaults,full=True,returns=False).items() if k != 'cfg'}\n", |
441 | 441 | "\n", |
|
463 | 463 | "metadata": {}, |
464 | 464 | "outputs": [], |
465 | 465 | "source": [ |
466 | | - "#|hide\n", |
| 466 | + "#| hide\n", |
467 | 467 | "try: _get_config,get_config = get_config,get_config.__wrapped__ # Bypass cache during dev and tests\n", |
468 | 468 | "except AttributeError: pass" |
469 | 469 | ] |
|
581 | 581 | "metadata": {}, |
582 | 582 | "outputs": [], |
583 | 583 | "source": [ |
584 | | - "#|export\n", |
| 584 | + "#| export\n", |
585 | 585 | "def config_key(c, default=None, path=True, missing_ok=None):\n", |
586 | 586 | " \"Deprecated: use `get_config().get` or `get_config().path` instead.\"\n", |
587 | 587 | " warn(\"`config_key` is deprecated. Use `get_config().get` or `get_config().path` instead.\", DeprecationWarning)\n", |
|
601 | 601 | "metadata": {}, |
602 | 602 | "outputs": [], |
603 | 603 | "source": [ |
604 | | - "#|export\n", |
| 604 | + "#| export\n", |
605 | 605 | "def is_nbdev(): return _nbdev_config_file().exists()" |
606 | 606 | ] |
607 | 607 | }, |
|
611 | 611 | "metadata": {}, |
612 | 612 | "outputs": [], |
613 | 613 | "source": [ |
614 | | - "#|hide\n", |
| 614 | + "#| hide\n", |
615 | 615 | "test_eq(is_nbdev(), True)" |
616 | 616 | ] |
617 | 617 | }, |
|
621 | 621 | "metadata": {}, |
622 | 622 | "outputs": [], |
623 | 623 | "source": [ |
624 | | - "#|export\n", |
| 624 | + "#| export\n", |
625 | 625 | "def create_output(txt, mime):\n", |
626 | 626 | " \"Add a cell output containing `txt` of the `mime` text MIME sub-type\"\n", |
627 | 627 | " return [{\"data\": { f\"text/{mime}\": str(txt).splitlines(True) },\n", |
|
634 | 634 | "metadata": {}, |
635 | 635 | "outputs": [], |
636 | 636 | "source": [ |
637 | | - "#|export\n", |
| 637 | + "#| export\n", |
638 | 638 | "def show_src(src, lang='python'): return Markdown(f'```{lang}\\n{src}\\n```')" |
639 | 639 | ] |
640 | 640 | }, |
|
697 | 697 | "metadata": {}, |
698 | 698 | "outputs": [], |
699 | 699 | "source": [ |
700 | | - "#|export\n", |
| 700 | + "#| export\n", |
701 | 701 | "_re_version = re.compile(r'^__version__\\s*=.*$', re.MULTILINE)\n", |
702 | 702 | "_re_proj = re.compile(r'^name\\s*=\\s*\".*$', re.MULTILINE)\n", |
703 | 703 | "_re_reqpy = re.compile(r'^requires-python\\s*=\\s*\".*$', re.MULTILINE)\n", |
|
769 | 769 | "metadata": {}, |
770 | 770 | "outputs": [], |
771 | 771 | "source": [ |
772 | | - "#|export\n", |
| 772 | + "#| export\n", |
773 | 773 | "def write_cells(cells, hdr, file, offset=0, cell_number=True, solo_nb=False):\n", |
774 | 774 | " \"Write `cells` to `file` along with header `hdr` starting at index `offset` (mainly for nbdev internal use).\"\n", |
775 | 775 | " for cell in cells:\n", |
|
784 | 784 | "metadata": {}, |
785 | 785 | "outputs": [], |
786 | 786 | "source": [ |
787 | | - "#|export\n", |
| 787 | + "#| export\n", |
788 | 788 | "def _basic_export_nb(fname, name, dest=None):\n", |
789 | 789 | " \"Basic exporter to bootstrap nbdev.\"\n", |
790 | 790 | " if dest is None: dest = get_config().lib_path\n", |
|
820 | 820 | "metadata": {}, |
821 | 821 | "outputs": [], |
822 | 822 | "source": [ |
823 | | - "#|hide\n", |
| 823 | + "#| hide\n", |
824 | 824 | "#| eval: false\n", |
825 | 825 | "path = Path('../nbdev')\n", |
826 | 826 | "(path/'config.py').unlink(missing_ok=True)\n", |
|
0 commit comments