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 *
28 from omniidl_be.python import run as run_parent
29 from omniidl import idlvisitor, idlast, idltype
30 
31 def _rreplace(s, old, new, occurrence):
32  li = s.rsplit(old, occurrence)
33  return new.join(li)
34 
35 class CommentToConstVisitor (idlvisitor.AstVisitor):
36  def __init__(self):
37  import re
38  self.commentStart = re.compile (r"^//+ ?")
39 
40  def _commentToConst (self, node, comments):
41  import re
42  texts = []
43  line = node.line()
44  # TODO here, we only use the comments before.
45  comment = node
46  for c in reversed(comments):
47  if line - c.line() != 1:
48  break
49  line = c.line()
50  comment = c
51  text = c.text()
52  texts.append (re.sub(self.commentStart, "", text))
53  if len(texts)==0:
54  if isinstance(node, idlast.Attribute):
55  for i in node.identifiers():
56  print (i + " documentation may be ill-formed.")
57  else:
58  print (node.identifier() + " documentation may be ill-formed.")
59  texts.reverse()
60  # Extract the prototype of the function
61  try:
62  if isinstance(node, idlast.Operation):
63  with open(node.file()) as fp:
64  for _ in range(node.line()-1): fp.readline()
65  line = fp.readline()
66  texts.append("\nPrototype:\t"+ line)
67  while line != "":
68  if ';' in line:
69  break
70  line = fp.readline()
71  texts.append(" \t"+ line)
72  except:
73  pass
74  if len(texts)==0: return None
75  text = ''.join (texts)
76  id = node.identifier() + "__doc__"
77  return idlast.Const (
78  comment.file(),
79  comment.line(),
80  node.mainFile(),
81  node.pragmas(),
82  [], # comments
83  id, #identifier
84  node.scopedName()[:-1] + [ id, ], #scopedName
85  _rreplace (node.repoId(), node.identifier(), id, 1), # repoId
86  idltype.String (0), # constType, 0 means unbounded
87  idltype.tk_string, # constKind
88  text # value
89  )
90  def _addDoc (self, parent, node):
91  #Commented in order to add the prototype to the documentation.
92  #if len(node.comments()) > 0:
93  if True:
94  const = self._commentToConst (node, node.comments())
95  if const is None: return
96  if isinstance(parent, idlast.Module):
97  parent._Module__definitions.append(const)
98  elif isinstance(parent, idlast.Interface):
99  parent._Interface__declarations.append(const)
100  parent._Interface__contents.append(const)
101  elif isinstance(parent, idlast.AST):
102  parent._AST__declarations.append(const)
103  else:
104  print ("Doc ignored: " + comment.text())
105 
106 
107  def visitAST(self, node):
108  for n in node.declarations():
109  if not output_inline and not n.mainFile(): continue
110 
111  if isinstance(n, idlast.Module) or isinstance(n, idlast.Interface):
112  self._addDoc (node, n)
113  n.accept(self)
114 
115  def visitModule(self, node):
116  for n in node.definitions():
117  if not output_inline and not n.mainFile(): continue
118 
119  if isinstance(n, idlast.Module) or isinstance(n, idlast.Interface):
120  self._addDoc (node, n)
121  n.accept(self)
122 
123  def visitInterface(self, node):
124  for c in node.callables():
125  self._addDoc (node, c)
126 
127 def run(tree, args):
128  ccv = CommentToConstVisitor ()
129  tree.accept(ccv)
130 
131  run_parent (tree, args)


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Apr 17 2021 02:37:59