Skip to content

Commit

Permalink
fix opening webbrowser (if not single docstring page)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Feb 25, 2018
1 parent 2cd4a5d commit 512d3a6
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions doc/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from contextlib import contextmanager
import webbrowser
import jinja2
import shutil

import pandas

Expand Down Expand Up @@ -82,8 +81,10 @@ class DocBuilder:
def __init__(self, num_jobs=1, include_api=True, single_doc=None):
self.num_jobs = num_jobs
self.include_api = include_api
self.single_doc = single_doc
self.single_doc_type = self._single_doc_type
self.single_doc = None
self.single_doc_type = None
if single_doc is not None:
self._process_single_doc(single_doc)
self.exclude_patterns = self._exclude_patterns

self._generate_index()
Expand All @@ -99,7 +100,7 @@ def _exclude_patterns(self):
rst_files = [f for f in os.listdir(SOURCE_PATH)
if ((f.endswith('.rst') or f.endswith('.ipynb'))
and (f != 'index.rst')
and (f != self.single_doc))]
and (f != '{0}.rst'.format(self.single_doc)))]
if self.single_doc_type != 'api':
rst_files += ['generated/*.rst']
elif not self.include_api:
Expand All @@ -112,33 +113,44 @@ def _exclude_patterns(self):

return exclude_patterns

@property
def _single_doc_type(self):
if self.single_doc:
if self.single_doc == 'api.rst':
return 'api'
if os.path.exists(os.path.join(SOURCE_PATH, self.single_doc)):
return 'rst'
def _process_single_doc(self, single_doc):
"""Extract self.single_doc (base name) and self.single_doc_type from
passed single_doc kwarg.
"""
self.include_api = False

if single_doc == 'api.rst':
self.single_doc_type = 'api'
self.single_doc = 'api'
elif os.path.exists(os.path.join(SOURCE_PATH, single_doc)):
self.single_doc_type = 'rst'
self.single_doc = os.path.splitext(os.path.basename(single_doc))[0]
elif single_doc is not None:
try:
obj = pandas
for name in self.single_doc.split('.'):
for name in single_doc.split('.'):
obj = getattr(obj, name)
except AttributeError:
raise ValueError('Single document not understood, it should '
'be a file in doc/source/*.rst (e.g. '
'"contributing.rst" or a pandas function or '
'method (e.g. "pandas.DataFrame.head")')
else:
return 'docstring'
self.single_doc_type = 'docstring'
if single_doc.startswith('pandas.'):
self.single_doc = single_doc[len('pandas.'):]
else:
self.single_doc = single_doc

def _copy_generated_docstring(self, method):
def _copy_generated_docstring(self):
"""Copy existing generated (from api.rst) docstring page because
this is more correct in certain cases (where a custom autodoc
template is used).
"""
fname = os.path.join(SOURCE_PATH, 'generated',
'pandas.{}.rst'.format(method))
'pandas.{}.rst'.format(self.single_doc))
temp_dir = os.path.join(SOURCE_PATH, 'generated_single')

try:
Expand All @@ -156,25 +168,15 @@ def _copy_generated_docstring(self, method):

def _generate_index(self):
"""Create index.rst file with the specified sections."""
if self.single_doc_type == 'rst':
single_doc = os.path.splitext(os.path.basename(self.single_doc))[0]
self.include_api = False
elif self.single_doc_type == 'docstring':
if self.single_doc.startswith('pandas.'):
single_doc = self.single_doc[len('pandas.'):]
else:
single_doc = self.single_doc
self.include_api = False
self._copy_generated_docstring(single_doc)
elif self.single_doc_type == 'api':
single_doc = 'api'
if self.single_doc_type == 'docstring':
self._copy_generated_docstring()

with open(os.path.join(SOURCE_PATH, 'index.rst.template')) as f:
t = jinja2.Template(f.read())

with open(os.path.join(SOURCE_PATH, 'index.rst'), 'w') as f:
f.write(t.render(include_api=self.include_api,
single_doc=single_doc,
single_doc=self.single_doc,
single_doc_type=self.single_doc_type))

@staticmethod
Expand Down Expand Up @@ -229,9 +231,13 @@ def _sphinx_build(self, kind):
os.path.join(BUILD_PATH, kind))

def _open_browser(self):
url = os.path.join(
'file://', DOC_PATH, 'build', 'html',
'generated_single', '{}.html'.format(self.single_doc))
base_url = os.path.join('file://', DOC_PATH, 'build', 'html')
if self.single_doc_type == 'docstring':
url = os.path.join(
base_url,
'generated_single', 'pandas.{}.html'.format(self.single_doc))
else:
url = os.path.join(base_url, '{}.html'.format(self.single_doc))
webbrowser.open(url, new=2)

def html(self):
Expand Down

0 comments on commit 512d3a6

Please sign in to comment.