|
2 | 2 | generate the rst files for the examples by iterating over the pylab examples |
3 | 3 | """ |
4 | 4 | from __future__ import print_function |
| 5 | +import io |
5 | 6 | import os, glob |
6 | 7 |
|
7 | 8 | import os |
@@ -37,15 +38,18 @@ def generate_example_rst(app): |
37 | 38 | continue |
38 | 39 |
|
39 | 40 | fullpath = os.path.join(root,fname) |
40 | | - contents = file(fullpath).read() |
| 41 | + if sys.version_info[0] >= 3: |
| 42 | + contents = io.open(fullpath, encoding='utf8').read() |
| 43 | + else: |
| 44 | + contents = io.open(fullpath).read() |
41 | 45 | # indent |
42 | 46 | relpath = os.path.split(root)[-1] |
43 | 47 | datad.setdefault(relpath, []).append((fullpath, fname, contents)) |
44 | 48 |
|
45 | | - subdirs = datad.keys() |
| 49 | + subdirs = list(datad.keys()) |
46 | 50 | subdirs.sort() |
47 | 51 |
|
48 | | - fhindex = file(os.path.join(exampledir, 'index.rst'), 'w') |
| 52 | + fhindex = open(os.path.join(exampledir, 'index.rst'), 'w') |
49 | 53 | fhindex.write("""\ |
50 | 54 | .. _examples-index: |
51 | 55 |
|
@@ -77,7 +81,7 @@ def generate_example_rst(app): |
77 | 81 | os.makedirs(outputdir) |
78 | 82 |
|
79 | 83 | subdirIndexFile = os.path.join(rstdir, 'index.rst') |
80 | | - fhsubdirIndex = file(subdirIndexFile, 'w') |
| 84 | + fhsubdirIndex = open(subdirIndexFile, 'w') |
81 | 85 | fhindex.write(' %s/index.rst\n\n'%subdir) |
82 | 86 |
|
83 | 87 | fhsubdirIndex.write("""\ |
@@ -122,14 +126,17 @@ def generate_example_rst(app): |
122 | 126 | ) and |
123 | 127 | not noplot_regex.search(contents)) |
124 | 128 | if not do_plot: |
125 | | - fhstatic = file(outputfile, 'w') |
| 129 | + fhstatic = open(outputfile, 'w') |
126 | 130 | fhstatic.write(contents) |
127 | 131 | fhstatic.close() |
128 | 132 |
|
129 | 133 | if not out_of_date(fullpath, outrstfile): |
130 | 134 | continue |
131 | 135 |
|
132 | | - fh = file(outrstfile, 'w') |
| 136 | + if sys.version_info[0] >= 3: |
| 137 | + fh = io.open(outrstfile, 'w', encoding='utf8') |
| 138 | + else: |
| 139 | + fh = io.open(outrstfile, 'w') |
133 | 140 | fh.write('.. _%s-%s:\n\n'%(subdir, basename)) |
134 | 141 | title = '%s example code: %s'%(subdir, fname) |
135 | 142 | #title = '<img src=%s> %s example code: %s'%(thumbfile, subdir, fname) |
|
0 commit comments