Robot Framework Integrated Development Environment (RIDE)
libraryscopes.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 inspect
17 
18 from robotide.lib.robot.utils import normalize, unic
19 
20 
21 def LibraryScope(libcode, library):
22  scope = _get_scope(libcode)
23  if scope == 'global':
24  return GlobalScope(library)
25  if scope == 'testsuite':
26  return TestSuiteScope(library)
27  return TestCaseScope(library)
28 
29 
30 def _get_scope(libcode):
31  if inspect.ismodule(libcode):
32  return 'global'
33  scope = getattr(libcode, 'ROBOT_LIBRARY_SCOPE', '')
34  return normalize(unic(scope), ignore='_')
35 
36 
37 class GlobalScope():
38  is_global = True
39 
40  def __init__(self, library):
41  self._register_listeners_register_listeners = library.register_listeners
42  self._unregister_listeners_unregister_listeners = library.unregister_listeners
43 
44  def start_suite(self):
45  self._register_listeners_register_listeners()
46 
47  def end_suite(self):
48  self._unregister_listeners_unregister_listeners()
49 
50  def start_test(self):
51  pass
52 
53  def end_test(self):
54  pass
55 
56  def __str__(self):
57  return 'global'
58 
59 
61  is_global = False
62 
63  def __init__(self, library):
64  GlobalScope.__init__(self, library)
65  self._reset_instance_reset_instance = library.reset_instance
66  self._instance_cache_instance_cache = []
67 
68  @property
69  is_global = property
70 
71  def is_global(self):
72  return False
73 
74  def start_suite(self):
75  prev = self._reset_instance_reset_instance()
76  self._instance_cache_instance_cache.append(prev)
77  self._register_listeners_register_listeners()
78 
79  def end_suite(self):
80  self._unregister_listeners_unregister_listeners(close=True)
81  prev = self._instance_cache_instance_cache.pop()
82  self._reset_instance_reset_instance(prev)
83 
84  def __str__(self):
85  return 'test suite'
86 
87 
89 
90  def start_test(self):
91  self._unregister_listeners_unregister_listeners()
92  prev = self._reset_instance_reset_instance()
93  self._instance_cache_instance_cache.append(prev)
94  self._register_listeners_register_listeners()
95 
96  def end_test(self):
97  self._unregister_listeners_unregister_listeners(close=True)
98  prev = self._instance_cache_instance_cache.pop()
99  self._reset_instance_reset_instance(prev)
100  self._register_listeners_register_listeners()
101 
102  def __str__(self):
103  return 'test case'
def normalize(string, ignore=(), caseless=True, spaceless=True)
Normalizes given string according to given spec.
Definition: normalizing.py:30