Robot Framework
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 from .normalizing import NormalizedDict
17 from .robottypes import is_string
18 
19 
20 
31 
32  def __init__(self, no_current_msg='No open connection.'):
33  self._no_current_no_current = NoConnection(no_current_msg)
34  self.currentcurrent = self._no_current_no_current #: Current active connection.
35  self._connections_connections = []
36  self._aliases_aliases = NormalizedDict()
37 
38  @property
39  current_index = property
40 
41  def current_index(self):
42  if not self:
43  return None
44  for index, conn in enumerate(self):
45  if conn is self.currentcurrent:
46  return index + 1
47 
48  @current_index.setter
49 
50  def current_index(self, index):
51  self.currentcurrent = self._connections_connections[index - 1] \
52  if index is not None else self._no_current_no_current
53 
54 
64  def register(self, connection, alias=None):
65  self.currentcurrent = connection
66  self._connections_connections.append(connection)
67  index = len(self._connections_connections)
68  if is_string(alias):
69  self._aliases_aliases[alias] = index
70  return index
71 
72 
81  def switch(self, alias_or_index):
82  self.currentcurrent = self.get_connectionget_connection(alias_or_index)
83  return self.currentcurrent
84 
85 
95  def get_connection(self, alias_or_index=None):
96  if alias_or_index is None:
97  if not self:
98  self.currentcurrent.raise_error()
99  return self.currentcurrent
100  try:
101  index = self.resolve_alias_or_indexresolve_alias_or_index(alias_or_index)
102  except ValueError as err:
103  raise RuntimeError(err.args[0])
104  return self._connections_connections[index-1]
105 
106  __getitem__ = get_connection
107 
108 
114  def close_all(self, closer_method='close'):
115  for conn in self._connections_connections:
116  getattr(conn, closer_method)()
117  self.empty_cacheempty_cache()
118  return self.currentcurrent
119 
120 
124  def empty_cache(self):
125  self.currentcurrent = self._no_current_no_current
126  self._connections_connections = []
127  self._aliases_aliases = NormalizedDict()
128 
129  def __iter__(self):
130  return iter(self._connections_connections)
131 
132  def __len__(self):
133  return len(self._connections_connections)
134 
135  def __bool__(self):
136  return self.currentcurrent is not self._no_current_no_current
137 
138  def resolve_alias_or_index(self, alias_or_index):
139  for resolver in self._resolve_alias_resolve_alias, self._resolve_index_resolve_index, self._is_connection_is_connection:
140  try:
141  return resolver(alias_or_index)
142  except ValueError:
143  pass
144  raise ValueError(f"Non-existing index or alias '{alias_or_index}'.")
145 
146  def _resolve_alias(self, alias):
147  if is_string(alias) and alias in self._aliases_aliases:
148  return self._aliases_aliases[alias]
149  raise ValueError
150 
151  def _resolve_index(self, index):
152  try:
153  index = int(index)
154  except TypeError:
155  raise ValueError
156  if not 0 < index <= len(self._connections_connections):
157  raise ValueError
158  return index
159 
160  def _is_connection(self, conn):
161  return self._connections_connections.index(conn) + 1
162 
163 
165 
166  def __init__(self, message):
167  self.messagemessage = message
168 
169  def __getattr__(self, name):
170  if name.startswith('__') and name.endswith('__'):
171  raise AttributeError
172  self.raise_errorraise_error()
173 
174  def raise_error(self):
175  raise RuntimeError(self.messagemessage)
176 
177  def __bool__(self):
178  return False
Cache for libraries to use with concurrent connections, processes, etc.
def empty_cache(self)
Empties the connection cache.
def get_connection(self, alias_or_index=None)
Get the connection specified by the given alias or index.
def switch(self, alias_or_index)
Switches to the connection specified by the given alias or index.
def __init__(self, no_current_msg='No open connection.')
def close_all(self, closer_method='close')
Closes connections using given closer method and empties cache.
def resolve_alias_or_index(self, alias_or_index)
def register(self, connection, alias=None)
Registers given connection with optional alias and returns its index.
Custom dictionary implementation automatically normalizing keys.
Definition: normalizing.py:50