From 4c754b5a0142e42084f1a3b066e2ececb681ff1d Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Sun, 25 Jun 2017 07:31:32 -0700 Subject: [PATCH 1/6] FIX: Use unicode --- sphinx_gallery/gen_gallery.py | 35 +++++++++++++++++------------------ sphinx_gallery/gen_rst.py | 4 ++-- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/sphinx_gallery/gen_gallery.py b/sphinx_gallery/gen_gallery.py index f19cdb98a..3ff3605d1 100644 --- a/sphinx_gallery/gen_gallery.py +++ b/sphinx_gallery/gen_gallery.py @@ -188,24 +188,23 @@ def generate_gallery_rst(app): computation_times += this_computation_times # we create an index.rst with all examples - fhindex = open(os.path.join(gallery_dir, 'index.rst'), 'w') - # :orphan: to suppress "not included in TOCTREE" sphinx warnings - fhindex.write(":orphan:\n\n" + this_fhindex) - for directory in sorted(os.listdir(examples_dir)): - if os.path.isdir(os.path.join(examples_dir, directory)): - src_dir = os.path.join(examples_dir, directory) - target_dir = os.path.join(gallery_dir, directory) - this_fhindex, this_computation_times = generate_dir_rst(src_dir, target_dir, gallery_conf, - seen_backrefs) - fhindex.write(this_fhindex) - computation_times += this_computation_times - - if gallery_conf['download_all_examples']: - download_fhindex = generate_zipfiles(gallery_dir) - fhindex.write(download_fhindex) - - fhindex.write(SPHX_GLR_SIG) - fhindex.flush() + with open(os.path.join(gallery_dir, 'index.rst'), 'wb') as fhindex: + # :orphan: to suppress "not included in TOCTREE" sphinx warnings + fhindex.write((u":orphan:\n\n" + this_fhindex).encode('utf-8')) + for directory in sorted(os.listdir(examples_dir)): + if os.path.isdir(os.path.join(examples_dir, directory)): + src_dir = os.path.join(examples_dir, directory) + target_dir = os.path.join(gallery_dir, directory) + this_fhindex, this_computation_times = generate_dir_rst(src_dir, target_dir, gallery_conf, + seen_backrefs) + fhindex.write(this_fhindex.encode('utf-8')) + computation_times += this_computation_times + + if gallery_conf['download_all_examples']: + download_fhindex = generate_zipfiles(gallery_dir) + fhindex.write(download_fhindex.encode('utf-8')) + + fhindex.write(SPHX_GLR_SIG) if gallery_conf['plot_gallery']: logger.info("Computation time summary:", color='white') diff --git a/sphinx_gallery/gen_rst.py b/sphinx_gallery/gen_rst.py index b60c691c2..65de9cb42 100644 --- a/sphinx_gallery/gen_rst.py +++ b/sphinx_gallery/gen_rst.py @@ -378,8 +378,8 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs): location=src_dir) return "", [] # because string is an expected return type - with open(os.path.join(src_dir, 'README.txt')) as fid: - fhindex = fid.read() + with open(os.path.join(src_dir, 'README.txt'), 'rb') as fid: + fhindex = fid.read().decode('utf-8') # Add empty lines to avoid bug in issue #165 fhindex += "\n\n" From cb8135f22385d953c0be6efb489fb0f88c1e92ba Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Sun, 25 Jun 2017 15:23:57 -0700 Subject: [PATCH 2/6] FIX: Missed one --- sphinx_gallery/gen_gallery.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx_gallery/gen_gallery.py b/sphinx_gallery/gen_gallery.py index 3ff3605d1..cc2e17589 100644 --- a/sphinx_gallery/gen_gallery.py +++ b/sphinx_gallery/gen_gallery.py @@ -204,7 +204,7 @@ def generate_gallery_rst(app): download_fhindex = generate_zipfiles(gallery_dir) fhindex.write(download_fhindex.encode('utf-8')) - fhindex.write(SPHX_GLR_SIG) + fhindex.write(SPHX_GLR_SIG.encode('utf-8')) if gallery_conf['plot_gallery']: logger.info("Computation time summary:", color='white') From 2a1cc809d9f48f016fb3c4596ddf470af73fb633 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Tue, 27 Jun 2017 10:50:27 -0700 Subject: [PATCH 3/6] FIX: Add test --- sphinx_gallery/tests/test_gen_rst.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/sphinx_gallery/tests/test_gen_rst.py b/sphinx_gallery/tests/test_gen_rst.py index 18f2e9bfb..c1bf757ea 100644 --- a/sphinx_gallery/tests/test_gen_rst.py +++ b/sphinx_gallery/tests/test_gen_rst.py @@ -18,8 +18,8 @@ import pytest import sphinx_gallery.gen_rst as sg -from sphinx_gallery import gen_gallery -from sphinx_gallery import downloads +from sphinx_gallery import gen_gallery, downloads +from sphinx_gallery.gen_gallery import generate_dir_rst # Need to import gen_rst before matplotlib.pyplot to set backend to 'Agg' import matplotlib.pyplot as plt @@ -111,7 +111,7 @@ def test_extract_intro(): finally: os.remove(f.name) assert 'Docstring' not in result - assert result == 'This is the description of the example which goes on and on, Óscar' + assert result == 'This is the description of the example which goes on and on, Óscar' # noqa assert 'second paragraph' not in result @@ -177,6 +177,21 @@ def test_fail_example(log_collector): raise ValueError('Did not stop executing script after error') +def test_gen_dir_rst(): + """Test gen_dir_rst.""" + gallery_conf = build_test_configuration() + print(os.listdir(gallery_conf['examples_dir'])) + args = (gallery_conf['src_dir'], gallery_conf['gallery_dir'], + gallery_conf, []) + out = generate_dir_rst(*args) + assert out[0] == "" + fname_readme = os.path.join(gallery_conf['src_dir'], 'README.txt') + with open(fname_readme, 'wb') as fid: + fid.write(u"Testing\n=======\n\nÓscar here.".encode('utf-8')) + out = generate_dir_rst(*args) + assert u"Óscar here" in out[0] + + def test_pattern_matching(log_collector): """Test if only examples matching pattern are executed""" From 5b2cdcd891ace4a63508a74e24269cf05f325697 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Tue, 27 Jun 2017 16:24:17 -0700 Subject: [PATCH 4/6] FIX: Use codecs.open --- sphinx_gallery/gen_gallery.py | 12 +++++++----- sphinx_gallery/gen_rst.py | 5 +++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/sphinx_gallery/gen_gallery.py b/sphinx_gallery/gen_gallery.py index cc2e17589..f1e78e2f7 100644 --- a/sphinx_gallery/gen_gallery.py +++ b/sphinx_gallery/gen_gallery.py @@ -11,6 +11,7 @@ from __future__ import division, print_function, absolute_import +import codecs import copy import re import os @@ -188,23 +189,24 @@ def generate_gallery_rst(app): computation_times += this_computation_times # we create an index.rst with all examples - with open(os.path.join(gallery_dir, 'index.rst'), 'wb') as fhindex: + with codecs.open(os.path.join(gallery_dir, 'index.rst'), 'w', + encoding='utf-8') as fhindex: # :orphan: to suppress "not included in TOCTREE" sphinx warnings - fhindex.write((u":orphan:\n\n" + this_fhindex).encode('utf-8')) + fhindex.write((u":orphan:\n\n" + this_fhindex)) for directory in sorted(os.listdir(examples_dir)): if os.path.isdir(os.path.join(examples_dir, directory)): src_dir = os.path.join(examples_dir, directory) target_dir = os.path.join(gallery_dir, directory) this_fhindex, this_computation_times = generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs) - fhindex.write(this_fhindex.encode('utf-8')) + fhindex.write(this_fhindex) computation_times += this_computation_times if gallery_conf['download_all_examples']: download_fhindex = generate_zipfiles(gallery_dir) - fhindex.write(download_fhindex.encode('utf-8')) + fhindex.write(download_fhindex) - fhindex.write(SPHX_GLR_SIG.encode('utf-8')) + fhindex.write(SPHX_GLR_SIG) if gallery_conf['plot_gallery']: logger.info("Computation time summary:", color='white') diff --git a/sphinx_gallery/gen_rst.py b/sphinx_gallery/gen_rst.py index 65de9cb42..0db899da0 100644 --- a/sphinx_gallery/gen_rst.py +++ b/sphinx_gallery/gen_rst.py @@ -378,8 +378,9 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs): location=src_dir) return "", [] # because string is an expected return type - with open(os.path.join(src_dir, 'README.txt'), 'rb') as fid: - fhindex = fid.read().decode('utf-8') + with codecs.open(os.path.join(src_dir, 'README.txt'), 'r', + encoding='utf-8') as fid: + fhindex = fid.read() # Add empty lines to avoid bug in issue #165 fhindex += "\n\n" From f667f9c92735e19b84650538b37d992130482536 Mon Sep 17 00:00:00 2001 From: Eric Larson Date: Tue, 27 Jun 2017 16:49:12 -0700 Subject: [PATCH 5/6] FIX: Fix for old Sphinx --- sphinx_gallery/tests/test_gen_rst.py | 40 +++++++++++++++++++--------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/sphinx_gallery/tests/test_gen_rst.py b/sphinx_gallery/tests/test_gen_rst.py index c1bf757ea..501ce7e91 100644 --- a/sphinx_gallery/tests/test_gen_rst.py +++ b/sphinx_gallery/tests/test_gen_rst.py @@ -9,17 +9,18 @@ import ast import codecs import copy +import logging import tempfile import re import os import shutil -import warnings import zipfile import pytest import sphinx_gallery.gen_rst as sg from sphinx_gallery import gen_gallery, downloads from sphinx_gallery.gen_gallery import generate_dir_rst +from sphinx_gallery import sphinx_compatibility # Need to import gen_rst before matplotlib.pyplot to set backend to 'Agg' import matplotlib.pyplot as plt @@ -177,19 +178,34 @@ def test_fail_example(log_collector): raise ValueError('Did not stop executing script after error') +class _FakeApp(object): + def __init__(self): + for key in ('info', 'warn', 'error', 'critical'): + setattr(self, key, lambda msg, **kwargs: logging.info(msg)) + + def status_iterator(self, iterable, *args, **kwargs): + for it in iterable: + yield it + + def test_gen_dir_rst(): """Test gen_dir_rst.""" - gallery_conf = build_test_configuration() - print(os.listdir(gallery_conf['examples_dir'])) - args = (gallery_conf['src_dir'], gallery_conf['gallery_dir'], - gallery_conf, []) - out = generate_dir_rst(*args) - assert out[0] == "" - fname_readme = os.path.join(gallery_conf['src_dir'], 'README.txt') - with open(fname_readme, 'wb') as fid: - fid.write(u"Testing\n=======\n\nÓscar here.".encode('utf-8')) - out = generate_dir_rst(*args) - assert u"Óscar here" in out[0] + _app = sphinx_compatibility._app + try: + sphinx_compatibility._app = _FakeApp() + gallery_conf = build_test_configuration() + print(os.listdir(gallery_conf['examples_dir'])) + args = (gallery_conf['src_dir'], gallery_conf['gallery_dir'], + gallery_conf, []) + out = generate_dir_rst(*args) + assert out[0] == "" + fname_readme = os.path.join(gallery_conf['src_dir'], 'README.txt') + with open(fname_readme, 'wb') as fid: + fid.write(u"Testing\n=======\n\nÓscar here.".encode('utf-8')) + out = generate_dir_rst(*args) + assert u"Óscar here" in out[0] + finally: + sphinx_compatibility._app = _app def test_pattern_matching(log_collector): From 7bb3e761d69a93656461cf54af0e1ac2b3b3366d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Wed, 28 Jun 2017 13:41:00 +0200 Subject: [PATCH 6/6] use fakesphinxapp fixture --- sphinx_gallery/tests/conftest.py | 3 ++- sphinx_gallery/tests/test_gen_rst.py | 39 +++++++++------------------- 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/sphinx_gallery/tests/conftest.py b/sphinx_gallery/tests/conftest.py index 52ead110c..4deeacf6a 100644 --- a/sphinx_gallery/tests/conftest.py +++ b/sphinx_gallery/tests/conftest.py @@ -24,7 +24,8 @@ def __init__(self): def status_iterator(self, *args, **kwargs): self.calls['status_iterator'].append(Params(args, kwargs)) - yield + for it in args[0]: + yield it def warning(self, *args, **kwargs): self.calls['warning'].append(Params(args, kwargs)) diff --git a/sphinx_gallery/tests/test_gen_rst.py b/sphinx_gallery/tests/test_gen_rst.py index 501ce7e91..44ff954ce 100644 --- a/sphinx_gallery/tests/test_gen_rst.py +++ b/sphinx_gallery/tests/test_gen_rst.py @@ -178,34 +178,19 @@ def test_fail_example(log_collector): raise ValueError('Did not stop executing script after error') -class _FakeApp(object): - def __init__(self): - for key in ('info', 'warn', 'error', 'critical'): - setattr(self, key, lambda msg, **kwargs: logging.info(msg)) - - def status_iterator(self, iterable, *args, **kwargs): - for it in iterable: - yield it - - -def test_gen_dir_rst(): +def test_gen_dir_rst(fakesphinxapp): """Test gen_dir_rst.""" - _app = sphinx_compatibility._app - try: - sphinx_compatibility._app = _FakeApp() - gallery_conf = build_test_configuration() - print(os.listdir(gallery_conf['examples_dir'])) - args = (gallery_conf['src_dir'], gallery_conf['gallery_dir'], - gallery_conf, []) - out = generate_dir_rst(*args) - assert out[0] == "" - fname_readme = os.path.join(gallery_conf['src_dir'], 'README.txt') - with open(fname_readme, 'wb') as fid: - fid.write(u"Testing\n=======\n\nÓscar here.".encode('utf-8')) - out = generate_dir_rst(*args) - assert u"Óscar here" in out[0] - finally: - sphinx_compatibility._app = _app + gallery_conf = build_test_configuration() + print(os.listdir(gallery_conf['examples_dir'])) + args = (gallery_conf['src_dir'], gallery_conf['gallery_dir'], + gallery_conf, []) + out = generate_dir_rst(*args) + assert out[0] == "" + fname_readme = os.path.join(gallery_conf['src_dir'], 'README.txt') + with open(fname_readme, 'wb') as fid: + fid.write(u"Testing\n=======\n\nÓscar here.".encode('utf-8')) + out = generate_dir_rst(*args) + assert u"Óscar here" in out[0] def test_pattern_matching(log_collector):