From c3989fbfbb9c35c4758458101112d9aaec491f2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Sat, 23 Jul 2016 16:57:33 +0200 Subject: [PATCH 1/5] On gen_gallery path.join relates paths Since build can only be triggered from the documentation source a quick fix was established to move to this directory when sphinx-gallery runs. On this first step I declare the directories from the information provided by sphinx. Still it does not work fully for retrieving images --- sphinx_gallery/docs_resolv.py | 6 +---- sphinx_gallery/gen_gallery.py | 44 +++++++++++++++++------------------ 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/sphinx_gallery/docs_resolv.py b/sphinx_gallery/docs_resolv.py index 065b3c81c..c059fe7c3 100644 --- a/sphinx_gallery/docs_resolv.py +++ b/sphinx_gallery/docs_resolv.py @@ -432,9 +432,5 @@ def embed_code_links(app, exception): gallery_conf = app.config.sphinx_gallery_conf - gallery_dirs = gallery_conf['gallery_dirs'] - if not isinstance(gallery_dirs, list): - gallery_dirs = [gallery_dirs] - - for gallery_dir in gallery_dirs: + for gallery_dir in gallery_conf['gallery_dirs']: _embed_code_links(app, gallery_conf, gallery_dir) diff --git a/sphinx_gallery/gen_gallery.py b/sphinx_gallery/gen_gallery.py index 352a71879..7dac92ab3 100644 --- a/sphinx_gallery/gen_gallery.py +++ b/sphinx_gallery/gen_gallery.py @@ -61,6 +61,16 @@ def clean_gallery_out(build_dir): os.remove(os.path.join(build_image_dir, filename)) +def build_paths(gallery_conf_dirs, srcdir): + + if not isinstance(gallery_conf_dirs, list): + gallery_conf_dirs = [gallery_conf_dirs] + + gallery_conf_dirs = [os.path.join(srcdir, work_dir) + for work_dir in gallery_conf_dirs] + return gallery_conf_dirs + + def generate_gallery_rst(app): """Generate the Main examples gallery reStructuredText @@ -84,28 +94,19 @@ def generate_gallery_rst(app): clean_gallery_out(app.builder.outdir) - examples_dirs = gallery_conf['examples_dirs'] - gallery_dirs = gallery_conf['gallery_dirs'] - - if not isinstance(examples_dirs, list): - examples_dirs = [examples_dirs] - if not isinstance(gallery_dirs, list): - gallery_dirs = [gallery_dirs] + examples_dirs = build_paths( + gallery_conf['examples_dirs'], app.builder.srcdir) + gallery_dirs = build_paths( + gallery_conf['gallery_dirs'], app.builder.srcdir) + mod_examples_dir = os.path.join( + app.builder.srcdir, gallery_conf['mod_example_dir']) + gallery_conf['mod_example_dir'] = mod_examples_dir - mod_examples_dir = os.path.relpath(gallery_conf['mod_example_dir'], - app.builder.srcdir) seen_backrefs = set() computation_times = [] - # cd to the appropriate directory regardless of sphinx configuration - working_dir = os.getcwd() - os.chdir(app.builder.srcdir) for examples_dir, gallery_dir in zip(examples_dirs, gallery_dirs): - examples_dir = os.path.relpath(examples_dir, - app.builder.srcdir) - gallery_dir = os.path.relpath(gallery_dir, - app.builder.srcdir) for workdir in [examples_dir, gallery_dir, mod_examples_dir]: if not os.path.exists(workdir): @@ -138,9 +139,6 @@ def generate_gallery_rst(app): fhindex.write(SPHX_GLR_SIG) fhindex.flush() - # Back to initial directory - os.chdir(working_dir) - print("Computation time summary:") for time_elapsed, fname in sorted(computation_times)[::-1]: if time_elapsed is not None: @@ -155,10 +153,9 @@ def touch_empty_backreferences(app, what, name, obj, options, lines): This avoids inclusion errors/warnings if there are no gallery examples for a class / module that is being parsed by autodoc""" - examples_path = os.path.join(app.srcdir, - app.config.sphinx_gallery_conf[ - "mod_example_dir"], - "%s.examples" % name) + examples_path = os.path.join(app.config.sphinx_gallery_conf[ + "mod_example_dir"], + "%s.examples" % name) if not os.path.exists(examples_path): # touch file @@ -180,6 +177,7 @@ def sumarize_failing_examples(app, exception): gallery_conf = app.config.sphinx_gallery_conf failing_examples = set(gallery_conf['failing_examples']) expected_failing_examples = set(gallery_conf['expected_failing_examples']) + print(failing_examples) examples_expected_to_fail = failing_examples.intersection( expected_failing_examples) From d90483df25243437381efcd4262ddd233c2fd300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Sat, 23 Jul 2016 17:49:23 +0200 Subject: [PATCH 2/5] Correct naming of links --- sphinx_gallery/backreferences.py | 3 ++- sphinx_gallery/gen_gallery.py | 21 +++++++++++---------- sphinx_gallery/gen_rst.py | 13 +++++++++---- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/sphinx_gallery/backreferences.py b/sphinx_gallery/backreferences.py index 4fe579c6a..9cc440174 100644 --- a/sphinx_gallery/backreferences.py +++ b/sphinx_gallery/backreferences.py @@ -165,6 +165,7 @@ def write_backreferences(seen_backrefs, gallery_conf, """Writes down back reference files, which include a thumbnail list of examples using a certain module""" example_file = os.path.join(target_dir, fname) + target_path_name = os.path.relpath(target_dir, gallery_conf['src_dir']) backrefs = scan_used_functions(example_file, gallery_conf) for backref in backrefs: include_path = os.path.join(gallery_conf['mod_example_dir'], @@ -175,6 +176,6 @@ def write_backreferences(seen_backrefs, gallery_conf, heading = '\n\nExamples using ``%s``' % backref ex_file.write(heading + '\n') ex_file.write('^' * len(heading) + '\n') - ex_file.write(_thumbnail_div(target_dir, fname, snippet, + ex_file.write(_thumbnail_div(target_path_name, fname, snippet, is_backref=True)) seen_backrefs.add(backref) diff --git a/sphinx_gallery/gen_gallery.py b/sphinx_gallery/gen_gallery.py index 7dac92ab3..91b49cd7a 100644 --- a/sphinx_gallery/gen_gallery.py +++ b/sphinx_gallery/gen_gallery.py @@ -61,14 +61,14 @@ def clean_gallery_out(build_dir): os.remove(os.path.join(build_image_dir, filename)) -def build_paths(gallery_conf_dirs, srcdir): +def build_paths(gallery_conf, path_name, srcdir): - if not isinstance(gallery_conf_dirs, list): - gallery_conf_dirs = [gallery_conf_dirs] + if not isinstance(gallery_conf[path_name], list): + gallery_conf[path_name] = [gallery_conf[path_name]] - gallery_conf_dirs = [os.path.join(srcdir, work_dir) - for work_dir in gallery_conf_dirs] - return gallery_conf_dirs + gallery_conf[path_name] = [os.path.join(srcdir, work_dir) + for work_dir in gallery_conf[path_name]] + return gallery_conf[path_name] def generate_gallery_rst(app): @@ -95,13 +95,13 @@ def generate_gallery_rst(app): clean_gallery_out(app.builder.outdir) examples_dirs = build_paths( - gallery_conf['examples_dirs'], app.builder.srcdir) + gallery_conf, 'examples_dirs', app.builder.srcdir) gallery_dirs = build_paths( - gallery_conf['gallery_dirs'], app.builder.srcdir) + gallery_conf, 'gallery_dirs', app.builder.srcdir) mod_examples_dir = os.path.join( app.builder.srcdir, gallery_conf['mod_example_dir']) gallery_conf['mod_example_dir'] = mod_examples_dir - + gallery_conf['src_dir'] = app.builder.srcdir seen_backrefs = set() computation_times = [] @@ -176,7 +176,8 @@ def sumarize_failing_examples(app, exception): gallery_conf = app.config.sphinx_gallery_conf failing_examples = set(gallery_conf['failing_examples']) - expected_failing_examples = set(gallery_conf['expected_failing_examples']) + expected_failing_examples = set(build_paths( + gallery_conf, 'expected_failing_examples', app.builder.srcdir)) print(failing_examples) examples_expected_to_fail = failing_examples.intersection( diff --git a/sphinx_gallery/gen_rst.py b/sphinx_gallery/gen_rst.py index 329f4623b..12fc852e8 100644 --- a/sphinx_gallery/gen_rst.py +++ b/sphinx_gallery/gen_rst.py @@ -357,11 +357,14 @@ def save_figures(image_path, fig_count, gallery_conf): images_rst = "" if len(figure_list) == 1: figure_name = figure_list[0] - images_rst = SINGLE_IMAGE % figure_name.lstrip('/') + fig_rst_path = os.path.relpath(figure_name, gallery_conf['src_dir']) + images_rst = SINGLE_IMAGE % fig_rst_path.lstrip('/') elif len(figure_list) > 1: images_rst = HLIST_HEADER for figure_name in figure_list: - images_rst += HLIST_IMAGE_TEMPLATE % figure_name.lstrip('/') + fig_rst_path = os.path.relpath( + figure_name, gallery_conf['src_dir']) + images_rst += HLIST_IMAGE_TEMPLATE % fig_rst_path.lstrip('/') return figure_list, images_rst @@ -459,6 +462,7 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs): if fname.endswith('.py')] entries_text = [] computation_times = [] + target_path_name = os.path.relpath(target_dir, gallery_conf['src_dir']) for fname in sorted_listdir: amount_of_code, time_elapsed = \ generate_file_rst(fname, target_dir, src_dir, gallery_conf) @@ -467,7 +471,7 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs): intro = extract_intro(new_fname) write_backreferences(seen_backrefs, gallery_conf, target_dir, fname, intro) - this_entry = _thumbnail_div(target_dir, fname, intro) + """ + this_entry = _thumbnail_div(target_path_name, fname, intro) + """ .. toctree:: :hidden: @@ -607,7 +611,8 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf): image_fname = 'sphx_glr_' + base_image_name + '_{0:03}.png' image_path_template = os.path.join(image_dir, image_fname) - ref_fname = example_file.replace(os.path.sep, '_') + file_target = os.path.relpath(example_file, gallery_conf['src_dir']) + ref_fname = file_target.replace(os.path.sep, '_') example_rst = """\n\n.. _sphx_glr_{0}:\n\n""".format(ref_fname) example_nb = Notebook(fname, target_dir) From d44af7cfb4313fa376ea7679d0f985694a6905db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Sat, 23 Jul 2016 17:58:49 +0200 Subject: [PATCH 3/5] Documentation resultion path updates --- setup.cfg | 4 ++-- sphinx_gallery/docs_resolv.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/setup.cfg b/setup.cfg index 4e43f4ca5..795d4ce07 100644 --- a/setup.cfg +++ b/setup.cfg @@ -7,6 +7,6 @@ with-doctest=1 doctest-extension=rst [build_sphinx] -source-dir = docs/ -build-dir = docs/_build +source-dir = doc/ +build-dir = doc_build all_files = 1 diff --git a/sphinx_gallery/docs_resolv.py b/sphinx_gallery/docs_resolv.py index c059fe7c3..ca765bb14 100644 --- a/sphinx_gallery/docs_resolv.py +++ b/sphinx_gallery/docs_resolv.py @@ -341,8 +341,8 @@ def _embed_code_links(app, gallery_conf, gallery_dir): "Error:\n".format(this_module)) print(e.args) - html_gallery_dir = os.path.abspath(os.path.join(app.builder.outdir, - gallery_dir)) + gallery_path = os.path.relpath(gallery_dir, app.builder.srcdir) + html_gallery_dir = os.path.join(app.builder.outdir, gallery_path) # patterns for replacement link_pattern = (' Date: Thu, 11 Aug 2016 23:06:12 +0200 Subject: [PATCH 4/5] Give a test source dir of empty to pass test --- sphinx_gallery/tests/test_gen_rst.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sphinx_gallery/tests/test_gen_rst.py b/sphinx_gallery/tests/test_gen_rst.py index 496c4e738..16fd05fe6 100644 --- a/sphinx_gallery/tests/test_gen_rst.py +++ b/sphinx_gallery/tests/test_gen_rst.py @@ -137,6 +137,7 @@ def build_test_configuration(**kwargs): gallery_conf = copy.deepcopy(gen_gallery.DEFAULT_GALLERY_CONF) gallery_conf.update(examples_dir=tempfile.mkdtemp(), + src_dir='', gallery_dir=tempfile.mkdtemp()) gallery_conf.update(kwargs) From cb6ac5085f771472fc347d3dc4d924a4b2847f16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Thu, 11 Aug 2016 23:52:44 +0200 Subject: [PATCH 5/5] Update test for save images in mayavi --- sphinx_gallery/tests/test_gen_rst.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sphinx_gallery/tests/test_gen_rst.py b/sphinx_gallery/tests/test_gen_rst.py index 16fd05fe6..bdb0d4d73 100644 --- a/sphinx_gallery/tests/test_gen_rst.py +++ b/sphinx_gallery/tests/test_gen_rst.py @@ -236,19 +236,19 @@ def test_thumbnail_number(): assert_equal(thumbnail_number, 2) -def test_save_figures(): +def test_save_figures_mayavi(): """Test file naming when saving figures. Requires mayavi.""" try: from mayavi import mlab except ImportError: raise nose.SkipTest('Mayavi not installed') mlab.options.offscreen = True - examples_dir = tempfile.mkdtemp() - gallery_conf = {'find_mayavi_figures': True} + gallery_conf = build_test_configuration(find_mayavi_figures=True) + mlab.test_plot3d() plt.plot(1, 1) - fname_template = os.path.join(examples_dir, 'image{0}.png') + fname_template = os.path.join(gallery_conf['examples_dir'], 'image{0}.png') fig_list, _ = sg.save_figures(fname_template, 0, gallery_conf) assert_equal(len(fig_list), 2) assert fig_list[0].endswith('image1.png') @@ -261,7 +261,7 @@ def test_save_figures(): assert fig_list[0].endswith('image3.png') assert fig_list[1].endswith('image4.png') - shutil.rmtree(examples_dir) + shutil.rmtree(gallery_conf['examples_dir']) def test_zip_notebooks():