Robot Framework Integrated Development Environment (RIDE)
connectioncache.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 warnings
17 
18 from .compat import py2to3
19 from .normalizing import NormalizedDict
20 from .robottypes import is_string
21 
22 
23 @py2to3
24 
36 
37  def __init__(self, no_current_msg='No open connection.'):
38  self._no_current_no_current = NoConnection(no_current_msg)
39  self.currentcurrent = self._no_current_no_current #: Current active connection.
40  self._connections_connections = []
41  self._aliases_aliases = NormalizedDict()
42 
43  @property
44  current_index = property
45 
46  def current_index(self):
47  if not self:
48  return None
49  for index, conn in enumerate(self):
50  if conn is self.currentcurrent:
51  return index + 1
52 
53  @current_index.setter
54 
55  def current_index(self, index):
56  self.currentcurrent = self._connections_connections[index - 1] \
57  if index is not None else self._no_current_no_current
58 
59 
69  def register(self, connection, alias=None):
70  self.currentcurrent = connection
71  self._connections_connections.append(connection)
72  index = len(self._connections_connections)
73  if is_string(alias):
74  self._aliases_aliases[alias] = index
75  return index
76 
77 
86  def switch(self, alias_or_index):
87  self.currentcurrent = self.get_connectionget_connection(alias_or_index)
88  return self.currentcurrent
89 
90 
100  def get_connection(self, alias_or_index=None):
101  if alias_or_index is None:
102  if not self:
103  self.currentcurrent.raise_error()
104  return self.currentcurrent
105  try:
106  index = self.resolve_alias_or_indexresolve_alias_or_index(alias_or_index)
107  except ValueError as err:
108  raise RuntimeError(err.args[0])
109  return self._connections_connections[index-1]
110 
111  __getitem__ = get_connection
112 
113 
119  def close_all(self, closer_method='close'):
120  for conn in self._connections_connections:
121  getattr(conn, closer_method)()
122  self.empty_cacheempty_cache()
123  return self.currentcurrent
124 
125 
129  def empty_cache(self):
130  self.currentcurrent = self._no_current_no_current
131  self._connections_connections = []
132  self._aliases_aliases = NormalizedDict()
133 
134  def __iter__(self):
135  return iter(self._connections_connections)
136 
137  def __len__(self):
138  return len(self._connections_connections)
139 
140  def __nonzero__(self):
141  return self.currentcurrent is not self._no_current_no_current
142 
143  def resolve_alias_or_index(self, alias_or_index):
144  for resolver in self._resolve_alias_resolve_alias, self._resolve_index_resolve_index:
145  try:
146  return resolver(alias_or_index)
147  except ValueError:
148  pass
149  raise ValueError("Non-existing index or alias '%s'." % alias_or_index)
150 
151  def _resolve_alias_or_index(self, alias_or_index):
152  # TODO: Change to more visible UserWarning in RF 3.2 and remove in 3.3.
153  # See https://github.com/robotframework/robotframework/issues/3125
154  warnings.warn("'ConnectionCache._resolve_alias_or_index' is "
155  "deprecated. Use 'resolve_alias_or_index' instead.",
156  DeprecationWarning)
157  return self.resolve_alias_or_indexresolve_alias_or_index(alias_or_index)
158 
159  def _resolve_alias(self, alias):
160  if is_string(alias) and alias in self._aliases_aliases:
161  return self._aliases_aliases[alias]
162  raise ValueError
163 
164  def _resolve_index(self, index):
165  try:
166  index = int(index)
167  except TypeError:
168  raise ValueError
169  if not 0 < index <= len(self._connections_connections):
170  raise ValueError
171  return index
172 
173 
174 @py2to3
175 class NoConnection():
176 
177  def __init__(self, message):
178  self.messagemessage = message
179 
180  def __getattr__(self, name):
181  if name.startswith('__') and name.endswith('__'):
182  raise AttributeError
183  self.raise_errorraise_error()
184 
185  def raise_error(self):
186  raise RuntimeError(self.messagemessage)
187 
188  def __nonzero__(self):
189  return False
Cache for test libs to use with concurrent connections, processes, etc.
def close_all(self, closer_method='close')
Closes connections using given closer method and empties cache.
def switch(self, alias_or_index)
Switches to the connection specified by the given alias or index.
def register(self, connection, alias=None)
Registers given connection with optional alias and returns its index.
def empty_cache(self)
Empties the connection cache.
def __init__(self, no_current_msg='No open connection.')
def get_connection(self, alias_or_index=None)
Get the connection specified by the given alias or index.
Custom dictionary implementation automatically normalizing keys.
Definition: normalizing.py:58