diff --git a/.clang-tidy b/.clang-tidy
new file mode 100644
index 00000000..03368ffa
--- /dev/null
+++ b/.clang-tidy
@@ -0,0 +1,2 @@
+Checks: '-*,modernize-*,cppcoreguidelines-*,-modernize-use-trailing-return-type'
+HeaderFilterRegex: 'triqs_dft_tools'
diff --git a/.dockerignore b/.dockerignore
index 6e7a76a5..b627e92a 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -1,2 +1,4 @@
+.travis.yml
Dockerfile
Jenkinsfile
+.git/objects/pack
diff --git a/.github/ISSUE_TEMPLATE/bug.md b/.github/ISSUE_TEMPLATE/bug.md
index 79dfa304..db102d9e 100644
--- a/.github/ISSUE_TEMPLATE/bug.md
+++ b/.github/ISSUE_TEMPLATE/bug.md
@@ -32,7 +32,7 @@ Please provide the application version that you used.
You can get this information from copy and pasting the output of
```bash
-python -c "from app4triqs.version import *; show_version(); show_git_hash();"
+python -c "from triqs_dft_tools.version import *; show_version(); show_git_hash();"
```
from the command line. Also, please include the OS you are running and its version.
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 00000000..226118b4
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+compile_commands.json
+doc/cpp2rst_generated
diff --git a/.travis.yml b/.travis.yml
index 1b2df662..7ffe737b 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,47 +1,34 @@
language: cpp
sudo: required
-dist: trusty
+dist: bionic
compiler:
- gcc
- # - clang
+ - clang
before_install:
- - sudo add-apt-repository 'deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-5.0 main' -y
- - wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
- - sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
- sudo apt-get update
- - sudo apt-get install -y --allow-unauthenticated g++-7 clang-5.0
- - export LIBRARY_PATH=/usr/lib/llvm-5.0/lib:$LIBRARY_PATH
- - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 60 --slave /usr/bin/g++ g++ /usr/bin/g++-7
- - sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-5.0 60 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-5.0
- - sudo apt-get install -y --allow-unauthenticated libboost-all-dev cmake git libgfortran3 gfortran openmpi-bin openmpi-common openmpi-doc libopenmpi-dev libblas-dev liblapack-dev libfftw3-dev libgmp-dev hdf5-tools libhdf5-serial-dev python-h5py python-dev python-numpy python-scipy python-jinja2 python-virtualenv python-matplotlib python-tornado python-zmq python-mpi4py python-mako clang-format-5.0 libclang-5.0-dev python-clang-5.0 python-sphinx libjs-mathjax valgrind libnfft3-dev
+ - sudo apt-get install -y --allow-unauthenticated libblas-dev libboost-all-dev libfftw3-dev libgfortran3 libhdf5-serial-dev libgmp-dev liblapack-dev libopenmpi-dev libclang-dev python-clang-6.0 python-dev python-h5py python-mako python-matplotlib python-mpi4py python-numpy python-scipy python-sphinx libjs-mathjax libnfft3-dev
install: true
script:
- # ===== Set up Cpp2Py
- - git clone https://github.com/triqs/cpp2py
- - mkdir cpp2py/build && cd cpp2py/build
- - git checkout master
- - cmake .. -DCMAKE_CXX_COMPILER=/usr/bin/${CXX} -DPYTHON_INTERPRETER=/usr/bin/python -DCMAKE_INSTALL_PREFIX=$TRAVIS_BUILD_DIR/root_install
- - make -j8 install
- - cd $TRAVIS_BUILD_DIR
- - source root_install/share/cpp2pyvars.sh
+ - export INSTALL_DIR=$HOME/root_install # We install outside the repository
# ===== Set up TRIQS
- - git clone https://github.com/TRIQS/triqs --branch $TRAVIS_BRANCH
- - mkdir triqs/build && cd triqs/build
- - cmake .. -DCMAKE_CXX_COMPILER=/usr/bin/${CXX} -DBuild_Tests=OFF -DCMAKE_INSTALL_PREFIX=$TRAVIS_BUILD_DIR/root_install -DCMAKE_BUILD_TYPE=Debug
- - make -j8 install
- cd $TRAVIS_BUILD_DIR
- - source root_install/share/triqsvars.sh
- # ===== Set up dft_tools and Test using fsanitize=address
+ - git clone https://github.com/TRIQS/triqs --branch unstable
+ - mkdir triqs/build && cd triqs/build
+ - cmake .. -DBuild_Tests=OFF -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR
+ - make -j2 install
+ - source $INSTALL_DIR/share/triqsvars.sh
+ # ===== Set up triqs_dft_tools and test
+ - cd $TRAVIS_BUILD_DIR
- mkdir build && cd build
- - cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER=/usr/bin/${CXX} -DCMAKE_CXX_FLAGS='-fsanitize=address -fno-omit-frame-pointer -fuse-ld=gold'
- - make -j8
- - export ASAN_SYMBOLIZER_PATH=/usr/lib/llvm-5.0/bin/llvm-symbolizer
+ - cmake .. -DASAN=ON -DUBSAN=ON
+ - export UBSAN_SYMBOLIZER_PATH=$(which llvm-symbolizer)
+ - export ASAN_SYMBOLIZER_PATH=$(which llvm-symbolizer)
+ - export UBSAN_OPTIONS=symbolize=1:print_stacktrace=1
- export ASAN_OPTIONS=symbolize=1:detect_leaks=0
- export CTEST_OUTPUT_ON_FAILURE=1
- - if [ "$CXX" = g++ ]; then export LD_PRELOAD=/usr/lib/gcc/x86_64-linux-gnu/7/libasan.so; elif [ "$CXX" = clang++ ]; then export LD_PRELOAD=/usr/lib/llvm-5.0/lib/clang/5.0.1/lib/linux/libclang_rt.asan-x86_64.so; fi
- - cd test && ctest
+ - make -j2 && make test
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 669c61a2..ceeeaf6f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,105 +1,164 @@
-# Start configuration
-cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
-project(triqs_dft_tools C CXX Fortran)
+# ##############################################################################
+#
+# triqs_dft_tools - An example application using triqs and cpp2py
+#
+# Copyright (C) ...
+#
+# triqs_dft_tools is free software: you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# triqs_dft_tools is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# triqs_dft_tools (in the file COPYING.txt in this directory). If not, see
+# .
+#
+# ##############################################################################
+
+cmake_minimum_required(VERSION 3.3.2 FATAL_ERROR)
+cmake_policy(VERSION 3.3.2)
if(POLICY CMP0074)
cmake_policy(SET CMP0074 NEW)
endif()
+if(POLICY CMP0077)
+ cmake_policy(SET CMP0077 NEW)
+endif()
+
+# ############
+# Define Project
+project(triqs_dft_tools VERSION 3.0.0 LANGUAGES C CXX Fortran)
+get_directory_property(IS_SUBPROJECT PARENT_DIRECTORY)
+
+# ############
+# Load TRIQS and CPP2PY
+find_package(TRIQS 3.0 REQUIRED)
+
+# Get the git hash & print status
+triqs_get_git_hash_of_source_dir(PROJECT_GIT_HASH)
+message(STATUS "${PROJECT_NAME} version : ${PROJECT_VERSION}")
+message(STATUS "${PROJECT_NAME} Git hash: ${PROJECT_GIT_HASH}")
+
+# Enforce Consistent Versioning
+if(NOT ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} VERSION_EQUAL ${TRIQS_VERSION_MAJOR}.${TRIQS_VERSION_MINOR})
+ message(FATAL_ERROR "The ${PROJECT_NAME} version ${PROJECT_VERSION} is not compatible with TRIQS version ${TRIQS_VERSION}.")
+endif()
+
+# Default Install directory to TRIQS_ROOT if not given or invalid.
+if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT OR (NOT IS_ABSOLUTE ${CMAKE_INSTALL_PREFIX}))
+ message(STATUS "No install prefix given (or invalid). Defaulting to TRIQS_ROOT")
+ set(CMAKE_INSTALL_PREFIX ${TRIQS_ROOT} CACHE PATH "default install path" FORCE)
+ set(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT FALSE)
+endif()
+if(NOT IS_SUBPROJECT)
+ message(STATUS "-------- CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX} --------")
+endif()
+set(${PROJECT_NAME}_BINARY_DIR ${PROJECT_BINARY_DIR} CACHE STRING "Binary directory of the ${PROJECT_NAME} Project")
+
+
+# ############
+# Options
+
+# Make additional Find Modules available
+list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/share/cmake/Modules)
# Default to Release build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Type of build" FORCE)
endif()
-message( STATUS "-------- BUILD-TYPE: ${CMAKE_BUILD_TYPE} --------")
-
-# Use shared libraries
-set(BUILD_SHARED_LIBS ON)
-
-# Load TRIQS and Cpp2Py
-find_package(TRIQS 2.2 REQUIRED)
-find_package(Cpp2Py 1.6 REQUIRED)
-
-if (NOT ${TRIQS_WITH_PYTHON_SUPPORT})
- MESSAGE(FATAL_ERROR "dft_tools require Python support in TRIQS")
+if(NOT IS_SUBPROJECT)
+ message(STATUS "-------- BUILD-TYPE: ${CMAKE_BUILD_TYPE} --------")
endif()
-# Default Install directory to TRIQS_ROOT if not given. Checks an absolute name is given.
-if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT OR (NOT IS_ABSOLUTE ${CMAKE_INSTALL_PREFIX}))
- message(STATUS " No install prefix given (or invalid). Defaulting to TRIQS_ROOT")
- set(CMAKE_INSTALL_PREFIX ${TRIQS_ROOT} CACHE PATH "default install path" FORCE)
+# Python Support
+option(PythonSupport "Build with Python support" ON)
+if(PythonSupport AND NOT TRIQS_WITH_PYTHON_SUPPORT)
+ message(FATAL_ERROR "TRIQS was installed without Python support. Cannot build the Python Interface. Disable the build with -DPythonSupport=OFF")
endif()
-message(STATUS "-------- CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX} -------------")
-# Define the dft_tools version numbers and get the git hash
-set(DFT_TOOLS_VERSION_MAJOR 2)
-set(DFT_TOOLS_VERSION_MINOR 2)
-set(DFT_TOOLS_VERSION_PATCH 0)
-set(DFT_TOOLS_VERSION ${DFT_TOOLS_VERSION_MAJOR}.${DFT_TOOLS_VERSION_MINOR}.${DFT_TOOLS_VERSION_PATCH})
-triqs_get_git_hash_of_source_dir(DFT_TOOLS_GIT_HASH)
-message(STATUS "Dft_tools version : ${DFT_TOOLS_VERSION}")
-message(STATUS "Git hash: ${DFT_TOOLS_GIT_HASH}")
+# Documentation
+option(Build_Documentation "Build documentation" OFF)
+# Testing
+option(Build_Tests "Build tests" ON)
+if(Build_Tests)
+ enable_testing()
+endif()
+
+# Export the list of compile-commands into compile_commands.json
+set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
+
+# Global compiler options
+option(BUILD_SHARED_LIBS "Enable compilation of shared libraries" OFF)
+add_compile_options($<$:-ggdb3>)
+
+# Create an Interface target for compiler warnings
+add_library(${PROJECT_NAME}_warnings INTERFACE)
+target_compile_options(${PROJECT_NAME}_warnings
+ INTERFACE
+ -Wall
+ -Wextra
+ -Wpedantic
+ -Wno-sign-compare
+ $<$:-Wshadow=local>
+ $<$:-Wno-attributes>
+ $<$:-Wshadow>
+ $<$:-Wno-gcc-compat>
+ $<$:-Wshadow>
+ $<$:-Wno-gcc-compat>
+)
+
+# #############
+# Build Project
+
+# Find / Build dependencies
+add_subdirectory(deps)
+
+# Build and install the library
+add_subdirectory(c++/${PROJECT_NAME})
+
+# add here stuff for the Fortran part in DFTTools
add_subdirectory(fortran/dmftproj)
-# Add the compiling options (-D... ) for C++
-message(STATUS "TRIQS : Adding compilation flags detected by the library (C++11/14, libc++, etc...) ")
-
-add_subdirectory(c++)
-add_subdirectory(python python/triqs_dft_tools)
-add_subdirectory(shells)
-
-#------------------------
-# tests
-#------------------------
-
-option(TEST_COVERAGE "Analyze the coverage of tests" OFF)
-
-# perform tests with coverage info
-if (${TEST_COVERAGE})
- # we try to locate the coverage program
- find_program(PYTHON_COVERAGE python-coverage)
- find_program(PYTHON_COVERAGE coverage)
- if(NOT PYTHON_COVERAGE)
- message(FATAL_ERROR "Program coverage (or python-coverage) not found.\nEither set PYTHON_COVERAGE explicitly or disable TEST_COVERAGE!\nYou need to install the python package coverage, e.g. with\n pip install coverage\nor with\n apt install python-coverage")
- endif()
-
- message(STATUS "Setting up test coverage")
- add_custom_target(coverage ${PYTHON_COVERAGE} combine --append .coverage plovasp/.coverage || true COMMAND ${PYTHON_COVERAGE} html COMMAND echo "Open ${CMAKE_BINARY_DIR}/test/htmlcov/index.html in browser!" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test)
+# Tests
+if(Build_Tests)
+ add_subdirectory(test)
endif()
-enable_testing()
-
-option(Build_Tests "Build the tests of the library " ON)
-if (Build_Tests)
- message(STATUS "-------- Preparing tests -------------")
- add_subdirectory(test)
+# Python
+if(PythonSupport)
+ add_subdirectory(python/${PROJECT_NAME})
endif()
-#------------------------
-# Documentation
-#------------------------
-option(Build_Documentation "Build documentation" OFF)
-if(${Build_Documentation})
- if(NOT ${TRIQS_WITH_DOCUMENTATION})
- message("Error: TRIQS library has not been compiled with its documentation")
- endif()
+# Docs
+if(Build_Documentation)
add_subdirectory(doc)
endif()
-#--------------------------------------------------------
-# Packaging
-#--------------------------------------------------------
+# dfttols vasp interface bash scripts
+add_subdirectory(bin)
+
+# Additional configuration files
+add_subdirectory(share)
+
+# #############
+# Debian Package
+
option(BUILD_DEBIAN_PACKAGE "Build a deb package" OFF)
-if(BUILD_DEBIAN_PACKAGE)
+if(BUILD_DEBIAN_PACKAGE AND NOT IS_SUBPROJECT)
if(NOT CMAKE_INSTALL_PREFIX STREQUAL "/usr")
message(FATAL_ERROR "CMAKE_INSTALL_PREFIX must be /usr for packaging")
endif()
- SET(CPACK_GENERATOR "DEB")
- SET(CPACK_PACKAGE_VERSION ${DFT_TOOLS_VERSION})
- SET(CPACK_PACKAGE_CONTACT "https://github.com/TRIQS/dft_tools")
- EXECUTE_PROCESS(COMMAND dpkg --print-architecture OUTPUT_VARIABLE CMAKE_DEBIAN_PACKAGE_ARCHITECTURE OUTPUT_STRIP_TRAILING_WHITESPACE)
- SET(CPACK_DEBIAN_PACKAGE_DEPENDS "triqs (>= 2.2)")
- SET(CPACK_DEBIAN_PACKAGE_CONFLICTS "dft_tools")
- SET(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
- SET(CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS ON)
- INCLUDE(CPack)
+ set(CPACK_PACKAGE_NAME ${PROJECT_NAME})
+ set(CPACK_GENERATOR "DEB")
+ set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
+ set(CPACK_PACKAGE_CONTACT "https://github.com/TRIQS/${PROJECT_NAME}")
+ execute_process(COMMAND dpkg --print-architecture OUTPUT_VARIABLE CMAKE_DEBIAN_PACKAGE_ARCHITECTURE OUTPUT_STRIP_TRAILING_WHITESPACE)
+ set(CPACK_DEBIAN_PACKAGE_DEPENDS "triqs (>= 3.0)")
+ set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
+ set(CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS ON)
+ include(CPack)
endif()
diff --git a/ChangeLog.md b/ChangeLog.md
new file mode 120000
index 00000000..9e080e26
--- /dev/null
+++ b/ChangeLog.md
@@ -0,0 +1 @@
+doc/ChangeLog.md
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
index 1d7149fb..e05e9a12 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,12 +1,16 @@
# See ../triqs/packaging for other options
-FROM flatironinstitute/triqs:master-ubuntu-clang
+FROM flatironinstitute/triqs:unstable-ubuntu-clang
+ARG APPNAME=triqs_dft_tools
-ARG APPNAME
-COPY . $SRC/$APPNAME
+COPY requirements.txt /src/$APPNAME/requirements.txt
+RUN pip3 install -r /src/$APPNAME/requirements.txt
+
+COPY --chown=build . $SRC/$APPNAME
WORKDIR $BUILD/$APPNAME
RUN chown build .
USER build
ARG BUILD_DOC=0
-RUN cmake $SRC/$APPNAME -DTRIQS_ROOT=${INSTALL} -DBuild_Documentation=${BUILD_DOC} && make -j2 && make test CTEST_OUTPUT_ON_FAILURE=1
+ARG BUILD_ID
+RUN cmake $SRC/$APPNAME -DTRIQS_ROOT=${INSTALL} -DBuild_Documentation=${BUILD_DOC} -DBuild_Deps=Always && make -j2 || make -j1 VERBOSE=1
USER root
RUN make install
diff --git a/Jenkinsfile b/Jenkinsfile
index 0cf7bdea..9338c5d1 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -1,5 +1,6 @@
def projectName = "dft_tools" /* set to app/repo name */
+def dockerName = projectName.toLowerCase();
/* which platform to build documentation on */
def documentationPlatform = "ubuntu-clang"
/* depend on triqs upstream branch/project */
@@ -37,7 +38,12 @@ for (int i = 0; i < dockerPlatforms.size(); i++) {
mv -f Dockerfile.jenkins Dockerfile
"""
/* build and tag */
- def img = docker.build("flatironinstitute/${projectName}:${env.BRANCH_NAME}-${env.STAGE_NAME}", "--build-arg APPNAME=${projectName} --build-arg BUILD_DOC=${platform==documentationPlatform} .")
+ def img = docker.build("flatironinstitute/${dockerName}:${env.BRANCH_NAME}-${env.STAGE_NAME}", "--build-arg APPNAME=${projectName} --build-arg BUILD_DOC=${platform==documentationPlatform} --build-arg BUILD_ID=${env.BUILD_TAG} .")
+ catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
+ img.inside() {
+ sh "make -C \$BUILD/${projectName} test CTEST_OUTPUT_ON_FAILURE=1"
+ }
+ }
if (!keepInstall) {
sh "docker rmi --no-prune ${img.imageName()}"
}
@@ -48,7 +54,7 @@ for (int i = 0; i < dockerPlatforms.size(); i++) {
/****************** osx builds (on host) */
def osxPlatforms = [
["gcc", ['CC=gcc-9', 'CXX=g++-9', 'FC=gfortran-9']],
- ["clang", ['CC=$BREW/opt/llvm/bin/clang', 'CXX=$BREW/opt/llvm/bin/clang++', 'CXXFLAGS=-I$BREW/opt/llvm/include', 'LDFLAGS=-L$BREW/opt/llvm/lib', 'FC=gfortran-9']]
+ ["clang", ['CC=$BREW/opt/llvm/bin/clang', 'CXX=$BREW/opt/llvm/bin/clang++', 'FC=gfortran-9', 'CXXFLAGS=-I$BREW/opt/llvm/include', 'LDFLAGS=-L$BREW/opt/llvm/lib']]
]
for (int i = 0; i < osxPlatforms.size(); i++) {
def platformEnv = osxPlatforms[i]
@@ -58,29 +64,37 @@ for (int i = 0; i < osxPlatforms.size(); i++) {
def srcDir = pwd()
def tmpDir = pwd(tmp:true)
def buildDir = "$tmpDir/build"
+ /* install real branches in a fixed predictable place so apps can find them */
def installDir = keepInstall ? "${env.HOME}/install/${projectName}/${env.BRANCH_NAME}/${platform}" : "$tmpDir/install"
def triqsDir = "${env.HOME}/install/triqs/${triqsBranch}/${platform}"
+ def venv = triqsDir
dir(installDir) {
deleteDir()
}
checkout scm
+
+ def hdf5 = "${env.BREW}/opt/hdf5@1.10"
dir(buildDir) { withEnv(platformEnv[1].collect { it.replace('\$BREW', env.BREW) } + [
- "PATH=$triqsDir/bin:${env.BREW}/bin:/usr/bin:/bin:/usr/sbin",
- "CPLUS_INCLUDE_PATH=$triqsDir/include:${env.BREW}/include",
- "LIBRARY_PATH=$triqsDir/lib:${env.BREW}/lib",
- "CMAKE_PREFIX_PATH=$triqsDir/lib/cmake/triqs"]) {
+ "PATH=$venv/bin:${env.BREW}/bin:/usr/bin:/bin:/usr/sbin",
+ "HDF5_ROOT=$hdf5",
+ "C_INCLUDE_PATH=$hdf5/include:${env.BREW}/include",
+ "CPLUS_INCLUDE_PATH=$venv/include:$hdf5/include:${env.BREW}/include",
+ "LIBRARY_PATH=$venv/lib:$hdf5/lib:${env.BREW}/lib",
+ "LD_LIBRARY_PATH=$hdf5/lib",
+ "PYTHONPATH=$installDir/lib/python3.7/site-packages",
+ "CMAKE_PREFIX_PATH=$venv/lib/cmake/triqs"]) {
deleteDir()
/* note: this is installing into the parent (triqs) venv (install dir), which is thus shared among apps and so not be completely safe */
- sh "pip install -r $srcDir/requirements.txt"
- sh "cmake $srcDir -DCMAKE_INSTALL_PREFIX=$installDir -DTRIQS_ROOT=$triqsDir"
- sh "make -j3"
- try {
+ sh "pip3 install -U -r $srcDir/requirements.txt"
+ sh "cmake $srcDir -DCMAKE_INSTALL_PREFIX=$installDir -DTRIQS_ROOT=$triqsDir -DBuild_Deps=Always"
+ sh "make -j2"
+ catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') { try {
sh "make test CTEST_OUTPUT_ON_FAILURE=1"
} catch (exc) {
archiveArtifacts(artifacts: 'Testing/Temporary/LastTest.log')
throw exc
- }
+ } }
sh "make install"
} }
} }
@@ -95,15 +109,20 @@ try {
stage("publish") { timeout(time: 5, unit: 'MINUTES') {
def commit = sh(returnStdout: true, script: "git rev-parse HEAD").trim()
def release = env.BRANCH_NAME == "master" || env.BRANCH_NAME == "unstable" || sh(returnStdout: true, script: "git describe --exact-match HEAD || true").trim()
- def workDir = pwd()
+ def workDir = pwd(tmp:true)
lock('triqs_publish') {
/* Update documention on gh-pages branch */
dir("$workDir/gh-pages") {
def subdir = "${projectName}/${env.BRANCH_NAME}"
git(url: "ssh://git@github.com/TRIQS/TRIQS.github.io.git", branch: "master", credentialsId: "ssh", changelog: false)
sh "rm -rf ${subdir}"
- docker.image("flatironinstitute/${projectName}:${env.BRANCH_NAME}-${documentationPlatform}").inside() {
- sh "cp -rp \$INSTALL/share/doc/triqs_${projectName} ${subdir}"
+ docker.image("flatironinstitute/${dockerName}:${env.BRANCH_NAME}-${documentationPlatform}").inside() {
+ sh """#!/bin/bash -ex
+ base=\$INSTALL/share/doc
+ dir="${projectName}"
+ [[ -d \$base/triqs_\$dir ]] && dir=triqs_\$dir || [[ -d \$base/\$dir ]]
+ cp -rp \$base/\$dir ${subdir}
+ """
}
sh "git add -A ${subdir}"
sh """
diff --git a/README.txt b/README.md
similarity index 100%
rename from README.txt
rename to README.md
diff --git a/shells/CMakeLists.txt b/bin/CMakeLists.txt
similarity index 80%
rename from shells/CMakeLists.txt
rename to bin/CMakeLists.txt
index 42e6bc38..0d5af60b 100644
--- a/shells/CMakeLists.txt
+++ b/bin/CMakeLists.txt
@@ -1,9 +1,9 @@
-configure_file(plovasp.bash.in plovasp)
+configure_file(plovasp.in plovasp)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/plovasp DESTINATION bin
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
-configure_file(vasp_dmft.bash.in vasp_dmft)
+configure_file(vasp_dmft.in vasp_dmft)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/vasp_dmft DESTINATION bin
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
diff --git a/shells/plovasp.bash.in b/bin/plovasp.in
similarity index 100%
rename from shells/plovasp.bash.in
rename to bin/plovasp.in
diff --git a/shells/vasp_dmft.bash.in b/bin/vasp_dmft.in
similarity index 100%
rename from shells/vasp_dmft.bash.in
rename to bin/vasp_dmft.in
diff --git a/c++/plovasp/atm/.gitignore b/c++/plovasp/atm/.gitignore
deleted file mode 100644
index 675673c3..00000000
--- a/c++/plovasp/atm/.gitignore
+++ /dev/null
@@ -1,5 +0,0 @@
-makefile
-Makefile
-*.so
-*.o
-*.pyc
diff --git a/c++/plovasp/atm/CMakeLists.txt b/c++/plovasp/atm/CMakeLists.txt
deleted file mode 100644
index afcb0176..00000000
--- a/c++/plovasp/atm/CMakeLists.txt
+++ /dev/null
@@ -1,7 +0,0 @@
-add_library(atm_c dos_tetra3d.hpp dos_tetra3d.cpp argsort.hpp argsort.cpp)
-target_link_libraries(atm_c triqs)
-target_compile_options(atm_c PRIVATE -std=c++17)
-
-install(TARGETS atm_c DESTINATION lib)
-
-add_subdirectory(test)
diff --git a/c++/plovasp/atm/test/CMakeLists.txt b/c++/plovasp/atm/test/CMakeLists.txt
deleted file mode 100644
index d592f23c..00000000
--- a/c++/plovasp/atm/test/CMakeLists.txt
+++ /dev/null
@@ -1,13 +0,0 @@
-enable_testing()
-
-FILE(GLOB TestList RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
-FOREACH( TestName1 ${TestList} )
- STRING(REPLACE ".cpp" "" TestName ${TestName1})
- add_executable( ${TestName} ${TestName}.cpp )
- target_link_libraries( ${TestName} atm_c triqs)
- triqs_set_rpath_for_target( ${TestName} )
- add_test(NAME ${TestName} COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${t})
-ENDFOREACH( TestName1 ${TestList} )
-
-
-
diff --git a/c++/triqs_dft_tools/CMakeLists.txt b/c++/triqs_dft_tools/CMakeLists.txt
new file mode 100644
index 00000000..e847bf2f
--- /dev/null
+++ b/c++/triqs_dft_tools/CMakeLists.txt
@@ -0,0 +1,81 @@
+file(GLOB_RECURSE sources *.cpp)
+add_library(${PROJECT_NAME}_c ${sources})
+add_library(${PROJECT_NAME}::${PROJECT_NAME}_c ALIAS ${PROJECT_NAME}_c)
+
+# Link against triqs and enable warnings
+target_link_libraries(${PROJECT_NAME}_c PUBLIC triqs PRIVATE $)
+
+# Configure target and compilation
+set_property(TARGET ${PROJECT_NAME}_c PROPERTY POSITION_INDEPENDENT_CODE ON)
+target_include_directories(${PROJECT_NAME}_c PUBLIC $)
+target_include_directories(${PROJECT_NAME}_c SYSTEM INTERFACE $)
+target_compile_definitions(${PROJECT_NAME}_c PUBLIC
+ TRIQS_DFT_TOOLS_GIT_HASH=${PROJECT_GIT_HASH}
+ TRIQS_GIT_HASH=${TRIQS_GIT_HASH}
+ $<$:TRIQS_DFT_TOOLS_DEBUG>
+ $<$:TRIQS_DEBUG>
+ $<$:TRIQS_ARRAYS_ENFORCE_BOUNDCHECK>
+ )
+
+# Install library and headers
+install(TARGETS ${PROJECT_NAME}_c EXPORT ${PROJECT_NAME}-targets DESTINATION lib)
+install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} DESTINATION include FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h")
+
+
+# ========= Static Analyzer Checks ==========
+
+option(ANALYZE_SOURCES OFF "Run static analyzer checks if found (clang-tidy, cppcheck)")
+if(ANALYZE_SOURCES)
+
+ # Locate static analyzer tools
+ find_program(CPPCHECK_EXECUTABLE NAMES "cppcheck" PATHS ENV PATH)
+ find_program(CLANG_TIDY_EXECUTABLE NAMES "clang-tidy" PATHS ENV PATH)
+
+ # Run clang-tidy if found
+ if(CLANG_TIDY_EXECUTABLE)
+ message(STATUS "clang-tidy found: ${CLANG_TIDY_EXECUTABLE}")
+ set_target_properties(${PROJECT_NAME}_c PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXECUTABLE}")
+ else()
+ message(STATUS "clang-tidy not found in $PATH. Please consider installing clang-tidy for additional checks!")
+ endif()
+
+ # Run cppcheck if found
+ if(CPPCHECK_EXECUTABLE)
+ message(STATUS "cppcheck found: ${CPPCHECK_EXECUTABLE}")
+ add_custom_command(
+ TARGET ${PROJECT_NAME}_c
+ COMMAND ${CPPCHECK_EXECUTABLE}
+ --enable=warning,style,performance,portability
+ --std=c++17
+ --template=gcc
+ --verbose
+ --force
+ --quiet
+ ${sources}
+ WORKING_DIRECTORY
+ ${CMAKE_CURRENT_SOURCE_DIR}
+ )
+ else()
+ message(STATUS "cppcheck not found in $PATH. Please consider installing cppcheck for additional checks!")
+ endif()
+
+endif()
+
+
+# ========= Dynamic Analyzer Checks ==========
+
+option(ASAN OFF "Compile library and executables with LLVM Address Sanitizer")
+option(UBSAN OFF "Compile library and executables with LLVM Undefined Behavior Sanitizer")
+
+if(ASAN)
+ if(NOT TARGET asan)
+ find_package(sanitizer REQUIRED "asan")
+ endif()
+ target_link_libraries(${PROJECT_NAME}_c PUBLIC $)
+endif()
+if(UBSAN)
+ if(NOT TARGET ubsan)
+ find_package(sanitizer REQUIRED "ubsan")
+ endif()
+ target_link_libraries(${PROJECT_NAME}_c PUBLIC $)
+endif()
diff --git a/c++/triqs_dft_tools/converters/vasp.hpp b/c++/triqs_dft_tools/converters/vasp.hpp
new file mode 100644
index 00000000..0aa018d7
--- /dev/null
+++ b/c++/triqs_dft_tools/converters/vasp.hpp
@@ -0,0 +1,3 @@
+#pragma once
+#include "./vasp/argsort.hpp"
+#include "./vasp/dos_tetra3d.hpp"
diff --git a/c++/plovasp/atm/__init__.py b/c++/triqs_dft_tools/converters/vasp/__init__.py
similarity index 100%
rename from c++/plovasp/atm/__init__.py
rename to c++/triqs_dft_tools/converters/vasp/__init__.py
diff --git a/c++/plovasp/atm/argsort.cpp b/c++/triqs_dft_tools/converters/vasp/argsort.cpp
similarity index 100%
rename from c++/plovasp/atm/argsort.cpp
rename to c++/triqs_dft_tools/converters/vasp/argsort.cpp
diff --git a/c++/plovasp/atm/argsort.hpp b/c++/triqs_dft_tools/converters/vasp/argsort.hpp
similarity index 100%
rename from c++/plovasp/atm/argsort.hpp
rename to c++/triqs_dft_tools/converters/vasp/argsort.hpp
diff --git a/c++/plovasp/atm/dos_tetra3d.cpp b/c++/triqs_dft_tools/converters/vasp/dos_tetra3d.cpp
similarity index 100%
rename from c++/plovasp/atm/dos_tetra3d.cpp
rename to c++/triqs_dft_tools/converters/vasp/dos_tetra3d.cpp
diff --git a/c++/plovasp/atm/dos_tetra3d.hpp b/c++/triqs_dft_tools/converters/vasp/dos_tetra3d.hpp
similarity index 86%
rename from c++/plovasp/atm/dos_tetra3d.hpp
rename to c++/triqs_dft_tools/converters/vasp/dos_tetra3d.hpp
index abdda06b..5420f9f2 100644
--- a/c++/plovasp/atm/dos_tetra3d.hpp
+++ b/c++/triqs_dft_tools/converters/vasp/dos_tetra3d.hpp
@@ -22,16 +22,14 @@
#include
-using triqs::arrays::array;
-using triqs::arrays::array_view;
/// DOS of a band by analytical tetrahedron method
///
/// Returns corner weights for all tetrahedra for a given band and real energy.
-array
-dos_tetra_weights_3d(array_view eigk, /// Band energies for each k-point
+triqs::arrays::array
+dos_tetra_weights_3d(triqs::arrays::array_view eigk, /// Band energies for each k-point
double en, /// Energy at which DOS weights are to be calculated
- array_view itt /// Tetrahedra defined by k-point indices
+ triqs::arrays::array_view itt /// Tetrahedra defined by k-point indices
);
//array
//dos_tetra_weights_3d(array eigk, /// Band energies for each k-point
diff --git a/c++/plovasp/atm/makefile.linux b/c++/triqs_dft_tools/converters/vasp/makefile.linux
similarity index 100%
rename from c++/plovasp/atm/makefile.linux
rename to c++/triqs_dft_tools/converters/vasp/makefile.linux
diff --git a/c++/plovasp/atm/setup.py b/c++/triqs_dft_tools/converters/vasp/setup.py
similarity index 100%
rename from c++/plovasp/atm/setup.py
rename to c++/triqs_dft_tools/converters/vasp/setup.py
diff --git a/c++/triqs_dft_tools/triqs_dft_tools.hpp b/c++/triqs_dft_tools/triqs_dft_tools.hpp
new file mode 100644
index 00000000..ee70a5dc
--- /dev/null
+++ b/c++/triqs_dft_tools/triqs_dft_tools.hpp
@@ -0,0 +1,3 @@
+#pragma once
+#include "./converters/vasp.hpp"
+
diff --git a/cmake/sitecustomize.py b/cmake/sitecustomize.py
deleted file mode 100644
index 0f31ba93..00000000
--- a/cmake/sitecustomize.py
+++ /dev/null
@@ -1,8 +0,0 @@
-def application_pytriqs_import(name,*args,**kwargs):
- if name.startswith('@package_name@'):
- name = name[len('@package_name@')+1:]
- return builtin_import(name,*args,**kwargs)
-
-import __builtin__
-__builtin__.__import__, builtin_import = application_pytriqs_import, __builtin__.__import__
-
diff --git a/deps/.gitignore b/deps/.gitignore
new file mode 100644
index 00000000..72e8ffc0
--- /dev/null
+++ b/deps/.gitignore
@@ -0,0 +1 @@
+*
diff --git a/deps/CMakeLists.txt b/deps/CMakeLists.txt
new file mode 100644
index 00000000..25c49960
--- /dev/null
+++ b/deps/CMakeLists.txt
@@ -0,0 +1,66 @@
+include(external_dependency.cmake)
+
+# Add your dependencies with the function
+#
+# external_dependency(name
+# [VERSION ]
+# [GIT_REPO ]
+# [GIT_TAG ]
+# [BUILD_ALWAYS]
+# [EXCLUDE_FROM_ALL]
+# )
+#
+# Resolve the dependency using the following steps in order.
+# If a step was successful, skip the remaining ones.
+#
+# 1. Use find_package(name [])
+# to locate the package in the system.
+# Skip this step if Build_Deps option is set.
+# 2. Try to find a directory containing the sources
+# at ${PROJECT_SOURCE_DIR}/deps/name. If found
+# build it as a cmake sub-project.
+# 3. If GIT_REPO is provided, git clone the sources,
+# and build them as a cmake sub-project.
+#
+# Addtional options:
+#
+# GIT_TAG - Use this keyword to specify the git-tag, branch or commit hash
+#
+# BUILD_ALWAYS - If set, this dependency will always be built from source
+# and will never be searched in the system.
+#
+# EXCLUDE_FROM_ALL - If set, targets of the dependency cmake subproject
+# will not be included in the ALL target of the project.
+# In particular the dependency will not be installed.
+
+if(NOT DEFINED Build_Deps)
+ set(Build_Deps "Never" CACHE STRING "Do we build dependencies from source? [Never/Always/IfNotFound]")
+else()
+ set(Build_Deps_Opts "Never" "Always" "IfNotFound")
+ if(NOT ${Build_Deps} IN_LIST Build_Deps_Opts)
+ message(FATAL_ERROR "Build_Deps option should be either 'Never', 'Always' or 'IfNotFound'")
+ endif()
+ set(Build_Deps ${Build_Deps} CACHE STRING "Do we build dependencies from source? [Never/Always/IfNotFound]")
+ if(NOT IS_SUBPROJECT AND NOT Build_Deps STREQUAL "Always" AND (ASAN OR UBSAN))
+ message(WARNING "For builds with llvm sanitizers (ASAN/UBSAN) it is recommended to use -DBuild_Deps=Always to avoid false positives.")
+ endif()
+endif()
+
+# -- Cpp2Py --
+if(PythonSupport OR Build_Documentation)
+ external_dependency(Cpp2Py
+ GIT_REPO https://github.com/TRIQS/cpp2py
+ VERSION 2.0
+ GIT_TAG master
+ BUILD_ALWAYS
+ EXCLUDE_FROM_ALL
+ )
+endif()
+
+# -- GTest --
+external_dependency(GTest
+ GIT_REPO https://github.com/google/googletest
+ GIT_TAG release-1.10.0
+ BUILD_ALWAYS
+ EXCLUDE_FROM_ALL
+)
diff --git a/deps/external_dependency.cmake b/deps/external_dependency.cmake
new file mode 100644
index 00000000..d29225eb
--- /dev/null
+++ b/deps/external_dependency.cmake
@@ -0,0 +1,70 @@
+###################################################################################
+#
+# TRIQS_DFT_TOOLS: a Toolbox for Research in Interacting Quantum Systems
+#
+# Copyright (C) 2020 Simons Foundation
+# authors: N. Wentzell
+#
+# TRIQS_DFT_TOOLS is free software: you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation, either version 3 of the License, or (at your option) any later
+# version.
+#
+# TRIQS_DFT_TOOLS is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# TRIQS_DFT_TOOLS. If not, see .
+#
+###################################################################################
+
+function(external_dependency)
+ cmake_parse_arguments(ARG "EXCLUDE_FROM_ALL;BUILD_ALWAYS" "VERSION;GIT_REPO;GIT_TAG" "" ${ARGN})
+
+ # -- Was dependency already found?
+ get_property(${ARGV0}_FOUND GLOBAL PROPERTY ${ARGV0}_FOUND)
+ if(${ARGV0}_FOUND)
+ message(STATUS "Dependency ${ARGV0} was already resolved.")
+ return()
+ endif()
+
+ # -- Try to find package in system.
+ if(NOT ARG_BUILD_ALWAYS AND NOT Build_Deps STREQUAL "Always")
+ find_package(${ARGV0} ${ARG_VERSION} QUIET HINTS ${CMAKE_INSTALL_PREFIX})
+ if(${ARGV0}_FOUND)
+ message(STATUS "Found dependency ${ARGV0} in system ${${ARGV0}_ROOT}")
+ return()
+ elseif(Build_Deps STREQUAL "Never")
+ message(FATAL_ERROR "Could not find dependency ${ARGV0} in system. Please install the dependency manually or use -DBuild_Deps=IfNotFound during cmake configuration to automatically build all dependencies that are not found.")
+ endif()
+ endif()
+
+ # -- Build package from source
+ message(STATUS " =============== Configuring Dependency ${ARGV0} =============== ")
+ if(ARG_EXCLUDE_FROM_ALL)
+ set(subdir_opts EXCLUDE_FROM_ALL)
+ set(Build_Tests OFF)
+ set(Build_Documentation OFF)
+ endif()
+ if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${ARGV0})
+ message(STATUS "Found sources for dependency ${ARGV0} at ${CMAKE_CURRENT_SOURCE_DIR}/${ARGV0}")
+ add_subdirectory(${ARGV0} ${subdir_opts})
+ elseif(ARG_GIT_REPO)
+ set(bin_dir ${CMAKE_CURRENT_BINARY_DIR}/${ARGV0})
+ set(src_dir ${bin_dir}_src)
+ if(NOT IS_DIRECTORY ${src_dir})
+ if(ARG_GIT_TAG)
+ set(clone_opts --branch ${ARG_GIT_TAG} -c advice.detachedHead=false)
+ endif()
+ execute_process(COMMAND git clone ${ARG_GIT_REPO} --depth 1 ${clone_opts} ${src_dir})
+ endif()
+ add_subdirectory(${src_dir} ${bin_dir} ${subdir_opts})
+ else()
+ message(FATAL_ERROR "Could not find or build dependency ${ARGV0}")
+ endif()
+ message(STATUS " =============== End ${ARGV0} Configuration =============== ")
+ set_property(GLOBAL PROPERTY ${ARGV0}_FOUND TRUE)
+
+endfunction()
diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt
index 0e137f0e..bde5a81c 100644
--- a/doc/CMakeLists.txt
+++ b/doc/CMakeLists.txt
@@ -1,23 +1,81 @@
-# generate the conf.py
+# Generate the sphinx config file
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/conf.py.in ${CMAKE_CURRENT_BINARY_DIR}/conf.py @ONLY)
+# -----------------------------------------------------------------------------
+# Create an optional target that allows us to regenerate the C++ doc with c++2rst
+# -----------------------------------------------------------------------------
+add_custom_target(${PROJECT_NAME}_docs_cpp2rst)
+include(${PROJECT_SOURCE_DIR}/share/cmake/extract_flags.cmake)
+extract_flags(${PROJECT_NAME}_c BUILD_INTERFACE)
+separate_arguments(${PROJECT_NAME}_c_CXXFLAGS)
+macro(generate_docs header_file)
+ add_custom_command(
+ TARGET ${PROJECT_NAME}_docs_cpp2rst
+ COMMAND rm -rf ${CMAKE_CURRENT_SOURCE_DIR}/cpp2rst_generated
+ COMMAND
+ PYTHONPATH=${CPP2PY_BINARY_DIR}:$ENV{PYTHONPATH}
+ PATH=${CPP2PY_BINARY_DIR}/bin:${CPP2PY_ROOT}/bin:$ENV{PATH}
+ c++2rst
+ ${header_file}
+ -N ${PROJECT_NAME}
+ --output_directory ${CMAKE_CURRENT_SOURCE_DIR}/cpp2rst_generated
+ -I${PROJECT_SOURCE_DIR}/c++
+ --cxxflags="${${PROJECT_NAME}_c_CXXFLAGS}"
+ )
+endmacro(generate_docs)
+
+generate_docs(${PROJECT_SOURCE_DIR}/c++/${PROJECT_NAME}/${PROJECT_NAME}.hpp)
+
+# --------------------------------------------------------
+# Build & Run the C++ doc examples and capture the output
+# --------------------------------------------------------
+
+add_custom_target(${PROJECT_NAME}_docs_example_output)
+file(GLOB_RECURSE ExampleList RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)
+foreach(example ${ExampleList})
+ get_filename_component(f ${example} NAME_WE)
+ get_filename_component(d ${example} DIRECTORY)
+ add_executable(${PROJECT_NAME}_doc_${f} EXCLUDE_FROM_ALL ${example})
+ set_property(TARGET ${PROJECT_NAME}_doc_${f} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${d})
+ target_link_libraries(${PROJECT_NAME}_doc_${f} triqs)
+ add_custom_command(TARGET ${PROJECT_NAME}_doc_${f}
+ COMMAND ${PROJECT_NAME}_doc_${f} > ${CMAKE_CURRENT_SOURCE_DIR}/${d}/${f}.output 2>/dev/null
+ WORKING_DIRECTORY ${d}
+ )
+ add_dependencies(${PROJECT_NAME}_docs_example_output ${PROJECT_NAME}_doc_${f})
+endforeach()
+
# ---------------------------------
# Top Sphinx target
# ---------------------------------
-# Sources
-file(GLOB_RECURSE sources *.rst)
+if(NOT DEFINED SPHINXBUILD_EXECUTABLE)
+ find_package(Sphinx)
+endif()
-# create documentation target
-set(sphinx_top ${CMAKE_CURRENT_BINARY_DIR}/html/index.html)
-add_custom_command(OUTPUT ${sphinx_top} DEPENDS ${sources}
- COMMAND ${TRIQS_SPHINXBUILD_EXECUTABLE} -c . -j8 -b html ${CMAKE_CURRENT_SOURCE_DIR} html)
-add_custom_target(doc_sphinx ALL DEPENDS ${sphinx_top} ${CMAKE_CURRENT_BINARY_DIR})
+# Sphinx has internal caching, always run it
+add_custom_target(${PROJECT_NAME}_docs_sphinx ALL)
+add_custom_command(
+ TARGET ${PROJECT_NAME}_docs_sphinx
+ COMMAND PYTHONPATH=${PROJECT_BINARY_DIR}/python:$ENV{PYTHONPATH} ${SPHINXBUILD_EXECUTABLE} -c . -j8 -b html ${CMAKE_CURRENT_SOURCE_DIR} html
+)
+
+option(Sphinx_Only "When building the documentation, skip the Python Modules and the generation of C++ Api and example outputs" OFF)
+if(NOT Sphinx_Only)
+ # Autodoc usage requires the python modules to be built first
+ get_property(CPP2PY_MODULES_LIST GLOBAL PROPERTY CPP2PY_MODULES_LIST)
+ if(CPP2PY_MODULES_LIST)
+ add_dependencies(${PROJECT_NAME}_docs_sphinx ${CPP2PY_MODULES_LIST})
+ endif()
+
+ # Generation of C++ Api and Example Outputs
+ add_dependencies(${PROJECT_NAME}_docs_sphinx ${PROJECT_NAME}_docs_cpp2rst ${PROJECT_NAME}_docs_example_output)
+endif()
# ---------------------------------
# Install
# ---------------------------------
-install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/ COMPONENT documentation DESTINATION share/doc/triqs_dft_tools
- FILES_MATCHING
- REGEX "\\.(html|pdf|png|gif|jpg|js|xsl|css|py|txt|inv|bib|cfg)$"
- PATTERN "_*"
- )
+install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html/ COMPONENT documentation DESTINATION share/doc/${PROJECT_NAME}
+ FILES_MATCHING
+ REGEX "\\.(html|pdf|png|gif|jpg|svg|js|xsl|css|py|txt|inv|bib)$"
+ PATTERN "_*"
+)
diff --git a/doc/_templates/sideb.html b/doc/_templates/sideb.html
index 24d2ffc2..563bba82 100644
--- a/doc/_templates/sideb.html
+++ b/doc/_templates/sideb.html
@@ -10,5 +10,5 @@
-
+
diff --git a/doc/changelog.rst b/doc/changelog.rst
index b1e75038..7465fef9 100644
--- a/doc/changelog.rst
+++ b/doc/changelog.rst
@@ -3,6 +3,6 @@
Changelog
=========
-This document describes the main changes in DFTTools.
+This document describes the main changes in TRIQS_DFT_TOOLS.
.. include:: ChangeLog.md
diff --git a/doc/conf.py.in b/doc/conf.py.in
index 90497936..4a931db2 100644
--- a/doc/conf.py.in
+++ b/doc/conf.py.in
@@ -3,34 +3,36 @@
# TRIQS documentation build configuration file
import sys
-sys.path.insert(0, "@TRIQS_SPHINXEXT_PATH@/numpydoc")
+sys.path.insert(0, "@CMAKE_CURRENT_SOURCE_DIR@/sphinxext")
+sys.path.insert(0, "@CMAKE_CURRENT_SOURCE_DIR@/sphinxext/numpydoc")
sys.path.insert(0, "@CMAKE_BINARY_DIR@/python")
extensions = ['sphinx.ext.autodoc',
'sphinx.ext.mathjax',
'sphinx.ext.intersphinx',
- 'matplotlib.sphinxext.plot_directive',
'sphinx.ext.doctest',
'sphinx.ext.todo',
'sphinx.ext.viewcode',
'sphinx.ext.autosummary',
- 'nbsphinx',
- 'numpydoc',
'sphinx.ext.githubpages',
- 'IPython.sphinxext.ipython_console_highlighting'
- ]
+ 'sphinx_autorun',
+ 'matplotlib.sphinxext.plot_directive',
+ 'nbsphinx',
+ 'IPython.sphinxext.ipython_console_highlighting',
+ 'numpydoc']
source_suffix = '.rst'
-project = u'TRIQS DFTTools'
-copyright = u'2011-2019'
-version = '@DFT_TOOLS_VERSION@'
+project = '@PROJECT_NAME@'
+version = '@PROJECT_VERSION@'
-mathjax_path = "@TRIQS_MATHJAX_PATH@/MathJax.js?config=default"
-templates_path = ['@CMAKE_SOURCE_DIR@/doc/_templates']
+copyright = '2011-2020'
+
+mathjax_path = "https://raw.githubusercontent.com/mathjax/MathJax/2.7.8/MathJax.js"
+templates_path = ['@CMAKE_CURRENT_SOURCE_DIR@/_templates']
html_theme = 'triqs'
-html_theme_path = ['@TRIQS_THEMES_PATH@']
+html_theme_path = ['@CMAKE_CURRENT_SOURCE_DIR@/themes']
html_show_sphinx = False
html_context = {'header_title': 'dft tools',
'header_subtitle': 'connecting TRIQS to DFT packages',
@@ -39,9 +41,9 @@ html_context = {'header_title': 'dft tools',
['Tutorials', 'tutorials'],
['Issues', 'issues'],
['About DFTTools', 'about']]}
-html_static_path = ['@CMAKE_SOURCE_DIR@/doc/_static']
+html_static_path = ['@CMAKE_CURRENT_SOURCE_DIR@/_static']
html_sidebars = {'index': ['sideb.html', 'searchbox.html']}
-htmlhelp_basename = 'TRIQSDFTToolsdoc'
+htmlhelp_basename = '@PROJECT_NAME@doc'
-intersphinx_mapping = {'python': ('http://docs.python.org/2.7', None), 'triqslibs': ('http://triqs.github.io/triqs/latest', None), 'triqscthyb': ('https://triqs.github.io/cthyb/latest', None)}
+intersphinx_mapping = {'python': ('https://docs.python.org/3.8', None), 'triqslibs': ('https://triqs.github.io/triqs/latest', None), 'triqscthyb': ('https://triqs.github.io/cthyb/latest', None)}
diff --git a/doc/contents.rst b/doc/contents.rst
index 4f990b6a..7b55d920 100644
--- a/doc/contents.rst
+++ b/doc/contents.rst
@@ -7,6 +7,7 @@ Table of contents
index
install
documentation
+ tutorials
issues
changelog
about
diff --git a/doc/guide/Sr2RuO4/calculate_dos_wannier_basis.py b/doc/guide/Sr2RuO4/calculate_dos_wannier_basis.py
index 7cb8462a..98c395d1 100644
--- a/doc/guide/Sr2RuO4/calculate_dos_wannier_basis.py
+++ b/doc/guide/Sr2RuO4/calculate_dos_wannier_basis.py
@@ -1,4 +1,4 @@
-from triqs_dft_tools.converters.wien2k_converter import Wien2kConverter
+from triqs_dft_tools.converters.wien2k import Wien2kConverter
from triqs_dft_tools import SumkDFTTools
filename = 'Sr2RuO4'
diff --git a/doc/guide/analysis.rst b/doc/guide/analysis.rst
index 90aca2bd..b511be78 100644
--- a/doc/guide/analysis.rst
+++ b/doc/guide/analysis.rst
@@ -37,7 +37,7 @@ class::
Note that all routines available in :class:`SumkDFT ` are also available here.
If required, we have to load and initialise the real-frequency self energy. Most conveniently,
-you have your self energy already stored as a real-frequency :class:`BlockGf ` object
+you have your self energy already stored as a real-frequency :class:`BlockGf ` object
in a hdf5 file::
with HDFArchive('case.h5', 'r') as ar:
@@ -45,10 +45,10 @@ in a hdf5 file::
You may also have your self energy stored in text files. For this case the :ref:`TRIQS ` library offers
the function :meth:`read_gf_from_txt`, which is able to load the data from text files of one Green function block
-into a real-frequency :class:`ReFreqGf ` object. Loading each block separately and
-building up a :class:´BlockGf ´ is done with::
+into a real-frequency :class:`ReFreqGf ` object. Loading each block separately and
+building up a :class:´BlockGf ´ is done with::
- from pytriqs.gf.tools import *
+ from triqs.gf.tools import *
# get block names
n_list = [n for n,nl in SK.gf_struct_solver[0].iteritems()]
# load sigma for each block - in this example sigma is composed of 1x1 blocks
@@ -128,7 +128,7 @@ Momentum resolved spectral function (with real-frequency self energy)
Another quantity of interest is the momentum-resolved spectral function, which can directly be compared to ARPES
experiments. First we have to execute `lapw1`, `lapw2 -almd` and :program:`dmftproj` with the `-band`
-option and use the :meth:`convert_bands_input `
+option and use the :meth:`convert_bands_input `
routine, which converts the required files (for a more detailed description see :ref:`conversion`). The spectral function is then calculated by typing::
SK.spaghettis(broadening=0.01,plot_shift=0.0,plot_range=None,ishell=None,save_to_file='Akw_')
diff --git a/doc/guide/blockstructure.rst b/doc/guide/blockstructure.rst
index 7d99ba24..2f51d937 100644
--- a/doc/guide/blockstructure.rst
+++ b/doc/guide/blockstructure.rst
@@ -21,7 +21,7 @@ We can create a simple :class:`BlockStructure ` in the same fashion as with the other `DFTTools` converters::
- from triqs_dft_tools.converters.vasp_converter import *
+ from triqs_dft_tools.converters.vasp import *
Converter = VaspConverter(filename = 'vasp')
Converter.convert_dft_input()
diff --git a/doc/guide/conv_wien2k.rst b/doc/guide/conv_wien2k.rst
index 61d387ab..342726b5 100644
--- a/doc/guide/conv_wien2k.rst
+++ b/doc/guide/conv_wien2k.rst
@@ -94,9 +94,9 @@ directory name):
Now we convert these files into an hdf5 file that can be used for the
DMFT calculations. For this purpose we
-use the python module :class:`Wien2kConverter `. It is initialized as::
+use the python module :class:`Wien2kConverter `. It is initialized as::
- from triqs_dft_tools.converters.wien2k_converter import *
+ from triqs_dft_tools.converters.wien2k import *
Converter = Wien2kConverter(filename = case)
The only necessary parameter to this construction is the parameter `filename`.
diff --git a/doc/guide/dftdmft_singleshot.rst b/doc/guide/dftdmft_singleshot.rst
index 1ec43e0a..57610af9 100644
--- a/doc/guide/dftdmft_singleshot.rst
+++ b/doc/guide/dftdmft_singleshot.rst
@@ -80,8 +80,8 @@ for :emphasis:`use_dc_formula` are:
At the end of the calculation, we can save the Green function and self energy into a file::
- from pytriqs.archive import HDFArchive
- import pytriqs.utility.mpi as mpi
+ from h5 import HDFArchive
+ import triqs.utility.mpi as mpi
if mpi.is_master_node():
ar = HDFArchive("YourDFTDMFTcalculation.h5",'w')
ar["G"] = S.G_iw
diff --git a/doc/guide/transport.rst b/doc/guide/transport.rst
index 2aa1d1e5..69db4753 100644
--- a/doc/guide/transport.rst
+++ b/doc/guide/transport.rst
@@ -52,13 +52,13 @@ real-frequency self energy.
it is crucial to perform the analytic continuation in such a way that the real-frequency self energy
is accurate around the Fermi energy as low-energy features strongly influence the final results.
-Besides the self energy the Wien2k files read by the transport converter (:meth:`convert_transport_input `) are:
+Besides the self energy the Wien2k files read by the transport converter (:meth:`convert_transport_input `) are:
* :file:`.struct`: The lattice constants specified in the struct file are used to calculate the unit cell volume.
* :file:`.outputs`: In this file the k-point symmetries are given.
* :file:`.oubwin`: Contains the indices of the bands within the projected subspace (written by :program:`dmftproj`) for each k-point.
* :file:`.pmat`: This file is the output of the Wien2k optics package and contains the velocity (momentum) matrix elements between all bands in the desired energy
window for each k-point. How to use the optics package is described below.
- * :file:`.h5`: The hdf5 archive has to be present and should contain the dft_input subgroup. Otherwise :meth:`convert_dft_input ` needs to be called before :meth:`convert_transport_input `.
+ * :file:`.h5`: The hdf5 archive has to be present and should contain the dft_input subgroup. Otherwise :meth:`convert_dft_input ` needs to be called before :meth:`convert_transport_input `.
Wien2k optics package
@@ -84,7 +84,7 @@ Using the transport code
First we have to read the Wien2k files and store the relevant information in the hdf5 archive::
- from triqs_dft_tools.converters.wien2k_converter import *
+ from triqs_dft_tools.converters.wien2k import *
from triqs_dft_tools.sumk_dft_tools import *
Converter = Wien2kConverter(filename='case', repacking=True)
@@ -92,7 +92,7 @@ First we have to read the Wien2k files and store the relevant information in the
SK = SumkDFTTools(hdf_file='case.h5', use_dft_blocks=True)
-The converter :meth:`convert_transport_input `
+The converter :meth:`convert_transport_input `
reads the required data of the Wien2k output and stores it in the `dft_transp_input` subgroup of your hdf file.
Additionally we need to read and set the self energy, the chemical potential and the double counting::
diff --git a/doc/install.rst b/doc/install.rst
index 8d2a1bc0..0def24bd 100644
--- a/doc/install.rst
+++ b/doc/install.rst
@@ -37,34 +37,41 @@ Compiling DFTTools from source
Prerequisites
-------------
-#. The :ref:`TRIQS ` toolbox.
-
+#. The :ref:`TRIQS ` library, see :ref:`TRIQS installation instruction `.
+ In the following, we assume that TRIQS is installed in the directory ``path_to_triqs``.
#. Likely, you will also need at least one impurity solver, e.g. the :ref:`CTHYB solver `.
-Installation steps
+Installation steps
------------------
-#. Download the source code by cloning the ``TRIQS/dft_tools`` repository from GitHub::
-
- $ git clone https://github.com/TRIQS/dft_tools.git dft_tools.src
-
+#. Download the source code of the latest stable version by cloning the ``TRIQS/dft_tools`` repository from GitHub::
+
+ $ git clone https://github.com/TRIQS/dft_tools dft_tools.src
+
+#. Make sure that all additional dependencies are installed on your system and available in your environment.
+ Alternatively build the dependencies from source instead with::
+
+ $ (cd deps && ./download.sh)
+
+ In this case they will be installed together with your application.
+
#. Create and move to a new directory where you will compile the code::
-
+
$ mkdir dft_tools.build && cd dft_tools.build
-
+
#. Ensure that your shell contains the TRIQS environment variables by sourcing the ``triqsvars.sh`` file from your TRIQS installation::
$ source path_to_triqs/share/triqsvarsh.sh
-
+
#. In the build directory call cmake, including any additional custom CMake options, see below::
$ cmake ../dft_tools.src
-
-#. Compile the code, run the tests and install the application::
-
- $ make
- $ make test
- $ make install
+
+#. Compile the code, run the tests and install the application::
+
+ $ make
+ $ make test
+ $ make install
Installation steps for the use with WIEN2K version 14.2 and older
@@ -114,37 +121,41 @@ Finally, you will have to change the calls to :program:`python_with_DMFT` to
your :program:`python` installation in the Wien2k :file:`path_to_Wien2k/run*` files.
-Version compatibility
+Version compatibility
---------------------
-
-Be careful that the version of the TRIQS library and of the :program:`DFTTools` must be
-compatible (more information on the :ref:`TRIQS website `.
-If you want to use a version of the :program:`DFTTools` that is not the latest one, go
-into the directory with the sources and look at all available versions::
-
- $ cd src && git tag
-
-Checkout the version of the code that you want, for instance::
-
- $ git co 2.1
-
-Then follow the steps 2 to 5 described above to compile the code.
+
+Keep in mind that the version of ``dft_tools`` must be compatible with your TRIQS library version,
+see :ref:`TRIQS website `.
+In particular the Major and Minor Version numbers have to be the same.
+To use a particular version, go into the directory with the sources, and look at all available versions::
+
+ $ cd dft_tools.src && git tag
+
+Checkout the version of the code that you want::
+
+ $ git checkout 2.1.0
+
+and follow steps 2 to 4 above to compile the code.
Custom CMake options
--------------------
-Functionality of ``dft_tools`` can be tweaked using extra compile-time options passed to CMake::
+The compilation of ``dft_tools`` can be configured using CMake-options::
- cmake -DOPTION1=value1 -DOPTION2=value2 ... ../dft_tools.src
+ cmake ../dft_tools.src -DOPTION1=value1 -DOPTION2=value2 ...
-+---------------------------------------------------------------+-----------------------------------------------+
-| Options | Syntax |
-+===============================================================+===============================================+
-| Disable testing (not recommended) | -DBuild_Tests=OFF |
-+---------------------------------------------------------------+-----------------------------------------------+
-| Build the documentation locally | -DBuild_Documentation=ON |
-+---------------------------------------------------------------+-----------------------------------------------+
-| Check test coverage when testing | -DTEST_COVERAGE=ON |
-| (run ``make coverage`` to show the results; requires the | |
-| python ``coverage`` package) | |
-+---------------------------------------------------------------+-----------------------------------------------+
++-----------------------------------------------------------------+-----------------------------------------------+
+| Options | Syntax |
++=================================================================+===============================================+
+| Specify an installation path other than path_to_triqs | -DCMAKE_INSTALL_PREFIX=path_to_dft_tools|
++-----------------------------------------------------------------+-----------------------------------------------+
+| Build in Debugging Mode | -DCMAKE_BUILD_TYPE=Debug |
++-----------------------------------------------------------------+-----------------------------------------------+
+| Disable testing (not recommended) | -DBuild_Tests=OFF |
++-----------------------------------------------------------------+-----------------------------------------------+
+| Build the documentation | -DBuild_Documentation=ON |
++-----------------------------------------------------------------+-----------------------------------------------+
+| Check test coverage when testing | -DTEST_COVERAGE=ON |
+| (run ``make coverage`` to show the results; requires the | |
+| python ``coverage`` package) | |
++-----------------------------------------------------------------+-----------------------------------------------+
diff --git a/doc/issues.rst b/doc/issues.rst
index efe3e9ca..d89de6d2 100644
--- a/doc/issues.rst
+++ b/doc/issues.rst
@@ -1,13 +1,14 @@
+.. _issues:
Reporting issues
================
Please report all problems and bugs directly at the github issue page
-``_. In order to make it easier
-for us to solve the issue please follow these guidelines:
+``_. In order to make it easier for us
+to solve the issue please follow these guidelines:
#. In all cases specify which version of the application you are using. You can
- find the version number in the file :file:`README.txt` at the root of the
+ find the version number in the file :file:`CMakeLists.txt` at the root of the
application sources.
#. If you have a problem during the installation, give us information about
diff --git a/doc/reference/converters.rst b/doc/reference/converters.rst
index 122dbd9c..ba448810 100644
--- a/doc/reference/converters.rst
+++ b/doc/reference/converters.rst
@@ -5,20 +5,20 @@ Converters
Wien2k Converter
----------------
-.. autoclass:: triqs_dft_tools.converters.wien2k_converter.Wien2kConverter
+.. autoclass:: triqs_dft_tools.converters.wien2k.Wien2kConverter
:members:
:special-members:
:show-inheritance:
H(k) Converter
--------------
-.. autoclass:: triqs_dft_tools.converters.hk_converter.HkConverter
+.. autoclass:: triqs_dft_tools.converters.hk.HkConverter
:members:
:special-members:
Wannier90 Converter
-------------------
-.. autoclass:: triqs_dft_tools.converters.wannier90_converter.Wannier90Converter
+.. autoclass:: triqs_dft_tools.converters.wannier90.Wannier90Converter
:members:
:special-members:
@@ -53,7 +53,7 @@ PLOVASP reference, the classes / functions are sorted the way the converter uses
VASP Converter
-------------------
.. _refVASPconverter:
-.. autoclass:: triqs_dft_tools.converters.vasp_converter.VaspConverter
+.. autoclass:: triqs_dft_tools.converters.vasp.VaspConverter
:members:
:special-members:
diff --git a/doc/sphinxext/numpydoc/apigen.py b/doc/sphinxext/numpydoc/apigen.py
new file mode 100644
index 00000000..2619fbbd
--- /dev/null
+++ b/doc/sphinxext/numpydoc/apigen.py
@@ -0,0 +1,427 @@
+"""Attempt to generate templates for module reference with Sphinx
+
+XXX - we exclude extension modules
+
+To include extension modules, first identify them as valid in the
+``_uri2path`` method, then handle them in the ``_parse_module`` script.
+
+We get functions and classes by parsing the text of .py files.
+Alternatively we could import the modules for discovery, and we'd have
+to do that for extension modules. This would involve changing the
+``_parse_module`` method to work via import and introspection, and
+might involve changing ``discover_modules`` (which determines which
+files are modules, and therefore which module URIs will be passed to
+``_parse_module``).
+
+NOTE: this is a modified version of a script originally shipped with the
+PyMVPA project, which we've adapted for NIPY use. PyMVPA is an MIT-licensed
+project."""
+
+# Stdlib imports
+import os
+import re
+
+# Functions and classes
+class ApiDocWriter:
+ ''' Class for automatic detection and parsing of API docs
+ to Sphinx-parsable reST format'''
+
+ # only separating first two levels
+ rst_section_levels = ['*', '=', '-', '~', '^']
+
+ def __init__(self,
+ package_name,
+ rst_extension='.rst',
+ package_skip_patterns=None,
+ module_skip_patterns=None,
+ ):
+ ''' Initialize package for parsing
+
+ Parameters
+ ----------
+ package_name : string
+ Name of the top-level package. *package_name* must be the
+ name of an importable package
+ rst_extension : string, optional
+ Extension for reST files, default '.rst'
+ package_skip_patterns : None or sequence of {strings, regexps}
+ Sequence of strings giving URIs of packages to be excluded
+ Operates on the package path, starting at (including) the
+ first dot in the package path, after *package_name* - so,
+ if *package_name* is ``sphinx``, then ``sphinx.util`` will
+ result in ``.util`` being passed for earching by these
+ regexps. If is None, gives default. Default is:
+ ['\.tests$']
+ module_skip_patterns : None or sequence
+ Sequence of strings giving URIs of modules to be excluded
+ Operates on the module name including preceding URI path,
+ back to the first dot after *package_name*. For example
+ ``sphinx.util.console`` results in the string to search of
+ ``.util.console``
+ If is None, gives default. Default is:
+ ['\.setup$', '\._']
+ '''
+ if package_skip_patterns is None:
+ package_skip_patterns = ['\\.tests$']
+ if module_skip_patterns is None:
+ module_skip_patterns = ['\\.setup$', '\\._']
+ self.package_name = package_name
+ self.rst_extension = rst_extension
+ self.package_skip_patterns = package_skip_patterns
+ self.module_skip_patterns = module_skip_patterns
+
+ def get_package_name(self):
+ return self._package_name
+
+ def set_package_name(self, package_name):
+ ''' Set package_name
+
+ >>> docwriter = ApiDocWriter('sphinx')
+ >>> import sphinx
+ >>> docwriter.root_path == sphinx.__path__[0]
+ True
+ >>> docwriter.package_name = 'docutils'
+ >>> import docutils
+ >>> docwriter.root_path == docutils.__path__[0]
+ True
+ '''
+ # It's also possible to imagine caching the module parsing here
+ self._package_name = package_name
+ self.root_module = __import__(package_name)
+ self.root_path = self.root_module.__path__[0]
+ self.written_modules = None
+
+ package_name = property(get_package_name, set_package_name, None,
+ 'get/set package_name')
+
+ def _get_object_name(self, line):
+ ''' Get second token in line
+ >>> docwriter = ApiDocWriter('sphinx')
+ >>> docwriter._get_object_name(" def func(): ")
+ 'func'
+ >>> docwriter._get_object_name(" class Klass: ")
+ 'Klass'
+ >>> docwriter._get_object_name(" class Klass: ")
+ 'Klass'
+ '''
+ name = line.split()[1].split('(')[0].strip()
+ # in case we have classes which are not derived from object
+ # ie. old style classes
+ return name.rstrip(':')
+
+ def _uri2path(self, uri):
+ ''' Convert uri to absolute filepath
+
+ Parameters
+ ----------
+ uri : string
+ URI of python module to return path for
+
+ Returns
+ -------
+ path : None or string
+ Returns None if there is no valid path for this URI
+ Otherwise returns absolute file system path for URI
+
+ Examples
+ --------
+ >>> docwriter = ApiDocWriter('sphinx')
+ >>> import sphinx
+ >>> modpath = sphinx.__path__[0]
+ >>> res = docwriter._uri2path('sphinx.builder')
+ >>> res == os.path.join(modpath, 'builder.py')
+ True
+ >>> res = docwriter._uri2path('sphinx')
+ >>> res == os.path.join(modpath, '__init__.py')
+ True
+ >>> docwriter._uri2path('sphinx.does_not_exist')
+
+ '''
+ if uri == self.package_name:
+ return os.path.join(self.root_path, '__init__.py')
+ path = uri.replace('.', os.path.sep)
+ path = path.replace(self.package_name + os.path.sep, '')
+ path = os.path.join(self.root_path, path)
+ # XXX maybe check for extensions as well?
+ if os.path.exists(path + '.py'): # file
+ path += '.py'
+ elif os.path.exists(os.path.join(path, '__init__.py')):
+ path = os.path.join(path, '__init__.py')
+ else:
+ return None
+ return path
+
+ def _path2uri(self, dirpath):
+ ''' Convert directory path to uri '''
+ relpath = dirpath.replace(self.root_path, self.package_name)
+ if relpath.startswith(os.path.sep):
+ relpath = relpath[1:]
+ return relpath.replace(os.path.sep, '.')
+
+ def _parse_module(self, uri):
+ ''' Parse module defined in *uri* '''
+ filename = self._uri2path(uri)
+ if filename is None:
+ # nothing that we could handle here.
+ return ([],[])
+ f = open(filename, 'rt')
+ functions, classes = self._parse_lines(f)
+ f.close()
+ return functions, classes
+
+ def _parse_lines(self, linesource):
+ ''' Parse lines of text for functions and classes '''
+ functions = []
+ classes = []
+ for line in linesource:
+ if line.startswith('def ') and line.count('('):
+ # exclude private stuff
+ name = self._get_object_name(line)
+ if not name.startswith('_'):
+ functions.append(name)
+ elif line.startswith('class '):
+ # exclude private stuff
+ name = self._get_object_name(line)
+ if not name.startswith('_'):
+ classes.append(name)
+ else:
+ pass
+ functions.sort()
+ classes.sort()
+ return functions, classes
+
+ def generate_api_doc(self, uri):
+ '''Make autodoc documentation template string for a module
+
+ Parameters
+ ----------
+ uri : string
+ python location of module - e.g 'sphinx.builder'
+
+ Returns
+ -------
+ S : string
+ Contents of API doc
+ '''
+ # get the names of all classes and functions
+ functions, classes = self._parse_module(uri)
+ if not len(functions) and not len(classes):
+ print('WARNING: Empty -',uri) # dbg
+ return ''
+
+ # Make a shorter version of the uri that omits the package name for
+ # titles
+ uri_short = re.sub(r'^%s\.' % self.package_name,'',uri)
+
+ ad = '.. AUTO-GENERATED FILE -- DO NOT EDIT!\n\n'
+
+ chap_title = uri_short
+ ad += (chap_title+'\n'+ self.rst_section_levels[1] * len(chap_title)
+ + '\n\n')
+
+ # Set the chapter title to read 'module' for all modules except for the
+ # main packages
+ if '.' in uri:
+ title = 'Module: :mod:`' + uri_short + '`'
+ else:
+ title = ':mod:`' + uri_short + '`'
+ ad += title + '\n' + self.rst_section_levels[2] * len(title)
+
+ if len(classes):
+ ad += '\nInheritance diagram for ``%s``:\n\n' % uri
+ ad += '.. inheritance-diagram:: %s \n' % uri
+ ad += ' :parts: 3\n'
+
+ ad += '\n.. automodule:: ' + uri + '\n'
+ ad += '\n.. currentmodule:: ' + uri + '\n'
+ multi_class = len(classes) > 1
+ multi_fx = len(functions) > 1
+ if multi_class:
+ ad += '\n' + 'Classes' + '\n' + \
+ self.rst_section_levels[2] * 7 + '\n'
+ elif len(classes) and multi_fx:
+ ad += '\n' + 'Class' + '\n' + \
+ self.rst_section_levels[2] * 5 + '\n'
+ for c in classes:
+ ad += '\n:class:`' + c + '`\n' \
+ + self.rst_section_levels[multi_class + 2 ] * \
+ (len(c)+9) + '\n\n'
+ ad += '\n.. autoclass:: ' + c + '\n'
+ # must NOT exclude from index to keep cross-refs working
+ ad += ' :members:\n' \
+ ' :undoc-members:\n' \
+ ' :show-inheritance:\n' \
+ ' :inherited-members:\n' \
+ '\n' \
+ ' .. automethod:: __init__\n'
+ if multi_fx:
+ ad += '\n' + 'Functions' + '\n' + \
+ self.rst_section_levels[2] * 9 + '\n\n'
+ elif len(functions) and multi_class:
+ ad += '\n' + 'Function' + '\n' + \
+ self.rst_section_levels[2] * 8 + '\n\n'
+ for f in functions:
+ # must NOT exclude from index to keep cross-refs working
+ ad += '\n.. autofunction:: ' + uri + '.' + f + '\n\n'
+ return ad
+
+ def _survives_exclude(self, matchstr, match_type):
+ ''' Returns True if *matchstr* does not match patterns
+
+ ``self.package_name`` removed from front of string if present
+
+ Examples
+ --------
+ >>> dw = ApiDocWriter('sphinx')
+ >>> dw._survives_exclude('sphinx.okpkg', 'package')
+ True
+ >>> dw.package_skip_patterns.append('^\\.badpkg$')
+ >>> dw._survives_exclude('sphinx.badpkg', 'package')
+ False
+ >>> dw._survives_exclude('sphinx.badpkg', 'module')
+ True
+ >>> dw._survives_exclude('sphinx.badmod', 'module')
+ True
+ >>> dw.module_skip_patterns.append('^\\.badmod$')
+ >>> dw._survives_exclude('sphinx.badmod', 'module')
+ False
+ '''
+ if match_type == 'module':
+ patterns = self.module_skip_patterns
+ elif match_type == 'package':
+ patterns = self.package_skip_patterns
+ else:
+ raise ValueError('Cannot interpret match type "%s"'
+ % match_type)
+ # Match to URI without package name
+ L = len(self.package_name)
+ if matchstr[:L] == self.package_name:
+ matchstr = matchstr[L:]
+ for pat in patterns:
+ try:
+ pat.search
+ except AttributeError:
+ pat = re.compile(pat)
+ if pat.search(matchstr):
+ return False
+ return True
+
+ def discover_modules(self):
+ ''' Return module sequence discovered from ``self.package_name``
+
+
+ Parameters
+ ----------
+ None
+
+ Returns
+ -------
+ mods : sequence
+ Sequence of module names within ``self.package_name``
+
+ Examples
+ --------
+ >>> dw = ApiDocWriter('sphinx')
+ >>> mods = dw.discover_modules()
+ >>> 'sphinx.util' in mods
+ True
+ >>> dw.package_skip_patterns.append('\.util$')
+ >>> 'sphinx.util' in dw.discover_modules()
+ False
+ >>>
+ '''
+ modules = [self.package_name]
+ # raw directory parsing
+ for dirpath, dirnames, filenames in os.walk(self.root_path):
+ # Check directory names for packages
+ root_uri = self._path2uri(os.path.join(self.root_path,
+ dirpath))
+ for dirname in dirnames[:]: # copy list - we modify inplace
+ package_uri = '.'.join((root_uri, dirname))
+ if (self._uri2path(package_uri) and
+ self._survives_exclude(package_uri, 'package')):
+ modules.append(package_uri)
+ else:
+ dirnames.remove(dirname)
+ # Check filenames for modules
+ for filename in filenames:
+ module_name = filename[:-3]
+ module_uri = '.'.join((root_uri, module_name))
+ if (self._uri2path(module_uri) and
+ self._survives_exclude(module_uri, 'module')):
+ modules.append(module_uri)
+ return sorted(modules)
+
+ def write_modules_api(self, modules,outdir):
+ # write the list
+ written_modules = []
+ for m in modules:
+ api_str = self.generate_api_doc(m)
+ if not api_str:
+ continue
+ # write out to file
+ outfile = os.path.join(outdir,
+ m + self.rst_extension)
+ fileobj = open(outfile, 'wt')
+ fileobj.write(api_str)
+ fileobj.close()
+ written_modules.append(m)
+ self.written_modules = written_modules
+
+ def write_api_docs(self, outdir):
+ """Generate API reST files.
+
+ Parameters
+ ----------
+ outdir : string
+ Directory name in which to store files
+ We create automatic filenames for each module
+
+ Returns
+ -------
+ None
+
+ Notes
+ -----
+ Sets self.written_modules to list of written modules
+ """
+ if not os.path.exists(outdir):
+ os.mkdir(outdir)
+ # compose list of modules
+ modules = self.discover_modules()
+ self.write_modules_api(modules,outdir)
+
+ def write_index(self, outdir, froot='gen', relative_to=None):
+ """Make a reST API index file from written files
+
+ Parameters
+ ----------
+ path : string
+ Filename to write index to
+ outdir : string
+ Directory to which to write generated index file
+ froot : string, optional
+ root (filename without extension) of filename to write to
+ Defaults to 'gen'. We add ``self.rst_extension``.
+ relative_to : string
+ path to which written filenames are relative. This
+ component of the written file path will be removed from
+ outdir, in the generated index. Default is None, meaning,
+ leave path as it is.
+ """
+ if self.written_modules is None:
+ raise ValueError('No modules written')
+ # Get full filename path
+ path = os.path.join(outdir, froot+self.rst_extension)
+ # Path written into index is relative to rootpath
+ if relative_to is not None:
+ relpath = outdir.replace(relative_to + os.path.sep, '')
+ else:
+ relpath = outdir
+ idx = open(path,'wt')
+ w = idx.write
+ w('.. AUTO-GENERATED FILE -- DO NOT EDIT!\n\n')
+ w('.. toctree::\n\n')
+ for f in self.written_modules:
+ w(' %s\n' % os.path.join(relpath,f))
+ idx.close()
diff --git a/doc/sphinxext/numpydoc/docscrape.py b/doc/sphinxext/numpydoc/docscrape.py
new file mode 100644
index 00000000..25489154
--- /dev/null
+++ b/doc/sphinxext/numpydoc/docscrape.py
@@ -0,0 +1,497 @@
+"""Extract reference documentation from the NumPy source tree.
+
+"""
+
+import inspect
+import textwrap
+import re
+import pydoc
+from io import StringIO
+from warnings import warn
+4
+class Reader:
+ """A line-based string reader.
+
+ """
+ def __init__(self, data):
+ """
+ Parameters
+ ----------
+ data : str
+ String with lines separated by '\n'.
+
+ """
+ if isinstance(data,list):
+ self._str = data
+ else:
+ self._str = data.split('\n') # store string as list of lines
+
+ self.reset()
+
+ def __getitem__(self, n):
+ return self._str[n]
+
+ def reset(self):
+ self._l = 0 # current line nr
+
+ def read(self):
+ if not self.eof():
+ out = self[self._l]
+ self._l += 1
+ return out
+ else:
+ return ''
+
+ def seek_next_non_empty_line(self):
+ for l in self[self._l:]:
+ if l.strip():
+ break
+ else:
+ self._l += 1
+
+ def eof(self):
+ return self._l >= len(self._str)
+
+ def read_to_condition(self, condition_func):
+ start = self._l
+ for line in self[start:]:
+ if condition_func(line):
+ return self[start:self._l]
+ self._l += 1
+ if self.eof():
+ return self[start:self._l+1]
+ return []
+
+ def read_to_next_empty_line(self):
+ self.seek_next_non_empty_line()
+ def is_empty(line):
+ return not line.strip()
+ return self.read_to_condition(is_empty)
+
+ def read_to_next_unindented_line(self):
+ def is_unindented(line):
+ return (line.strip() and (len(line.lstrip()) == len(line)))
+ return self.read_to_condition(is_unindented)
+
+ def peek(self,n=0):
+ if self._l + n < len(self._str):
+ return self[self._l + n]
+ else:
+ return ''
+
+ def is_empty(self):
+ return not ''.join(self._str).strip()
+
+
+class NumpyDocString:
+ def __init__(self,docstring):
+ docstring = textwrap.dedent(docstring).split('\n')
+
+ self._doc = Reader(docstring)
+ self._parsed_data = {
+ 'Signature': '',
+ 'Summary': [''],
+ 'Extended Summary': [],
+ 'Parameters': [],
+ 'Returns': [],
+ 'Raises': [],
+ 'Warns': [],
+ 'Other Parameters': [],
+ 'Attributes': [],
+ 'Methods': [],
+ 'See Also': [],
+ 'Notes': [],
+ 'Warnings': [],
+ 'References': '',
+ 'Examples': '',
+ 'index': {}
+ }
+
+ self._parse()
+
+ def __getitem__(self,key):
+ return self._parsed_data[key]
+
+ def __setitem__(self,key,val):
+ if key not in self._parsed_data:
+ warn("Unknown section %s" % key)
+ else:
+ self._parsed_data[key] = val
+
+ def _is_at_section(self):
+ self._doc.seek_next_non_empty_line()
+
+ if self._doc.eof():
+ return False
+
+ l1 = self._doc.peek().strip() # e.g. Parameters
+
+ if l1.startswith('.. index::'):
+ return True
+
+ l2 = self._doc.peek(1).strip() # ---------- or ==========
+ return l2.startswith('-'*len(l1)) or l2.startswith('='*len(l1))
+
+ def _strip(self,doc):
+ i = 0
+ j = 0
+ for i,line in enumerate(doc):
+ if line.strip(): break
+
+ for j,line in enumerate(doc[::-1]):
+ if line.strip(): break
+
+ return doc[i:len(doc)-j]
+
+ def _read_to_next_section(self):
+ section = self._doc.read_to_next_empty_line()
+
+ while not self._is_at_section() and not self._doc.eof():
+ if not self._doc.peek(-1).strip(): # previous line was empty
+ section += ['']
+
+ section += self._doc.read_to_next_empty_line()
+
+ return section
+
+ def _read_sections(self):
+ while not self._doc.eof():
+ data = self._read_to_next_section()
+ name = data[0].strip()
+
+ if name.startswith('..'): # index section
+ yield name, data[1:]
+ elif len(data) < 2:
+ yield StopIteration
+ else:
+ yield name, self._strip(data[2:])
+
+ def _parse_param_list(self,content):
+ r = Reader(content)
+ params = []
+ while not r.eof():
+ header = r.read().strip()
+ if ' : ' in header:
+ arg_name, arg_type = header.split(' : ')[:2]
+ else:
+ arg_name, arg_type = header, ''
+
+ desc = r.read_to_next_unindented_line()
+ desc = dedent_lines(desc)
+
+ params.append((arg_name,arg_type,desc))
+
+ return params
+
+
+ _name_rgx = re.compile(r"^\s*(:(?P\w+):`(?P[a-zA-Z0-9_.-]+)`|"
+ r" (?P[a-zA-Z0-9_.-]+))\s*", re.X)
+ def _parse_see_also(self, content):
+ """
+ func_name : Descriptive text
+ continued text
+ another_func_name : Descriptive text
+ func_name1, func_name2, :meth:`func_name`, func_name3
+
+ """
+ items = []
+
+ def parse_item_name(text):
+ """Match ':role:`name`' or 'name'"""
+ m = self._name_rgx.match(text)
+ if m:
+ g = m.groups()
+ if g[1] is None:
+ return g[3], None
+ else:
+ return g[2], g[1]
+ raise ValueError("%s is not a item name" % text)
+
+ def push_item(name, rest):
+ if not name:
+ return
+ name, role = parse_item_name(name)
+ items.append((name, list(rest), role))
+ del rest[:]
+
+ current_func = None
+ rest = []
+
+ for line in content:
+ if not line.strip(): continue
+
+ m = self._name_rgx.match(line)
+ if m and line[m.end():].strip().startswith(':'):
+ push_item(current_func, rest)
+ current_func, line = line[:m.end()], line[m.end():]
+ rest = [line.split(':', 1)[1].strip()]
+ if not rest[0]:
+ rest = []
+ elif not line.startswith(' '):
+ push_item(current_func, rest)
+ current_func = None
+ if ',' in line:
+ for func in line.split(','):
+ push_item(func, [])
+ elif line.strip():
+ current_func = line
+ elif current_func is not None:
+ rest.append(line.strip())
+ push_item(current_func, rest)
+ return items
+
+ def _parse_index(self, section, content):
+ """
+ .. index: default
+ :refguide: something, else, and more
+
+ """
+ def strip_each_in(lst):
+ return [s.strip() for s in lst]
+
+ out = {}
+ section = section.split('::')
+ if len(section) > 1:
+ out['default'] = strip_each_in(section[1].split(','))[0]
+ for line in content:
+ line = line.split(':')
+ if len(line) > 2:
+ out[line[1]] = strip_each_in(line[2].split(','))
+ return out
+
+ def _parse_summary(self):
+ """Grab signature (if given) and summary"""
+ if self._is_at_section():
+ return
+
+ summary = self._doc.read_to_next_empty_line()
+ summary_str = " ".join([s.strip() for s in summary]).strip()
+ if re.compile('^([\w., ]+=)?\s*[\w\.]+\(.*\)$').match(summary_str):
+ self['Signature'] = summary_str
+ if not self._is_at_section():
+ self['Summary'] = self._doc.read_to_next_empty_line()
+ else:
+ self['Summary'] = summary
+
+ if not self._is_at_section():
+ self['Extended Summary'] = self._read_to_next_section()
+
+ def _parse(self):
+ self._doc.reset()
+ self._parse_summary()
+
+ for (section,content) in self._read_sections():
+ if not section.startswith('..'):
+ section = ' '.join([s.capitalize() for s in section.split(' ')])
+ if section in ('Parameters', 'Attributes', 'Methods',
+ 'Returns', 'Raises', 'Warns'):
+ self[section] = self._parse_param_list(content)
+ elif section.startswith('.. index::'):
+ self['index'] = self._parse_index(section, content)
+ elif section == 'See Also':
+ self['See Also'] = self._parse_see_also(content)
+ else:
+ self[section] = content
+
+ # string conversion routines
+
+ def _str_header(self, name, symbol='-'):
+ return [name, len(name)*symbol]
+
+ def _str_indent(self, doc, indent=4):
+ out = []
+ for line in doc:
+ out += [' '*indent + line]
+ return out
+
+ def _str_signature(self):
+ if self['Signature']:
+ return [self['Signature'].replace('*','\*')] + ['']
+ else:
+ return ['']
+
+ def _str_summary(self):
+ if self['Summary']:
+ return self['Summary'] + ['']
+ else:
+ return []
+
+ def _str_extended_summary(self):
+ if self['Extended Summary']:
+ return self['Extended Summary'] + ['']
+ else:
+ return []
+
+ def _str_param_list(self, name):
+ out = []
+ if self[name]:
+ out += self._str_header(name)
+ for param,param_type,desc in self[name]:
+ out += ['%s : %s' % (param, param_type)]
+ out += self._str_indent(desc)
+ out += ['']
+ return out
+
+ def _str_section(self, name):
+ out = []
+ if self[name]:
+ out += self._str_header(name)
+ out += self[name]
+ out += ['']
+ return out
+
+ def _str_see_also(self, func_role):
+ if not self['See Also']: return []
+ out = []
+ out += self._str_header("See Also")
+ last_had_desc = True
+ for func, desc, role in self['See Also']:
+ if role:
+ link = ':%s:`%s`' % (role, func)
+ elif func_role:
+ link = ':%s:`%s`' % (func_role, func)
+ else:
+ link = "`%s`_" % func
+ if desc or last_had_desc:
+ out += ['']
+ out += [link]
+ else:
+ out[-1] += ", %s" % link
+ if desc:
+ out += self._str_indent([' '.join(desc)])
+ last_had_desc = True
+ else:
+ last_had_desc = False
+ out += ['']
+ return out
+
+ def _str_index(self):
+ idx = self['index']
+ out = []
+ out += ['.. index:: %s' % idx.get('default','')]
+ for section, references in idx.items():
+ if section == 'default':
+ continue
+ out += [' :%s: %s' % (section, ', '.join(references))]
+ return out
+
+ def __str__(self, func_role=''):
+ out = []
+ out += self._str_signature()
+ out += self._str_summary()
+ out += self._str_extended_summary()
+ for param_list in ('Parameters','Returns','Raises'):
+ out += self._str_param_list(param_list)
+ out += self._str_section('Warnings')
+ out += self._str_see_also(func_role)
+ for s in ('Notes','References','Examples'):
+ out += self._str_section(s)
+ out += self._str_index()
+ return '\n'.join(out)
+
+
+def indent(str,indent=4):
+ indent_str = ' '*indent
+ if str is None:
+ return indent_str
+ lines = str.split('\n')
+ return '\n'.join(indent_str + l for l in lines)
+
+def dedent_lines(lines):
+ """Deindent a list of lines maximally"""
+ return textwrap.dedent("\n".join(lines)).split("\n")
+
+def header(text, style='-'):
+ return text + '\n' + style*len(text) + '\n'
+
+
+class FunctionDoc(NumpyDocString):
+ def __init__(self, func, role='func', doc=None):
+ self._f = func
+ self._role = role # e.g. "func" or "meth"
+ if doc is None:
+ doc = inspect.getdoc(func) or ''
+ try:
+ NumpyDocString.__init__(self, doc)
+ except ValueError as e:
+ print('*'*78)
+ print("ERROR: '%s' while parsing `%s`" % (e, self._f))
+ print('*'*78)
+ #print "Docstring follows:"
+ #print doclines
+ #print '='*78
+
+ if not self['Signature']:
+ func, func_name = self.get_func()
+ try:
+ # try to read signature
+ argspec = inspect.getargspec(func)
+ argspec = inspect.formatargspec(*argspec)
+ argspec = argspec.replace('*','\*')
+ signature = '%s%s' % (func_name, argspec)
+ except TypeError as e:
+ signature = '%s()' % func_name
+ self['Signature'] = signature
+
+ def get_func(self):
+ func_name = getattr(self._f, '__name__', self.__class__.__name__)
+ if inspect.isclass(self._f):
+ func = getattr(self._f, '__call__', self._f.__init__)
+ else:
+ func = self._f
+ return func, func_name
+
+ def __str__(self):
+ out = ''
+
+ func, func_name = self.get_func()
+ signature = self['Signature'].replace('*', '\*')
+
+ roles = {'func': 'function',
+ 'meth': 'method'}
+
+ if self._role:
+ if self._role not in roles:
+ print("Warning: invalid role %s" % self._role)
+ out += '.. %s:: %s\n \n\n' % (roles.get(self._role,''),
+ func_name)
+
+ out += super(FunctionDoc, self).__str__(func_role=self._role)
+ return out
+
+
+class ClassDoc(NumpyDocString):
+ def __init__(self,cls,modulename='',func_doc=FunctionDoc,doc=None):
+ if not inspect.isclass(cls):
+ raise ValueError("Initialise using a class. Got %r" % cls)
+ self._cls = cls
+
+ if modulename and not modulename.endswith('.'):
+ modulename += '.'
+ self._mod = modulename
+ self._name = cls.__name__
+ self._func_doc = func_doc
+
+ if doc is None:
+ doc = pydoc.getdoc(cls)
+
+ NumpyDocString.__init__(self, doc)
+
+ @property
+ def methods(self):
+ return [name for name,func in inspect.getmembers(self._cls)
+ if not name.startswith('_') and callable(func)]
+
+ def __str__(self):
+ out = ''
+ out += super(ClassDoc, self).__str__()
+ out += "\n\n"
+
+ #for m in self.methods:
+ # print "Parsing `%s`" % m
+ # out += str(self._func_doc(getattr(self._cls,m), 'meth')) + '\n\n'
+ # out += '.. index::\n single: %s; %s\n\n' % (self._name, m)
+
+ return out
+
+
diff --git a/doc/sphinxext/numpydoc/docscrape_sphinx.py b/doc/sphinxext/numpydoc/docscrape_sphinx.py
new file mode 100644
index 00000000..2660f14f
--- /dev/null
+++ b/doc/sphinxext/numpydoc/docscrape_sphinx.py
@@ -0,0 +1,136 @@
+import re, inspect, textwrap, pydoc
+from docscrape import NumpyDocString, FunctionDoc, ClassDoc
+
+class SphinxDocString(NumpyDocString):
+ # string conversion routines
+ def _str_header(self, name, symbol='`'):
+ return ['.. rubric:: ' + name, '']
+
+ def _str_field_list(self, name):
+ return [':' + name + ':']
+
+ def _str_indent(self, doc, indent=4):
+ out = []
+ for line in doc:
+ out += [' '*indent + line]
+ return out
+
+ def _str_signature(self):
+ return ['']
+ if self['Signature']:
+ return ['``%s``' % self['Signature']] + ['']
+ else:
+ return ['']
+
+ def _str_summary(self):
+ return self['Summary'] + ['']
+
+ def _str_extended_summary(self):
+ return self['Extended Summary'] + ['']
+
+ def _str_param_list(self, name):
+ out = []
+ if self[name]:
+ out += self._str_field_list(name)
+ out += ['']
+ for param,param_type,desc in self[name]:
+ out += self._str_indent(['**%s** : %s' % (param.strip(),
+ param_type)])
+ out += ['']
+ out += self._str_indent(desc,8)
+ out += ['']
+ return out
+
+ def _str_section(self, name):
+ out = []
+ if self[name]:
+ out += self._str_header(name)
+ out += ['']
+ content = textwrap.dedent("\n".join(self[name])).split("\n")
+ out += content
+ out += ['']
+ return out
+
+ def _str_see_also(self, func_role):
+ out = []
+ if self['See Also']:
+ see_also = super(SphinxDocString, self)._str_see_also(func_role)
+ out = ['.. seealso::', '']
+ out += self._str_indent(see_also[2:])
+ return out
+
+ def _str_warnings(self):
+ out = []
+ if self['Warnings']:
+ out = ['.. warning::', '']
+ out += self._str_indent(self['Warnings'])
+ return out
+
+ def _str_index(self):
+ idx = self['index']
+ out = []
+ if len(idx) == 0:
+ return out
+
+ out += ['.. index:: %s' % idx.get('default','')]
+ for section, references in idx.items():
+ if section == 'default':
+ continue
+ elif section == 'refguide':
+ out += [' single: %s' % (', '.join(references))]
+ else:
+ out += [' %s: %s' % (section, ','.join(references))]
+ return out
+
+ def _str_references(self):
+ out = []
+ if self['References']:
+ out += self._str_header('References')
+ if isinstance(self['References'], str):
+ self['References'] = [self['References']]
+ out.extend(self['References'])
+ out += ['']
+ return out
+
+ def __str__(self, indent=0, func_role="obj"):
+ out = []
+ out += self._str_signature()
+ out += self._str_index() + ['']
+ out += self._str_summary()
+ out += self._str_extended_summary()
+ for param_list in ('Parameters', 'Attributes', 'Methods',
+ 'Returns','Raises'):
+ out += self._str_param_list(param_list)
+ out += self._str_warnings()
+ out += self._str_see_also(func_role)
+ out += self._str_section('Notes')
+ out += self._str_references()
+ out += self._str_section('Examples')
+ out = self._str_indent(out,indent)
+ return '\n'.join(out)
+
+class SphinxFunctionDoc(SphinxDocString, FunctionDoc):
+ pass
+
+class SphinxClassDoc(SphinxDocString, ClassDoc):
+ pass
+
+def get_doc_object(obj, what=None, doc=None):
+ if what is None:
+ if inspect.isclass(obj):
+ what = 'class'
+ elif inspect.ismodule(obj):
+ what = 'module'
+ elif callable(obj):
+ what = 'function'
+ else:
+ what = 'object'
+ if what == 'class':
+ return SphinxClassDoc(obj, '', func_doc=SphinxFunctionDoc, doc=doc)
+ elif what in ('function', 'method'):
+ return SphinxFunctionDoc(obj, '', doc=doc)
+ else:
+ if doc is None:
+ doc = pydoc.getdoc(obj)
+ return SphinxDocString(doc)
+
diff --git a/doc/sphinxext/numpydoc/inheritance_diagram.py b/doc/sphinxext/numpydoc/inheritance_diagram.py
new file mode 100644
index 00000000..a1ba7e7e
--- /dev/null
+++ b/doc/sphinxext/numpydoc/inheritance_diagram.py
@@ -0,0 +1,407 @@
+"""
+Defines a docutils directive for inserting inheritance diagrams.
+
+Provide the directive with one or more classes or modules (separated
+by whitespace). For modules, all of the classes in that module will
+be used.
+
+Example::
+
+ Given the following classes:
+
+ class A: pass
+ class B(A): pass
+ class C(A): pass
+ class D(B, C): pass
+ class E(B): pass
+
+ .. inheritance-diagram: D E
+
+ Produces a graph like the following:
+
+ A
+ / \
+ B C
+ / \ /
+ E D
+
+The graph is inserted as a PNG+image map into HTML and a PDF in
+LaTeX.
+"""
+
+import inspect
+import os
+import re
+import subprocess
+try:
+ from hashlib import md5
+except ImportError:
+ from md5 import md5
+
+from docutils.nodes import Body, Element
+from docutils.parsers.rst import directives
+from sphinx.roles import xfileref_role
+
+def my_import(name):
+ """Module importer - taken from the python documentation.
+
+ This function allows importing names with dots in them."""
+
+ mod = __import__(name)
+ components = name.split('.')
+ for comp in components[1:]:
+ mod = getattr(mod, comp)
+ return mod
+
+class DotException(Exception):
+ pass
+
+class InheritanceGraph:
+ """
+ Given a list of classes, determines the set of classes that
+ they inherit from all the way to the root "object", and then
+ is able to generate a graphviz dot graph from them.
+ """
+ def __init__(self, class_names, show_builtins=False):
+ """
+ *class_names* is a list of child classes to show bases from.
+
+ If *show_builtins* is True, then Python builtins will be shown
+ in the graph.
+ """
+ self.class_names = class_names
+ self.classes = self._import_classes(class_names)
+ self.all_classes = self._all_classes(self.classes)
+ if len(self.all_classes) == 0:
+ raise ValueError("No classes found for inheritance diagram")
+ self.show_builtins = show_builtins
+
+ py_sig_re = re.compile(r'''^([\w.]*\.)? # class names
+ (\w+) \s* $ # optionally arguments
+ ''', re.VERBOSE)
+
+ def _import_class_or_module(self, name):
+ """
+ Import a class using its fully-qualified *name*.
+ """
+ try:
+ path, base = self.py_sig_re.match(name).groups()
+ except:
+ raise ValueError(
+ "Invalid class or module '%s' specified for inheritance diagram" % name)
+ fullname = (path or '') + base
+ path = (path and path.rstrip('.'))
+ if not path:
+ path = base
+ try:
+ module = __import__(path, None, None, [])
+ # We must do an import of the fully qualified name. Otherwise if a
+ # subpackage 'a.b' is requested where 'import a' does NOT provide
+ # 'a.b' automatically, then 'a.b' will not be found below. This
+ # second call will force the equivalent of 'import a.b' to happen
+ # after the top-level import above.
+ my_import(fullname)
+
+ except ImportError:
+ raise ValueError(
+ "Could not import class or module '%s' specified for inheritance diagram" % name)
+
+ try:
+ todoc = module
+ for comp in fullname.split('.')[1:]:
+ todoc = getattr(todoc, comp)
+ except AttributeError:
+ raise ValueError(
+ "Could not find class or module '%s' specified for inheritance diagram" % name)
+
+ # If a class, just return it
+ if inspect.isclass(todoc):
+ return [todoc]
+ elif inspect.ismodule(todoc):
+ classes = []
+ for cls in list(todoc.__dict__.values()):
+ if inspect.isclass(cls) and cls.__module__ == todoc.__name__:
+ classes.append(cls)
+ return classes
+ raise ValueError(
+ "'%s' does not resolve to a class or module" % name)
+
+ def _import_classes(self, class_names):
+ """
+ Import a list of classes.
+ """
+ classes = []
+ for name in class_names:
+ classes.extend(self._import_class_or_module(name))
+ return classes
+
+ def _all_classes(self, classes):
+ """
+ Return a list of all classes that are ancestors of *classes*.
+ """
+ all_classes = {}
+
+ def recurse(cls):
+ all_classes[cls] = None
+ for c in cls.__bases__:
+ if c not in all_classes:
+ recurse(c)
+
+ for cls in classes:
+ recurse(cls)
+
+ return list(all_classes.keys())
+
+ def class_name(self, cls, parts=0):
+ """
+ Given a class object, return a fully-qualified name. This
+ works for things I've tested in matplotlib so far, but may not
+ be completely general.
+ """
+ module = cls.__module__
+ if module == '__builtin__':
+ fullname = cls.__name__
+ else:
+ fullname = "%s.%s" % (module, cls.__name__)
+ if parts == 0:
+ return fullname
+ name_parts = fullname.split('.')
+ return '.'.join(name_parts[-parts:])
+
+ def get_all_class_names(self):
+ """
+ Get all of the class names involved in the graph.
+ """
+ return [self.class_name(x) for x in self.all_classes]
+
+ # These are the default options for graphviz
+ default_graph_options = {
+ "rankdir": "LR",
+ "size": '"8.0, 12.0"'
+ }
+ default_node_options = {
+ "shape": "box",
+ "fontsize": 10,
+ "height": 0.25,
+ "fontname": "Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",
+ "style": '"setlinewidth(0.5)"'
+ }
+ default_edge_options = {
+ "arrowsize": 0.5,
+ "style": '"setlinewidth(0.5)"'
+ }
+
+ def _format_node_options(self, options):
+ return ','.join(["%s=%s" % x for x in list(options.items())])
+ def _format_graph_options(self, options):
+ return ''.join(["%s=%s;\n" % x for x in list(options.items())])
+
+ def generate_dot(self, fd, name, parts=0, urls={},
+ graph_options={}, node_options={},
+ edge_options={}):
+ """
+ Generate a graphviz dot graph from the classes that
+ were passed in to __init__.
+
+ *fd* is a Python file-like object to write to.
+
+ *name* is the name of the graph
+
+ *urls* is a dictionary mapping class names to http urls
+
+ *graph_options*, *node_options*, *edge_options* are
+ dictionaries containing key/value pairs to pass on as graphviz
+ properties.
+ """
+ g_options = self.default_graph_options.copy()
+ g_options.update(graph_options)
+ n_options = self.default_node_options.copy()
+ n_options.update(node_options)
+ e_options = self.default_edge_options.copy()
+ e_options.update(edge_options)
+
+ fd.write('digraph %s {\n' % name)
+ fd.write(self._format_graph_options(g_options))
+
+ for cls in self.all_classes:
+ if not self.show_builtins and cls in list(__builtins__.values()):
+ continue
+
+ name = self.class_name(cls, parts)
+
+ # Write the node
+ this_node_options = n_options.copy()
+ url = urls.get(self.class_name(cls))
+ if url is not None:
+ this_node_options['URL'] = '"%s"' % url
+ fd.write(' "%s" [%s];\n' %
+ (name, self._format_node_options(this_node_options)))
+
+ # Write the edges
+ for base in cls.__bases__:
+ if not self.show_builtins and base in list(__builtins__.values()):
+ continue
+
+ base_name = self.class_name(base, parts)
+ fd.write(' "%s" -> "%s" [%s];\n' %
+ (base_name, name,
+ self._format_node_options(e_options)))
+ fd.write('}\n')
+
+ def run_dot(self, args, name, parts=0, urls={},
+ graph_options={}, node_options={}, edge_options={}):
+ """
+ Run graphviz 'dot' over this graph, returning whatever 'dot'
+ writes to stdout.
+
+ *args* will be passed along as commandline arguments.
+
+ *name* is the name of the graph
+
+ *urls* is a dictionary mapping class names to http urls
+
+ Raises DotException for any of the many os and
+ installation-related errors that may occur.
+ """
+ try:
+ dot = subprocess.Popen(['dot'] + list(args),
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+ close_fds=True)
+ except OSError:
+ raise DotException("Could not execute 'dot'. Are you sure you have 'graphviz' installed?")
+ except ValueError:
+ raise DotException("'dot' called with invalid arguments")
+ except:
+ raise DotException("Unexpected error calling 'dot'")
+
+ self.generate_dot(dot.stdin, name, parts, urls, graph_options,
+ node_options, edge_options)
+ dot.stdin.close()
+ result = dot.stdout.read()
+ returncode = dot.wait()
+ if returncode != 0:
+ raise DotException("'dot' returned the errorcode %d" % returncode)
+ return result
+
+class inheritance_diagram(Body, Element):
+ """
+ A docutils node to use as a placeholder for the inheritance
+ diagram.
+ """
+ pass
+
+def inheritance_diagram_directive(name, arguments, options, content, lineno,
+ content_offset, block_text, state,
+ state_machine):
+ """
+ Run when the inheritance_diagram directive is first encountered.
+ """
+ node = inheritance_diagram()
+
+ class_names = arguments
+
+ # Create a graph starting with the list of classes
+ graph = InheritanceGraph(class_names)
+
+ # Create xref nodes for each target of the graph's image map and
+ # add them to the doc tree so that Sphinx can resolve the
+ # references to real URLs later. These nodes will eventually be
+ # removed from the doctree after we're done with them.
+ for name in graph.get_all_class_names():
+ refnodes, x = xfileref_role(
+ 'class', ':class:`%s`' % name, name, 0, state)
+ node.extend(refnodes)
+ # Store the graph object so we can use it to generate the
+ # dot file later
+ node['graph'] = graph
+ # Store the original content for use as a hash
+ node['parts'] = options.get('parts', 0)
+ node['content'] = " ".join(class_names)
+ return [node]
+
+def get_graph_hash(node):
+ return md5(node['content'] + str(node['parts'])).hexdigest()[-10:]
+
+def html_output_graph(self, node):
+ """
+ Output the graph for HTML. This will insert a PNG with clickable
+ image map.
+ """
+ graph = node['graph']
+ parts = node['parts']
+
+ graph_hash = get_graph_hash(node)
+ name = "inheritance%s" % graph_hash
+ path = '_images'
+ dest_path = os.path.join(setup.app.builder.outdir, path)
+ if not os.path.exists(dest_path):
+ os.makedirs(dest_path)
+ png_path = os.path.join(dest_path, name + ".png")
+ path = setup.app.builder.imgpath
+
+ # Create a mapping from fully-qualified class names to URLs.
+ urls = {}
+ for child in node:
+ if child.get('refuri') is not None:
+ urls[child['reftitle']] = child.get('refuri')
+ elif child.get('refid') is not None:
+ urls[child['reftitle']] = '#' + child.get('refid')
+
+ # These arguments to dot will save a PNG file to disk and write
+ # an HTML image map to stdout.
+ image_map = graph.run_dot(['-Tpng', '-o%s' % png_path, '-Tcmapx'],
+ name, parts, urls)
+ return ('%s' %
+ (path, name, name, image_map))
+
+def latex_output_graph(self, node):
+ """
+ Output the graph for LaTeX. This will insert a PDF.
+ """
+ graph = node['graph']
+ parts = node['parts']
+
+ graph_hash = get_graph_hash(node)
+ name = "inheritance%s" % graph_hash
+ dest_path = os.path.abspath(os.path.join(setup.app.builder.outdir, '_images'))
+ if not os.path.exists(dest_path):
+ os.makedirs(dest_path)
+ pdf_path = os.path.abspath(os.path.join(dest_path, name + ".pdf"))
+
+ graph.run_dot(['-Tpdf', '-o%s' % pdf_path],
+ name, parts, graph_options={'size': '"6.0,6.0"'})
+ return '\n\\includegraphics{%s}\n\n' % pdf_path
+
+def visit_inheritance_diagram(inner_func):
+ """
+ This is just a wrapper around html/latex_output_graph to make it
+ easier to handle errors and insert warnings.
+ """
+ def visitor(self, node):
+ try:
+ content = inner_func(self, node)
+ except DotException as e:
+ # Insert the exception as a warning in the document
+ warning = self.document.reporter.warning(str(e), line=node.line)
+ warning.parent = node
+ node.children = [warning]
+ else:
+ source = self.document.attributes['source']
+ self.body.append(content)
+ node.children = []
+ return visitor
+
+def do_nothing(self, node):
+ pass
+
+def setup(app):
+ setup.app = app
+ setup.confdir = app.confdir
+
+ app.add_node(
+ inheritance_diagram,
+ latex=(visit_inheritance_diagram(latex_output_graph), do_nothing),
+ html=(visit_inheritance_diagram(html_output_graph), do_nothing))
+ app.add_directive(
+ 'inheritance-diagram', inheritance_diagram_directive,
+ False, (1, 100, 0), parts = directives.nonnegative_int)
diff --git a/doc/sphinxext/numpydoc/ipython_console_highlighting.py b/doc/sphinxext/numpydoc/ipython_console_highlighting.py
new file mode 100644
index 00000000..217b779d
--- /dev/null
+++ b/doc/sphinxext/numpydoc/ipython_console_highlighting.py
@@ -0,0 +1,114 @@
+"""reST directive for syntax-highlighting ipython interactive sessions.
+
+XXX - See what improvements can be made based on the new (as of Sept 2009)
+'pycon' lexer for the python console. At the very least it will give better
+highlighted tracebacks.
+"""
+
+#-----------------------------------------------------------------------------
+# Needed modules
+
+# Standard library
+import re
+
+# Third party
+from pygments.lexer import Lexer, do_insertions
+from pygments.lexers.agile import (PythonConsoleLexer, PythonLexer,
+ PythonTracebackLexer)
+from pygments.token import Comment, Generic
+
+from sphinx import highlighting
+
+#-----------------------------------------------------------------------------
+# Global constants
+line_re = re.compile('.*?\n')
+
+#-----------------------------------------------------------------------------
+# Code begins - classes and functions
+
+class IPythonConsoleLexer(Lexer):
+ """
+ For IPython console output or doctests, such as:
+
+ .. sourcecode:: ipython
+
+ In [1]: a = 'foo'
+
+ In [2]: a
+ Out[2]: 'foo'
+
+ In [3]: print a
+ foo
+
+ In [4]: 1 / 0
+
+ Notes:
+
+ - Tracebacks are not currently supported.
+
+ - It assumes the default IPython prompts, not customized ones.
+ """
+
+ name = 'IPython console session'
+ aliases = ['ipython']
+ mimetypes = ['text/x-ipython-console']
+ input_prompt = re.compile("(In \[[0-9]+\]: )|( \.\.\.+:)")
+ output_prompt = re.compile("(Out\[[0-9]+\]: )|( \.\.\.+:)")
+ continue_prompt = re.compile(" \.\.\.+:")
+ tb_start = re.compile("\-+")
+
+ def get_tokens_unprocessed(self, text):
+ pylexer = PythonLexer(**self.options)
+ tblexer = PythonTracebackLexer(**self.options)
+
+ curcode = ''
+ insertions = []
+ for match in line_re.finditer(text):
+ line = match.group()
+ input_prompt = self.input_prompt.match(line)
+ continue_prompt = self.continue_prompt.match(line.rstrip())
+ output_prompt = self.output_prompt.match(line)
+ if line.startswith("#"):
+ insertions.append((len(curcode),
+ [(0, Comment, line)]))
+ elif input_prompt is not None:
+ insertions.append((len(curcode),
+ [(0, Generic.Prompt, input_prompt.group())]))
+ curcode += line[input_prompt.end():]
+ elif continue_prompt is not None:
+ insertions.append((len(curcode),
+ [(0, Generic.Prompt, continue_prompt.group())]))
+ curcode += line[continue_prompt.end():]
+ elif output_prompt is not None:
+ # Use the 'error' token for output. We should probably make
+ # our own token, but error is typicaly in a bright color like
+ # red, so it works fine for our output prompts.
+ insertions.append((len(curcode),
+ [(0, Generic.Error, output_prompt.group())]))
+ curcode += line[output_prompt.end():]
+ else:
+ if curcode:
+ for item in do_insertions(insertions,
+ pylexer.get_tokens_unprocessed(curcode)):
+ yield item
+ curcode = ''
+ insertions = []
+ yield match.start(), Generic.Output, line
+ if curcode:
+ for item in do_insertions(insertions,
+ pylexer.get_tokens_unprocessed(curcode)):
+ yield item
+
+
+def setup(app):
+ """Setup as a sphinx extension."""
+
+ # This is only a lexer, so adding it below to pygments appears sufficient.
+ # But if somebody knows that the right API usage should be to do that via
+ # sphinx, by all means fix it here. At least having this setup.py
+ # suppresses the sphinx warning we'd get without it.
+ pass
+
+#-----------------------------------------------------------------------------
+# Register the extension as a valid pygments lexer
+highlighting.lexers['ipython'] = IPythonConsoleLexer()
diff --git a/doc/sphinxext/numpydoc/numpydoc.py b/doc/sphinxext/numpydoc/numpydoc.py
new file mode 100644
index 00000000..eea482dc
--- /dev/null
+++ b/doc/sphinxext/numpydoc/numpydoc.py
@@ -0,0 +1,116 @@
+"""
+========
+numpydoc
+========
+
+Sphinx extension that handles docstrings in the Numpy standard format. [1]
+
+It will:
+
+- Convert Parameters etc. sections to field lists.
+- Convert See Also section to a See also entry.
+- Renumber references.
+- Extract the signature from the docstring, if it can't be determined otherwise.
+
+.. [1] http://projects.scipy.org/scipy/numpy/wiki/CodingStyleGuidelines#docstring-standard
+
+"""
+
+import os, re, pydoc
+from docscrape_sphinx import get_doc_object, SphinxDocString
+import inspect
+
+def mangle_docstrings(app, what, name, obj, options, lines,
+ reference_offset=[0]):
+ if what == 'module':
+ # Strip top title
+ title_re = re.compile(r'^\s*[#*=]{4,}\n[a-z0-9 -]+\n[#*=]{4,}\s*',
+ re.I|re.S)
+ lines[:] = title_re.sub('', "\n".join(lines)).split("\n")
+ else:
+ doc = get_doc_object(obj, what, "\n".join(lines))
+ lines[:] = str(doc).split("\n")
+
+ if app.config.numpydoc_edit_link and hasattr(obj, '__name__') and \
+ obj.__name__:
+ if hasattr(obj, '__module__'):
+ v = dict(full_name="%s.%s" % (obj.__module__, obj.__name__))
+ else:
+ v = dict(full_name=obj.__name__)
+ lines += ['', '.. htmlonly::', '']
+ lines += [' %s' % x for x in
+ (app.config.numpydoc_edit_link % v).split("\n")]
+
+ # replace reference numbers so that there are no duplicates
+ references = []
+ for l in lines:
+ l = l.strip()
+ if l.startswith('.. ['):
+ try:
+ references.append(int(l[len('.. ['):l.index(']')]))
+ except ValueError:
+ print("WARNING: invalid reference in %s docstring" % name)
+
+ # Start renaming from the biggest number, otherwise we may
+ # overwrite references.
+ references.sort()
+ if references:
+ for i, line in enumerate(lines):
+ for r in references:
+ new_r = reference_offset[0] + r
+ lines[i] = lines[i].replace('[%d]_' % r,
+ '[%d]_' % new_r)
+ lines[i] = lines[i].replace('.. [%d]' % r,
+ '.. [%d]' % new_r)
+
+ reference_offset[0] += len(references)
+
+def mangle_signature(app, what, name, obj, options, sig, retann):
+ # Do not try to inspect classes that don't define `__init__`
+ if (inspect.isclass(obj) and
+ 'initializes x; see ' in pydoc.getdoc(obj.__init__)):
+ return '', ''
+
+ if not (callable(obj) or hasattr(obj, '__argspec_is_invalid_')): return
+ if not hasattr(obj, '__doc__'): return
+
+ doc = SphinxDocString(pydoc.getdoc(obj))
+ if doc['Signature']:
+ sig = re.sub("^[^(]*", "", doc['Signature'])
+ return sig, ''
+
+def initialize(app):
+ try:
+ app.connect('autodoc-process-signature', mangle_signature)
+ except:
+ monkeypatch_sphinx_ext_autodoc()
+
+def setup(app, get_doc_object_=get_doc_object):
+ global get_doc_object
+ get_doc_object = get_doc_object_
+
+ app.connect('autodoc-process-docstring', mangle_docstrings)
+ app.connect('builder-inited', initialize)
+ app.add_config_value('numpydoc_edit_link', None, True)
+
+#------------------------------------------------------------------------------
+# Monkeypatch sphinx.ext.autodoc to accept argspecless autodocs (Sphinx < 0.5)
+#------------------------------------------------------------------------------
+
+def monkeypatch_sphinx_ext_autodoc():
+ global _original_format_signature
+ import sphinx.ext.autodoc
+
+ if sphinx.ext.autodoc.format_signature is our_format_signature:
+ return
+
+ print("[numpydoc] Monkeypatching sphinx.ext.autodoc ...")
+ _original_format_signature = sphinx.ext.autodoc.format_signature
+ sphinx.ext.autodoc.format_signature = our_format_signature
+
+def our_format_signature(what, obj):
+ r = mangle_signature(None, what, None, obj, None, None, None)
+ if r is not None:
+ return r[0]
+ else:
+ return _original_format_signature(what, obj)
diff --git a/doc/sphinxext/numpydoc/plot_directive.py b/doc/sphinxext/numpydoc/plot_directive.py
new file mode 100644
index 00000000..be03c357
--- /dev/null
+++ b/doc/sphinxext/numpydoc/plot_directive.py
@@ -0,0 +1,773 @@
+"""
+A directive for including a matplotlib plot in a Sphinx document.
+
+By default, in HTML output, `plot` will include a .png file with a
+link to a high-res .png and .pdf. In LaTeX output, it will include a
+.pdf.
+
+The source code for the plot may be included in one of three ways:
+
+ 1. **A path to a source file** as the argument to the directive::
+
+ .. plot:: path/to/plot.py
+
+ When a path to a source file is given, the content of the
+ directive may optionally contain a caption for the plot::
+
+ .. plot:: path/to/plot.py
+
+ This is the caption for the plot
+
+ Additionally, one my specify the name of a function to call (with
+ no arguments) immediately after importing the module::
+
+ .. plot:: path/to/plot.py plot_function1
+
+ 2. Included as **inline content** to the directive::
+
+ .. plot::
+
+ import matplotlib.pyplot as plt
+ import matplotlib.image as mpimg
+ import numpy as np
+ img = mpimg.imread('_static/stinkbug.png')
+ imgplot = plt.imshow(img)
+
+ 3. Using **doctest** syntax::
+
+ .. plot::
+ A plotting example:
+ >>> import matplotlib.pyplot as plt
+ >>> plt.plot([1,2,3], [4,5,6])
+
+Options
+-------
+
+The ``plot`` directive supports the following options:
+
+ format : {'python', 'doctest'}
+ Specify the format of the input
+
+ include-source : bool
+ Whether to display the source code. The default can be changed
+ using the `plot_include_source` variable in conf.py
+
+ encoding : str
+ If this source file is in a non-UTF8 or non-ASCII encoding,
+ the encoding must be specified using the `:encoding:` option.
+ The encoding will not be inferred using the ``-*- coding -*-``
+ metacomment.
+
+ context : bool
+ If provided, the code will be run in the context of all
+ previous plot directives for which the `:context:` option was
+ specified. This only applies to inline code plot directives,
+ not those run from files.
+
+ nofigs : bool
+ If specified, the code block will be run, but no figures will
+ be inserted. This is usually useful with the ``:context:``
+ option.
+
+Additionally, this directive supports all of the options of the
+`image` directive, except for `target` (since plot will add its own
+target). These include `alt`, `height`, `width`, `scale`, `align` and
+`class`.
+
+Configuration options
+---------------------
+
+The plot directive has the following configuration options:
+
+ plot_include_source
+ Default value for the include-source option
+
+ plot_pre_code
+ Code that should be executed before each plot.
+
+ plot_basedir
+ Base directory, to which ``plot::`` file names are relative
+ to. (If None or empty, file names are relative to the
+ directoly where the file containing the directive is.)
+
+ plot_formats
+ File formats to generate. List of tuples or strings::
+
+ [(suffix, dpi), suffix, ...]
+
+ that determine the file format and the DPI. For entries whose
+ DPI was omitted, sensible defaults are chosen.
+
+ plot_html_show_formats
+ Whether to show links to the files in HTML.
+
+ plot_rcparams
+ A dictionary containing any non-standard rcParams that should
+ be applied before each plot.
+
+"""
+
+import sys, os, glob, shutil, imp, warnings, io, re, textwrap, \
+ traceback, exceptions
+
+from docutils.parsers.rst import directives
+from docutils import nodes
+from docutils.parsers.rst.directives.images import Image
+align = Image.align
+import sphinx
+
+sphinx_version = sphinx.__version__.split(".")
+# The split is necessary for sphinx beta versions where the string is
+# '6b1'
+sphinx_version = tuple([int(re.split('[a-z]', x)[0])
+ for x in sphinx_version[:2]])
+
+try:
+ # Sphinx depends on either Jinja or Jinja2
+ import jinja2
+ def format_template(template, **kw):
+ return jinja2.Template(template).render(**kw)
+except ImportError:
+ import jinja
+ def format_template(template, **kw):
+ return jinja.from_string(template, **kw)
+
+import matplotlib
+import matplotlib.cbook as cbook
+matplotlib.use('Agg')
+import matplotlib.pyplot as plt
+from matplotlib import _pylab_helpers
+
+__version__ = 2
+
+#------------------------------------------------------------------------------
+# Relative pathnames
+#------------------------------------------------------------------------------
+
+# os.path.relpath is new in Python 2.6
+try:
+ from os.path import relpath
+except ImportError:
+ # Copied from Python 2.7
+ if 'posix' in sys.builtin_module_names:
+ def relpath(path, start=os.path.curdir):
+ """Return a relative version of a path"""
+ from os.path import sep, curdir, join, abspath, commonprefix, \
+ pardir
+
+ if not path:
+ raise ValueError("no path specified")
+
+ start_list = abspath(start).split(sep)
+ path_list = abspath(path).split(sep)
+
+ # Work out how much of the filepath is shared by start and path.
+ i = len(commonprefix([start_list, path_list]))
+
+ rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
+ if not rel_list:
+ return curdir
+ return join(*rel_list)
+ elif 'nt' in sys.builtin_module_names:
+ def relpath(path, start=os.path.curdir):
+ """Return a relative version of a path"""
+ from os.path import sep, curdir, join, abspath, commonprefix, \
+ pardir, splitunc
+
+ if not path:
+ raise ValueError("no path specified")
+ start_list = abspath(start).split(sep)
+ path_list = abspath(path).split(sep)
+ if start_list[0].lower() != path_list[0].lower():
+ unc_path, rest = splitunc(path)
+ unc_start, rest = splitunc(start)
+ if bool(unc_path) ^ bool(unc_start):
+ raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
+ % (path, start))
+ else:
+ raise ValueError("path is on drive %s, start on drive %s"
+ % (path_list[0], start_list[0]))
+ # Work out how much of the filepath is shared by start and path.
+ for i in range(min(len(start_list), len(path_list))):
+ if start_list[i].lower() != path_list[i].lower():
+ break
+ else:
+ i += 1
+
+ rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
+ if not rel_list:
+ return curdir
+ return join(*rel_list)
+ else:
+ raise RuntimeError("Unsupported platform (no relpath available!)")
+
+#------------------------------------------------------------------------------
+# Registration hook
+#------------------------------------------------------------------------------
+
+def plot_directive(name, arguments, options, content, lineno,
+ content_offset, block_text, state, state_machine):
+ return run(arguments, content, options, state_machine, state, lineno)
+plot_directive.__doc__ = __doc__
+
+def _option_boolean(arg):
+ if not arg or not arg.strip():
+ # no argument given, assume used as a flag
+ return True
+ elif arg.strip().lower() in ('no', '0', 'false'):
+ return False
+ elif arg.strip().lower() in ('yes', '1', 'true'):
+ return True
+ else:
+ raise ValueError('"%s" unknown boolean' % arg)
+
+def _option_format(arg):
+ return directives.choice(arg, ('python', 'doctest'))
+
+def _option_align(arg):
+ return directives.choice(arg, ("top", "middle", "bottom", "left", "center",
+ "right"))
+
+def mark_plot_labels(app, document):
+ """
+ To make plots referenceable, we need to move the reference from
+ the "htmlonly" (or "latexonly") node to the actual figure node
+ itself.
+ """
+ for name, explicit in document.nametypes.items():
+ if not explicit:
+ continue
+ labelid = document.nameids[name]
+ if labelid is None:
+ continue
+ node = document.ids[labelid]
+ if node.tagname in ('html_only', 'latex_only'):
+ for n in node:
+ if n.tagname == 'figure':
+ sectname = name
+ for c in n:
+ if c.tagname == 'caption':
+ sectname = c.astext()
+ break
+
+ node['ids'].remove(labelid)
+ node['names'].remove(name)
+ n['ids'].append(labelid)
+ n['names'].append(name)
+ document.settings.env.labels[name] = \
+ document.settings.env.docname, labelid, sectname
+ break
+
+def setup(app):
+ setup.app = app
+ setup.config = app.config
+ setup.confdir = app.confdir
+
+ options = {'alt': directives.unchanged,
+ 'height': directives.length_or_unitless,
+ 'width': directives.length_or_percentage_or_unitless,
+ 'scale': directives.nonnegative_int,
+ 'align': _option_align,
+ 'class': directives.class_option,
+ 'include-source': _option_boolean,
+ 'format': _option_format,
+ 'context': directives.flag,
+ 'nofigs': directives.flag,
+ 'encoding': directives.encoding
+ }
+
+ app.add_directive('plot', plot_directive, True, (0, 2, False), **options)
+ app.add_config_value('plot_pre_code', None, True)
+ app.add_config_value('plot_include_source', False, True)
+ app.add_config_value('plot_formats', ['png', 'hires.png', 'pdf'], True)
+ app.add_config_value('plot_basedir', None, True)
+ app.add_config_value('plot_html_show_formats', True, True)
+ app.add_config_value('plot_rcparams', {}, True)
+
+ app.connect('doctree-read', mark_plot_labels)
+
+#------------------------------------------------------------------------------
+# Doctest handling
+#------------------------------------------------------------------------------
+
+def contains_doctest(text):
+ try:
+ # check if it's valid Python as-is
+ compile(text, '', 'exec')
+ return False
+ except SyntaxError:
+ pass
+ r = re.compile(r'^\s*>>>', re.M)
+ m = r.search(text)
+ return bool(m)
+
+def unescape_doctest(text):
+ """
+ Extract code from a piece of text, which contains either Python code
+ or doctests.
+
+ """
+ if not contains_doctest(text):
+ return text
+
+ code = ""
+ for line in text.split("\n"):
+ m = re.match(r'^\s*(>>>|\.\.\.) (.*)$', line)
+ if m:
+ code += m.group(2) + "\n"
+ elif line.strip():
+ code += "# " + line.strip() + "\n"
+ else:
+ code += "\n"
+ return code
+
+def split_code_at_show(text):
+ """
+ Split code at plt.show()
+
+ """
+
+ parts = []
+ is_doctest = contains_doctest(text)
+
+ part = []
+ for line in text.split("\n"):
+ if (not is_doctest and line.strip() == 'plt.show()') or \
+ (is_doctest and line.strip() == '>>> plt.show()'):
+ part.append(line)
+ parts.append("\n".join(part))
+ part = []
+ else:
+ part.append(line)
+ if "\n".join(part).strip():
+ parts.append("\n".join(part))
+ return parts
+
+#------------------------------------------------------------------------------
+# Template
+#------------------------------------------------------------------------------
+
+
+TEMPLATE = """
+{{ source_code }}
+
+{{ only_html }}
+
+ {% if source_link or (html_show_formats and not multi_image) %}
+ (
+ {%- if source_link -%}
+ `Source code <{{ source_link }}>`__
+ {%- endif -%}
+ {%- if html_show_formats and not multi_image -%}
+ {%- for img in images -%}
+ {%- for fmt in img.formats -%}
+ {%- if source_link or not loop.first -%}, {% endif -%}
+ `{{ fmt }} <{{ dest_dir }}/{{ img.basename }}.{{ fmt }}>`__
+ {%- endfor -%}
+ {%- endfor -%}
+ {%- endif -%}
+ )
+ {% endif %}
+
+ {% for img in images %}
+ .. figure:: {{ build_dir }}/{{ img.basename }}.png
+ {%- for option in options %}
+ {{ option }}
+ {% endfor %}
+
+ {% if html_show_formats and multi_image -%}
+ (
+ {%- for fmt in img.formats -%}
+ {%- if not loop.first -%}, {% endif -%}
+ `{{ fmt }} <{{ dest_dir }}/{{ img.basename }}.{{ fmt }}>`__
+ {%- endfor -%}
+ )
+ {%- endif -%}
+
+ {{ caption }}
+ {% endfor %}
+
+{{ only_latex }}
+
+ {% for img in images %}
+ .. image:: {{ build_dir }}/{{ img.basename }}.pdf
+ {% endfor %}
+
+"""
+
+exception_template = """
+.. htmlonly::
+
+ [`source code <%(linkdir)s/%(basename)s.py>`__]
+
+Exception occurred rendering plot.
+
+"""
+
+# the context of the plot for all directives specified with the
+# :context: option
+plot_context = dict()
+
+class ImageFile:
+ def __init__(self, basename, dirname):
+ self.basename = basename
+ self.dirname = dirname
+ self.formats = []
+
+ def filename(self, format):
+ return os.path.join(self.dirname, "%s.%s" % (self.basename, format))
+
+ def filenames(self):
+ return [self.filename(fmt) for fmt in self.formats]
+
+def out_of_date(original, derived):
+ """
+ Returns True if derivative is out-of-date wrt original,
+ both of which are full file paths.
+ """
+ return (not os.path.exists(derived) or
+ (os.path.exists(original) and
+ os.stat(derived).st_mtime < os.stat(original).st_mtime))
+
+class PlotError(RuntimeError):
+ pass
+
+def run_code(code, code_path, ns=None, function_name=None):
+ """
+ Import a Python module from a path, and run the function given by
+ name, if function_name is not None.
+ """
+
+ # Change the working directory to the directory of the example, so
+ # it can get at its data files, if any. Add its path to sys.path
+ # so it can import any helper modules sitting beside it.
+
+ pwd = os.getcwd()
+ old_sys_path = list(sys.path)
+ if code_path is not None:
+ dirname = os.path.abspath(os.path.dirname(code_path))
+ os.chdir(dirname)
+ sys.path.insert(0, dirname)
+
+ # Redirect stdout
+ stdout = sys.stdout
+ sys.stdout = io.StringIO()
+
+ # Reset sys.argv
+ old_sys_argv = sys.argv
+ sys.argv = [code_path]
+
+ try:
+ try:
+ code = unescape_doctest(code)
+ if ns is None:
+ ns = {}
+ if not ns:
+ if setup.config.plot_pre_code is None:
+ exec("import numpy as np\nfrom matplotlib import pyplot as plt\n", ns)
+ else:
+ exec(setup.config.plot_pre_code, ns)
+ if "__main__" in code:
+ exec("__name__ = '__main__'", ns)
+ exec(code, ns)
+ if function_name is not None:
+ exec(function_name + "()", ns)
+ except (Exception, SystemExit) as err:
+ raise PlotError(traceback.format_exc())
+ finally:
+ os.chdir(pwd)
+ sys.argv = old_sys_argv
+ sys.path[:] = old_sys_path
+ sys.stdout = stdout
+ return ns
+
+def clear_state(plot_rcparams):
+ plt.close('all')
+ matplotlib.rc_file_defaults()
+ matplotlib.rcParams.update(plot_rcparams)
+
+def render_figures(code, code_path, output_dir, output_base, context,
+ function_name, config):
+ """
+ Run a pyplot script and save the low and high res PNGs and a PDF
+ in outdir.
+
+ Save the images under *output_dir* with file names derived from
+ *output_base*
+ """
+ # -- Parse format list
+ default_dpi = {'png': 80, 'hires.png': 200, 'pdf': 200}
+ formats = []
+ plot_formats = config.plot_formats
+ if isinstance(plot_formats, str):
+ plot_formats = eval(plot_formats)
+ for fmt in plot_formats:
+ if isinstance(fmt, str):
+ formats.append((fmt, default_dpi.get(fmt, 80)))
+ elif type(fmt) in (tuple, list) and len(fmt)==2:
+ formats.append((str(fmt[0]), int(fmt[1])))
+ else:
+ raise PlotError('invalid image format "%r" in plot_formats' % fmt)
+
+ # -- Try to determine if all images already exist
+
+ code_pieces = split_code_at_show(code)
+
+ # Look for single-figure output files first
+ # Look for single-figure output files first
+ all_exists = True
+ img = ImageFile(output_base, output_dir)
+ for format, dpi in formats:
+ if out_of_date(code_path, img.filename(format)):
+ all_exists = False
+ break
+ img.formats.append(format)
+
+ if all_exists:
+ return [(code, [img])]
+
+ # Then look for multi-figure output files
+ results = []
+ all_exists = True
+ for i, code_piece in enumerate(code_pieces):
+ images = []
+ for j in range(1000):
+ if len(code_pieces) > 1:
+ img = ImageFile('%s_%02d_%02d' % (output_base, i, j), output_dir)
+ else:
+ img = ImageFile('%s_%02d' % (output_base, j), output_dir)
+ for format, dpi in formats:
+ if out_of_date(code_path, img.filename(format)):
+ all_exists = False
+ break
+ img.formats.append(format)
+
+ # assume that if we have one, we have them all
+ if not all_exists:
+ all_exists = (j > 0)
+ break
+ images.append(img)
+ if not all_exists:
+ break
+ results.append((code_piece, images))
+
+ if all_exists:
+ return results
+
+ # We didn't find the files, so build them
+
+ results = []
+ if context:
+ ns = plot_context
+ else:
+ ns = {}
+
+ for i, code_piece in enumerate(code_pieces):
+ if not context:
+ clear_state(config.plot_rcparams)
+ run_code(code_piece, code_path, ns, function_name)
+
+ images = []
+ fig_managers = _pylab_helpers.Gcf.get_all_fig_managers()
+ for j, figman in enumerate(fig_managers):
+ if len(fig_managers) == 1 and len(code_pieces) == 1:
+ img = ImageFile(output_base, output_dir)
+ elif len(code_pieces) == 1:
+ img = ImageFile("%s_%02d" % (output_base, j), output_dir)
+ else:
+ img = ImageFile("%s_%02d_%02d" % (output_base, i, j),
+ output_dir)
+ images.append(img)
+ for format, dpi in formats:
+ try:
+ figman.canvas.figure.savefig(img.filename(format), dpi=dpi)
+ except Exception as err:
+ raise PlotError(traceback.format_exc())
+ img.formats.append(format)
+
+ results.append((code_piece, images))
+
+ if not context:
+ clear_state(config.plot_rcparams)
+
+ return results
+
+def run(arguments, content, options, state_machine, state, lineno):
+ # The user may provide a filename *or* Python code content, but not both
+ if arguments and content:
+ raise RuntimeError("plot:: directive can't have both args and content")
+
+ document = state_machine.document
+ config = document.settings.env.config
+ nofigs = 'nofigs' in options
+
+ options.setdefault('include-source', config.plot_include_source)
+ context = 'context' in options
+
+ rst_file = document.attributes['source']
+ rst_dir = os.path.dirname(rst_file)
+
+ if len(arguments):
+ if not config.plot_basedir:
+ source_file_name = os.path.join(setup.app.builder.srcdir,
+ directives.uri(arguments[0]))
+ else:
+ source_file_name = os.path.join(setup.app.builder.srcdir, config.plot_basedir,
+ directives.uri(arguments[0]))
+
+ # If there is content, it will be passed as a caption.
+ caption = '\n'.join(content)
+
+ # If the optional function name is provided, use it
+ if len(arguments) == 2:
+ function_name = arguments[1]
+ else:
+ function_name = None
+
+ fd = open(source_file_name, 'r')
+ code = fd.read()
+ fd.close()
+ output_base = os.path.basename(source_file_name)
+ else:
+ source_file_name = rst_file
+ code = textwrap.dedent("\n".join(map(str, content)))
+ counter = document.attributes.get('_plot_counter', 0) + 1
+ document.attributes['_plot_counter'] = counter
+ base, ext = os.path.splitext(os.path.basename(source_file_name))
+ output_base = '%s-%d.py' % (base, counter)
+ function_name = None
+ caption = ''
+
+ base, source_ext = os.path.splitext(output_base)
+ if source_ext in ('.py', '.rst', '.txt'):
+ output_base = base
+ else:
+ source_ext = ''
+
+ # ensure that LaTeX includegraphics doesn't choke in foo.bar.pdf filenames
+ output_base = output_base.replace('.', '-')
+
+ # is it in doctest format?
+ is_doctest = contains_doctest(code)
+ if 'format' in options:
+ if options['format'] == 'python':
+ is_doctest = False
+ else:
+ is_doctest = True
+
+ # determine output directory name fragment
+ source_rel_name = relpath(source_file_name, setup.app.srcdir)
+ source_rel_dir = os.path.dirname(source_rel_name)
+ while source_rel_dir.startswith(os.path.sep):
+ source_rel_dir = source_rel_dir[1:]
+
+ # build_dir: where to place output files (temporarily)
+ build_dir = os.path.join(os.path.dirname(setup.app.doctreedir),
+ 'plot_directive',
+ source_rel_dir)
+ # get rid of .. in paths, also changes pathsep
+ # see note in Python docs for warning about symbolic links on Windows.
+ # need to compare source and dest paths at end
+ build_dir = os.path.normpath(build_dir)
+
+ if not os.path.exists(build_dir):
+ os.makedirs(build_dir)
+
+ # output_dir: final location in the builder's directory
+ dest_dir = os.path.abspath(os.path.join(setup.app.builder.outdir,
+ source_rel_dir))
+ if not os.path.exists(dest_dir):
+ os.makedirs(dest_dir) # no problem here for me, but just use built-ins
+
+ # how to link to files from the RST file
+ dest_dir_link = os.path.join(relpath(setup.app.srcdir, rst_dir),
+ source_rel_dir).replace(os.path.sep, '/')
+ build_dir_link = relpath(build_dir, rst_dir).replace(os.path.sep, '/')
+ source_link = dest_dir_link + '/' + output_base + source_ext
+
+ # make figures
+ try:
+ results = render_figures(code, source_file_name, build_dir, output_base,
+ context, function_name, config)
+ errors = []
+ except PlotError as err:
+ reporter = state.memo.reporter
+ sm = reporter.system_message(
+ 2, "Exception occurred in plotting %s\n from %s:\n%s" % (output_base,
+ source_file_name, err),
+ line=lineno)
+ results = [(code, [])]
+ errors = [sm]
+
+ # Properly indent the caption
+ caption = '\n'.join(' ' + line.strip()
+ for line in caption.split('\n'))
+
+ # generate output restructuredtext
+ total_lines = []
+ for j, (code_piece, images) in enumerate(results):
+ if options['include-source']:
+ if is_doctest:
+ lines = ['']
+ lines += [row.rstrip() for row in code_piece.split('\n')]
+ else:
+ lines = ['.. code-block:: python', '']
+ lines += [' %s' % row.rstrip()
+ for row in code_piece.split('\n')]
+ source_code = "\n".join(lines)
+ else:
+ source_code = ""
+
+ if nofigs:
+ images = []
+
+ opts = [':%s: %s' % (key, val) for key, val in list(options.items())
+ if key in ('alt', 'height', 'width', 'scale', 'align', 'class')]
+
+ only_html = ".. only:: html"
+ only_latex = ".. only:: latex"
+
+ if j == 0:
+ src_link = source_link
+ else:
+ src_link = None
+
+ result = format_template(
+ TEMPLATE,
+ dest_dir=dest_dir_link,
+ build_dir=build_dir_link,
+ source_link=src_link,
+ multi_image=len(images) > 1,
+ only_html=only_html,
+ only_latex=only_latex,
+ options=opts,
+ images=images,
+ source_code=source_code,
+ html_show_formats=config.plot_html_show_formats,
+ caption=caption)
+
+ total_lines.extend(result.split("\n"))
+ total_lines.extend("\n")
+
+ if total_lines:
+ state_machine.insert_input(total_lines, source=source_file_name)
+
+ # copy image files to builder's output directory, if necessary
+ if not os.path.exists(dest_dir):
+ cbook.mkdirs(dest_dir)
+
+ for code_piece, images in results:
+ for img in images:
+ for fn in img.filenames():
+ destimg = os.path.join(dest_dir, os.path.basename(fn))
+ if fn != destimg:
+ shutil.copyfile(fn, destimg)
+
+ # copy script (if necessary)
+ #if source_file_name == rst_file:
+ target_name = os.path.join(dest_dir, output_base + source_ext)
+ f = open(target_name, 'w')
+ f.write(unescape_doctest(code))
+ f.close()
+
+ return errors
diff --git a/doc/sphinxext/sphinx_autorun/__init__.py b/doc/sphinxext/sphinx_autorun/__init__.py
new file mode 100644
index 00000000..1afa0377
--- /dev/null
+++ b/doc/sphinxext/sphinx_autorun/__init__.py
@@ -0,0 +1,93 @@
+# -*- coding: utf-8 -*-
+"""
+sphinxcontirb.autorun
+~~~~~~~~~~~~~~~~~~~~~~
+
+Run the code and insert stdout after the code block.
+"""
+import os
+from subprocess import PIPE, Popen
+
+from docutils import nodes
+from docutils.parsers.rst import Directive, directives
+from sphinx.errors import SphinxError
+
+from sphinx_autorun import version
+
+__version__ = version.version
+
+
+class RunBlockError(SphinxError):
+ category = 'runblock error'
+
+
+class AutoRun(object):
+ here = os.path.abspath(__file__)
+ pycon = os.path.join(os.path.dirname(here), 'pycon.py')
+ config = {
+ 'pycon': 'python ' + pycon,
+ 'pycon_prefix_chars': 4,
+ 'pycon_show_source': False,
+ 'console': 'bash',
+ 'console_prefix_chars': 1,
+ }
+
+ @classmethod
+ def builder_init(cls, app):
+ cls.config.update(app.builder.config.autorun_languages)
+
+
+class RunBlock(Directive):
+ has_content = True
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = False
+ option_spec = {
+ 'linenos': directives.flag,
+ }
+
+ def run(self):
+ config = AutoRun.config
+ language = self.arguments[0]
+
+ if language not in config:
+ raise RunBlockError('Unknown language %s' % language)
+
+ # Get configuration values for the language
+ args = config[language].split()
+ input_encoding = config.get(language+'_input_encoding', 'utf8')
+ output_encoding = config.get(language+'_output_encoding', 'utf8')
+ prefix_chars = config.get(language+'_prefix_chars', 0)
+ show_source = config.get(language+'_show_source', True)
+
+ # Build the code text
+ proc = Popen(args, bufsize=1, stdin=PIPE, stdout=PIPE, stderr=PIPE)
+ codelines = (line[prefix_chars:] for line in self.content)
+ code = u'\n'.join(codelines).encode(input_encoding)
+
+ # Run the code
+ stdout, stderr = proc.communicate(code)
+
+ # Process output
+ if stdout:
+ out = stdout.decode(output_encoding)
+ if stderr:
+ out = stderr.decode(output_encoding)
+
+ # Get the original code with prefixes
+ if show_source:
+ code = u'\n'.join(self.content)
+ code_out = u'\n'.join((code, out))
+ else:
+ code_out = out
+
+ literal = nodes.literal_block(code_out, code_out)
+ literal['language'] = language
+ literal['linenos'] = 'linenos' in self.options
+ return [literal]
+
+
+def setup(app):
+ app.add_directive('runblock', RunBlock)
+ app.connect('builder-inited', AutoRun.builder_init)
+ app.add_config_value('autorun_languages', AutoRun.config, 'env')
diff --git a/doc/sphinxext/sphinx_autorun/pycon.py b/doc/sphinxext/sphinx_autorun/pycon.py
new file mode 100644
index 00000000..c0edf861
--- /dev/null
+++ b/doc/sphinxext/sphinx_autorun/pycon.py
@@ -0,0 +1,31 @@
+import sys
+from code import InteractiveInterpreter
+
+
+def main():
+ """
+ Print lines of input along with output.
+ """
+ source_lines = (line.rstrip() for line in sys.stdin)
+ console = InteractiveInterpreter()
+ source = ''
+ try:
+ while True:
+ source = next(source_lines)
+ # Allow the user to ignore specific lines of output.
+ if not source.endswith('# ignore'):
+ print('>>>', source)
+ more = console.runsource(source)
+ while more:
+ next_line = next(source_lines)
+ print('...', next_line)
+ source += '\n' + next_line
+ more = console.runsource(source)
+ except StopIteration:
+ if more:
+ print('... ')
+ more = console.runsource(source + '\n')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/doc/sphinxext/sphinx_autorun/version.py b/doc/sphinxext/sphinx_autorun/version.py
new file mode 100644
index 00000000..433d173a
--- /dev/null
+++ b/doc/sphinxext/sphinx_autorun/version.py
@@ -0,0 +1,4 @@
+# coding: utf-8
+# file generated by setuptools_scm
+# don't change, don't track in version control
+version = '1.1.1'
diff --git a/doc/sphinxext/triqs_example/triqs_example.py b/doc/sphinxext/triqs_example/triqs_example.py
new file mode 100644
index 00000000..2c90ac4c
--- /dev/null
+++ b/doc/sphinxext/triqs_example/triqs_example.py
@@ -0,0 +1,123 @@
+import tempfile
+# -*- coding: utf-8 -*-
+# seems to be executed at the level of the conf.py
+# so we need to link the lib at that place...
+"""
+"""
+import os
+import codecs
+from os import path
+from subprocess import Popen,PIPE
+from docutils import nodes
+from docutils.parsers.rst import Directive
+from docutils.parsers.rst import directives
+from sphinx.errors import SphinxError
+
+class TriqsExampleError(SphinxError):
+ category = 'triqs_example error'
+
+class TriqsExampleRun:
+ #here = os.path.abspath(__file__)
+ #pycon = os.path.join(os.path.dirname(here),'pycon.py')
+ config = dict(
+ )
+ @classmethod
+ def builder_init(cls,app):
+ #cls.config.update(app.builder.config.autorun_languages)
+ #cls.config.update(app.builder.config.autocompile_opts)
+ pass
+
+class TriqsExample(Directive):
+ has_content = True
+ required_arguments = 1
+ optional_arguments = 0
+ final_argument_whitespace = False
+ option_spec = {
+ 'linenos': directives.flag,
+ }
+
+ def run(self):
+ document = self.state.document
+ filename = self.arguments[0]
+ if not document.settings.file_insertion_enabled:
+ return [document.reporter.warning('File insertion disabled',
+ line=self.lineno)]
+ env = document.settings.env
+ if filename.startswith('/') or filename.startswith(os.sep):
+ rel_fn = filename[1:]
+ else:
+ docdir = path.dirname(env.doc2path(env.docname, base=None))
+ rel_fn = path.normpath(path.join(docdir, filename))
+ try:
+ fn = path.join(env.srcdir, rel_fn)
+ except UnicodeDecodeError:
+ # the source directory is a bytestring with non-ASCII characters;
+ # let's try to encode the rel_fn in the file system encoding
+ rel_fn = rel_fn.encode(sys.getfilesystemencoding())
+ fn = path.join(env.srcdir, rel_fn)
+
+ encoding = self.options.get('encoding', env.config.source_encoding)
+ try:
+ f = codecs.open(fn, 'rU', encoding)
+ lines = f.readlines()
+ f.close()
+ except (IOError, OSError):
+ return [document.reporter.warning(
+ 'Include file %r not found or reading it failed' % filename,
+ line=self.lineno)]
+ except UnicodeError:
+ return [document.reporter.warning(
+ 'Encoding %r used for reading included file %r seems to '
+ 'be wrong, try giving an :encoding: option' %
+ (encoding, filename))]
+
+ config = TriqsExampleRun.config
+
+ # Get configuration values for the language
+ input_encoding = 'utf8' #config.get(language+'_input_encoding','ascii')
+ output_encoding = 'utf8' #config.get(language+'_output_encoding','ascii')
+ show_source = True
+
+ # Build the code text
+ code = ''.join(lines).strip()
+ filename_clean = filename.rsplit('.',1)[0]
+ if filename_clean.startswith('./') : filename_clean = filename_clean[2:]
+ #print "Running the example ....",filename_clean
+ #print "Root ?", env.doc2path(env.docname, base=None)
+
+ import subprocess as S
+ error = True
+ try :
+ stdout =''
+ #resout = S.check_output("./example_bin/doc_%s"%(filename_clean) ,stderr=S.STDOUT,shell=True)
+ resout = S.check_output("./%s/doc_%s"%(docdir,filename_clean) ,stderr=S.STDOUT,shell=True)
+ if resout :
+ stdout = '---------- Result is -------\n' + resout.strip()
+ error = False
+ except S.CalledProcessError as E :
+ stdout ='---------- RunTime error -------\n'
+ stdout += E.output
+
+ # Process output
+ if stdout:
+ stdout = stdout.decode(output_encoding,'ignore')
+ out = ''.join(stdout).decode(output_encoding)
+ else:
+ out = '' #.join(stderr).decode(output_encoding)
+
+ # Get the original code with prefixes
+ code_out = '\n'.join((code,out))
+
+ if error : # report on console
+ print(" Error in processing ")
+ print(code_out)
+
+ literal = nodes.literal_block(code_out,code_out)
+ literal['language'] = 'c'
+ literal['linenos'] = 'linenos' in self.options
+ return [literal]
+
+def setup(app):
+ app.add_directive('triqs_example', TriqsExample)
+ app.connect('builder-inited',TriqsExampleRun.builder_init)
+
diff --git a/doc/themes/agogo/layout.html b/doc/themes/agogo/layout.html
new file mode 100644
index 00000000..869d76ad
--- /dev/null
+++ b/doc/themes/agogo/layout.html
@@ -0,0 +1,92 @@
+{#
+ agogo/layout.html
+ ~~~~~~~~~~~~~~~~~
+
+ Sphinx layout template for the agogo theme, originally written
+ by Andi Albrecht.
+
+ :copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
+ :license: BSD, see LICENSE for details.
+#}
+{% extends "basic/layout.html" %}
+
+{% block header %}
+