diff --git a/nbdev/export.py b/nbdev/export.py index ae9ecda78..423cdb4df 100644 --- a/nbdev/export.py +++ b/nbdev/export.py @@ -401,17 +401,12 @@ def update_baseurl(): with open(fname, 'w') as f: f.write(code) # Cell -#hide -def _get_paths(pth:str, names:list): return [Path(pth)/n for n in names if '/.' not in pth] - -# Cell -def nbglob(fname=None, recursive=None, extension='.ipynb', config_key='nbs_path') -> L: +def nbglob(fname=None, recursive=False, extension='.ipynb', config_key='nbs_path') -> L: "Find all files in a directory matching an extension given a `config_key`. Ignores hidden directories and filenames starting with `_`" fname = Config().path(config_key) if fname is None else Path(fname) - if fname.is_file(): return L([fname]) - if recursive: fls = L(os.walk(fname)).map(lambda x: _get_paths(x[0], x[2])).concat() - else: fls = fname.glob(f'*{extension}') - return L(fls).filter(lambda x: not x.name.startswith('_') and x.name.endswith(extension)) + if fname.is_dir(): fname = f'{fname.absolute()}/**/*{extension}' if recursive else f'{fname.absolute()}/*{extension}' + fls = L(glob.glob(str(fname), recursive=recursive)).filter(lambda x: '/.' not in x).map(Path) + return fls.filter(lambda x: not x.name.startswith('_') and x.name.endswith(extension)) # Cell def notebook2script(fname=None, silent=False, to_dict=False, bare=False, recursive=None): diff --git a/nbs/00_export.ipynb b/nbs/00_export.ipynb index a34fa1b45..11620c5da 100644 --- a/nbs/00_export.ipynb +++ b/nbs/00_export.ipynb @@ -1644,8 +1644,12 @@ "outputs": [], "source": [ "#export\n", - "#hide\n", - "def _get_paths(pth:str, names:list): return [Path(pth)/n for n in names if '/.' not in pth]" + "def nbglob(fname=None, recursive=False, extension='.ipynb', config_key='nbs_path') -> L:\n", + " \"Find all files in a directory matching an extension given a `config_key`. Ignores hidden directories and filenames starting with `_`\"\n", + " fname = Config().path(config_key) if fname is None else Path(fname)\n", + " if fname.is_dir(): fname = f'{fname.absolute()}/**/*{extension}' if recursive else f'{fname.absolute()}/*{extension}'\n", + " fls = L(glob.glob(str(fname), recursive=recursive)).filter(lambda x: '/.' not in x).map(Path)\n", + " return fls.filter(lambda x: not x.name.startswith('_') and x.name.endswith(extension))" ] }, { @@ -1654,23 +1658,32 @@ "metadata": {}, "outputs": [], "source": [ - "#export\n", - "def nbglob(fname=None, recursive=None, extension='.ipynb', config_key='nbs_path') -> L:\n", - " \"Find all files in a directory matching an extension given a `config_key`. Ignores hidden directories and filenames starting with `_`\"\n", - " fname = Config().path(config_key) if fname is None else Path(fname)\n", - " if fname.is_file(): return L([fname])\n", - " if recursive: fls = L(os.walk(fname)).map(lambda x: _get_paths(x[0], x[2])).concat()\n", - " else: fls = fname.glob(f'*{extension}')\n", - " return L(fls).filter(lambda x: not x.name.startswith('_') and x.name.endswith(extension))" + "#hide\n", + "with tempfile.TemporaryDirectory() as d:\n", + " os.makedirs(Path(d)/'a', exist_ok=True)\n", + " (Path(d)/'a'/'a.ipynb').touch()\n", + " (Path(d)/'a'/'fake_a.ipynb').touch()\n", + " os.makedirs(Path(d)/'a/b', exist_ok=True)\n", + " (Path(d)/'a'/'b'/'fake_b.ipynb').touch()\n", + " os.makedirs(Path(d)/'a/b/c', exist_ok=True)\n", + " (Path(d)/'a'/'b'/'c'/'fake_c.ipynb').touch()\n", + " (Path(d)/'a'/'b'/'c'/'foo_c.ipynb').touch()\n", + " \n", + " assert len(nbglob(f'{str(d)}/**/foo*', recursive=True)) == 1\n", + " assert len(nbglob(d, recursive=True)) == 5\n", + " assert len(nbglob(d)) == 0\n", + " assert len(nbglob(f'{d}/a')) == 2" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ - "Optionally you can pass a `config_key` to dictate which directory you are pointing to. By default it's `nbs_path` as without any parameters passed in, it will check for notebooks. To have it instead find library files simply pass in `lib_path` instead.\n", - "\n", - "> Note: it will only search for paths in `Config().path`" + "#hide\n", + "assert len(nbglob('*')) > 1\n", + "assert len(nbglob('*')) > len(nbglob('0*'))" ] }, { @@ -1690,18 +1703,19 @@ "source": [ "#hide\n", "fnames = nbglob()\n", - "test_eq(len(fnames) > 0, True)" + "test_eq(len(fnames) > 0, True)\n", + "\n", + "fnames = nbglob(fnames[0])\n", + "test_eq(len(fnames), 1)" ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "#hide\n", - "fnames = nbglob(fnames[0])\n", - "test_eq(len(fnames), 1)" + "Optionally you can pass a `config_key` to dictate which directory you are pointing to. By default it's `nbs_path` as without any parameters passed in, it will check for notebooks. To have it instead find library files simply pass in `lib_path` instead.\n", + "\n", + "> Note: it will only search for paths in `Config().path`" ] }, {