Skip to content

Fix indexst variable does not exist when own index gallery is first#1383

Merged
lucyleeow merged 11 commits intosphinx-gallery:masterfrom
lucyleeow:fix_index_only
Oct 5, 2024
Merged

Fix indexst variable does not exist when own index gallery is first#1383
lucyleeow merged 11 commits intosphinx-gallery:masterfrom
lucyleeow:fix_index_only

Conversation

@lucyleeow
Copy link
Copy Markdown
Contributor

@lucyleeow lucyleeow commented Oct 2, 2024

closes #1382

Bug introduced by refactor in #1332

When the first gallery in the example gallery loop, in generate_gallery_rst, is one with its own index.rst file, the indexst variable does not exist which is a problem because we pass it to _finish_index_rst. This variable is not used in _finish_index_rst when the gallery has its own index.rst file but _finish_index_rst does various other things so needs to be run always, even when the gallery has its own index.rst.

Adds test.

Will do a patch release once this is merged

@lucyleeow lucyleeow added the bug label Oct 2, 2024
@lucyleeow
Copy link
Copy Markdown
Contributor Author

lucyleeow commented Oct 3, 2024

A __pycache__ gets added to sphinx_gallery/tests/testconfs/src during the new test_own_index_first test.

When a gallery folder has it's own index.rst file, we do not check for index.rst or gallery header files when we collect subgallery folders. We do not need a index.rst/header file in the sub-gallery folders as we do not write the root gallery index or any nested sub gallery index files when the user provides their own root gallery index.rst.
(Initially I thought we should be more strict and always ensure sub-gallery folders do have an index.rst. or gallery header file, but then realised that technically these are not needed when the user providers their own index.rst file in the root gallery folder)

The test thus fails as the __pycache__ is collected as a subgallery directory, which then obviously fails as it does not have a gallery header file.

@larsoner I've amended the code such that we do not collect any folders that called __pycache__. I think we could further this and avoid collecting any 'dunder' named folder as a nested subgallery as well. WDYT?

@larsoner
Copy link
Copy Markdown
Contributor

larsoner commented Oct 3, 2024

I've amended the code such that we do not collect any folders that called pycache. I think we could further this and avoid collecting any 'dunder' named folder as a nested subgallery as well. WDYT?

I think just avoiding __pycache__ is good. It's unlikely but possible that someone in the wild is actually using a dunder folder intentionally somewhere and we'd break things for them. And I'm not sure what folder other than __pycache__ we'd definitely want to exclude. So I say let's keep it as is for now!

Copy link
Copy Markdown
Contributor

@larsoner larsoner left a comment

Choose a reason for hiding this comment

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

Just one question about a comment otherwise LGTM!

subfolders = [
subfolder
for subfolder in subfolders
# Returns `None` if `index.rst` or gallery header found
Copy link
Copy Markdown
Contributor

@larsoner larsoner Oct 3, 2024

Choose a reason for hiding this comment

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

Right?

Suggested change
# Returns `None` if `index.rst` or gallery header found
# Returns `None` if neither `index.rst` nor gallery header found

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Turns out we are both wrong, this whole thing is pretty confusing.

def _get_gallery_header(dir_, gallery_conf, raise_error=True):
"""Get gallery header from GALLERY_HEADER.[ext] or README.[ext] file.
Returns `None` if user supplied an index.rst or no gallery header file
found and `raise_error=False`.
"""
# First check if user supplies an index.rst and that index.rst is in the
# copyfile regexp:
if re.match(gallery_conf["copyfile_regex"], "index.rst"):
fpth = os.path.join(dir_, "index.rst")
if os.path.isfile(fpth):
return None
# Next look for GALLERY_HEADER.[ext] (and for backward-compatibility README.[ext]
extensions = [".txt"] + sorted(gallery_conf["source_suffix"])
for ext in extensions:
for fname in ("GALLERY_HEADER", "README", "readme"):
fpth = os.path.join(dir_, fname + ext)
if os.path.isfile(fpth):
return fpth
if raise_error:
raise ExtensionError(
"Example directory {} does not have a GALLERY_HEADER file with "
"one of the expected file extensions {}. Please write one to "
"introduce your gallery.".format(dir_, extensions)
)
return None

If there is an index.rst -> return None
If there is a gallery header -> return file path
If there is no index.rst, no gallery header and raise is false -> None

So we want a subsection dir to contain a gallery header, so we check that the return is not None, which only happens when there is a gallery header. Let me amend the comment to be what happens when return is not None, as this is what we are checking for anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually the check_for_index parameter name is pretty confusing here, what about we change it to check_for_header or check_for_gallery_header but thats a bit verbose.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Either is fine by me!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

changed to check_for_header

@lucyleeow lucyleeow merged commit a44edd8 into sphinx-gallery:master Oct 5, 2024
@lucyleeow lucyleeow deleted the fix_index_only branch October 5, 2024 05:03
@lucyleeow
Copy link
Copy Markdown
Contributor Author

I think I should release now with just this PR, unless you think we should wait for #1384 or #1385 @larsoner ?

@larsoner
Copy link
Copy Markdown
Contributor

larsoner commented Oct 7, 2024

I think those are both close enough we could wait for them!

@lucyleeow
Copy link
Copy Markdown
Contributor Author

Happy to wait. Should it be a minor release then, or patch still?

@larsoner
Copy link
Copy Markdown
Contributor

larsoner commented Oct 8, 2024

Might as well make it a minor release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Variable 'indexst' is undefined when using custom index.rst

2 participants