API¶
Sphinx-Needs
provides an open API for other sphinx-extensions to provide specific need-types, create needs or
make usage of the filter possibilities.
The API is designed to allow the injection of extra configuration. The overall manipulation (e.g remove need types) is not supported to keep the final configuration transparent for the Sphinx project authors.
For some implementation ideas, take a look into the Sphinx extension Sphinx-Test-Reports and its source code.
Configuration¶
API to get or add specific sphinx needs configuration parameters.
All functions here are available under sphinxcontrib.api
. So do not import this module directly.
- add_dynamic_function(app, function, name=None)¶
Registers a new dynamic function for sphinx-needs.
If
name
is not given, the name to call the function is automatically taken from the provided function. The used name must be unique.Usage:
from sphinxcontrib.needs.api import add_dynamic_function def my_function(app, need, needs, *args, **kwargs): # Do magic here return "some data" add_dynamic_function(app, my_function)
Read Dynamic functions for details about how to use dynamic functions.
- Parameters
app – Sphinx application object
function – Function to register
name – Name of the dynamic function as string
- Returns
None
- add_extra_option(app: sphinx.application.Sphinx, name: str)¶
Adds an extra option to the configuration. This option can then later be used inside needs or
add_need
.Same impact as using needs_extra_options manually.
Usage:
from sphinxcontrib.needs.api import add_extra_option add_extra_option(app, 'my_extra_option')
- Parameters
app – Sphinx application object
name – Name as string of the extra option
- Returns
None
- add_need_type(app: sphinx.application.Sphinx, directive: str, title: str, prefix, color='#ffffff', style='node')¶
Adds a new need_type to the configuration.
The given directive must no exist, otherwise NeedsApiConfigException gets raised.
Same impact as using needs_types manually.
Usage:
from sphinxcontrib.needs.api import add_need_type add_need_type(app, 'awesome', 'Awesome', 'AW_', '#000000', 'cloud')
- Parameters
app – Sphinx application object
directive – Name of the directive, e.g. ‘story’
title – Long, human-readable title, e.g. ‘User-Story’
prefix – Prefix, if IDs get automatically generated. E.g.:
'US_'
color – Hex-color code used in needflow representation. Default:
'#ffffff'
style – Plantuml-style for needflow representation. Default: ‘node’
- Returns
None
- add_warning(app, name, function=None, filter_string=None)¶
Registers a warning.
A warning can be based on the result of a given filter_string or an own defined function.
- Parameters
app – Sphinx app object
name – Name as string for the warning
function – function to execute to check the warning
filter_string – filter_string to use for the warning
- Returns
None
- get_need_types(app: sphinx.application.Sphinx)¶
Returns a list of directive-names from all configured need_types.
Usage:
from sphinxcontrib.needs.api import get_need_types all_types = get_need_types(app)
- Parameters
app – Sphinx application object
- Returns
list of strings
Need¶
- add_external_need(app, need_type, title, id=None, external_url=None, external_css='external_link', content='', status=None, tags=None, links_string=None, **kwargs)¶
Adds an external need from an external source. This need does not have any representation in the current documentation project. However, it can be linked and filtered. It’s reference will open a link to another, external sphinx documentation project.
It return an empty list (without any nodes), so no nodes will be added to the document.
- Parameters
app – Sphinx application object.
need_type – Name of the need type to create.
title – String as title.
id – ID as string. If not given, a id will get generated.
external_url – URL as string, which shall be used as link to the original need source
content – Content as single string.
status – Status as string.
tags – Tags as single string.
links_string – Links as single string.
external_css – CSS class name as string, which is set for the <a> tag.
kwargs –
- Returns
Empty list
- add_need(app: sphinx.application.Sphinx, state, docname: str, lineno: int, need_type, title: str, id: Optional[str] = None, content: str = '', status: Optional[str] = None, tags=None, links_string: Optional[str] = None, hide=False, hide_tags=False, hide_status=False, collapse=None, style=None, layout=None, template=None, pre_template=None, post_template=None, is_external=False, external_url=None, external_css='external_link', **kwargs)¶
Creates a new need and returns its node.
add_need
allows to create needs programmatically and use its returned node to be integrated in any docutils based structure.kwags
can contain options defined inneeds_extra_options
andneeds_extra_links
. If an entry is found inkwags
, which is not specified in the configuration or registered e.g. viaadd_extra_option
, an exception is raised.If
is_external
is set toTrue
, no node will be created. Instead the need is referencing an external url. Used mostly for needs_external_needs to integrate and reference needs from external documentation.Usage:
Normally needs get created during handling of a specialised directive. So this pseudo-code shows how to use
add_need
inside such a directive.from docutils.parsers.rst import Directive from sphinxcontrib.needs.api import add_need class MyDirective(Directive) # configs and init routine def run(): main_section = [] docname = self.state.document.settings.env.docname # All needed sphinx-internal information we can take from our current directive class. # e..g app, state, lineno main_section += add_need(self.env.app, self.state, docname, self.lineno, need_type="req", title="my title", id="ID_001" content=self.content) # Feel free to add custom stuff to main_section like sections, text, ... return main_section
- Parameters
app – Sphinx application object.
state – Current state object.
docname – documentation name.
lineno – line number.
need_type – Name of the need type to create.
title – String as title.
id – ID as string. If not given, a id will get generated.
content – Content as single string.
status – Status as string.
tags – Tags as single string.
links_string – Links as single string.
hide – boolean value.
hide_tags – boolean value. (Not used with Sphinx-Needs >0.5.0)
hide_status – boolean value. (Not used with Sphinx-Needs >0.5.0)
collapse – boolean value.
style – String value of class attribute of node.
layout – String value of layout definition to use
template – Template name to use for the content of this need
pre_template – Template name to use for content added before need
post_template – Template name to use for the content added after need
is_external – Is true, no node is created and need is referencing external url
external_url – URL as string, which is used as target if
is_external
isTrue
external_css – CSS class name as string, which is set for the <a> tag.
- Returns
node
- del_need(app, id)¶
Deletes an existing need.
- Parameters
app – Sphinx application object.
id – Sphinx need id.
- make_hashed_id(app: sphinx.application.Sphinx, need_type: str, full_title, content, id_length=None)¶
Creates an ID based on title or need.
Also cares about the correct prefix, which is specified for each need type.
- Parameters
app – Sphinx application object
need_type – name of the need directive, e.g. req
full_title – full title of the need
content – content of the need
id_length – maximum length of the generated ID
- Returns
ID as string
Exceptions¶
- exception NeedsApiConfigException¶
A configuration changes collides with the already provided configuration by the user.
Example: An extension wants to add an already existing needs_type.
- exception NeedsApiConfigWarning¶
- exception NeedsDuplicatedId¶
- exception NeedsInvalidException¶
- exception NeedsInvalidFilter¶
- exception NeedsInvalidOption¶
- exception NeedsNoIdException¶
- exception NeedsNotLoadedException¶
Sphinx-Needs is not loaded. Therefore configuration parameters and functions are missing.
Make sure
sphinxcontrib.needs
is added to the``extension`` parameter ofconf.py
-
- exception NeedsStatusNotAllowed¶
- exception NeedsTagNotAllowed¶
- exception NeedsTemplateException¶