From 48b7357c1c6a382f9435e274694e98a36cc29745 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Mon, 31 Jul 2017 18:44:01 +0200 Subject: [PATCH] Expose examples sortkey --- doc/conf.py | 3 ++- sphinx_gallery/gen_gallery.py | 1 + sphinx_gallery/gen_rst.py | 25 +++++++++---------------- sphinx_gallery/sorting.py | 22 ++++++++++++++++++++++ 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index 7b8c85fb0..962fb705e 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -300,7 +300,7 @@ def setup(app): 'matplotlib': ('https://matplotlib.org/', None), } -from sphinx_gallery.sorting import ExplicitOrder +from sphinx_gallery.sorting import ExplicitOrder, amount_of_code examples_dirs = ['../examples', '../tutorials'] gallery_dirs = ['auto_examples', 'tutorials'] @@ -330,6 +330,7 @@ def setup(app): 'subsection_order': ExplicitOrder(['../examples/sin_func', '../examples/no_output', '../tutorials/seaborn']), + 'within_subsection_order': amount_of_code, 'find_mayavi_figures': find_mayavi_figures, 'expected_failing_examples': ['../examples/no_output/plot_raise.py', '../examples/no_output/plot_syntaxerror.py'] diff --git a/sphinx_gallery/gen_gallery.py b/sphinx_gallery/gen_gallery.py index 0951e4bd0..0f1e0f72d 100644 --- a/sphinx_gallery/gen_gallery.py +++ b/sphinx_gallery/gen_gallery.py @@ -32,6 +32,7 @@ 'filename_pattern': re.escape(os.sep) + 'plot', 'examples_dirs': os.path.join('..', 'examples'), 'subsection_order': None, + 'within_subsection_order': None, 'gallery_dirs': 'auto_examples', 'backreferences_dir': None, 'doc_module': (), diff --git a/sphinx_gallery/gen_rst.py b/sphinx_gallery/gen_rst.py index aa6d06f95..fe299c8fe 100644 --- a/sphinx_gallery/gen_rst.py +++ b/sphinx_gallery/gen_rst.py @@ -375,8 +375,10 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs): if not os.path.exists(target_dir): os.makedirs(target_dir) - sorted_listdir = [fname for fname in sorted(os.listdir(src_dir)) - if fname.endswith('.py')] + 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)) entries_text = [] computation_times = [] build_target_dir = os.path.relpath(target_dir, gallery_conf['src_dir']) @@ -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( fname, target_dir, src_dir, gallery_conf) computation_times.append((time_elapsed, fname)) - new_fname = os.path.join(src_dir, fname) this_entry = _thumbnail_div(build_target_dir, fname, intro) + """ .. toctree:: :hidden: /%s\n""" % os.path.join(build_target_dir, fname[:-3]).replace(os.sep, '/') - entries_text.append((amount_of_code, this_entry)) + entries_text.append(this_entry) if gallery_conf['backreferences_dir']: write_backreferences(seen_backrefs, gallery_conf, target_dir, fname, intro) - # sort to have the smallest entries in the beginning - entries_text.sort() - - for _, entry_text in entries_text: + for entry_text in entries_text: fhindex += entry_text # clear at the end of the section @@ -531,8 +529,6 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf): ------- intro: str The introduction of the example - amount_of_code : int - character count of the corresponding python script in file time_elapsed : float seconds required to run the script """ @@ -541,13 +537,10 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf): example_file = os.path.join(target_dir, fname) shutil.copyfile(src_file, example_file) file_conf, script_blocks = split_code_and_text_blocks(src_file) - amount_of_code = sum([len(bcontent) - for blabel, bcontent, lineno in script_blocks - if blabel == 'code']) intro = extract_intro(fname, script_blocks[0][1]) if md5sum_is_current(example_file): - return intro, amount_of_code, 0 + return intro, 0 image_dir = os.path.join(target_dir, 'images') if not os.path.exists(image_dir): @@ -646,4 +639,4 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf): if block_vars['execute_script']: logger.debug("%s ran in : %.2g seconds", src_file, time_elapsed) - return intro, amount_of_code, time_elapsed + return intro, time_elapsed diff --git a/sphinx_gallery/sorting.py b/sphinx_gallery/sorting.py index 98b7dbe0d..27e0c0ea5 100644 --- a/sphinx_gallery/sorting.py +++ b/sphinx_gallery/sorting.py @@ -13,6 +13,8 @@ import os import types +from .py_source_parser import split_code_and_text_blocks + class ExplicitOrder(object): """Sorting key for all galleries subsections @@ -46,3 +48,23 @@ def __call__(self, item): raise ValueError('If you use an explicit folder ordering, you ' 'must specify all folders. Explicit order not ' 'found for {}'.format(item)) + + +def amount_of_code(src_dir): + """Sort examples in src_dir by amount of code """ + def sort_files(filename): + src_file = os.path.normpath(os.path.join(src_dir, filename)) + file_conf, script_blocks = split_code_and_text_blocks(src_file) + amount_of_code = sum([len(bcontent) + for blabel, bcontent, lineno in script_blocks + if blabel == 'code']) + return amount_of_code + return sort_files + + +def file_size(src_dir): + """Sort examples in src_dir by file size""" + def sort_files(filename): + src_file = os.path.normpath(os.path.join(src_dir, filename)) + return os.stat(src_file).st_size + return sort_files