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

Skip to content

Commit f1a98fb

Browse files
committed
FIX: Minor fixes
1 parent 7118a09 commit f1a98fb

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

sphinx_gallery/gen_rst.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
Files that generate images should start with 'plot'
1313
1414
"""
15-
from __future__ import division, print_function, absolute_import
15+
from __future__ import (division, print_function, absolute_import,
16+
unicode_literals)
1617
from time import time
1718
import ast
19+
import codecs
1820
import hashlib
1921
import os
2022
import re
@@ -49,9 +51,9 @@ def prefixed_lines():
4951
return ''.join(prefixed_lines())
5052

5153
try:
52-
from StringIO import StringIO
54+
from BytesIO import BytesIO
5355
except ImportError:
54-
from io import StringIO
56+
from io import BytesIO
5557

5658
try:
5759
# make sure that the Agg backend is set before importing any
@@ -87,13 +89,23 @@ def __init__(self, file1, file2):
8789

8890
def write(self, data):
8991
self.file1.write(data)
90-
self.file2.write(unicode(data)) # this is the StringIO
92+
self.file2.write(data)
9193

9294
def flush(self):
9395
self.file1.flush()
9496
self.file2.flush()
9597

9698

99+
class MyBytesIO(BytesIO):
100+
"""Helper to deal with unicode not having buffer interface on Py2.7"""
101+
def write(self, data):
102+
# "unicode" objects don't have a buffer interface on Py2.7,
103+
# so we need to manually convert
104+
if isinstance(data, unicode):
105+
data = data.encode('utf-8')
106+
super(MyBytesIO, self).write(data)
107+
108+
97109
###############################################################################
98110
CODE_DOWNLOAD = """**Total running time of the script:**
99111
({0:.0f} minutes {1:.3f} seconds)\n\n
@@ -125,7 +137,7 @@ def flush(self):
125137
"""
126138

127139

128-
CODE_OUTPUT = u""".. rst-class:: sphx-glr-script-out
140+
CODE_OUTPUT = """.. rst-class:: sphx-glr-script-out
129141
130142
Out::
131143
@@ -144,6 +156,9 @@ def get_docstring_and_rest(filename):
144156
rest: str
145157
`filename` content without the docstring
146158
"""
159+
# can't use codecs.open(filename, 'r', 'utf-8') here b/c ast doesn't
160+
# seem to work with unicode strings in Python2.7
161+
# "SyntaxError: encoding declaration in Unicode string"
147162
with open(filename, 'rb') as f:
148163
content = f.read()
149164

@@ -206,8 +221,8 @@ def split_code_and_text_blocks(source_file):
206221

207222
def codestr2rst(codestr, lang='python'):
208223
"""Return reStructuredText code block from code string"""
209-
code_directive = u"\n.. code-block:: {0}\n\n".format(lang)
210-
indented_block = indent(codestr, u' ' * 4)
224+
code_directive = "\n.. code-block:: {0}\n\n".format(lang)
225+
indented_block = indent(codestr, ' ' * 4)
211226
return code_directive + indented_block
212227

213228

@@ -464,7 +479,7 @@ def execute_script(code_block, example_globals, image_path, fig_count,
464479
# First cd in the original example dir, so that any file
465480
# created by the example get created in this directory
466481
os.chdir(os.path.dirname(src_file))
467-
my_buffer = StringIO()
482+
my_buffer = MyBytesIO()
468483
my_stdout = Tee(sys.stdout, my_buffer)
469484
sys.stdout = my_stdout
470485

@@ -474,9 +489,9 @@ def execute_script(code_block, example_globals, image_path, fig_count,
474489

475490
sys.stdout = orig_stdout
476491

477-
my_stdout = my_buffer.getvalue().strip().expandtabs()
492+
my_stdout = my_buffer.getvalue().decode('utf-8').strip().expandtabs()
478493
if my_stdout:
479-
stdout = CODE_OUTPUT.format(indent(my_stdout, u' ' * 4))
494+
stdout = CODE_OUTPUT.format(indent(my_stdout, ' ' * 4))
480495
os.chdir(cwd)
481496
figure_list = save_figures(image_path, fig_count, gallery_conf)
482497

@@ -518,7 +533,7 @@ def execute_script(code_block, example_globals, image_path, fig_count,
518533
os.chdir(cwd)
519534

520535
print(" - time elapsed : %.2g sec" % time_elapsed)
521-
code_output = u"\n{0}\n\n{1}\n\n".format(image_list, stdout)
536+
code_output = "\n{0}\n\n{1}\n\n".format(image_list, stdout)
522537

523538
return code_output, time_elapsed, fig_count + len(figure_list)
524539

@@ -554,7 +569,7 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf):
554569
time_elapsed = 0
555570

556571
ref_fname = example_file.replace(os.path.sep, '_')
557-
example_rst = u"""\n\n.. _sphx_glr_{0}:\n\n""".format(ref_fname)
572+
example_rst = """\n\n.. _sphx_glr_{0}:\n\n""".format(ref_fname)
558573
example_nb = Notebook(fname, target_dir)
559574

560575
filename_pattern = gallery_conf.get('filename_pattern')
@@ -613,9 +628,10 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf):
613628

614629
time_m, time_s = divmod(time_elapsed, 60)
615630
example_nb.save_file()
616-
with open(os.path.join(target_dir, base_image_name + '.rst'), 'wb') as f:
631+
with codecs.open(os.path.join(target_dir, base_image_name + '.rst'),
632+
'w', 'utf-8') as f:
617633
example_rst += CODE_DOWNLOAD.format(time_m, time_s, fname,
618634
example_nb.file_name)
619-
f.write(example_rst.encode('utf-8'))
635+
f.write(example_rst)
620636

621637
return amount_of_code, time_elapsed

0 commit comments

Comments
 (0)