1919from contextlib import contextmanager
2020import webbrowser
2121import jinja2
22- import shutil
2322
2423import pandas
2524
@@ -82,8 +81,10 @@ class DocBuilder:
8281 def __init__ (self , num_jobs = 1 , include_api = True , single_doc = None ):
8382 self .num_jobs = num_jobs
8483 self .include_api = include_api
85- self .single_doc = single_doc
86- self .single_doc_type = self ._single_doc_type
84+ self .single_doc = None
85+ self .single_doc_type = None
86+ if single_doc is not None :
87+ self ._process_single_doc (single_doc )
8788 self .exclude_patterns = self ._exclude_patterns
8889
8990 self ._generate_index ()
@@ -99,7 +100,7 @@ def _exclude_patterns(self):
99100 rst_files = [f for f in os .listdir (SOURCE_PATH )
100101 if ((f .endswith ('.rst' ) or f .endswith ('.ipynb' ))
101102 and (f != 'index.rst' )
102- and (f != self .single_doc ))]
103+ and (f != '{0}.rst' . format ( self .single_doc ) ))]
103104 if self .single_doc_type != 'api' :
104105 rst_files += ['generated/*.rst' ]
105106 elif not self .include_api :
@@ -112,33 +113,40 @@ def _exclude_patterns(self):
112113
113114 return exclude_patterns
114115
115- @property
116- def _single_doc_type (self ):
117- if self .single_doc :
118- if self .single_doc == 'api.rst' :
119- return 'api'
120- if os .path .exists (os .path .join (SOURCE_PATH , self .single_doc )):
121- return 'rst'
116+ def _process_single_doc (self , single_doc ):
117+ self .include_api = False
118+
119+ if single_doc == 'api.rst' :
120+ self .single_doc_type = 'api'
121+ self .single_doc = 'api'
122+ elif os .path .exists (os .path .join (SOURCE_PATH , single_doc )):
123+ self .single_doc_type = 'rst'
124+ self .single_doc = os .path .splitext (os .path .basename (single_doc ))[0 ]
125+ elif single_doc is not None :
122126 try :
123127 obj = pandas
124- for name in self . single_doc .split ('.' ):
128+ for name in single_doc .split ('.' ):
125129 obj = getattr (obj , name )
126130 except AttributeError :
127131 raise ValueError ('Single document not understood, it should '
128132 'be a file in doc/source/*.rst (e.g. '
129133 '"contributing.rst" or a pandas function or '
130134 'method (e.g. "pandas.DataFrame.head")' )
131135 else :
132- return 'docstring'
136+ self .single_doc_type = 'docstring'
137+ if single_doc .startswith ('pandas.' ):
138+ self .single_doc = single_doc [len ('pandas.' ):]
139+ else :
140+ self .single_doc = single_doc
133141
134- def _copy_generated_docstring (self , method ):
142+ def _copy_generated_docstring (self ):
135143 """Copy existing generated (from api.rst) docstring page because
136144 this is more correct in certain cases (where a custom autodoc
137145 template is used).
138146
139147 """
140148 fname = os .path .join (SOURCE_PATH , 'generated' ,
141- 'pandas.{}.rst' .format (method ))
149+ 'pandas.{}.rst' .format (self . single_doc ))
142150 temp_dir = os .path .join (SOURCE_PATH , 'generated_single' )
143151
144152 try :
@@ -156,25 +164,15 @@ def _copy_generated_docstring(self, method):
156164
157165 def _generate_index (self ):
158166 """Create index.rst file with the specified sections."""
159- if self .single_doc_type == 'rst' :
160- single_doc = os .path .splitext (os .path .basename (self .single_doc ))[0 ]
161- self .include_api = False
162- elif self .single_doc_type == 'docstring' :
163- if self .single_doc .startswith ('pandas.' ):
164- single_doc = self .single_doc [len ('pandas.' ):]
165- else :
166- single_doc = self .single_doc
167- self .include_api = False
168- self ._copy_generated_docstring (single_doc )
169- elif self .single_doc_type == 'api' :
170- single_doc = 'api'
167+ if self .single_doc_type == 'docstring' :
168+ self ._copy_generated_docstring ()
171169
172170 with open (os .path .join (SOURCE_PATH , 'index.rst.template' )) as f :
173171 t = jinja2 .Template (f .read ())
174172
175173 with open (os .path .join (SOURCE_PATH , 'index.rst' ), 'w' ) as f :
176174 f .write (t .render (include_api = self .include_api ,
177- single_doc = single_doc ,
175+ single_doc = self . single_doc ,
178176 single_doc_type = self .single_doc_type ))
179177
180178 @staticmethod
@@ -229,9 +227,13 @@ def _sphinx_build(self, kind):
229227 os .path .join (BUILD_PATH , kind ))
230228
231229 def _open_browser (self ):
232- url = os .path .join (
233- 'file://' , DOC_PATH , 'build' , 'html' ,
234- 'generated_single' , '{}.html' .format (self .single_doc ))
230+ base_url = os .path .join ('file://' , DOC_PATH , 'build' , 'html' )
231+ if self .single_doc_type == 'docstring' :
232+ url = os .path .join (
233+ base_url ,
234+ 'generated_single' , 'pandas.{}.html' .format (self .single_doc ))
235+ else :
236+ url = os .path .join (base_url , '{}.html' .format (self .single_doc ))
235237 webbrowser .open (url , new = 2 )
236238
237239 def html (self ):
0 commit comments