build-rapi-resources-doc 2.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/python
#

# Copyright (C) 2008 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

"""Script to generate documentation for remote API resources.

23
24
25
This is hard-coded to the section numbering we have in the master RST
document.

26
27
28
29
30
31
"""

import re
import cgi
import inspect

32
from ganeti.rapi import rlib2
Iustin Pop's avatar
Iustin Pop committed
33
from ganeti.rapi import connector
34
35
36
37
38


CHECKED_COMMANDS = ["GET", "POST", "PUT", "DELETE"]


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def beautify(text):
  """A couple of small enhancements, epydoc-to-rst.

  """
  pairs = [
    ("@return:", "Returns:"),
    ]

  for old, new in pairs:
    text = text.replace(old, new)

  return text


def indent(text):
  """Returns a text block with all lines indented.

  """
  lines = text.splitlines()
  lines = ["  " + l for l in lines]
  return "\n".join(lines)


62
63
def main():
  # Get list of all resources
64
  all = list(connector.CONNECTOR.itervalues())
65

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
66
  # Sort rlib by URI
67
68
  all.sort(cmp=lambda a, b: cmp(a.DOC_URI, b.DOC_URI))

69
  print ".. Automatically generated, do not edit\n"
70
71

  for cls in all:
72
73
    title = cls.DOC_URI
    print "%s\n%s\n" % (title, "+" * len(title))
74
75
76
77

    # Class docstring
    description = inspect.getdoc(cls)
    if description:
78
79
80
81
82
83
      print "::\n"
      print indent(description.strip())
    print
    supported = [cmd for cmd in CHECKED_COMMANDS if hasattr(cls, cmd)]
    print "It supports the following commands: %s." % (", ".join(supported))
    print
84
85
86
87
88

    for cmd in CHECKED_COMMANDS:
      if not hasattr(cls, cmd):
        continue

89
90
      print "%s\n%s\n" % (cmd, "~" * len(cmd))

91
92
      # Get docstring
      text = inspect.getdoc(getattr(cls, cmd))
93
94
95
96
97
      if text:
        text = beautify(text)
        print "::\n"
        print indent(text)
        print
98
99
100
101


if __name__ == "__main__":
  main()