Robot Framework
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 
35 
36 import sys
37 import os
38 
39 # Allows running as a script. __name__ check needed with multiprocessing:
40 # https://github.com/robotframework/robotframework/issues/1137
41 if 'robot' not in sys.modules and __name__ == '__main__':
42  import pythonpathsetter
43 
44 from robot.utils import Application, seq2str
45 from robot.errors import DataError
46 from robot.libdocpkg import LibraryDocumentation, ConsoleViewer
47 
48 
49 USAGE = """Libdoc -- Robot Framework library documentation generator
50 
51 Version: <VERSION>
52 
53 Usage: libdoc [options] library_or_resource output_file
54  or: libdoc [options] library_or_resource list|show|version [names]
55 
56 Libdoc can generate documentation for Robot Framework libraries and resource
57 files. It can generate HTML documentation for humans as well as machine
58 readable spec files in XML and JSON formats. Libdoc also has few special
59 commands to show library or resource information on the console.
60 
61 Libdoc supports all library and resource types and also earlier generated XML
62 and JSON specs can be used as input. If a library needs arguments, they must be
63 given as part of the library name and separated by two colons, for example,
64 like `LibraryName::arg1::arg2`.
65 
66 The easiest way to run Libdoc is using the `libdoc` command created as part of
67 the normal installation. Alternatively it is possible to execute the
68 `robot.libdoc` module directly like `python -m robot.libdoc`, where `python`
69 can be replaced with any supported Python interpreter. Yet another alternative
70 is running the module as a script like `python path/to/robot/libdoc.py`.
71 
72 The separate `libdoc` command and the support for JSON spec files are new in
73 Robot Framework 4.0.
74 
75 Options
76 =======
77 
78  -f --format HTML|XML|JSON|LIBSPEC
79  Specifies whether to generate an HTML output for
80  humans or a machine readable spec file in XML or JSON
81  format. The LIBSPEC format means XML spec with
82  documentations converted to HTML. The default format
83  is got from the output file extension.
84  -s --specdocformat RAW|HTML
85  Specifies the documentation format used with XML and
86  JSON spec files. RAW means preserving the original
87  documentation format and HTML means converting
88  documentation to HTML. The default is RAW with XML
89  spec files and HTML with JSON specs and when using
90  the special LIBSPEC format. New in RF 4.0.
91  -F --docformat ROBOT|HTML|TEXT|REST
92  Specifies the source documentation format. Possible
93  values are Robot Framework's documentation format,
94  HTML, plain text, and reStructuredText. The default
95  value can be specified in library source code and
96  the initial default value is ROBOT.
97  --theme DARK|LIGHT|NONE
98  Use dark or light HTML theme. If this option is not
99  used, or the value is NONE, the theme is selected
100  based on the browser color scheme. New in RF 6.0.
101  -n --name name Sets the name of the documented library or resource.
102  -v --version version Sets the version of the documented library or
103  resource.
104  --quiet Do not print the path of the generated output file
105  to the console. New in RF 4.0.
106  -P --pythonpath path * Additional locations where to search for libraries
107  and resources.
108  -h -? --help Print this help.
109 
110 Creating documentation
111 ======================
112 
113 When creating documentation in HTML, XML or JSON format, the output file must
114 be specified as the second argument after the library or resource name or path.
115 
116 Output format is got automatically from the output file extension. In addition
117 to `*.html`, `*.xml` and `*.json` extensions, it is possible to use the special
118 `*.libspec` extensions which means XML spec with actual library and keyword
119 documentation converted to HTML. The format can also be set explicitly using
120 the `--format` option.
121 
122 Examples:
123 
124  libdoc src/MyLibrary.py doc/MyLibrary.html
125  libdoc doc/MyLibrary.json doc/MyLibrary.html
126  libdoc --name MyLibrary Remote::10.0.0.42:8270 MyLibrary.xml
127  libdoc MyLibrary MyLibrary.libspec
128 
129 Viewing information on console
130 ==============================
131 
132 Libdoc has three special commands to show information on the console. These
133 commands are used instead of the name of the output file, and they can also
134 take additional arguments.
135 
136 list: List names of the keywords the library/resource contains. Can be
137  limited to show only certain keywords by passing optional patterns as
138  arguments. Keyword is listed if its name contains any given pattern.
139 show: Show library/resource documentation. Can be limited to show only
140  certain keywords by passing names as arguments. Keyword is shown if
141  its name matches any given name. Special argument `intro` will show
142  the library introduction and importing sections.
143 version: Show library version
144 
145 Optional patterns given to `list` and `show` are case and space insensitive.
146 Both also accept `*` and `?` as wildcards.
147 
148 Examples:
149 
150  libdoc Dialogs list
151  libdoc SeleniumLibrary list browser
152  libdoc Remote::10.0.0.42:8270 show
153  libdoc Dialogs show PauseExecution execute*
154  libdoc SeleniumLibrary show intro
155  libdoc SeleniumLibrary version
156 
157 Alternative execution
158 =====================
159 
160 Libdoc works with all interpreters supported by Robot Framework.
161  In the examples above Libdoc is executed as an
162 installed module, but it can also be executed as a script like
163 `python path/robot/libdoc.py`.
164 
165 For more information about Libdoc and other built-in tools, see
166 http://robotframework.org/robotframework/#built-in-tools.
167 """
168 
169 
170 class LibDoc(Application):
171 
172  def __init__(self):
173  Application.__init__(self, USAGE, arg_limits=(2,), auto_version=False)
174 
175  def validate(self, options, arguments):
176  if ConsoleViewer.handles(arguments[1]):
177  ConsoleViewer.validate_command(arguments[1], arguments[2:])
178  return options, arguments
179  if len(arguments) > 2:
180  raise DataError('Only two arguments allowed when writing output.')
181  return options, arguments
182 
183  def main(self, args, name='', version='', format=None, docformat=None,
184  specdocformat=None, theme=None, pythonpath=None, quiet=False):
185  if pythonpath:
186  sys.path = pythonpath + sys.path
187  lib_or_res, output = args[:2]
188  docformat = self._get_docformat_get_docformat(docformat)
189  libdoc = LibraryDocumentation(lib_or_res, name, version, docformat)
190  if ConsoleViewer.handles(output):
191  ConsoleViewer(libdoc).view(output, *args[2:])
192  return
193  format, specdocformat \
194  = self._get_format_and_specdocformat_get_format_and_specdocformat(format, specdocformat, output)
195  if (format == 'HTML'
196  or specdocformat == 'HTML'
197  or format in ('JSON', 'LIBSPEC') and specdocformat != 'RAW'):
198  libdoc.convert_docs_to_html()
199  libdoc.save(output, format, self._validate_theme_validate_theme(theme, format))
200  if not quiet:
201  self.console(os.path.abspath(output))
202 
203  def _get_docformat(self, docformat):
204  return self._validate_validate('Doc format', docformat, 'ROBOT', 'TEXT', 'HTML', 'REST')
205 
206  def _get_format_and_specdocformat(self, format, specdocformat, output):
207  extension = os.path.splitext(output)[1][1:]
208  format = self._validate_validate('Format', format or extension,
209  'HTML', 'XML', 'JSON', 'LIBSPEC')
210  specdocformat = self._validate_validate('Spec doc format', specdocformat, 'RAW', 'HTML')
211  if format == 'HTML' and specdocformat:
212  raise DataError("The --specdocformat option is not applicable with "
213  "HTML outputs.")
214  return format, specdocformat
215 
216  def _validate(self, kind, value, *valid):
217  if not value:
218  return None
219  value = value.upper()
220  if value not in valid:
221  raise DataError(f"{kind} must be {seq2str(valid, lastsep=' or ')}, "
222  f"got '{value}'.")
223  return value
224 
225  def _validate_theme(self, theme, format):
226  theme = self._validate_validate('Theme', theme, 'DARK', 'LIGHT', 'NONE')
227  if not theme or theme == 'NONE':
228  return None
229  if format != 'HTML':
230  raise DataError("The --theme option is only applicable with HTML outputs.")
231  return theme
232 
233 
234 
248 def libdoc_cli(arguments=None, exit=True):
249  if arguments is None:
250  arguments = sys.argv[1:]
251  LibDoc().execute_cli(arguments, exit=exit)
252 
253 
254 
287 def libdoc(library_or_resource, outfile, name='', version='', format=None,
288  docformat=None, specdocformat=None, quiet=False):
289  return LibDoc().execute(
290  library_or_resource, outfile, name=name, version=version, format=format,
291  docformat=docformat, specdocformat=specdocformat, quiet=quiet
292  )
293 
294 
295 if __name__ == '__main__':
296  libdoc_cli(sys.argv[1:])
def _get_docformat(self, docformat)
Definition: libdoc.py:203
def main(self, args, name='', version='', format=None, docformat=None, specdocformat=None, theme=None, pythonpath=None, quiet=False)
Definition: libdoc.py:184
def _validate_theme(self, theme, format)
Definition: libdoc.py:225
def _validate(self, kind, value, *valid)
Definition: libdoc.py:216
def __init__(self)
Definition: libdoc.py:172
def _get_format_and_specdocformat(self, format, specdocformat, output)
Definition: libdoc.py:206
def validate(self, options, arguments)
Definition: libdoc.py:175
def libdoc_cli(arguments=None, exit=True)
Executes Libdoc similarly as from the command line.
Definition: libdoc.py:248
def libdoc(library_or_resource, outfile, name='', version='', format=None, docformat=None, specdocformat=None, quiet=False)
Executes Libdoc.
Definition: libdoc.py:288
def LibraryDocumentation(library_or_resource, name=None, version=None, doc_format=None)
Generate keyword documentation for the given library, resource or suite file.
Definition: builder.py:55