18
18
import argparse
19
19
from contextlib import contextmanager
20
20
import jinja2
21
+ import shutil
21
22
import webbrowser
22
23
23
24
@@ -50,7 +51,9 @@ def _generate_index(include_api=True, single_doc=None):
50
51
51
52
52
53
def _generate_exclude_pattern (include_api = True , single_doc = None ):
54
+ """
53
55
56
+ """
54
57
if not include_api :
55
58
rst_files = ['api.rst' , 'generated/*.rst' ]
56
59
elif single_doc is not None :
@@ -67,34 +70,60 @@ def _generate_exclude_pattern(include_api=True, single_doc=None):
67
70
return exclude_patterns
68
71
69
72
70
- def _write_temp_file2 (classtype , module , function ):
71
-
72
- s = """{1}.{2}
73
- =================================
74
-
75
- .. currentmodule:: {1}
76
-
77
- .. auto{0}:: {2}""" .format (classtype , module , function )
78
-
79
- with open (os .path .join (SOURCE_PATH , "temp.rst" ), 'w' ) as f :
80
- f .write (s )
81
-
73
+ def _generate_temp_docstring_file (method ):
74
+ """
75
+ """
76
+ fname = os .path .join (SOURCE_PATH , 'generated' , '{}.rst' .format (method ))
77
+
78
+ # # remove the target file to make sure it is updated again (to build
79
+ # # latest version)
80
+ # try:
81
+ # os.remove(fname)
82
+ # except OSError:
83
+ # pass
84
+ #
85
+ # # generate docstring pages
86
+ # print("Running sphinx-autogen to generate docstring stub pages")
87
+ # os.system("sphinx-autogen -t _templates -o source/generated source/*.rst")
88
+
89
+ # create the temporary directory in which we will link the target file
90
+ try :
91
+ os .makedirs (os .path .join (SOURCE_PATH , 'generated_temp' ))
92
+ except OSError :
93
+ pass
82
94
83
- def _write_temp_file (classtype , module , function ):
95
+ if os .path .exists (fname ):
96
+ # link the target file
97
+ try :
98
+ # os.symlink(fname, os.path.join(SOURCE_PATH, 'generated_temp',
99
+ # '{}.rst'.format(method)),
100
+ # target_is_directory=False)
101
+ # copying to make sure sphinx always thinks it is new
102
+ # and needs to be re-generated (to pick source code changes)
103
+ shutil .copy (fname , os .path .join (SOURCE_PATH , 'generated_temp' ))
104
+ linked = True
105
+ except : # noqa
106
+ linked = False
107
+ else :
108
+ linked = False
84
109
85
- s = """API docs
86
- ========
110
+ s = """Built docstrings
111
+ ================
87
112
88
113
.. autosummary::
89
- :toctree: generated_temp/
114
+ {toctree}
115
+ {name}
90
116
91
- {0}.{1}
92
-
93
- """ .format (module , function )
117
+ """ .format (name = method ,
118
+ toctree = ':toctree: generated_temp/\n ' if not linked else '' )
94
119
95
120
with open (os .path .join (SOURCE_PATH , "temp.rst" ), 'w' ) as f :
96
121
f .write (s )
97
122
123
+ if not linked :
124
+ print ("Running sphinx-autogen on manually created file" )
125
+ os .system ("sphinx-autogen -o source/generated_temp source/temp.rst" )
126
+
98
127
99
128
@contextmanager
100
129
def _maybe_exclude_notebooks ():
@@ -310,13 +339,20 @@ def main():
310
339
os .environ ['PYTHONPATH' ] = args .python_path
311
340
312
341
if args .docstring is not None :
313
- _write_temp_file ('method' , 'pandas' , args .docstring )
342
+ shutil .rmtree (os .path .join (BUILD_PATH , 'html' , 'generated_temp' ),
343
+ ignore_errors = True )
344
+ _generate_temp_docstring_file (args .docstring )
314
345
exclude_patterns = _generate_exclude_pattern (single_doc = 'temp.rst' )
315
346
_generate_index (single_doc = 'temp.rst' )
316
347
DocBuilder (args .num_jobs , exclude_patterns ).build_docstring ()
317
- url = "file://" + os .getcwd () + "/build/html/temp.html"
348
+ # open generated page in new browser tab
349
+ url = os .path .join ("file://" , DOC_PATH , "build" , "html" ,
350
+ "generated_temp" , "{}.html" .format (args .docstring ))
318
351
webbrowser .open (url , new = 2 )
319
- #os.remove('source/temp.rst')
352
+ # clean-up generated files
353
+ os .remove ('source/temp.rst' )
354
+ shutil .rmtree (os .path .join (SOURCE_PATH , 'generated_temp' ),
355
+ ignore_errors = True )
320
356
321
357
else :
322
358
_generate_index (not args .no_api , args .single )
0 commit comments