Robot Framework
builder.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import os
17 
18 from robot.errors import DataError
19 from robot.utils import get_error_message
20 
21 from .jsonbuilder import JsonDocBuilder
22 from .robotbuilder import LibraryDocBuilder, ResourceDocBuilder, SuiteDocBuilder
23 from .xmlbuilder import XmlDocBuilder
24 
25 
26 RESOURCE_EXTENSIONS = ('resource', 'robot', 'txt', 'tsv', 'rst', 'rest')
27 XML_EXTENSIONS = ('xml', 'libspec')
28 
29 
30 
55 def LibraryDocumentation(library_or_resource, name=None, version=None, doc_format=None):
56  libdoc = DocumentationBuilder().build(library_or_resource)
57  if name:
58  libdoc.name = name
59  if version:
60  libdoc.version = version
61  if doc_format:
62  libdoc.doc_format = doc_format
63  return libdoc
64 
65 
66 
72 
73 
78  def __init__(self, library_or_resource=None):
79  pass
80 
81  def build(self, source):
82  builder = self._get_builder(source)
83  return self._build(builder, source)
84 
85  def _get_builder(self, source):
86  if os.path.exists(source):
87  extension = self._get_extension_get_extension(source)
88  if extension == 'resource':
89  return ResourceDocBuilder()
90  if extension in RESOURCE_EXTENSIONS:
91  return SuiteDocBuilder()
92  if extension in XML_EXTENSIONS:
93  return XmlDocBuilder()
94  if extension == 'json':
95  return JsonDocBuilder()
96  return LibraryDocBuilder()
97 
98  def _get_extension(self, source):
99  path, *args = source.split('::')
100  return os.path.splitext(path)[1][1:].lower()
101 
102  def _build(self, builder, source):
103  try:
104  return builder.build(source)
105  except DataError:
106  # Possible resource file in PYTHONPATH. Something like `xxx.resource` that
107  # did not exist has been considered to be a library earlier, now we try to
108  # parse it as a resource file.
109  if (isinstance(builder, LibraryDocBuilder)
110  and not os.path.exists(source)
111  and self._get_extension_get_extension(source) in RESOURCE_EXTENSIONS):
112  return self._build_build(ResourceDocBuilder(), source)
113  # Resource file with other extension than '.resource' parsed as a suite file.
114  if isinstance(builder, SuiteDocBuilder):
115  return self._build_build(ResourceDocBuilder(), source)
116  raise
117  except Exception:
118  raise DataError(f"Building library '{source}' failed: {get_error_message()}")
Keyword documentation builder.
Definition: builder.py:71
def _build(self, builder, source)
Definition: builder.py:102
def __init__(self, library_or_resource=None)
library_or_resource is accepted for backwards compatibility reasons.
Definition: builder.py:78
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