88itself, whereas the generatable content must be edited via templates in
99this file.
1010"""
11- # We did try to do the wrapping the smart way,
12- # with callable functions and new.function, but could never get the
13- # docstrings right for python2.2. See
14- # http://groups.google.com/group/comp.lang.python/browse_frm/thread/dcd63ec13096a0f6/1b14640f3a4ad3dc?#1b14640f3a4ad3dc
15- # For some later history, see
16- # http://thread.gmane.org/gmane.comp.python.matplotlib.devel/7068
11+
12+ # Although it is possible to dynamically generate the pyplot functions at
13+ # runtime with the proper signatures, a static pyplot.py is simpler for static
14+ # analysis tools to parse.
1715
1816import inspect
1917from inspect import Signature , Parameter
20- import os
21- import random
18+ from pathlib import Path
2219import textwrap
2320
24- # this line imports the installed copy of matplotlib, and not the local copy
21+ # This line imports the installed copy of matplotlib, and not the local copy.
2522import numpy as np
2623from matplotlib import mlab
2724from matplotlib .axes import Axes
2825
2926
30- # this is the magic line that must exist in pyplot, after which the boilerplate content will be
31- # appended
32- PYPLOT_MAGIC_HEADER = '################# REMAINING CONTENT GENERATED BY boilerplate.py ##############\n '
33-
34- PYPLOT_PATH = os .path .join (os .path .dirname (__file__ ), 'lib' , 'matplotlib' ,
35- 'pyplot.py' )
36-
27+ # This is the magic line that must exist in pyplot, after which the boilerplate
28+ # content will be appended.
29+ PYPLOT_MAGIC_HEADER = (
30+ "################# REMAINING CONTENT GENERATED BY boilerplate.py "
31+ "##############\n " )
3732
3833AUTOGEN_MSG = """
3934# Autogenerated by boilerplate.py. Do not edit as changes will be lost."""
4035
41-
4236CMAPPABLE_TEMPLATE = AUTOGEN_MSG + """
4337@_autogen_docstring(Axes.%(real_name)s)
4438def %(func)s%(sig)s:
@@ -47,7 +41,6 @@ def %(func)s%(sig)s:
4741 return __ret
4842"""
4943
50-
5144NON_CMAPPABLE_TEMPLATE = AUTOGEN_MSG + """
5245@docstring.copy_dedent(Axes.%(real_name)s)
5346def %(func)s%(sig)s:
@@ -269,25 +262,22 @@ def __repr__(self):
269262
270263
271264def build_pyplot ():
272- pyplot_path = os .path .join (os .path .dirname (__file__ ), ".." , 'lib' ,
273- 'matplotlib' , 'pyplot.py' )
274-
275- pyplot_orig = open (pyplot_path , 'r' ).readlines ()
265+ pyplot_path = Path (__file__ ).parent / "../lib/matplotlib/pyplot.py"
276266
267+ pyplot_orig = pyplot_path .read_text ().splitlines (keepends = True )
277268 try :
278269 pyplot_orig = pyplot_orig [:pyplot_orig .index (PYPLOT_MAGIC_HEADER ) + 1 ]
279270 except IndexError :
280271 raise ValueError ('The pyplot.py file *must* have the exact line: %s'
281272 % PYPLOT_MAGIC_HEADER )
282273
283- pyplot = open (pyplot_path , 'w' )
284- pyplot .writelines (pyplot_orig )
285- pyplot .write ('\n ' )
286-
287- pyplot .writelines (boilerplate_gen ())
288- pyplot .write ('\n ' )
274+ with pyplot_path .open ('w' ) as pyplot :
275+ pyplot .writelines (pyplot_orig )
276+ pyplot .write ('\n ' )
277+ pyplot .writelines (boilerplate_gen ())
278+ pyplot .write ('\n ' )
289279
290280
291281if __name__ == '__main__' :
292- # Write the matplotlib.pyplot file
282+ # Write the matplotlib.pyplot file.
293283 build_pyplot ()
0 commit comments