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,40 @@ 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
+ 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 :
122
126
try :
123
127
obj = pandas
124
- for name in self . single_doc .split ('.' ):
128
+ for name in single_doc .split ('.' ):
125
129
obj = getattr (obj , name )
126
130
except AttributeError :
127
131
raise ValueError ('Single document not understood, it should '
128
132
'be a file in doc/source/*.rst (e.g. '
129
133
'"contributing.rst" or a pandas function or '
130
134
'method (e.g. "pandas.DataFrame.head")' )
131
135
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
133
141
134
- def _copy_generated_docstring (self , method ):
142
+ def _copy_generated_docstring (self ):
135
143
"""Copy existing generated (from api.rst) docstring page because
136
144
this is more correct in certain cases (where a custom autodoc
137
145
template is used).
138
146
139
147
"""
140
148
fname = os .path .join (SOURCE_PATH , 'generated' ,
141
- 'pandas.{}.rst' .format (method ))
149
+ 'pandas.{}.rst' .format (self . single_doc ))
142
150
temp_dir = os .path .join (SOURCE_PATH , 'generated_single' )
143
151
144
152
try :
@@ -156,25 +164,15 @@ def _copy_generated_docstring(self, method):
156
164
157
165
def _generate_index (self ):
158
166
"""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 ()
171
169
172
170
with open (os .path .join (SOURCE_PATH , 'index.rst.template' )) as f :
173
171
t = jinja2 .Template (f .read ())
174
172
175
173
with open (os .path .join (SOURCE_PATH , 'index.rst' ), 'w' ) as f :
176
174
f .write (t .render (include_api = self .include_api ,
177
- single_doc = single_doc ,
175
+ single_doc = self . single_doc ,
178
176
single_doc_type = self .single_doc_type ))
179
177
180
178
@staticmethod
@@ -229,9 +227,13 @@ def _sphinx_build(self, kind):
229
227
os .path .join (BUILD_PATH , kind ))
230
228
231
229
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 ))
235
237
webbrowser .open (url , new = 2 )
236
238
237
239
def html (self ):
0 commit comments