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