Robot Framework
settings.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 robot.utils import normalize, normalize_whitespace, RecommendationFinder
17 
18 from .tokens import Token
19 
20 
21 class Settings:
22  names = ()
23  aliases = {}
24  multi_use = (
25  'Metadata',
26  'Library',
27  'Resource',
28  'Variables'
29  )
30  single_value = (
31  'Resource',
32  'Test Timeout',
33  'Test Template',
34  'Timeout',
35  'Template'
36  )
37  name_and_arguments = (
38  'Metadata',
39  'Suite Setup',
40  'Suite Teardown',
41  'Test Setup',
42  'Test Teardown',
43  'Test Template',
44  'Setup',
45  'Teardown',
46  'Template',
47  'Resource',
48  'Variables'
49  )
50  name_arguments_and_with_name = (
51  'Library',
52  )
53 
54  def __init__(self, languages):
55  self.settingssettings = {n: None for n in self.namesnames}
56  self.languageslanguages = languages
57 
58  def lex(self, statement):
59  setting = statement[0]
60  orig = self._format_name_format_name(setting.value)
61  name = normalize_whitespace(orig).title()
62  name = self.languageslanguages.settings.get(name, name)
63  if name in self.aliasesaliases:
64  name = self.aliasesaliases[name]
65  try:
66  self._validate_validate(orig, name, statement)
67  except ValueError as err:
68  self._lex_error_lex_error(setting, statement[1:], err.args[0])
69  else:
70  self._lex_setting_lex_setting(setting, statement[1:], name)
71 
72  def _format_name(self, name):
73  return name
74 
75  def _validate(self, orig, name, statement):
76  if name not in self.settingssettings:
77  message = self._get_non_existing_setting_message_get_non_existing_setting_message(orig, name)
78  raise ValueError(message)
79  if self.settingssettings[name] is not None and name not in self.multi_usemulti_use:
80  raise ValueError("Setting '%s' is allowed only once. "
81  "Only the first value is used." % orig)
82  if name in self.single_valuesingle_value and len(statement) > 2:
83  raise ValueError("Setting '%s' accepts only one value, got %s."
84  % (orig, len(statement) - 1))
85 
86  def _get_non_existing_setting_message(self, name, normalized):
87  if normalized in (set(TestCaseFileSettings.names) |
88  set(TestCaseFileSettings.aliases)):
89  is_resource = isinstance(self, ResourceFileSettings)
90  return "Setting '%s' is not allowed in %s file." % (
91  name, 'resource' if is_resource else 'suite initialization'
92  )
93  return RecommendationFinder(normalize).find_and_format(
94  name=normalized,
95  candidates=tuple(self.settingssettings) + tuple(self.aliasesaliases),
96  message="Non-existing setting '%s'." % name
97  )
98 
99  def _lex_error(self, setting, values, error):
100  setting.set_error(error)
101  for token in values:
102  token.type = Token.COMMENT
103 
104  def _lex_setting(self, setting, values, name):
105  self.settingssettings[name] = values
106  # TODO: Change token type from 'FORCE TAGS' to 'TEST TAGS' in RF 7.0.
107  setting.type = name.upper() if name != 'Test Tags' else 'FORCE TAGS'
108  if name in self.name_and_argumentsname_and_arguments:
109  self._lex_name_and_arguments_lex_name_and_arguments(values)
110  elif name in self.name_arguments_and_with_namename_arguments_and_with_name:
111  self._lex_name_arguments_and_with_name_lex_name_arguments_and_with_name(values)
112  else:
113  self._lex_arguments_lex_arguments(values)
114 
115  def _lex_name_and_arguments(self, tokens):
116  if tokens:
117  tokens[0].type = Token.NAME
118  self._lex_arguments_lex_arguments(tokens[1:])
119 
121  self._lex_name_and_arguments_lex_name_and_arguments(tokens)
122  if len(tokens) > 1 and \
123  normalize_whitespace(tokens[-2].value) in ('WITH NAME', 'AS'):
124  tokens[-2].type = Token.WITH_NAME
125  tokens[-1].type = Token.NAME
126 
127  def _lex_arguments(self, tokens):
128  for token in tokens:
129  token.type = Token.ARGUMENT
130 
131 
133  names = (
134  'Documentation',
135  'Metadata',
136  'Suite Setup',
137  'Suite Teardown',
138  'Test Setup',
139  'Test Teardown',
140  'Test Template',
141  'Test Timeout',
142  'Test Tags',
143  'Default Tags',
144  'Keyword Tags',
145  'Library',
146  'Resource',
147  'Variables'
148  )
149  aliases = {
150  'Force Tags': 'Test Tags',
151  'Task Tags': 'Test Tags',
152  'Task Setup': 'Test Setup',
153  'Task Teardown': 'Test Teardown',
154  'Task Template': 'Test Template',
155  'Task Timeout': 'Test Timeout',
156  }
157 
158 
160  names = (
161  'Documentation',
162  'Metadata',
163  'Suite Setup',
164  'Suite Teardown',
165  'Test Setup',
166  'Test Teardown',
167  'Test Timeout',
168  'Test Tags',
169  'Keyword Tags',
170  'Library',
171  'Resource',
172  'Variables'
173  )
174  aliases = {
175  'Force Tags': 'Test Tags',
176  'Task Tags': 'Test Tags',
177  'Task Setup': 'Test Setup',
178  'Task Teardown': 'Test Teardown',
179  'Task Timeout': 'Test Timeout',
180  }
181 
182 
184  names = (
185  'Documentation',
186  'Keyword Tags',
187  'Library',
188  'Resource',
189  'Variables'
190  )
191 
192 
194  names = (
195  'Documentation',
196  'Tags',
197  'Setup',
198  'Teardown',
199  'Template',
200  'Timeout'
201  )
202 
203  def __init__(self, parent, markers):
204  super().__init__(markers)
205  self.parentparent = parent
206 
207  def _format_name(self, name):
208  return name[1:-1].strip()
209 
210  @property
211  template_set = property
212 
213  def template_set(self):
214  template = self.settingssettings['Template']
215  if self._has_disabling_value_has_disabling_value(template):
216  return False
217  parent_template = self.parentparent.settings['Test Template']
218  return self._has_value_has_value(template) or self._has_value_has_value(parent_template)
219 
220  def _has_disabling_value(self, setting):
221  if setting is None:
222  return False
223  return setting == [] or setting[0].value.upper() == 'NONE'
224 
225  def _has_value(self, setting):
226  return setting and setting[0].value
227 
228 
230  names = (
231  'Documentation',
232  'Arguments',
233  'Teardown',
234  'Timeout',
235  'Tags',
236  'Return'
237  )
238 
239  def _format_name(self, name):
240  return name[1:-1].strip()
def __init__(self, languages)
Definition: settings.py:54
def _validate(self, orig, name, statement)
Definition: settings.py:75
def _lex_name_arguments_and_with_name(self, tokens)
Definition: settings.py:120
def _lex_setting(self, setting, values, name)
Definition: settings.py:104
def _lex_error(self, setting, values, error)
Definition: settings.py:99
def _lex_name_and_arguments(self, tokens)
Definition: settings.py:115
def _get_non_existing_setting_message(self, name, normalized)
Definition: settings.py:86
def __init__(self, parent, markers)
Definition: settings.py:203
def normalize_whitespace(string)
Definition: normalizing.py:45