Belle II Software light-2406-ragdoll
basf2domain.py
1
8
9from sphinx import addnodes
10from sphinx.roles import XRefRole
11from sphinx.directives import ObjectDescription
12from sphinx.domains import Domain, ObjType, Index
13from sphinx.util.docfields import TypedField, Field
14from sphinx.util.nodes import make_refnode
15from sphinx.util import logging
16logger = logging.getLogger(__name__)
17
18
19class Basf2Object(ObjectDescription):
20 """
21 A basf2 x-ref directive registered with Sphinx.add_object_type().
22 Basically copied together from sphinxcontrib-domaintools and sphinxcontrib-adadomain.
23 """
24
25## \cond Doxygen_suppress
26
27 doc_field_types = {
28 TypedField("parameter", label="Parameters", names=("param", "parameter", "arg", "argument"),
29 typenames=("paramtype", "type"), typerolename=None, can_collapse=True),
30 Field('returnvalue', label='Returns', has_arg=False,
31 names=('returns', 'return')),
32 }
33
34 def handle_signature(self, sig, signode):
35 signode.clear()
36
37 # check if we have arguments
38 try:
39 name, args = sig.split("(", 1)
40 except ValueError:
41 name = sig
42 args = None
43
44 # Add the name of the signature
45 name = name.strip()
46 signode += addnodes.desc_name(name, name)
47
48 # if there was a opening parenthesis add all the arguments to the
49 # parameter list, separate them by comma
50 if args:
51 paramlist = addnodes.desc_parameterlist()
52 for arg in (e.strip() for e in args[:-1].split(",")):
53 if not arg:
54 continue
55 paramlist += addnodes.desc_parameter(arg, arg)
56 signode += paramlist
57
58 # return the object name for referencing
59 return name
60
61
62
63 def add_target_and_index(self, name, sig, signode):
64 # Create a full id from objtype and name
65 targetname = f'{self.objtype}-{name}'
66 # and append the id to the object node
67 signode['ids'].append(targetname)
68 self.state.document.note_explicit_target(signode)
69
70 # remember the name -> (document, targetname) mapping in the domain data
71 # dictionary so that we can use it to resolve x-refs
72 ddata = self.env.domaindata["b2"][self.objtype + 's']
73
74 if name in ddata:
75 # already exists, give warning
76 logger.warn(f"Duplicate description of basf2 {self.objtype} {name}, " +
77 "Other instance in " + self.env.doc2path(ddata[name][0]) +
78 ", please add ':noindex:' to one",
79 location=(self.env.docname, self.lineno))
80 else:
81 ddata[name] = (self.env.docname, targetname)
82
83
84class Basf2ModuleIndex(Index):
85 """Create an alphabetic index of all modules"""
86
87 name = "modindex"
88 localname = "basf2 Module Index"
89 shortname = "basf2 modules"
90
91 def generate(self, docnames=None):
92 content = {}
93 modules = self.domain.data["modules"].items()
94 for modname, (docname, target) in sorted(modules):
95 letter = modname[0].upper()
96 content.setdefault(letter, [])
97 content[letter].append([modname, 0, docname, target, "", "", ""])
98 return list(content.items()), False
99
100
101
103 """Create an alphabetic index of all variables"""
104
105 name = "varindex"
106 localname = "basf2 Variable Index"
107 shortname = "basf2 variables"
108
109 def generate(self, docnames=None):
110 content = {}
111 modules = self.domain.data["variables"].items()
112 modules = sorted(modules, key=lambda x: x[0].lower())
113 for modname, (docname, target) in modules:
114 letter = modname[0].upper()
115 content.setdefault(letter, [])
116 content[letter].append([modname, 0, docname, target, "", "", ""])
117 return list(content.items()), False
118
119
120
121class Basf2Domain(Domain):
122 """basf2 Software Domain"""
123
124 name = "b2"
125 label = "Belle II Software"
126 object_types = {
127 "module": ObjType("module", "mod"),
128 "variable": ObjType("variable", "var")
129 }
130
131 directives = {
132 "module": Basf2Object,
133 "variable": Basf2Object,
134 }
135 roles = {
136 "mod": XRefRole(),
137 "var": XRefRole(),
138 }
139 initial_data = {
140 "modules": {},
141 "variables": {},
142 }
143 indices = [
144 Basf2ModuleIndex,
145 Basf2VariableIndex,
146 ]
147
148
149 def clear_doc(self, docname):
150 """Remove the existing domain data for a given document name"""
151 for t in "modules", "variables":
152 try:
153 for name, (doc, target) in list(self.data[t].items()):
154 if doc == docname:
155 del self.data[t][name]
156 except Exception:
157 pass
158
159
160 def get_objects(self):
161 for i, type in enumerate(["modules", "variables"]):
162 for name, (docname, target) in self.data[type].items():
163 yield(name, name, type, docname, target, i)
164
165 def get_type_name(self, type, primary=False):
166 # never prepend "Default"
167 return type.lname
168
169 def resolve_xref(self, env, fromdocname, builder,
170 typ, target, node, contnode):
171 t = {"mod": "modules", "var": "variables"}[typ]
172 try:
173 docname, labelid = self.data[t][target]
174 return make_refnode(builder, fromdocname, docname,
175 labelid, contnode)
176 except Exception:
177 return None
178
def clear_doc(self, docname)
Definition: basf2domain.py:149