Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix recurse issue #391

Merged
merged 2 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions nbdev/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW @jph00 I had to make this smart enough to handle directories because it picks up the directory from settings.ini

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):
Expand Down
56 changes: 35 additions & 21 deletions nbs/00_export.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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))"
]
},
{
Expand All @@ -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*'))"
]
},
{
Expand All @@ -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`"
]
},
{
Expand Down