From 3263c3848f3b1d34a4dbb68dc2d4f8497a1da14d Mon Sep 17 00:00:00 2001 From: Chris Lenk Date: Tue, 20 Feb 2018 14:48:53 -0500 Subject: [PATCH] List STIX objects' properties in documentation --- docs/conf.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/docs/conf.py b/docs/conf.py index c337e79..50aef1d 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,6 +1,11 @@ import os import sys +from six import class_types +from sphinx.ext.autodoc import ClassDocumenter + +from stix2.base import _STIXBase + sys.path.insert(0, os.path.abspath('..')) extensions = [ @@ -50,3 +55,32 @@ latex_elements = {} latex_documents = [ (master_doc, 'stix2.tex', 'stix2 Documentation', 'OASIS', 'manual'), ] + +class STIXAttributeDocumenter(ClassDocumenter): + """Custom Sphinx extension to auto-document STIX properties. + + Needed because descendants of _STIXBase use `_properties` dictionaries + instead of instance variables for STIX 2 objects' properties. + + """ + objtype = "stixattr" + directivetype = "class" + priority = 999 + + @classmethod + def can_document_member(cls, member, membername, isattr, parent): + return isinstance(member, class_types) and \ + issubclass(member, _STIXBase) and \ + hasattr(member, '_properties') + + def add_content(self, more_content, no_docstring=False): + ClassDocumenter.add_content(self, more_content, no_docstring) + + obj = self.object + self.add_line(":Properties:", "") + for prop in obj._properties: + self.add_line(" - %s" % prop, "") + self.add_line("", "") + +def setup(app): + app.add_autodocumenter(STIXAttributeDocumenter)