omniidl_be_python_with_docstring.py
Go to the documentation of this file.
1 # -*- python -*-
2 # Copyright (C) 2008-2018 LAAS-CNRS, JRL AIST-CNRS.
3 # Author: Joseph Mirabel
4 #
5 # Redistribution and use in source and binary forms, with or without modification,
6 # are permitted provided that the following conditions are met:
7 #
8 # 1. Redistributions of source code must retain the above copyright notice,
9 # this list of conditions and the following disclaimer.
10 #
11 # 2. Redistributions in binary form must reproduce the above copyright notice,
12 # this list of conditions and the following disclaimer in the documentation
13 # and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
26 
27 from omniidl_be.python import comment, output_inline
28 from omniidl_be.python import run as run_parent
29 from omniidl import idlvisitor, idlast, idltype
30 
31 
32 def _rreplace(s, old, new, occurrence):
33  li = s.rsplit(old, occurrence)
34  return new.join(li)
35 
36 
37 class CommentToConstVisitor(idlvisitor.AstVisitor):
38  def __init__(self):
39  import re
40 
41  self.commentStart = re.compile(r"^//+ ?")
42 
43  def _commentToConst(self, node, comments):
44  import re
45 
46  texts = []
47  line = node.line()
48  # TODO here, we only use the comments before.
49  comment = node
50  for c in reversed(comments):
51  if line - c.line() != 1:
52  break
53  line = c.line()
54  comment = c
55  text = c.text()
56  texts.append(re.sub(self.commentStart, "", text))
57  if len(texts) == 0:
58  if isinstance(node, idlast.Attribute):
59  for i in node.identifiers():
60  print(i + " documentation may be ill-formed.")
61  else:
62  print(node.identifier() + " documentation may be ill-formed.")
63  texts.reverse()
64  # Extract the prototype of the function
65  try:
66  if isinstance(node, idlast.Operation):
67  with open(node.file()) as fp:
68  for _ in range(node.line() - 1):
69  fp.readline()
70  line = fp.readline()
71  texts.append("\nPrototype:\t" + line)
72  while line != "":
73  if ";" in line:
74  break
75  line = fp.readline()
76  texts.append(" \t" + line)
77  except Exception:
78  pass
79  if len(texts) == 0:
80  return None
81  text = "".join(texts)
82  id = node.identifier() + "__doc__"
83  return idlast.Const(
84  comment.file(),
85  comment.line(),
86  node.mainFile(),
87  node.pragmas(),
88  [], # comments
89  id, # identifier
90  node.scopedName()[:-1]
91  + [
92  id,
93  ], # scopedName
94  _rreplace(node.repoId(), node.identifier(), id, 1), # repoId
95  idltype.String(0), # constType, 0 means unbounded
96  idltype.tk_string, # constKind
97  text, # value
98  )
99 
100  def _addDoc(self, parent, node):
101  # Commented in order to add the prototype to the documentation.
102  # if len(node.comments()) > 0:
103  if True:
104  const = self._commentToConst(node, node.comments())
105  if const is None:
106  return
107  if isinstance(parent, idlast.Module):
108  parent._Module__definitions.append(const)
109  elif isinstance(parent, idlast.Interface):
110  parent._Interface__declarations.append(const)
111  parent._Interface__contents.append(const)
112  elif isinstance(parent, idlast.AST):
113  parent._AST__declarations.append(const)
114  else:
115  print("Doc ignored: " + comment.text())
116 
117  def visitAST(self, node):
118  for n in node.declarations():
119  if not output_inline and not n.mainFile():
120  continue
121 
122  if isinstance(n, idlast.Module) or isinstance(n, idlast.Interface):
123  self._addDoc(node, n)
124  n.accept(self)
125 
126  def visitModule(self, node):
127  for n in node.definitions():
128  if not output_inline and not n.mainFile():
129  continue
130 
131  if isinstance(n, idlast.Module) or isinstance(n, idlast.Interface):
132  self._addDoc(node, n)
133  n.accept(self)
134 
135  def visitInterface(self, node):
136  for c in node.callables():
137  self._addDoc(node, c)
138 
139 
140 def run(tree, args):
141  ccv = CommentToConstVisitor()
142  tree.accept(ccv)
143 
144  run_parent(tree, args)
def _rreplace(s, old, new, occurrence)


dynamic-graph-python
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:55:50