Robot Framework Integrated Development Environment (RIDE)
local_namespace.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 robotide import utils
17 from robotide.spec.iteminfo import LocalVariableInfo
18 
19 
20 def LocalNamespace(controller, namespace, row=None):
21  if row is not None: # can be 0!
22  return LocalRowNamespace(controller, namespace, row)
23  return LocalMacroNamespace(controller, namespace)
24 
25 
27 
28  def __init__(self, controller, namespace):
29  self._controller_controller = controller
30  self._namespace_namespace = namespace
31 
32  def get_suggestions(self, start):
33  return self._namespace_namespace.get_suggestions_for(self._controller_controller, start)
34 
35  def has_name(self, value):
36  for sug in self._namespace_namespace.get_suggestions_for(self._controller_controller, value):
37  if sug.name == value:
38  return True
39  try:
40  if value in sug.assign:
41  return True
42  except AttributeError:
43  pass
44  return False
45 
46 
48 
49  def __init__(self, controller, namespace, row):
50  LocalMacroNamespace.__init__(self, controller, namespace)
51  self._row_row = row
52 
53  def get_suggestions(self, start):
54  suggestions = LocalMacroNamespace.get_suggestions(self, start)
55  if self._could_be_variable_could_be_variable(start):
56  suggestions = self._harvest_local_variables_harvest_local_variables(start, suggestions)
57  else:
58  suggestions = self._harvest_local_variables_harvest_local_variables('${'+start, suggestions)
59  suggestions = self._harvest_local_variables_harvest_local_variables('@{'+start, suggestions)
60  suggestions = self._harvest_local_variables_harvest_local_variables('&{'+start, suggestions)
61  return suggestions
62 
63  def _harvest_local_variables(self, start, suggestions):
64  matching_assignments = set()
65  for row, step in enumerate(self._controller_controller.steps):
66  if self._row_row == row:
67  break
68  matching_assignments = matching_assignments.union(
69  val.replace('=', '').strip() for val in step.assignments if
70  val.startswith(start))
71  if matching_assignments:
72  local_variables = [LocalVariableInfo(name) for name
73  in matching_assignments]
74  suggestions = sorted(self._remove_duplicates_remove_duplicates(suggestions,
75  local_variables))
76  return suggestions
77 
78  def _could_be_variable(self, start):
79  return len(start) == 0 or start[0] in ['$', '@', '&']
80 
81  def has_name(self, value):
82  if self._row_row is not None:
83  for row, step in enumerate(self._controller_controller.steps):
84  if self._row_row == row:
85  break
86  if step.is_assigning(value):
87  return True
88  return LocalMacroNamespace.has_name(self, value)
89 
90  def _remove_duplicates(self, suggestions, local_variables):
91  def is_unique(gvar):
92  return utils.normalize(gvar.name) not in \
93  [utils.normalize(lvar.name) for lvar in local_variables]
94  unique = [gvar for gvar in suggestions if is_unique(gvar)]
95  return unique + local_variables
def __init__(self, controller, namespace, row)
def _harvest_local_variables(self, start, suggestions)
def _remove_duplicates(self, suggestions, local_variables)
def LocalNamespace(controller, namespace, row=None)