Robot Framework SeleniumLibrary
robotlibcore.py
Go to the documentation of this file.
1 # Copyright 2017- Robot Framework Foundation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 
21 
22 import inspect
23 import sys
24 
25 try:
26  from robot.api.deco import keyword
27 except ImportError: # Support RF < 2.9
28  def keyword(name=None, tags=()):
29  if callable(name):
30  return keyword()(name)
31  def decorator(func):
32  func.robot_name = name
33  func.robot_tags = tags
34  return func
35  return decorator
36 
37 
38 PY2 = sys.version_info < (3,)
39 
40 __version__ = '1.0.1.dev1'
41 
42 
43 class HybridCore():
44 
45  def __init__(self, library_components):
46  self.keywordskeywords = {}
47  self.attributesattributes = {}
48  self.add_library_componentsadd_library_components(library_components)
49  self.add_library_componentsadd_library_components([self])
50 
51  def add_library_components(self, library_components):
52  for component in library_components:
53  for name, func in self._get_members_get_members(component):
54  if callable(func) and hasattr(func, 'robot_name'):
55  kw = getattr(component, name)
56  kw_name = func.robot_name or name
57  self.keywordskeywords[kw_name] = kw
58  # Expose keywords as attributes both using original
59  # method names as well as possible custom names.
60  self.attributesattributes[name] = self.attributesattributes[kw_name] = kw
61 
62  def _get_members(self, component):
63  if inspect.ismodule(component):
64  return inspect.getmembers(component)
65  if inspect.isclass(component):
66  raise TypeError('Libraries must be modules or instances, got '
67  'class {!r} instead.'.format(component.__name__))
68  if type(component) != component.__class__:
69  raise TypeError('Libraries must be modules or new-style class '
70  'instances, got old-style class {!r} instead.'
71  .format(component.__class__.__name__))
72  return self._get_members_from_instance_get_members_from_instance(component)
73 
74  def _get_members_from_instance(self, instance):
75  # Avoid calling properties by getting members from class, not instance.
76  cls = type(instance)
77  for name in dir(instance):
78  owner = cls if hasattr(cls, name) else instance
79  yield name, getattr(owner, name)
80 
81  def __getattr__(self, name):
82  if name in self.attributesattributes:
83  return self.attributesattributes[name]
84  raise AttributeError('{!r} object has no attribute {!r}'
85  .format(type(self).__name__, name))
86 
87  def __dir__(self):
88  if PY2:
89  my_attrs = dir(type(self)) + list(self.__dict__)
90  else:
91  my_attrs = super().__dir__()
92  return sorted(set(my_attrs) | set(self.attributesattributes))
93 
94  def get_keyword_names(self):
95  return sorted(self.keywordskeywords)
96 
97 
99 
102  _get_keyword_tags_supported = False # get_keyword_tags is new in RF 3.0.2
103 
104  def run_keyword(self, name, args, kwargs):
105  return self.keywordskeywords[name](*args, **kwargs)
106 
107  def get_keyword_arguments(self, name):
108  kw = self.keywordskeywords[name] if name != '__init__' else self.__init____init__
109  args, defaults, varargs, kwargs = self._get_arg_spec_get_arg_spec(kw)
110  args += ['{}={}'.format(name, value) for name, value in defaults]
111  if varargs:
112  args.append('*{}'.format(varargs))
113  if kwargs:
114  args.append('**{}'.format(kwargs))
115  return args
116 
117  def _get_arg_spec(self, kw):
118  if PY2:
119  spec = inspect.getargspec(kw)
120  keywords = spec.keywords
121  else:
122  spec = inspect.getfullargspec(kw)
123  keywords = spec.varkw
124  args = spec.args[1:] if inspect.ismethod(kw) else spec.args # drop self
125  defaults = spec.defaults or ()
126  nargs = len(args) - len(defaults)
127  mandatory = args[:nargs]
128  defaults = zip(args[nargs:], defaults)
129  return mandatory, defaults, spec.varargs, keywords
130 
131  def get_keyword_tags(self, name):
132  self._get_keyword_tags_supported_get_keyword_tags_supported_get_keyword_tags_supported = True
133  return self.keywordskeywords[name].robot_tags
134 
135  def get_keyword_documentation(self, name):
136  if name == '__intro__':
137  return inspect.getdoc(self) or ''
138  if name == '__init__':
139  return inspect.getdoc(self.__init____init__) or ''
140  kw = self.keywordskeywords[name]
141  doc = inspect.getdoc(kw) or ''
142  if kw.robot_tags and not self._get_keyword_tags_supported_get_keyword_tags_supported_get_keyword_tags_supported:
143  tags = 'Tags: {}'.format(', '.join(kw.robot_tags))
144  doc = '{}\n\n{}'.format(doc, tags) if doc else tags
145  return doc
146 
147 
149 
150  def __init__(self):
151  HybridCore.__init__(self, [])
def run_keyword(self, name, args, kwargs)
def add_library_components(self, library_components)
Definition: robotlibcore.py:51
def __init__(self, library_components)
Definition: robotlibcore.py:45
def keyword(name=None, tags=())
Definition: robotlibcore.py:28