Skip to content

Commit

Permalink
Implementing single api doc building using temp directory
Browse files Browse the repository at this point in the history
  • Loading branch information
datapythonista committed Feb 22, 2018
1 parent ca27ee9 commit 5b4cc3b
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions doc/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import shutil
import subprocess
import argparse
import tempfile
from contextlib import contextmanager
import jinja2

Expand Down Expand Up @@ -123,14 +124,18 @@ def _run_os(*args):
"""
subprocess.check_call(args, stderr=subprocess.STDOUT)

def _sphinx_build(self, kind):
def _sphinx_build(self, kind, source_path=None, build_path=None):
"""Call sphinx to build documentation.
Attribute `num_jobs` from the class is used.
Parameters
----------
kind : {'html', 'latex'}
source_path: str or None
Directory with the sources to build
build_path: str or None
Target directory where built files will be generated
Examples
--------
Expand All @@ -139,13 +144,18 @@ def _sphinx_build(self, kind):
if kind not in ('html', 'latex'):
raise ValueError('kind must be html or latex, not {}'.format(kind))

if source_path is None:
source_path = SOURCE_PATH
if build_path is None:
build_path = os.path.join(BUILD_PATH, kind)

self._run_os('sphinx-build',
'-j{}'.format(self.num_jobs),
'-b{}'.format(kind),
'-d{}'.format(os.path.join(BUILD_PATH,
'doctrees')),
SOURCE_PATH,
os.path.join(BUILD_PATH, kind))
source_path,
build_path)

def html(self):
"""Build HTML documentation."""
Expand Down Expand Up @@ -199,6 +209,42 @@ def zip_html(self):
'-q',
*fnames)

def html_single(self, method='pandas.DataFrame.reset_index'):
# TODO: call apidoc?
temp_dir = tempfile.mkdtemp()
os.mkdir(os.path.join(temp_dir, 'source'))
os.mkdir(os.path.join(temp_dir, 'build'))
symlinks = ('sphinxext',
'_templates',
os.path.join('source', '_static'),
os.path.join('source', 'themes'))
for dirname in symlinks:
os.symlink(os.path.join(DOC_PATH, dirname),
os.path.join(temp_dir, dirname),
target_is_directory=True)
os.symlink(os.path.join(DOC_PATH, 'source', 'conf.py'),
os.path.join(temp_dir, 'source', 'conf.py'),
target_is_directory=False)
os.symlink(os.path.join(DOC_PATH, 'source', 'generated',
'{}.rst'.format(method)),
os.path.join(temp_dir, 'source', '{}.rst'.format(method)),
target_is_directory=False)

idx_content = '.. toctree::\n\t:maxdepth: 2\n\t\n\t{}'.format(method)
with open(os.path.join(temp_dir, 'source', 'index.rst'), 'w') as f:
f.write(idx_content)

self._sphinx_build('html',
os.path.join(temp_dir, 'source'),
os.path.join(temp_dir, 'build'))

os.makedirs(os.path.join(BUILD_PATH, 'html', 'generated'))
shutil.copy(
os.path.join(temp_dir, 'build', '{}.html'.format(method)),
os.path.join(BUILD_PATH, 'html', 'generated',
'{}.html'.format(method)))
shutil.rmtree(temp_dir)


def main():
cmds = [method for method in dir(DocBuilder) if not method.startswith('_')]
Expand Down

0 comments on commit 5b4cc3b

Please sign in to comment.