19
19
from contextlib import contextmanager
20
20
import webbrowser
21
21
import jinja2
22
- import shutil
23
22
24
23
import pandas
25
24
@@ -82,8 +81,10 @@ class DocBuilder:
82
81
def __init__ (self , num_jobs = 1 , include_api = True , single_doc = None ):
83
82
self .num_jobs = num_jobs
84
83
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 )
87
88
self .exclude_patterns = self ._exclude_patterns
88
89
89
90
self ._generate_index ()
@@ -99,7 +100,7 @@ def _exclude_patterns(self):
99
100
rst_files = [f for f in os .listdir (SOURCE_PATH )
100
101
if ((f .endswith ('.rst' ) or f .endswith ('.ipynb' ))
101
102
and (f != 'index.rst' )
102
- and (f != self .single_doc ))]
103
+ and (f != '{0}.rst' . format ( self .single_doc ) ))]
103
104
if self .single_doc_type != 'api' :
104
105
rst_files += ['generated/*.rst' ]
105
106
elif not self .include_api :
@@ -112,33 +113,44 @@ def _exclude_patterns(self):
112
113
113
114
return exclude_patterns
114
115
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
+ """Extract self.single_doc (base name) and self.single_doc_type from
118
+ passed single_doc kwarg.
119
+
120
+ """
121
+ self .include_api = False
122
+
123
+ if single_doc == 'api.rst' :
124
+ self .single_doc_type = 'api'
125
+ self .single_doc = 'api'
126
+ elif os .path .exists (os .path .join (SOURCE_PATH , single_doc )):
127
+ self .single_doc_type = 'rst'
128
+ self .single_doc = os .path .splitext (os .path .basename (single_doc ))[0 ]
129
+ elif single_doc is not None :
122
130
try :
123
131
obj = pandas
124
- for name in self . single_doc .split ('.' ):
132
+ for name in single_doc .split ('.' ):
125
133
obj = getattr (obj , name )
126
134
except AttributeError :
127
135
raise ValueError ('Single document not understood, it should '
128
136
'be a file in doc/source/*.rst (e.g. '
129
137
'"contributing.rst" or a pandas function or '
130
138
'method (e.g. "pandas.DataFrame.head")' )
131
139
else :
132
- return 'docstring'
140
+ self .single_doc_type = 'docstring'
141
+ if single_doc .startswith ('pandas.' ):
142
+ self .single_doc = single_doc [len ('pandas.' ):]
143
+ else :
144
+ self .single_doc = single_doc
133
145
134
- def _copy_generated_docstring (self , method ):
146
+ def _copy_generated_docstring (self ):
135
147
"""Copy existing generated (from api.rst) docstring page because
136
148
this is more correct in certain cases (where a custom autodoc
137
149
template is used).
138
150
139
151
"""
140
152
fname = os .path .join (SOURCE_PATH , 'generated' ,
141
- 'pandas.{}.rst' .format (method ))
153
+ 'pandas.{}.rst' .format (self . single_doc ))
142
154
temp_dir = os .path .join (SOURCE_PATH , 'generated_single' )
143
155
144
156
try :
@@ -156,25 +168,15 @@ def _copy_generated_docstring(self, method):
156
168
157
169
def _generate_index (self ):
158
170
"""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'
171
+ if self .single_doc_type == 'docstring' :
172
+ self ._copy_generated_docstring ()
171
173
172
174
with open (os .path .join (SOURCE_PATH , 'index.rst.template' )) as f :
173
175
t = jinja2 .Template (f .read ())
174
176
175
177
with open (os .path .join (SOURCE_PATH , 'index.rst' ), 'w' ) as f :
176
178
f .write (t .render (include_api = self .include_api ,
177
- single_doc = single_doc ,
179
+ single_doc = self . single_doc ,
178
180
single_doc_type = self .single_doc_type ))
179
181
180
182
@staticmethod
@@ -229,9 +231,13 @@ def _sphinx_build(self, kind):
229
231
os .path .join (BUILD_PATH , kind ))
230
232
231
233
def _open_browser (self ):
232
- url = os .path .join (
233
- 'file://' , DOC_PATH , 'build' , 'html' ,
234
- 'generated_single' , '{}.html' .format (self .single_doc ))
234
+ base_url = os .path .join ('file://' , DOC_PATH , 'build' , 'html' )
235
+ if self .single_doc_type == 'docstring' :
236
+ url = os .path .join (
237
+ base_url ,
238
+ 'generated_single' , 'pandas.{}.html' .format (self .single_doc ))
239
+ else :
240
+ url = os .path .join (base_url , '{}.html' .format (self .single_doc ))
235
241
webbrowser .open (url , new = 2 )
236
242
237
243
def html (self ):
0 commit comments