Robot Framework Integrated Development Environment (RIDE)
robotbuilder.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 import sys
18 
19 from robotide.lib.robot.errors import DataError
20 from robotide.lib.robot.parsing import disable_curdir_processing
21 from robotide.lib.robot.running import TestLibrary, UserLibrary, UserErrorHandler
22 from robotide.lib.robot.utils import split_tags_from_doc, unescape
23 
24 from .model import LibraryDoc, KeywordDoc
25 
26 
28 
31  _argument_separator = '::'
32 
33  def build(self, library):
34  name, args = self._split_library_name_and_args_split_library_name_and_args(library)
35  lib = TestLibrary(name, args)
36  libdoc = LibraryDoc(name=lib.name,
37  doc=self._get_doc_get_doc(lib),
38  version=lib.version,
39  scope=str(lib.scope),
40  doc_format=lib.doc_format)
41  libdoc.inits = self._get_initializers_get_initializers(lib)
42  libdoc.keywords = KeywordDocBuilder().build_keywords(lib)
43  return libdoc
44 
45  def _split_library_name_and_args(self, library):
46  args = library.split(self._argument_separator_argument_separator)
47  name = args.pop(0)
48  return self._normalize_library_path_normalize_library_path(name), args
49 
50  def _normalize_library_path(self, library):
51  path = library.replace('/', os.sep)
52  if os.path.exists(path):
53  return os.path.abspath(path)
54  return library
55 
56  def _get_doc(self, lib):
57  return lib.doc or "Documentation for library ``%s``." % lib.name
58 
59  def _get_initializers(self, lib):
60  if lib.init.arguments.maxargs:
61  return [KeywordDocBuilder().build_keyword(lib.init)]
62  return []
63 
64 
66 
67  def build(self, path):
68  res = self._import_resource_import_resource(path)
69  libdoc = LibraryDoc(name=res.name,
70  doc=self._get_doc_get_doc(res),
71  type='resource')
72  libdoc.keywords = KeywordDocBuilder(resource=True).build_keywords(res)
73  return libdoc
74 
75  @disable_curdir_processing
76  def _import_resource(self, path):
77  return UserLibrary(self._find_resource_file_find_resource_file(path))
78 
79  def _find_resource_file(self, path):
80  if os.path.isfile(path):
81  return os.path.normpath(path)
82  for dire in [item for item in sys.path if os.path.isdir(item)]:
83  candidate = os.path.normpath(os.path.join(dire, path))
84  if os.path.isfile(candidate):
85  return candidate
86  raise DataError("Resource file '%s' does not exist." % path)
87 
88  def _get_doc(self, res):
89  if res.doc:
90  return unescape(res.doc)
91  return "Documentation for resource file ``%s``." % res.name
92 
93 
95 
96  def __init__(self, resource=False):
97  self._resource_resource = resource
98 
99  def build_keywords(self, lib):
100  return [self.build_keywordbuild_keyword(kw) for kw in lib.handlers]
101 
102  def build_keyword(self, kw):
103  doc, tags = self._get_doc_and_tags_get_doc_and_tags(kw)
104  return KeywordDoc(name=kw.name,
105  args=self._get_args_get_args(kw.arguments),
106  doc=doc,
107  tags=tags)
108 
109  def _get_doc_and_tags(self, kw):
110  doc = self._get_doc_get_doc(kw)
111  doc, tags = split_tags_from_doc(doc)
112  return doc, kw.tags + tags
113 
114  def _get_doc(self, kw):
115  if self._resource_resource and not isinstance(kw, UserErrorHandler):
116  return unescape(kw.doc)
117  return kw.doc
118 
119 
122  def _get_args(self, argspec):
123  args = [self._format_arg_format_arg(arg, argspec) for arg in argspec.positional]
124  if argspec.varargs:
125  args.append('*%s' % self._format_arg_format_arg(argspec.varargs, argspec))
126  if argspec.kwonlyargs:
127  if not argspec.varargs:
128  args.append('*')
129  args.extend(self._format_arg_format_arg(arg, argspec)
130  for arg in argspec.kwonlyargs)
131  if argspec.kwargs:
132  args.append('**%s' % self._format_arg_format_arg(argspec.kwargs, argspec))
133  return args
134 
135  def _format_arg(self, arg, argspec):
136  result = arg
137  if argspec.types is not None and arg in argspec.types:
138  result = '%s: %s' % (result, self._format_type_format_type(argspec.types[arg]))
139  if arg in argspec.defaults:
140  result = '%s=%s' % (result, argspec.defaults[arg])
141  return result
142 
143  def _format_type(self, type_):
144  return type_.__name__ if isinstance(type_, type) else type_
Used when variable does not exist.
Definition: errors.py:67
def _get_args(self, argspec)
:type argspec: :py:class:robot.running.arguments.ArgumentSpec
def TestLibrary(name, args=None, variables=None, create_handlers=True)
def split_tags_from_doc(doc)
Definition: text.py:158