Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[NO MRG - WIP] Improved use of relative paths #133

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

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion sphinx_gallery/backreferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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)
10 changes: 3 additions & 7 deletions sphinx_gallery/docs_resolv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ('<a href="%s" class="sphx-glr-code-links" '
Expand Down Expand Up @@ -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)
49 changes: 24 additions & 25 deletions sphinx_gallery/gen_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def clean_gallery_out(build_dir):
os.remove(os.path.join(build_image_dir, filename))


def build_paths(gallery_conf, path_name, srcdir):

if not isinstance(gallery_conf[path_name], list):
gallery_conf[path_name] = [gallery_conf[path_name]]

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):
"""Generate the Main examples gallery reStructuredText

Expand All @@ -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]

mod_examples_dir = os.path.relpath(gallery_conf['mod_example_dir'],
app.builder.srcdir)
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
gallery_conf['src_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):
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -179,7 +176,9 @@ 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(
expected_failing_examples)
Expand Down
13 changes: 9 additions & 4 deletions sphinx_gallery/gen_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down Expand Up @@ -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)

Expand Down
11 changes: 6 additions & 5 deletions sphinx_gallery/tests/test_gen_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -235,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')
Expand All @@ -260,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():
Expand Down