Description¶
This package provides a light framework that can be used to automatically generate exceptions documentation marked up in reStructuredText. The pexdoc.exdoc module details how to register exceptions, how to traced them and how to generate their documentation
Interpreter¶
The package has been developed and tested with Python 2.6, 2.7, 3.3, 3.4 and 3.5 under Linux (Debian, Ubuntu), Apple OS X and Microsoft Windows
Installing¶
$ pip install pexdoc
Documentation¶
Available at Read the Docs
Contributing¶
Abide by the adopted code of conduct
Fork the repository from GitHub and then clone personal copy [1]:
$ git clone \ https://github.com/[github-user-name]/pexdoc.git Cloning into 'pexdoc'... ... $ cd pexdoc $ export PEXDOC_DIR=${PWD}
Install the project’s Git hooks and build the documentation. The pre-commit hook does some minor consistency checks, namely trailing whitespace and PEP8 compliance via Pylint. Assuming the directory to which the repository was cloned is in the
$PEXDOC_DIR
shell environment variable:$ ${PEXDOC_DIR}/sbin/complete-cloning.sh Installing Git hooks Building pexdoc package documentation ...
Ensure that the Python interpreter can find the package modules (update the
$PYTHONPATH
environment variable, or use sys.paths(), etc.)$ export PYTHONPATH=${PYTHONPATH}:${PEXDOC_DIR}
Install the dependencies (if needed, done automatically by pip):
- Astroid (Python 2.6: older than 1.4, Python 2.7 or newer: 1.3.8 or newer)
- Cog (2.4 or newer)
- Coverage (3.7.1 or newer)
- Decorator (3.4.2 or newer)
- Docutils (0.12 or newer)
- Funcsigs (Python 2.x only, 0.4 or newer)
- Inline Syntax Highlight Sphinx Extension (0.2 or newer)
- Mock (Python 2.x only, 1.0.1 or newer)
- Pmisc (1.0.0 or newer)
- Ptrie (1.0.0 or newer)
- Py.test (2.7.0 or newer)
- PyContracts (1.7.2 or newer except 1.7.7)
- Pylint (Python 2.6: 1.3 or newer and older than 1.4, Python 2.7 or newer: 1.3.1 or newer)
- Pytest-coverage (1.8.0 or newer)
- Pytest-xdist (optional, 1.8.0 or newer)
- ReadTheDocs Sphinx theme (0.1.9 or newer)
- Sphinx (1.2.3 or newer)
- Tox (1.9.0 or newer)
- Virtualenv (13.1.2 or newer)
Implement a new feature or fix a bug
Write a unit test which shows that the contributed code works as expected. Run the package tests to ensure that the bug fix or new feature does not have adverse side effects. If possible achieve 100% code and branch coverage of the contribution. Thorough package validation can be done via Tox and Py.test:
$ tox GLOB sdist-make: .../pexdoc/setup.py py26-pkg inst-nodeps: .../pexdoc/.tox/dist/pexdoc-...zip
Setuptools can also be used (Tox is configured as its virtual environment manager):
$ python setup.py tests running tests running egg_info writing requirements to pexdoc.egg-info/requires.txt writing pexdoc.egg-info/PKG-INFO ...
Tox (or Setuptools via Tox) runs with the following default environments:
py26-pkg
,py27-pkg
,py33-pkg
,py34-pkg
andpy35-pkg
[2]. These use the Python 2.6, 2.7, 3.3, 3.4 and 3.5 interpreters, respectively, to test all code in the documentation (both in Sphinx*.rst
source files and in docstrings), run all unit tests, measure test coverage and re-build the exceptions documentation. To pass arguments to Py.test (the test runner) use a double dash (--
) after all the Tox arguments, for example:$ tox -e py27-pkg -- -n 4 GLOB sdist-make: .../pexdoc/setup.py py27-pkg inst-nodeps: .../pexdoc/.tox/dist/pexdoc-...zip ...
Or use the
-a
Setuptools optional argument followed by a quoted string with the arguments for Py.test. For example:$ python setup.py tests -a "-e py27-pkg -- -n 4" running tests ...
There are other convenience environments defined for Tox [3]:
py26-repl
,py27-repl
,py33-repl
,py34-repl
andpy35-repl
run the Python 2.6, 2.7, 3.3, 3.4 or 3.5 REPL, respectively, in the appropriate virtual environment. Thepexdoc
package is pip-installed by Tox when the environments are created. Arguments to the interpreter can be passed in the command line after a double dash (--
)py26-test
,py27-test
,py33-test
,py34-test
andpy35-test
run py.test using the Python 2.6, 2.7, 3.3, 3.4 or Python 3.5 interpreter, respectively, in the appropriate virtual environment. Arguments to py.test can be passed in the command line after a double dash (--
) , for example:$ tox -e py34-test -- -x test_pexdoc.py GLOB sdist-make: [...]/pexdoc/setup.py py34-test inst-nodeps: [...]/pexdoc/.tox/dist/pexdoc-[...].zip py34-test runtests: PYTHONHASHSEED='680528711' py34-test runtests: [...]py.test -x test_pexdoc.py ============== test session starts ============== platform linux -- Python 3.4.2 -- py-1.4.30 -- [...] ...
py26-cov
,py27-cov
,py33-cov
,py34-cov
andpy35-cov
test code and branch coverage using the Python 2.6, 2.7, 3.3, 3.4 or 3.5 interpreter, respectively, in the appropriate virtual environment. Arguments to py.test can be passed in the command line after a double dash (--
). The report can be found in${PEXDOC_DIR}/.tox/py[PV]/usr/share/pexdoc/tests/htmlcov/ index.html
where[PV]
stands for26
,27
,33
,34
or35
depending on the interpreter used
Verify that continuous integration tests pass. The package has continuous integration configured for Linux (via Travis) and for Microsoft Windows (via Appveyor). Aggregation/cloud code coverage is configured via Codecov. It is assumed that the Codecov repository upload token in the Travis build is stored in the
${CODECOV_TOKEN}
environment variable (securely defined in the Travis repository settings page).Document the new feature or bug fix (if needed). The script
${PEXDOC_DIR}/sbin/build_docs.py
re-builds the whole package documentation (re-generates images, cogs source files, etc.):$ ${PMISC_DIR}/sbin/build_docs.py -h usage: build_docs.py [-h] [-d DIRECTORY] Build pexdoc package documentation optional arguments: -h, --help show this help message and exit -d DIRECTORY, --directory DIRECTORY specify source file directory (default ../pexdoc)
Output of shell commands can be automatically included in reStructuredText source files with the help of Cog and the
docs.support.term_echo
module.-
docs.support.term_echo.
ste
(command, nindent, mdir, fpointer) Simplified terminal echo; prints STDOUT resulting from a given Bash shell command (relative to the package
sbin
directory) formatted in reStructuredTextParameters: - command (string) – Bash shell command, relative to
${PMISC_DIR}/sbin
- nindent (integer) – Indentation level
- mdir (string) – Module directory
- fpointer (function object) – Output function pointer. Normally is
cog.out
butprint
or other functions can be used for debugging
For example:
.. This is a reStructuredText file snippet .. [[[cog .. import os, sys .. from docs.support.term_echo import term_echo .. file_name = sys.modules['docs.support.term_echo'].__file__ .. mdir = os.path.realpath( .. os.path.dirname( .. os.path.dirname(os.path.dirname(file_name)) .. ) .. ) .. [[[cog ste('build_docs.py -h', 0, mdir, cog.out) ]]] .. code-block:: bash $ ${PMISC_DIR}/sbin/build_docs.py -h usage: build_docs.py [-h] [-d DIRECTORY] [-n NUM_CPUS] ... .. ]]]
- command (string) – Bash shell command, relative to
-
docs.support.term_echo.
term_echo
(command, nindent=0, env=None, fpointer=None, cols=60) Terminal echo; prints STDOUT resulting from a given Bash shell command formatted in reStructuredText
Parameters: - command (string) – Bash shell command
- nindent (integer) – Indentation level
- env (dictionary) – Environment variable replacement dictionary. The Bash
command is pre-processed and any environment variable
represented in the full notation (
${...}
) is replaced. The dictionary key is the environment variable name and the dictionary value is the replacement value. For example, if command is'${PYTHON_CMD} -m "x=5"'
and env is{'PYTHON_CMD':'python3'}
the actual command issued is'python3 -m "x=5"'
- fpointer (function object) – Output function pointer. Normally is
cog.out
butprint
or other functions can be used for debugging - cols (integer) – Number of columns of output
Similarly Python files can be included in docstrings with the help of Cog and the
docs.support.incfile
module-
docs.support.incfile.
incfile
(fname, fpointer, lrange='1, 6-', sdir=None) Includes a Python source file in a docstring formatted in reStructuredText
Parameters: - fname (string) – File name, relative to environment variable
${TRACER_DIR}
- fpointer (function object) – Output function pointer. Normally is
cog.out
butprint
or other functions can be used for debugging - lrange (string) – Line range to include, similar to Sphinx literalinclude directive
- sdir (string) – Source file directory. If None the
${TRACER_DIR}
environment variable is used if it is defined, otherwise the directory where thedocs.support.incfile
module is located is used
For example:
def func(): """ This is a docstring. This file shows how to use it: .. =[=cog .. import docs.support.incfile .. docs.support.incfile.incfile('func_example.py', cog.out) .. =]= .. code-block:: python # func_example.py if __name__ == '__main__': func() .. =[=end=]= """ return 'This is func output'
- fname (string) – File name, relative to environment variable
-
Footnotes
[1] | All examples are for the bash shell |
[2] | It is assumed that all the Python interpreters are in the executables path. Source code for the interpreters can be downloaded from Python’s main site |
[3] | Tox configuration largely inspired by Ionel’s codelog |
Changelog¶
- 1.0.0 [2016-04-23]: Final release of 1.0.0 branch
- 1.0.0rc1 [2016-04-22]: Initial commit, forked a subset from putil PyPI package
License¶
The MIT License (MIT)
Copyright (c) 2013-2016 Pablo Acosta-Serafini
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Contents¶
Description¶
This package provides a light framework that can be used to automatically generate exceptions documentation marked up in reStructuredText. The pexdoc.exdoc module details how to register exceptions, how to traced them and how to generate their documentation
Interpreter¶
The package has been developed and tested with Python 2.6, 2.7, 3.3, 3.4 and 3.5 under Linux (Debian, Ubuntu), Apple OS X and Microsoft Windows
Installing¶
$ pip install pexdoc
Documentation¶
Available at Read the Docs
Contributing¶
Abide by the adopted code of conduct
Fork the repository from GitHub and then clone personal copy [1]:
$ git clone \ https://github.com/[github-user-name]/pexdoc.git Cloning into 'pexdoc'... ... $ cd pexdoc $ export PEXDOC_DIR=${PWD}
Install the project’s Git hooks and build the documentation. The pre-commit hook does some minor consistency checks, namely trailing whitespace and PEP8 compliance via Pylint. Assuming the directory to which the repository was cloned is in the
$PEXDOC_DIR
shell environment variable:$ ${PEXDOC_DIR}/sbin/complete-cloning.sh Installing Git hooks Building pexdoc package documentation ...
Ensure that the Python interpreter can find the package modules (update the
$PYTHONPATH
environment variable, or use sys.paths(), etc.)$ export PYTHONPATH=${PYTHONPATH}:${PEXDOC_DIR}
Install the dependencies (if needed, done automatically by pip):
- Astroid (Python 2.6: older than 1.4, Python 2.7 or newer: 1.3.8 or newer)
- Cog (2.4 or newer)
- Coverage (3.7.1 or newer)
- Decorator (3.4.2 or newer)
- Docutils (0.12 or newer)
- Funcsigs (Python 2.x only, 0.4 or newer)
- Inline Syntax Highlight Sphinx Extension (0.2 or newer)
- Mock (Python 2.x only, 1.0.1 or newer)
- Pmisc (1.0.0 or newer)
- Ptrie (1.0.0 or newer)
- Py.test (2.7.0 or newer)
- PyContracts (1.7.2 or newer except 1.7.7)
- Pylint (Python 2.6: 1.3 or newer and older than 1.4, Python 2.7 or newer: 1.3.1 or newer)
- Pytest-coverage (1.8.0 or newer)
- Pytest-xdist (optional, 1.8.0 or newer)
- ReadTheDocs Sphinx theme (0.1.9 or newer)
- Sphinx (1.2.3 or newer)
- Tox (1.9.0 or newer)
- Virtualenv (13.1.2 or newer)
Implement a new feature or fix a bug
Write a unit test which shows that the contributed code works as expected. Run the package tests to ensure that the bug fix or new feature does not have adverse side effects. If possible achieve 100% code and branch coverage of the contribution. Thorough package validation can be done via Tox and Py.test:
$ tox GLOB sdist-make: .../pexdoc/setup.py py26-pkg inst-nodeps: .../pexdoc/.tox/dist/pexdoc-...zip
Setuptools can also be used (Tox is configured as its virtual environment manager):
$ python setup.py tests running tests running egg_info writing requirements to pexdoc.egg-info/requires.txt writing pexdoc.egg-info/PKG-INFO ...
Tox (or Setuptools via Tox) runs with the following default environments:
py26-pkg
,py27-pkg
,py33-pkg
,py34-pkg
andpy35-pkg
[2]. These use the Python 2.6, 2.7, 3.3, 3.4 and 3.5 interpreters, respectively, to test all code in the documentation (both in Sphinx*.rst
source files and in docstrings), run all unit tests, measure test coverage and re-build the exceptions documentation. To pass arguments to Py.test (the test runner) use a double dash (--
) after all the Tox arguments, for example:$ tox -e py27-pkg -- -n 4 GLOB sdist-make: .../pexdoc/setup.py py27-pkg inst-nodeps: .../pexdoc/.tox/dist/pexdoc-...zip ...
Or use the
-a
Setuptools optional argument followed by a quoted string with the arguments for Py.test. For example:$ python setup.py tests -a "-e py27-pkg -- -n 4" running tests ...
There are other convenience environments defined for Tox [3]:
py26-repl
,py27-repl
,py33-repl
,py34-repl
andpy35-repl
run the Python 2.6, 2.7, 3.3, 3.4 or 3.5 REPL, respectively, in the appropriate virtual environment. Thepexdoc
package is pip-installed by Tox when the environments are created. Arguments to the interpreter can be passed in the command line after a double dash (--
)py26-test
,py27-test
,py33-test
,py34-test
andpy35-test
run py.test using the Python 2.6, 2.7, 3.3, 3.4 or Python 3.5 interpreter, respectively, in the appropriate virtual environment. Arguments to py.test can be passed in the command line after a double dash (--
) , for example:$ tox -e py34-test -- -x test_pexdoc.py GLOB sdist-make: [...]/pexdoc/setup.py py34-test inst-nodeps: [...]/pexdoc/.tox/dist/pexdoc-[...].zip py34-test runtests: PYTHONHASHSEED='680528711' py34-test runtests: [...]py.test -x test_pexdoc.py ============== test session starts ============== platform linux -- Python 3.4.2 -- py-1.4.30 -- [...] ...
py26-cov
,py27-cov
,py33-cov
,py34-cov
andpy35-cov
test code and branch coverage using the Python 2.6, 2.7, 3.3, 3.4 or 3.5 interpreter, respectively, in the appropriate virtual environment. Arguments to py.test can be passed in the command line after a double dash (--
). The report can be found in${PEXDOC_DIR}/.tox/py[PV]/usr/share/pexdoc/tests/htmlcov/ index.html
where[PV]
stands for26
,27
,33
,34
or35
depending on the interpreter used
Verify that continuous integration tests pass. The package has continuous integration configured for Linux (via Travis) and for Microsoft Windows (via Appveyor). Aggregation/cloud code coverage is configured via Codecov. It is assumed that the Codecov repository upload token in the Travis build is stored in the
${CODECOV_TOKEN}
environment variable (securely defined in the Travis repository settings page).Document the new feature or bug fix (if needed). The script
${PEXDOC_DIR}/sbin/build_docs.py
re-builds the whole package documentation (re-generates images, cogs source files, etc.):$ ${PMISC_DIR}/sbin/build_docs.py -h usage: build_docs.py [-h] [-d DIRECTORY] Build pexdoc package documentation optional arguments: -h, --help show this help message and exit -d DIRECTORY, --directory DIRECTORY specify source file directory (default ../pexdoc)
Output of shell commands can be automatically included in reStructuredText source files with the help of Cog and the
docs.support.term_echo
module.-
docs.support.term_echo.
ste
(command, nindent, mdir, fpointer) Simplified terminal echo; prints STDOUT resulting from a given Bash shell command (relative to the package
sbin
directory) formatted in reStructuredTextParameters: - command (string) – Bash shell command, relative to
${PMISC_DIR}/sbin
- nindent (integer) – Indentation level
- mdir (string) – Module directory
- fpointer (function object) – Output function pointer. Normally is
cog.out
butprint
or other functions can be used for debugging
For example:
.. This is a reStructuredText file snippet .. [[[cog .. import os, sys .. from docs.support.term_echo import term_echo .. file_name = sys.modules['docs.support.term_echo'].__file__ .. mdir = os.path.realpath( .. os.path.dirname( .. os.path.dirname(os.path.dirname(file_name)) .. ) .. ) .. [[[cog ste('build_docs.py -h', 0, mdir, cog.out) ]]] .. code-block:: bash $ ${PMISC_DIR}/sbin/build_docs.py -h usage: build_docs.py [-h] [-d DIRECTORY] [-n NUM_CPUS] ... .. ]]]
- command (string) – Bash shell command, relative to
-
docs.support.term_echo.
term_echo
(command, nindent=0, env=None, fpointer=None, cols=60) Terminal echo; prints STDOUT resulting from a given Bash shell command formatted in reStructuredText
Parameters: - command (string) – Bash shell command
- nindent (integer) – Indentation level
- env (dictionary) – Environment variable replacement dictionary. The Bash
command is pre-processed and any environment variable
represented in the full notation (
${...}
) is replaced. The dictionary key is the environment variable name and the dictionary value is the replacement value. For example, if command is'${PYTHON_CMD} -m "x=5"'
and env is{'PYTHON_CMD':'python3'}
the actual command issued is'python3 -m "x=5"'
- fpointer (function object) – Output function pointer. Normally is
cog.out
butprint
or other functions can be used for debugging - cols (integer) – Number of columns of output
Similarly Python files can be included in docstrings with the help of Cog and the
docs.support.incfile
module-
docs.support.incfile.
incfile
(fname, fpointer, lrange='1, 6-', sdir=None) Includes a Python source file in a docstring formatted in reStructuredText
Parameters: - fname (string) – File name, relative to environment variable
${TRACER_DIR}
- fpointer (function object) – Output function pointer. Normally is
cog.out
butprint
or other functions can be used for debugging - lrange (string) – Line range to include, similar to Sphinx literalinclude directive
- sdir (string) – Source file directory. If None the
${TRACER_DIR}
environment variable is used if it is defined, otherwise the directory where thedocs.support.incfile
module is located is used
For example:
def func(): """ This is a docstring. This file shows how to use it: .. =[=cog .. import docs.support.incfile .. docs.support.incfile.incfile('func_example.py', cog.out) .. =]= .. code-block:: python # func_example.py if __name__ == '__main__': func() .. =[=end=]= """ return 'This is func output'
- fname (string) – File name, relative to environment variable
-
Footnotes
[1] | All examples are for the bash shell |
[2] | It is assumed that all the Python interpreters are in the executables path. Source code for the interpreters can be downloaded from Python’s main site |
[3] | Tox configuration largely inspired by Ionel’s codelog |
Changelog¶
- 1.0.0 [2016-04-23]: Final release of 1.0.0 branch
- 1.0.0rc1 [2016-04-22]: Initial commit, forked a subset from putil PyPI package
License¶
The MIT License (MIT)
Copyright (c) 2013-2016 Pablo Acosta-Serafini
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
API¶
exdoc module¶
This module can be used to automatically generate exceptions documentation marked up in reStructuredText with help from cog and the pexdoc.exh module.
The exceptions to auto-document need to be defined with either the pexdoc.exh.ExHandle.addex() function, the pexdoc.exh.ExHandle.addai() function or via contracts of the pexdoc.pcontracts module, and “traced” before the documentation is generated. In general tracing consists of calling the methods, functions and/or class properties so that all the required exceptions are covered (exceptions raised by contracts are automatically traced when the functions with the contracts are used). A convenient way of tracing a module is to simply run its test suite, provided that it covers the exceptions that need to be documented (for maximum speed the unit tests that cover the exceptions may be segregated so that only these tests need to be executed).
For example, it is desired to auto-document the exceptions of a module
my_module.py
, which has tests in test_my_module.py
. Then a tracing
module trace_my_module.py
can be created to leverage the already written
tests:
# trace_my_module_1.py
# Option 1: use already written test bench
from __future__ import print_function
import copy, os, pytest, pexdoc
def trace_module(no_print=True):
""" Trace my_module exceptions """
pwd = os.path.dirname(__file__)
script_name = repr(os.path.join(pwd, 'test_my_module.py'))
with pexdoc.ExDocCxt() as exdoc_obj:
if pytest.main('-s -vv -x {0}'.format(script_name)):
raise RuntimeError(
'Tracing did not complete successfully'
)
if not no_print:
module_prefix = 'docs.support.my_module.'
callable_names = ['func', 'MyClass.value']
for callable_name in callable_names:
callable_name = module_prefix+callable_name
print('\nCallable: {0}'.format(callable_name))
print(exdoc_obj.get_sphinx_doc(callable_name, width=70))
print('\n')
return copy.copy(exdoc_obj)
if __name__ == '__main__':
trace_module(False)
The context manager pexdoc.exdoc.ExDocCxt sets up the tracing environment and returns a pexdoc.exdoc.ExDoc object that can the be used in the documentation string of each callable to extract the exceptions documentation. In this example it is assumed that the tests are written using pytest, but any test framework can be used. Another way to trace the module is to simply call all the functions, methods or class properties that need to be documented. For example:
# trace_my_module_2.py
# Option 2: manually use all callables to document
from __future__ import print_function
import copy, pexdoc, docs.support.my_module
def trace_module(no_print=True):
""" Trace my_module_original exceptions """
with pexdoc.ExDocCxt() as exdoc_obj:
try:
docs.support.my_module.func('John')
obj = docs.support.my_module.MyClass()
obj.value = 5
obj.value
except:
raise RuntimeError(
'Tracing did not complete successfully'
)
if not no_print:
module_prefix = 'docs.support.my_module.'
callable_names = ['func', 'MyClass.value']
for callable_name in callable_names:
callable_name = module_prefix+callable_name
print('\nCallable: {0}'.format(callable_name))
print(exdoc_obj.get_sphinx_doc(callable_name, width=70))
print('\n')
return copy.copy(exdoc_obj)
if __name__ == '__main__':
trace_module(False)
And the actual module my_module
code is (before auto-documentation):
# my_module.py
# Exception tracing initialization code
"""
[[[cog
import os, sys
sys.path.append(os.environ['TRACER_DIR'])
import trace_my_module_1
exobj = trace_my_module_1.trace_module(no_print=True)
]]]
[[[end]]]
"""
import pexdoc
def func(name):
r"""
Prints your name
:param name: Name to print
:type name: string
.. [[[cog cog.out(exobj.get_sphinx_autodoc(width=69))]]]
.. [[[end]]]
"""
# Raise condition evaluated in same call as exception addition
pexdoc.addex(
TypeError, 'Argument `name` is not valid', not isinstance(name, str)
)
return 'My name is {0}'.format(name)
class MyClass(object):
""" Stores a value """
def __init__(self, value=None):
self._value = None if not value else value
def _get_value(self):
# Raise condition not evaluated in same call as
# exception additions
exobj = pexdoc.addex(RuntimeError, 'Attribute `value` not set')
exobj(not self._value)
return self._value
def _set_value(self, value):
exobj = pexdoc.addex(RuntimeError, 'Argument `value` is not valid')
exobj(not isinstance(value, int))
self._value = value
value = property(_get_value, _set_value)
r"""
Sets or returns a value
:type: integer
:rtype: integer or None
.. [[[cog cog.out(exobj.get_sphinx_autodoc(width=69))]]]
.. [[[end]]]
"""
A simple shell script can be written to automate the cogging of the
my_module.py
file:
#!/bin/bash
set -e
finish() {
export TRACER_DIR=""
cd ${cpwd}
}
trap finish EXIT
input_file=${1:-my_module.py}
output_file=${2:-my_module.py}
export TRACER_DIR=$(dirname ${input_file})
cog.py -e -x -o ${input_file}.tmp ${input_file} > /dev/null && \
mv -f ${input_file}.tmp ${input_file}
cog.py -e -o ${input_file}.tmp ${input_file} > /dev/null && \
mv -f ${input_file}.tmp ${output_file}
After the script is run and the auto-documentation generated, each callable has
a reStructuredText marked-up :raises:
section:
# my_module_ref.py
# Exception tracing initialization code
"""
[[[cog
import os, sys
sys.path.append(os.environ['TRACER_DIR'])
import trace_my_module_1
exobj = trace_my_module_1.trace_module(no_print=True)
]]]
[[[end]]]
"""
import pexdoc
def func(name):
r"""
Prints your name
:param name: Name to print
:type name: string
.. [[[cog cog.out(exobj.get_sphinx_autodoc(width=69))]]]
.. Auto-generated exceptions documentation for
.. docs.support.my_module.func
:raises: TypeError (Argument \`name\` is not valid)
.. [[[end]]]
"""
# Raise condition evaluated in same call as exception addition
pexdoc.addex(
TypeError, 'Argument `name` is not valid', not isinstance(name, str)
)
return 'My name is {0}'.format(name)
class MyClass(object):
""" Stores a value """
def __init__(self, value=None):
self._value = None if not value else value
def _get_value(self):
# Raise condition not evaluated in same call as
# exception additions
exobj = pexdoc.addex(RuntimeError, 'Attribute `value` not set')
exobj(not self._value)
return self._value
def _set_value(self, value):
exobj = pexdoc.addex(RuntimeError, 'Argument `value` is not valid')
exobj(not isinstance(value, int))
self._value = value
value = property(_get_value, _set_value)
r"""
Sets or returns a value
:type: integer
:rtype: integer or None
.. [[[cog cog.out(exobj.get_sphinx_autodoc(width=69))]]]
.. Auto-generated exceptions documentation for
.. docs.support.my_module.MyClass.value
:raises:
* When assigned
* RuntimeError (Argument \`value\` is not valid)
* When retrieved
* RuntimeError (Attribute \`value\` not set)
.. [[[end]]]
"""
Warning
Due to the limited introspection capabilities of class properties, only properties defined using the property built-in function can be documented with pexdoc.exdoc.ExDoc.get_sphinx_autodoc(). Properties defined by other methods can still be auto-documented with pexdoc.exdoc.ExDoc.get_sphinx_doc() and explicitly providing the method/function name.
Context managers¶
Classes¶
exh module¶
This module can be used to register exceptions and then raise them if a given condition is true. For example:
# exh_example.py
from __future__ import print_function
from pexdoc.exh import addex
def my_func(name):
""" Sample function """
# Add exception
exobj = addex(TypeError, 'Argument `name` is not valid')
# Conditionally raise exception
exobj(not isinstance(name, str))
print('My name is {0}'.format(name))
>>> import docs.support.exh_example
>>> docs.support.exh_example.my_func('Tom')
My name is Tom
>>> docs.support.exh_example.my_func(5)
Traceback (most recent call last):
...
TypeError: Argument `name` is not valid
When my_func()
gets called with anything but a string as an argument
a TypeError
exception is raised with the message
'Argument `name ` is not valid'
. While adding an exception with
pexdoc.exh.addex() and conditionally raising it takes the same number
of lines of code as an exception raised inside an if
block (or less
since the raise condition can be evaluated in the same
pexdoc.exh.addex() call) and incurs a slight performance penalty, using
the pexdoc.exh module allows for automatic documentation of the
exceptions raised by any function, method or class property with the help of
the pexdoc.exdoc module.
Functions¶
Classes¶
pcontracts module¶
This module is a thin wrapper around the PyContracts library that enables customization of the exception type raised and limited customization of the exception message. Additionally, custom contracts specified via pexdoc.pcontracts.new_contract() and enforced via pexdoc.pcontracts.contract() register exceptions using the pexdoc.exh module, which means that the exceptions raised by these contracts can be automatically documented using the pexdoc.exdoc module.
The way a contract is specified is identical to the decorator way of specifying
a contract with the PyContracts
library. By default a RuntimeError
exception with the message
'Argument `*[argument_name]* ` is not valid'
is raised unless a
custom contract specifies a different exception (the token
*[argument_name]*
is replaced by the argument name the contract is
attached to). For example, the definitions of the custom contracts
pexdoc.ptypes.file_name() and
pexdoc.ptypes.file_name_exists() are:
@pexdoc.pcontracts.new_contract()
def file_name(obj):
r"""
Validates if an object is a legal name for a file
(i.e. does not have extraneous characters, etc.)
:param obj: Object
:type obj: any
:raises: RuntimeError (Argument \`*[argument_name]*\` is not
valid). The token \*[argument_name]\* is replaced by the name
of the argument the contract is attached to
:rtype: None
"""
msg = pexdoc.pcontracts.get_exdesc()
# Check that argument is a string
if ((not isinstance(obj, str) or
(isinstance(obj, str) and ('\0' in obj)))):
raise ValueError(msg)
# If file exists, argument is a valid file name, otherwise test
# if file can be created. User may not have permission to
# write file, but call to os.access should not fail if the file
# name is correct
try:
if not os.path.exists(obj):
os.access(obj, os.W_OK)
except (TypeError, ValueError): # pragma: no cover
raise ValueError(msg)
@pexdoc.pcontracts.new_contract(
argument_invalid='Argument `*[argument_name]*` is not valid',
file_not_found=(OSError, 'File *[fname]* could not be found')
)
def file_name_exists(obj):
r"""
Validates if an object is a legal name for a file
(i.e. does not have extraneous characters, etc.) *and* that the
file exists
:param obj: Object
:type obj: any
:raises:
* OSError (File *[fname]* could not be found). The
token \*[fname]\* is replaced by the *value* of the
argument the contract is attached to
* RuntimeError (Argument \`*[argument_name]*\` is not valid).
The token \*[argument_name]\* is replaced by the name of
the argument the contract is attached to
:rtype: None
"""
exdesc = pexdoc.pcontracts.get_exdesc()
msg = exdesc['argument_invalid']
# Check that argument is a string
if ((not isinstance(obj, str) or
(isinstance(obj, str) and ('\0' in obj)))):
raise ValueError(msg)
# Check that file name is valid
try:
os.path.exists(obj)
except (TypeError, ValueError): # pragma: no cover
raise ValueError(msg)
# Check that file exists
obj = _normalize_windows_fname(obj)
if not os.path.exists(obj):
msg = exdesc['file_not_found']
raise ValueError(msg)
This is nearly identical to the way custom contracts are defined using the PyContracts library with two exceptions:
- To avoid repetition and errors, the exception messages defined in the pexdoc.pcontracts.new_contract() decorator are available in the contract definition function via pexdoc.pcontracts.get_exdesc().
- A PyContracts new contract can return False or raise a
ValueError
exception to indicate a contract breach, however a new contract specified via the pexdoc.pcontracts.new_contract() decorator has to raise aValueError
exception to indicate a contract breach.
Exceptions can be specified in a variety of ways and verbosity is minimized by having reasonable defaults (see pexdoc.pcontracts.new_contract() for a full description). What follows is a simple usage example of the two contracts shown above and the exceptions they produce:
# pcontracts_example_1.py
from __future__ import print_function
import pexdoc
@pexdoc.pcontracts.contract(name='file_name')
def print_if_fname_valid(name):
""" Sample function 1 """
print('Valid file name: {0}'.format(name))
@pexdoc.pcontracts.contract(num=int, name='file_name_exists')
def print_if_fname_exists(num, name):
""" Sample function 2 """
print('Valid file name: [{0}] {1}'.format(num, name))
>>> import os
>>> from docs.support.pcontracts_example_1 import *
>>> print_if_fname_valid('some_file.txt')
Valid file name: some_file.txt
>>> print_if_fname_valid('invalid_fname.txt\0')
Traceback (most recent call last):
...
RuntimeError: Argument `name` is not valid
>>> fname = os.path.join('..', 'docs', 'pcontracts.rst')
>>> print_if_fname_exists(10, fname)
Valid file name: [10] ...pcontracts.rst
>>> print_if_fname_exists('hello', fname)
Traceback (most recent call last):
...
RuntimeError: Argument `num` is not valid
>>> print_if_fname_exists(5, 'another_invalid_fname.txt\0')
Traceback (most recent call last):
...
RuntimeError: Argument `name` is not valid
>>> print_if_fname_exists(5, '/dev/null/some_file.txt')
Traceback (most recent call last):
...
OSError: File /dev/null/some_file.txt could not be found
Functions¶
Decorators¶
-
pexdoc.pcontracts.
contract
(**contract_args)¶ Wraps PyContracts contract() decorator (only the decorator way of specifying a contract is supported and tested). A
RuntimeError
exception with the message'Argument `*[argument_name]* ` is not valid'
is raised when a contract is breached ('*[argument_name]*'
is replaced by the argument name the contract is attached to) unless the contract is custom and specified with the pexdoc.pcontracts.new_contract() decorator. In this case the exception type and message are controlled by the custom contract specification.
-
pexdoc.pcontracts.
new_contract
(*args, **kwargs)¶ Defines a new (custom) contract with custom exceptions.
Raises: - RuntimeError (Attempt to redefine custom contract `*[contract_name]*`)
- TypeError (Argument `contract_exceptions` is of the wrong type)
- TypeError (Argument `contract_name` is of the wrong type)
- TypeError (Contract exception definition is of the wrong type)
- TypeError (Illegal custom contract exception definition)
- ValueError (Empty custom contract exception message)
- ValueError (Contract exception messages are not unique)
- ValueError (Contract exception names are not unique)
- ValueError (Multiple replacement fields to be substituted by argument value)
The decorator argument(s) is(are) the exception(s) that can be raised by the contract. The most general way to define an exception is using a 2-item tuple with the following members:
- exception type (type) – Either a built-in exception or
sub-classed from Exception. Default is
RuntimeError
- exception message (string) – Default is
'Argument `*[argument_name]*` is not valid'
, where the token*[argument_name]*
is replaced by the argument name the contract is attached to
The order of the tuple elements is not important, i.e. the following are valid exception specifications and define the same exception:
@pexdoc.pcontracts.new_contract(ex1=(RuntimeError, 'Invalid name')) def custom_contract1(arg): if not arg: raise ValueError(pexdoc.pcontracts.get_exdesc()) @pexdoc.pcontracts.new_contract(ex1=('Invalid name', RuntimeError)) def custom_contract2(arg): if not arg: raise ValueError(pexdoc.pcontracts.get_exdesc())
The exception definition simplifies to just one of the exception definition tuple items if the other exception definition tuple item takes its default value. For example, the same exception is defined in these two contracts:
@pexdoc.pcontracts.new_contract(ex1=ValueError) def custom_contract3(arg): if not arg: raise ValueError(pexdoc.pcontracts.get_exdesc()) @pexdoc.pcontracts.new_contract(ex1=( ValueError, 'Argument `*[argument_name]*` is not valid' )) def custom_contract4(arg): if not arg: raise ValueError(pexdoc.pcontracts.get_exdesc())
and these contracts also define the same exception (but different from that of the previous example):
@pexdoc.pcontracts.new_contract(ex1='Invalid name') def custom_contract5(arg): if not arg: raise ValueError(pexdoc.pcontracts.get_exdesc()) @pexdoc.pcontracts.new_contract(ex1=('Invalid name', RuntimeError)) def custom_contract6(arg): if not arg: raise ValueError(pexdoc.pcontracts.get_exdesc())
In fact the exception need not be specified by keyword if the contract only uses one exception. All of the following are valid one-exception contract specifications:
@pexdoc.pcontracts.new_contract( (OSError, 'File could not be opened') ) def custom_contract7(arg): if not arg: raise ValueError(pexdoc.pcontracts.get_exdesc()) @pexdoc.pcontracts.new_contract('Invalid name') def custom_contract8(arg): if not arg: raise ValueError(pexdoc.pcontracts.get_exdesc()) @pexdoc.pcontracts.new_contract(TypeError) def custom_contract9(arg): if not arg: raise ValueError(pexdoc.pcontracts.get_exdesc())
No arguments are needed if a contract only needs a single exception and the default exception type and message suffice:
@pexdoc.pcontracts.new_contract() def custom_contract10(arg): if not arg: raise ValueError(pexdoc.pcontracts.get_exdesc())
For code conciseness and correctness the exception message(s) should be retrieved via the pexdoc.pcontracts.get_exdesc() function.
A PyContracts new contract can return False or raise a
ValueError
exception to indicate a contract breach, however a new contract specified via the pexdoc.pcontracts.new_contract() decorator has to raise aValueError
exception to indicate a contract breach.The exception message can have substitution “tokens” of the form
*[token_name]*
. The token*[argument_name]*
is substituted with the argument name the contract it is attached to. For example:@pexdoc.pcontracts.new_contract(( TypeError, 'Argument `*[argument_name]*` has to be a string' )) def custom_contract11(city): if not isinstance(city, str): raise ValueError(pexdoc.pcontracts.get_exdesc()) @pexdoc.pcontracts.contract(city_name='custom_contract11') def print_city_name(city_name): return 'City: {0}'.format(city_name)
>>> from __future__ import print_function >>> from docs.support.pcontracts_example_3 import print_city_name >>> print(print_city_name('Omaha')) City: Omaha >>> print(print_city_name(5)) Traceback (most recent call last): ... TypeError: Argument `city_name` has to be a string
Any other token is substituted with the argument value. For example:
@pexdoc.pcontracts.new_contract(( OSError, 'File `*[fname]*` not found' )) def custom_contract12(fname): if not os.path.exists(fname): raise ValueError(pexdoc.pcontracts.get_exdesc()) @pexdoc.pcontracts.contract(fname='custom_contract12') def print_fname(fname): print('File name to find: {0}'.format(fname))
>>> from __future__ import print_function >>> import os >>> from docs.support.pcontracts_example_3 import print_fname >>> fname = os.path.join(os.sep, 'dev', 'null', '_not_a_file_') >>> print(print_fname(fname)) Traceback (most recent call last): ... OSError: File `..._not_a_file_` not found
pinspect module¶
This module supplements Python’s introspection capabilities. The class pexdoc.pinspect.Callables “traces” modules and produces a database of callables (functions, classes, methods and class properties) and their attributes (callable type, file name, starting line number). Enclosed functions and classes are supported. For example:
# pinspect_example_1.py
from __future__ import print_function
import math
def my_func(version):
""" Enclosing function """
class MyClass(object):
""" Enclosed class """
if version == 2:
import docs.support.python2_module as pm
else:
import docs.support.python3_module as pm
def __init__(self, value):
self._value = value
def _get_value(self):
return self._value
value = property(_get_value, pm._set_value, None, 'Value property')
def print_name(name):
print('My name is {0}, and sqrt(2) = {1}'.format(name, math.sqrt(2)))
with
# python2_module.py
def _set_value(self, value):
self._value = value+2
and
# python3_module.py
def _set_value(self, value):
self._value = value+3
gives:
>>> from __future__ import print_function
>>> import docs.support.pinspect_example_1, pexdoc.pinspect, sys
>>> cobj = pexdoc.pinspect.Callables(
... [sys.modules['docs.support.pinspect_example_1'].__file__]
... )
>>> print(cobj)
Modules:
docs.support.pinspect_example_1
Classes:
docs.support.pinspect_example_1.my_func.MyClass
docs.support.pinspect_example_1.my_func: func (9-25)
docs.support.pinspect_example_1.my_func.MyClass: class (11-25)
docs.support.pinspect_example_1.my_func.MyClass.__init__: meth (18-20)
docs.support.pinspect_example_1.my_func.MyClass._get_value: meth (21-23)
docs.support.pinspect_example_1.my_func.MyClass.value: prop (24-25)
docs.support.pinspect_example_1.print_name: func (26-27)
The numbers in parenthesis indicate the line number in which the callable starts and ends within the file it is defined in.
Functions¶
Classes¶
ptypes module¶
This module provides several pseudo-type definitions which can be enforced and/or validated with custom contracts defined using the pexdoc.pcontracts module
Pseudo-types¶
FileName¶
String with a valid file name
FileNameExists¶
String with a file name that exists in the file system
Function¶
Callable pointer or None
NonNegativeInteger¶
Integer greater or equal to zero
NonNullString¶
String of length 1 or higher
OffsetRange¶
Number in the [0, 1] range
PositiveRealNum¶
Integer or float greater than zero or None
RealNum¶
Integer, float or None