-
Notifications
You must be signed in to change notification settings - Fork 207
MRG: Allow sorting subsection files #281
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
Conversation
Rebased, docs added, and CIs are happy. Ready for review/merge from my end. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general I think this looks great - I have a few little comments and one less-little one about assuming where the example titles are in the .py
files...other than this it LGTM
doc/advanced_configuration.rst
Outdated
by the amount of code, i.e. :class:`sphinx_gallery.sorting.AmountOfCodeSortKey` | ||
as:: | ||
|
||
from sphinx_gallery.sorting import AmountOfCodeSortKey |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not NumberOfLines
sortkey? I know that's kind of a bikeshed but I think it's more explicit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not just number of lines but more number of code characters IIUC
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, number of code lines rather (which you note below)
doc/advanced_configuration.rst
Outdated
Sorting gallery examples | ||
======================== | ||
|
||
Within a given gallery (sub)section, the example files by default are ordered |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have any short description in the docs that just explains that ordering in general is handled by "sorting keys" which are a way of defining a custom sorting behavior with the sorted
function? Maybe that's too much info but I was confused the first time I saw sort keys being used.
doc/advanced_configuration.rst
Outdated
``within_subsection_order``: | ||
|
||
- :class:`sphinx_gallery.sorting.FileSizeSortKey` to sort by file size. | ||
- :class:`sphinx_gallery.sorting.FileNameSortKey` to sort by file name. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a thought on ExplicitFilenameSortKey
(which I know you aren't implementing here): I think it would get really confusing if we implemented this, since right now doing explicit sorting for folders requres that all folders are listed. We couldn't do the same thing for explicit filenames because there would be too many IMO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we'll have to think harder about that one, but I don't think that the current API will make the problem harder necessarily
listdir = [fname for fname in os.listdir(src_dir) | ||
if fname.endswith('.py')] | ||
sorted_listdir = sorted( | ||
listdir, key=gallery_conf['within_subsection_order'](src_dir)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should have a check somewhere that sees if one of the allowed keys are given, and if not (e.g., if somebody use some other wonky object) we'd throw an error that says "this must be one of XXX sortkeys"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But people in principle can use their own
@@ -385,29 +387,25 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs): | |||
'Generating gallery for %s ' % build_target_dir, | |||
length=len(sorted_listdir)) | |||
for fname in iterator: | |||
intro, amount_of_code, time_elapsed = generate_file_rst( | |||
intro, time_elapsed = generate_file_rst( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that this is being factored out
|
||
def __call__(self, filename): | ||
src_file = os.path.normpath(os.path.join(self.src_dir, filename)) | ||
file_conf, script_blocks = split_code_and_text_blocks(src_file) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah I see that it isn't strictly NumberOfLines
per-se, since you're only counting the code lines. I think it's fine to keep it AmountOfCode
in that case, but maybe clarify in the docs that this will count only code lines and not all the lines in the .py
files
The source directory. | ||
""" | ||
|
||
def __call__(self, filename): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will break for many people's sphinx-gallery setups, as there are a few projects (ahem, MNE-python being one of them) that put sphinx tags just above their titles. I feel like I opened an issue about this a while back but the dev team wasn't keen on supporting that use-case...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes I should probably parse the title however it's found and parsed elsewhere (and refactor to DRY)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this already uses split_code_and_text_blocks
which calls get_docstring_and_rest
, and script_blocks[0]
is the docstring
, which is already cleaned. I even added this to the code:
assert script_blocks[0][1].strip().split('\n')[0][0] == '='
And it passed, so we should be okay here.
_check_order(config_app, 'filesize') | ||
|
||
|
||
@pytest.mark.conf_file(content=""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests feel like magic to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conf stuff? Agreed but I copy pasted (then mostly simplified) from earlier code so I assumed thus was best practice for the repo
Okay @choldgraf I changed the name to |
@@ -1,7 +1,9 @@ | |||
#!/usr/bin/env python2 | |||
# -*- coding: utf-8 -*- | |||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no no - it's not encoding lines, it's sphinx references. People put stuff like
.. _my_ref:
My title
--------
which you're technically not needed to do since SG auto generates references, but people do it anyway :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh okay, I'll probably have to do something smarter then, I'll look into it more.
doc/advanced_configuration.rst
Outdated
} | ||
|
||
However, other options exist that can be instantiated for use with | ||
However, multiple convenience classes are provided for use with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're going to make edits can you make this "In addition" instead of "However"?
If there aren't any more commits needed then I can edit and then merge (but see my other comment)
sphinx_gallery/gen_rst.py
Outdated
@@ -153,12 +153,15 @@ def codestr2rst(codestr, lang='python', lineno=None): | |||
return code_directive + indented_block | |||
|
|||
|
|||
def extract_intro(filename, docstring): | |||
def extract_intro_title(filename, docstring): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this naming makes me thing that it extracts just the title of the introduction. could we make it extract_intro_and_title
?
sphinx_gallery/gen_rst.py
Outdated
""" Extract the first paragraph of module-level docstring. max:95 char""" | ||
|
||
# lstrip is just in case docstring has a '\n\n' at the beginning | ||
paragraphs = docstring.lstrip().split('\n\n') | ||
paragraphs = [p for p in paragraphs if not p.startswith('..')] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is going to fix some other bugs in the process 🎉
sphinx_gallery/gen_rst.py
Outdated
if len(paragraphs) > 1: | ||
title = paragraphs[0].split('\n') | ||
title = ' '.join(t for t in title if t[0] not in ['=', '-', '^', '~']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this could be a list constant somewhere? RST_TITLE_CHARACTERS
?
@@ -172,6 +172,7 @@ def _check_order(config_app, key): | |||
regex = '.*:%s=(.):.*' % key | |||
with codecs.open(index_fname, 'r', 'utf-8') as fid: | |||
for line in fid: | |||
print(line) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just making sure this is intentional and not for debugging
In general this looks great - a few small comments from me then LGTM |
@choldgraf done, I made the check for RST title chars better based on their official description and renamed the function. |
nice - so it sounds like this will work if you either put LGTM...I'll give it 24 hrs and merge if nobody else brings anything up. That OK w/ you? |
Yeah and RST supports both so we should be good there. Ready for merge from my end. |
🎉 🎉 🎉 |
Sorry I completely missed this one. I think this is great stuff. Quickly looking at the diff, I have the following comments though: I think we should have an instance (rather than a class) as the value associated with the Another consistency problem, it would be good to have a consistent naming for sorting keys (i.e. finishing with |
I think this is a good point. No harm in having people give
+1, either is fine with me but maybe I have a small preference for |
What eventually gets called is e.g. This is also why the subsection ordering and within-subsection ordering need to be kept separate -- the former operates on effectively just I used the name |
The hope when we were discussing the design was that they would be exactly the same. Is there a good reason the Personally I am fine with SortKey because I know what a sort key is. The argument was that a significant chunk of our users may not have heard of a sort key. In the end I think that's why |
It is eventually called that way -- any class for |
Closes #275.
Closes #278. (Take over of this PR.)
This adds sort keys for sorting within subsections. I don't need explicit reordering currently (filename-based is good enough for now) so I didn't bother implementing it for now. But it at least sets up the framework for it later.
WIP for now because I'm not updating the doc until #279 is merged and I can rebase to avoid conflicts.