Robot Framework Integrated Development Environment (RIDE)
libdoc.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright 2008-2015 Nokia Networks
4 # Copyright 2016- Robot Framework Foundation
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 
18 
33 
34 import sys
35 import os
36 
37 # Allows running as a script. __name__ check needed with multiprocessing:
38 # https://github.com/robotframework/robotframework/issues/1137
39 if 'robot' not in sys.modules and __name__ == '__main__':
40  import pythonpathsetter
41 
42 from robotide.lib.robot.utils import Application, seq2str
43 from robotide.lib.robot.errors import DataError
44 from robotide.lib.robot.libdocpkg import LibraryDocumentation, ConsoleViewer
45 
46 
47 USAGE = """robot.libdoc -- Robot Framework library documentation generator
48 
49 Version: <VERSION>
50 
51 Usage: python -m robot.libdoc [options] library output_file
52  or: python -m robot.libdoc [options] library list|show|version [names]
53 
54 Libdoc tool can generate keyword documentation in HTML and XML formats both
55 for test libraries and resource files. HTML format is suitable for humans and
56 XML specs for RIDE and other tools. Libdoc also has few special commands to
57 show library or resource information on the console.
58 
59 Libdoc supports all library and resource types and also earlier generated XML
60 specs can be used as input. If a library needs arguments, they must be given
61 as part of the library name and separated by two colons, for example, like
62 `LibraryName::arg1::arg2`.
63 
64 Options
65 =======
66 
67  -f --format HTML|XML Specifies whether to generate HTML or XML output.
68  If this options is not used, the format is got
69  from the extension of the output file.
70  -F --docformat ROBOT|HTML|TEXT|REST
71  Specifies the source documentation format. Possible
72  values are Robot Framework's documentation format,
73  HTML, plain text, and reStructuredText. The default
74  value can be specified in test library source code
75  and the initial default value is `ROBOT`.
76  -n --name newname Sets the name of the documented library or resource.
77  -v --version newversion Sets the version of the documented library or
78  resource.
79  -P --pythonpath path * Additional locations where to search for libraries
80  and resources.
81  -E --escape what:with * Deprecated. Use console escape mechanism instead.
82  -h -? --help Print this help.
83 
84 Creating documentation
85 ======================
86 
87 When creating documentation in HTML or XML format, the output file must
88 be specified as a second argument after the library/resource name or path.
89 Output format is got automatically from the extension but can also be set
90 with `--format` option.
91 
92 Examples:
93 
94  python -m robot.libdoc src/MyLib.py doc/MyLib.html
95  jython -m robot.libdoc MyJavaLibrary.java MyJavaLibrary.html
96  python -m robot.libdoc --name MyLib Remote::10.0.0.42:8270 MyLib.xml
97 
98 Viewing information on console
99 ==============================
100 
101 Libdoc has three special commands to show information on the console. These
102 commands are used instead of the name of the output file, and they can also
103 take additional arguments.
104 
105 list: List names of the keywords the library/resource contains. Can be
106  limited to show only certain keywords by passing optional patterns as
107  arguments. Keyword is listed if its name contains any given pattern.
108 show: Show library/resource documentation. Can be limited to show only
109  certain keywords by passing names as arguments. Keyword is shown if
110  its name matches any given name. Special argument `intro` will show
111  the library introduction and importing sections.
112 version: Show library version
113 
114 Optional patterns given to `list` and `show` are case and space insensitive.
115 Both also accept `*` and `?` as wildcards.
116 
117 Examples:
118 
119  python -m robot.libdoc Dialogs list
120  python -m robot.libdoc Selenium2Library list browser
121  python -m robot.libdoc Remote::10.0.0.42:8270 show
122  python -m robot.libdoc Dialogs show PauseExecution execute*
123  python -m robot.libdoc Selenium2Library show intro
124  python -m robot.libdoc Selenium2Library version
125 
126 Alternative execution
127 =====================
128 
129 Libdoc works with all interpreters supported by Robot Framework (Python,
130 Jython and IronPython). In the examples above Libdoc is executed as an
131 installed module, but it can also be executed as a script like
132 `python path/robot/libdoc.py`.
133 
134 For more information about Libdoc and other built-in tools, see
135 http://robotframework.org/robotframework/#built-in-tools.
136 """
137 
138 
139 class LibDoc(Application):
140 
141  def __init__(self):
142  Application.__init__(self, USAGE, arg_limits=(2,), auto_version=False)
143 
144  def validate(self, options, arguments):
145  if ConsoleViewer.handles(arguments[1]):
146  ConsoleViewer.validate_command(arguments[1], arguments[2:])
147  elif len(arguments) > 2:
148  raise DataError('Only two arguments allowed when writing output.')
149  return options, arguments
150 
151  def main(self, args, name='', version='', format=None, docformat=None):
152  lib_or_res, output = args[:2]
153  libdoc = LibraryDocumentation(lib_or_res, name, version,
154  self._get_doc_format_get_doc_format(docformat))
155  if ConsoleViewer.handles(output):
156  ConsoleViewer(libdoc).view(output, *args[2:])
157  else:
158  libdoc.save(output, self._get_output_format_get_output_format(format, output))
159  self.console(os.path.abspath(output))
160 
161  def _get_doc_format(self, format):
162  if not format:
163  return None
164  return self._verify_format_verify_format('Doc format', format,
165  ['ROBOT', 'TEXT', 'HTML', 'REST'])
166 
167  def _get_output_format(self, format, output):
168  default = os.path.splitext(output)[1][1:]
169  return self._verify_format_verify_format('Format', format or default, ['HTML', 'XML'])
170 
171  def _verify_format(self, type, format, valid):
172  format = format.upper()
173  if format not in valid:
174  raise DataError("%s must be %s, got '%s'."
175  % (type, seq2str(valid, lastsep=' or '), format))
176  return format
177 
178 
179 
193 def libdoc_cli(arguments):
194  LibDoc().execute_cli(arguments)
195 
196 
197 
222 def libdoc(library_or_resource, outfile, name='', version='', format=None,
223  docformat=None):
224  LibDoc().execute(library_or_resource, outfile, name=name, version=version,
225  format=format, docformat=docformat)
226 
227 
228 if __name__ == '__main__':
229  libdoc_cli(sys.argv[1:])
Used when variable does not exist.
Definition: errors.py:67
def _get_output_format(self, format, output)
Definition: libdoc.py:167
def _get_doc_format(self, format)
Definition: libdoc.py:161
def _verify_format(self, type, format, valid)
Definition: libdoc.py:171
def main(self, args, name='', version='', format=None, docformat=None)
Definition: libdoc.py:151
def validate(self, options, arguments)
Definition: libdoc.py:144
def libdoc(library_or_resource, outfile, name='', version='', format=None, docformat=None)
Executes Libdoc.
Definition: libdoc.py:223
def libdoc_cli(arguments)
Executes Libdoc similarly as from the command line.
Definition: libdoc.py:193
def LibraryDocumentation(library_or_resource, name=None, version=None, doc_format=None)
Definition: __init__.py:32
def seq2str(sequence, quote="'", sep=', ', lastsep=' and ')
Returns sequence in format ‘'item 1’, 'item 2' and 'item 3'`.
Definition: misc.py:115