|
7 | 7 | Files that generate images should start with 'plot' |
8 | 8 |
|
9 | 9 | """ |
10 | | -from __future__ import division, print_function |
11 | 10 | from time import time |
12 | 11 | import ast |
13 | 12 | import os |
|
20 | 19 | import posixpath |
21 | 20 | import subprocess |
22 | 21 | import warnings |
23 | | -import six |
24 | 22 |
|
| 23 | +from io import StringIO |
| 24 | +import pickle |
| 25 | +import urllib.request |
| 26 | +import urllib.error |
| 27 | +import urllib.parse |
| 28 | +from urllib.error import HTTPError, URLError |
25 | 29 |
|
26 | | -# Try Python 2 first, otherwise load from Python 3 |
27 | | -try: |
28 | | - from StringIO import StringIO |
29 | | - import cPickle as pickle |
30 | | - import urllib2 as urllib |
31 | | - from urllib2 import HTTPError, URLError |
32 | | -except ImportError: |
33 | | - from io import StringIO |
34 | | - import pickle |
35 | | - import urllib.request |
36 | | - import urllib.error |
37 | | - import urllib.parse |
38 | | - from urllib.error import HTTPError, URLError |
39 | | - |
40 | | - |
41 | | -try: |
42 | | - # Python 2 built-in |
43 | | - execfile |
44 | | -except NameError: |
45 | | - def execfile(filename, global_vars=None, local_vars=None): |
46 | | - with open(filename, encoding='utf-8') as f: |
47 | | - code = compile(f.read(), filename, 'exec') |
48 | | - exec(code, global_vars, local_vars) |
49 | 30 |
|
50 | | -try: |
51 | | - basestring |
52 | | -except NameError: |
53 | | - basestring = str |
| 31 | +def execfile(filename, global_vars=None, local_vars=None): |
| 32 | + with open(filename, encoding='utf-8') as f: |
| 33 | + code = compile(f.read(), filename, 'exec') |
| 34 | + exec(code, global_vars, local_vars) |
54 | 35 |
|
55 | 36 | import token |
56 | 37 | import tokenize |
@@ -93,13 +74,8 @@ def flush(self): |
93 | 74 | def _get_data(url): |
94 | 75 | """Helper function to get data over http or from a local file""" |
95 | 76 | if url.startswith('http://'): |
96 | | - # Try Python 2, use Python 3 on exception |
97 | | - try: |
98 | | - resp = urllib.urlopen(url) |
99 | | - encoding = resp.headers.dict.get('content-encoding', 'plain') |
100 | | - except AttributeError: |
101 | | - resp = urllib.request.urlopen(url) |
102 | | - encoding = resp.headers.get('content-encoding', 'plain') |
| 77 | + resp = urllib.request.urlopen(url) |
| 78 | + encoding = resp.headers.get('content-encoding', 'plain') |
103 | 79 | data = resp.read() |
104 | 80 | if encoding == 'plain': |
105 | 81 | pass |
@@ -427,10 +403,8 @@ def resolve(self, cobj, this_url): |
427 | 403 | def extract_docstring(filename, ignore_heading=False): |
428 | 404 | """ Extract a module-level docstring, if any |
429 | 405 | """ |
430 | | - if six.PY2: |
431 | | - lines = open(filename).readlines() |
432 | | - else: |
433 | | - lines = open(filename, encoding='utf-8').readlines() |
| 406 | + with open(filename, encoding='utf-8') as f: |
| 407 | + lines = f.readlines() |
434 | 408 | start_row = 0 |
435 | 409 | if lines[0].startswith('#!'): |
436 | 410 | lines.pop(0) |
@@ -526,10 +500,8 @@ def generate_example_rst(app): |
526 | 500 | def extract_line_count(filename, target_dir): |
527 | 501 | # Extract the line count of a file |
528 | 502 | example_file = os.path.join(target_dir, filename) |
529 | | - if six.PY2: |
530 | | - lines = open(example_file).readlines() |
531 | | - else: |
532 | | - lines = open(example_file, encoding='utf-8').readlines() |
| 503 | + with open(example_file, encoding='utf-8') as f: |
| 504 | + lines = f.readlines() |
533 | 505 | start_row = 0 |
534 | 506 | if lines and lines[0].startswith('#!'): |
535 | 507 | lines.pop(0) |
@@ -620,7 +592,7 @@ def generate_dir_rst(directory, fhindex, example_dir, root_dir, plot_gallery, se |
620 | 592 | %s |
621 | 593 |
|
622 | 594 |
|
623 | | -""" % open(os.path.join(src_dir, 'README.txt')).read()) |
| 595 | +""" % open(os.path.join(src_dir, 'README.txt'), encoding='utf-8').read()) |
624 | 596 | if not os.path.exists(target_dir): |
625 | 597 | os.makedirs(target_dir) |
626 | 598 | sorted_listdir = line_count_sort(os.listdir(src_dir), |
@@ -676,8 +648,8 @@ def make_thumbnail(in_fname, out_fname, width, height): |
676 | 648 | import Image |
677 | 649 | img = Image.open(in_fname) |
678 | 650 | width_in, height_in = img.size |
679 | | - scale_w = width / float(width_in) |
680 | | - scale_h = height / float(height_in) |
| 651 | + scale_w = width / width_in |
| 652 | + scale_h = height / height_in |
681 | 653 |
|
682 | 654 | if height_in * scale_w <= height: |
683 | 655 | scale = scale_w |
@@ -727,7 +699,7 @@ class NameFinder(ast.NodeVisitor): |
727 | 699 | """ |
728 | 700 |
|
729 | 701 | def __init__(self): |
730 | | - super(NameFinder, self).__init__() |
| 702 | + super().__init__() |
731 | 703 | self.imported_names = {} |
732 | 704 | self.accessed_names = set() |
733 | 705 |
|
@@ -964,11 +936,8 @@ def generate_file_rst(fname, target_dir, src_dir, root_dir, plot_gallery): |
964 | 936 | f.flush() |
965 | 937 |
|
966 | 938 | # save variables so we can later add links to the documentation |
967 | | - if six.PY2: |
968 | | - example_code_obj = identify_names(open(example_file).read()) |
969 | | - else: |
970 | | - example_code_obj = \ |
971 | | - identify_names(open(example_file, encoding='utf-8').read()) |
| 939 | + with open(example_file, encoding='utf-8') as f: |
| 940 | + example_code_obj = identify_names(f.read()) |
972 | 941 | if example_code_obj: |
973 | 942 | codeobj_fname = example_file[:-3] + '_codeobj.pickle' |
974 | 943 | with open(codeobj_fname, 'wb') as fid: |
|
0 commit comments