Robot Framework Integrated Development Environment (RIDE)
cache.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 time
18 
19 from ..robotapi import normpath
20 from ..spec.iteminfo import BlockKeywordInfo
21 
22 
23 class LibraryCache():
24 
25  def __init__(self, settings, libraries_need_refresh_listener,
26  library_manager):
27  self._settings_settings = settings
28  if library_manager:
29  self.set_library_managerset_library_manager(library_manager)
30  self._libraries_need_refresh_listener_libraries_need_refresh_listener = libraries_need_refresh_listener
31  self._library_keywords_library_keywords = {}
32  self.__default_libraries__default_libraries = None
33  self.__default_kws__default_kws = None
34 
35  def set_library_manager(self, library_manager):
36  self._library_manager_library_manager = library_manager
37 
38  def expire(self):
39  self.__init____init__(self._settings_settings, self._libraries_need_refresh_listener_libraries_need_refresh_listener,
40  self._library_manager_library_manager)
41 
42  @property
43  _default_libraries = property
44 
45  def _default_libraries(self):
46  if self.__default_libraries__default_libraries is None:
47  self.__default_libraries__default_libraries = self._get_default_libraries_get_default_libraries()
48  return self.__default_libraries__default_libraries
49 
50  @property
51  _default_kws = property
52 
53  def _default_kws(self):
54  if self.__default_kws__default_kws is None:
55  self.__default_kws__default_kws = self._build_default_kws_build_default_kws()
56  return self.__default_kws__default_kws
57 
59  return [name for name, _ in self._library_keywords_library_keywords]
60 
61  def _get_library(self, name, args):
62  library_database = \
63  self._library_manager_library_manager.get_new_connection_to_library_database()
64  try:
65  last_updated = library_database.get_library_last_updated(name, args)
66  if last_updated:
67  if time.time() - last_updated > 10.0:
68  self._library_manager_library_manager.fetch_keywords(
69  name, args, self._libraries_need_refresh_listener_libraries_need_refresh_listener)
70  return library_database.fetch_library_keywords(name, args)
71  return self._library_manager_library_manager.get_and_insert_keywords(name, args)
72  finally:
73  library_database.close()
74 
75  def _key(self, name, args):
76  return name, str(tuple(args or ''))
77 
78  def get_library_keywords(self, name, args=None, alias=None):
79  args_with_alias = self._alias_to_args_alias_to_args(alias, args)
80  key = self._key_key(name, args_with_alias)
81  if not key in self._library_keywords_library_keywords:
82  self._library_keywords_library_keywords[key] = [k.with_alias(alias) for k in
83  self._get_library_get_library(name, args)]
84 
85  return self._library_keywords_library_keywords[key]
86 
87  def _alias_to_args(self, alias, args):
88  if alias:
89  if args:
90  args = tuple(args) + ('WITH NAME', alias)
91  else:
92  args = ('WITH NAME', alias)
93  return args
94 
96  return self._default_kws_default_kws_default_kws[:]
97 
98  def _build_default_kws(self):
99  kws = []
100  for keywords_in_library in self._default_libraries_default_libraries_default_libraries.values():
101  kws.extend(keywords_in_library)
102  # DEBUG fake FOR and END
103  # obj1 = BlockKeywordInfo('FOR', 'To create loops. See `BuiltIn` (docs)[https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#for-loops].')
104  obj1 = BlockKeywordInfo('FOR', 'To create loops. Arguments: (scalars) loop_variables, selector: one of IN, IN RANGE, IN ENUMERATE, IN ZIP, values=scalars, lists, dictionaries. See `BuiltIn.FOR` docs at\n https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#for-loops.', 'ROBOT', 'BuiltIn', 'loop_variables', 'selector', '*values')
105  kws.append(obj1)
106  obj2 = BlockKeywordInfo('END', 'Ends `FOR` loops, `IF/ELSE`, `WHILE` and `TRY/EXCEPT` blocks. See `BuiltIn.FOR` docs at\n https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#for-loops.')
107  kws.append(obj2)
108  obj3 = BlockKeywordInfo(': FOR', '*DEPRECATED*\nSee `BuiltIn.FOR` docs.')
109  kws.append(obj3)
110  obj4 = BlockKeywordInfo('WHILE', 'To create loops. Arguments: (boolean) condition, (optional) limit=counter or time. See `BuiltIn` docs at\n https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#while-loops.', 'ROBOT', 'BuiltIn', '(boolean) condition', '(optional) limit=')
111  kws.append(obj4)
112  obj5 = BlockKeywordInfo('BREAK', 'To control loops. See `BuiltIn` docs at\n https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#loop-control-using-break-and-continue.')
113  kws.append(obj5)
114  obj6 = BlockKeywordInfo('CONTINUE', 'To control loops. See `BuiltIn` docs at\n https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#loop-control-using-break-and-continue.')
115  kws.append(obj6)
116  obj7 = BlockKeywordInfo('IF', 'To condition blocks of keywords. Arguments: (boolean) condition. See `BuiltIn` docs at\n https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#if-else-syntax.', 'ROBOT', 'BuiltIn', '(boolean) condition')
117  kws.append(obj7)
118  obj8 = BlockKeywordInfo('ELSE', 'To condition blocks of keywords. See `BuiltIn.IF` docs at\n https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#if-else-syntax.\n Note: See also TRY/EXCEPT.')
119  kws.append(obj8)
120  obj9 = BlockKeywordInfo('ELSE IF', 'To condition blocks of keywords. Arguments: (boolean) condition. See `BuiltIn.IF` docs at\n https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#if-else-syntax.', 'ROBOT', 'BuiltIn', '(boolean) condition')
121  kws.append(obj9)
122  obj10 = BlockKeywordInfo('TRY', 'To prevent test failures based on messages from keywords. See `BuiltIn.TRY` docs at\n https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#try-except-syntax.')
123  kws.append(obj10)
124  obj11 = BlockKeywordInfo('EXCEPT', 'To prevent test failures based on messages from keywords.\n\nArguments: (optional) message=string, (optional) glob, regex, scalar, (optional) type, glob or error capture setting.\n\n See `BuiltIn.TRY` docs at\n https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#try-except-syntax.', 'ROBOT', 'BuiltIn', '*options')
125  kws.append(obj11)
126  obj12 = BlockKeywordInfo('FINALLY', 'To prevent test failures based on messages from keywords. See `BuiltIn.TRY` docs at\n https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#try-except-syntax.')
127  kws.append(obj12)
128  return kws
129 
131  default_libs = {}
132  for libsetting in self._settings_settings['auto imports'] + ['BuiltIn']:
133  name, args = self._get_name_and_args_get_name_and_args(libsetting)
134  default_libs[name] = self._get_library_get_library(name, args)
135  return default_libs
136 
137  def _get_name_and_args(self, libsetting):
138  parts = libsetting.split('|')
139  if len(parts) == 1:
140  return parts[0], None
141  return parts[0], parts[1:]
142 
143 
145 
146  def __init__(self, timeout=0.5):
147  self._cache_cache = {}
148  self._timeout_timeout = timeout
149 
150  def get(self, key):
151  if key in self._cache_cache:
152  key_time, values = self._cache_cache[key]
153  if self._is_valid_is_valid(key_time):
154  return values
155  return None
156 
157  def _is_valid(self, key_time):
158  return (time.time() - key_time) < self._timeout_timeout
159 
160  def put(self, key, values):
161  self._cache_cache[key] = (time.time(), values)
162 
163  def _get_from_cache(self, source, name):
164  try:
165  return self._resource_files[name]
166  except KeyError:
167  path = normpath(os.path.join(os.path.dirname(source), name))
168  return self._resource_files[path]
def put(self, key, values)
Definition: cache.py:160
def __init__(self, timeout=0.5)
Definition: cache.py:146
def _is_valid(self, key_time)
Definition: cache.py:157
def _get_from_cache(self, source, name)
Definition: cache.py:163
def _get_library(self, name, args)
Definition: cache.py:61
def get_library_keywords(self, name, args=None, alias=None)
Definition: cache.py:78
def _get_name_and_args(self, libsetting)
Definition: cache.py:137
def __init__(self, settings, libraries_need_refresh_listener, library_manager)
Definition: cache.py:26
def _key(self, name, args)
Definition: cache.py:75
def _alias_to_args(self, alias, args)
Definition: cache.py:87
def set_library_manager(self, library_manager)
Definition: cache.py:35
Special Info for FOR and END, documentation and since 5.0, IF, ELSE, ELSEIF, WHILE,...
Definition: iteminfo.py:309
def normpath(path, case_normalize=False)
Replacement for os.path.normpath with some enhancements.
Definition: robotpath.py:68