|
| 1 | +# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 2 | +# |
| 3 | +# This program is free software: you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License as published by |
| 5 | +# the Free Software Foundation, either version 3 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# |
| 8 | +# This program is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License |
| 14 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | +"""Autodoc extension for configurable traits. |
| 16 | +This code auto documents traits from Cylc UI Server: |
| 17 | +Acknowledgment: |
| 18 | +Code derived from the Jupyter Hub source (BSD). |
| 19 | +
|
| 20 | +https://github.com/jupyterhub/autodoc-traits/ |
| 21 | + autodoc_traits.py |
| 22 | +
|
| 23 | +BSD 3-Clause License |
| 24 | +
|
| 25 | +Copyright (c) Project Jupyter Contributors |
| 26 | +All rights reserved. |
| 27 | +
|
| 28 | +Redistribution and use in source and binary forms, with or without |
| 29 | +modification, are permitted provided that the following conditions are met: |
| 30 | +
|
| 31 | +* Redistributions of source code must retain the above copyright notice, this |
| 32 | + list of conditions and the following disclaimer. |
| 33 | +
|
| 34 | +* Redistributions in binary form must reproduce the above copyright notice, |
| 35 | + this list of conditions and the following disclaimer in the documentation |
| 36 | + and/or other materials provided with the distribution. |
| 37 | +
|
| 38 | +* Neither the name of the copyright holder nor the names of its |
| 39 | + contributors may be used to endorse or promote products derived from |
| 40 | + this software without specific prior written permission. |
| 41 | +
|
| 42 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 43 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 44 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 45 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 46 | +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 47 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 48 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 49 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 50 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 51 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 52 | +""" |
| 53 | +from sphinx.ext.autodoc import AttributeDocumenter |
| 54 | +from sphinx.ext.autodoc import ClassDocumenter |
| 55 | +from traitlets import TraitType |
| 56 | +from traitlets import Undefined |
| 57 | + |
| 58 | + |
| 59 | +class ConfigurableDocumenter(ClassDocumenter): |
| 60 | + """Specialized Documenter subclass for traits with config=True""" |
| 61 | + |
| 62 | + objtype = "configurable" |
| 63 | + directivetype = "class" |
| 64 | + |
| 65 | + def get_object_members(self, want_all): |
| 66 | + """Add traits with .tag(config=True) to members list""" |
| 67 | + check, members = super().get_object_members(want_all) |
| 68 | + get_traits = ( |
| 69 | + self.object.class_own_traits |
| 70 | + if self.options.inherited_members |
| 71 | + else self.object.class_traits |
| 72 | + ) |
| 73 | + trait_members = [] |
| 74 | + for name, trait in sorted(get_traits(config=True).items()): |
| 75 | + # put help in __doc__ where autodoc will look for it |
| 76 | + trait.__doc__ = trait.help |
| 77 | + trait_members.append((name, trait)) |
| 78 | + return check, trait_members |
| 79 | + |
| 80 | + |
| 81 | +class TraitDocumenter(AttributeDocumenter): |
| 82 | + objtype = "trait" |
| 83 | + directivetype = "attribute" |
| 84 | + member_order = 1 |
| 85 | + priority = 100 |
| 86 | + |
| 87 | + @classmethod |
| 88 | + def can_document_member(cls, member, membername, isattr, parent): |
| 89 | + return isinstance(member, TraitType) |
| 90 | + |
| 91 | + def add_directive_header(self, sig): |
| 92 | + default = self.object.get_default_value() |
| 93 | + if default is Undefined: |
| 94 | + default_s = "" |
| 95 | + else: |
| 96 | + default_s = repr(default) |
| 97 | + self.options.annotation = "c.{name} = {trait}({default})".format( |
| 98 | + name=self.format_name(), |
| 99 | + trait=self.object.__class__.__name__, |
| 100 | + default=default_s, |
| 101 | + ) |
| 102 | + super().add_directive_header(sig) |
| 103 | + |
| 104 | + |
| 105 | +def setup(app): |
| 106 | + app.add_autodocumenter(ConfigurableDocumenter) |
| 107 | + app.add_autodocumenter(TraitDocumenter) |
0 commit comments