Robot Framework
control.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 setter
17 
18 from .body import Body, BodyItem, Branches
19 from .keyword import Keywords
20 
21 
22 @Body.register
23 class For(BodyItem):
24  type = BodyItem.FOR
25  body_class = Body
26  repr_args = ('variables', 'flavor', 'values')
27  __slots__ = ['variables', 'flavor', 'values']
28 
29  def __init__(self, variables=(), flavor='IN', values=(), parent=None):
30  self.variablesvariables = variables
31  self.flavorflavor = flavor
32  self.valuesvalues = values
33  self.parentparent = parent
34  self.bodybodybody = None
35 
36  @setter
37  def body(self, body):
38  return self.body_classbody_class(self, body)
39 
40  @property
41 
42  keywords = property
43 
44  def keywords(self):
45  return Keywords(self, self.bodybodybody)
46 
47  @keywords.setter
48 
49  def keywords(self, keywords):
50  Keywords.raise_deprecation_error()
51 
52  def visit(self, visitor):
53  visitor.visit_for(self)
54 
55  def __str__(self):
56  variables = ' '.join(self.variablesvariables)
57  values = ' '.join(self.valuesvalues)
58  return 'FOR %s %s %s' % (variables, self.flavorflavor, values)
59 
60 
61 @Body.register
62 class While(BodyItem):
63  type = BodyItem.WHILE
64  body_class = Body
65  repr_args = ('condition', 'limit')
66  __slots__ = ['condition', 'limit']
67 
68  def __init__(self, condition=None, limit=None, parent=None):
69  self.conditioncondition = condition
70  self.limitlimit = limit
71  self.parentparent = parent
72  self.bodybodybody = None
73 
74  @setter
75  def body(self, body):
76  return self.body_classbody_class(self, body)
77 
78  def visit(self, visitor):
79  visitor.visit_while(self)
80 
81  def __str__(self):
82  return f'WHILE {self.condition}' + (f' {self.limit}' if self.limitlimit else '')
83 
84 
86  body_class = Body
87  repr_args = ('type', 'condition')
88  __slots__ = ['type', 'condition']
89 
90  def __init__(self, type=BodyItem.IF, condition=None, parent=None):
91  self.typetypetype = type
92  self.conditioncondition = condition
93  self.parentparent = parent
94  self.bodybodybody = None
95 
96  @setter
97  def body(self, body):
98  return self.body_classbody_class(self, body)
99 
100  @property
101 
102  id = property
103 
104  def id(self):
105  if not self.parentparent:
106  return 'k1'
107  if not self.parentparent.parent:
108  return 'k%d' % (self.parentparent.body.index(self) + 1)
109  return self._get_id_get_id(self.parentparent.parent)
110 
111  def __str__(self):
112  if self.typetypetype == self.IFIF:
113  return 'IF %s' % self.conditioncondition
114  if self.typetypetype == self.ELSE_IFELSE_IF:
115  return 'ELSE IF %s' % self.conditioncondition
116  return 'ELSE'
117 
118  def visit(self, visitor):
119  visitor.visit_if_branch(self)
120 
121 
122 @Body.register
123 
124 class If(BodyItem):
125  type = BodyItem.IF_ELSE_ROOT
126  branch_class = IfBranch
127  branches_class = Branches
128  __slots__ = ['parent']
129 
130  def __init__(self, parent=None):
131  self.parentparent = parent
132  self.bodybodybody = None
133 
134  @setter
135  def body(self, branches):
136  return self.branches_classbranches_class(self.branch_classbranch_class, self, branches)
137 
138  @property
139 
140  id = property
141 
142  def id(self):
143  return None
144 
145  def visit(self, visitor):
146  visitor.visit_if(self)
147 
148 
150  body_class = Body
151  repr_args = ('type', 'patterns', 'pattern_type', 'variable')
152  __slots__ = ['type', 'patterns', 'pattern_type', 'variable']
153 
154  def __init__(self, type=BodyItem.TRY, patterns=(), pattern_type=None,
155  variable=None, parent=None):
156  if (patterns or pattern_type or variable) and type != BodyItem.EXCEPT:
157  raise TypeError(f"'{type}' branches do not accept patterns or variables.")
158  self.typetypetype = type
159  self.patternspatterns = patterns
160  self.pattern_typepattern_type = pattern_type
161  self.variablevariable = variable
162  self.parentparent = parent
163  self.bodybodybody = None
164 
165  @setter
166  def body(self, body):
167  return self.body_classbody_class(self, body)
168 
169  @property
170 
171  id = property
172 
173  def id(self):
174  if not self.parentparent:
175  return 'k1'
176  if not self.parentparent.parent:
177  return 'k%d' % (self.parentparent.body.index(self) + 1)
178  return self._get_id_get_id(self.parentparent.parent)
179 
180  def __str__(self):
181  if self.typetypetype != BodyItem.EXCEPT:
182  return self.typetypetype
183  parts = ['EXCEPT'] + list(self.patternspatterns)
184  if self.pattern_typepattern_type:
185  parts.append(f'type={self.pattern_type}')
186  if self.variablevariable:
187  parts.extend(['AS', self.variablevariable])
188  return ' '.join(parts)
189 
190  def __repr__(self):
191  repr_args = self.repr_argsrepr_argsrepr_args if self.typetypetype == BodyItem.EXCEPT else ['type']
192  return self._repr_repr(repr_args)
193 
194  def visit(self, visitor):
195  visitor.visit_try_branch(self)
196 
197 
198 @Body.register
199 
200 class Try(BodyItem):
201  type = BodyItem.TRY_EXCEPT_ROOT
202  branch_class = TryBranch
203  branches_class = Branches
204  __slots__ = []
205 
206  def __init__(self, parent=None):
207  self.parentparent = parent
208  self.bodybodybody = None
209 
210  @setter
211  def body(self, branches):
212  return self.branches_classbranches_class(self.branch_classbranch_class, self, branches)
213 
214  @property
215  try_branch = property
216 
217  def try_branch(self):
218  if self.bodybodybody and self.bodybodybody[0].type == BodyItem.TRY:
219  return self.bodybodybody[0]
220  raise TypeError("No 'TRY' branch or 'TRY' branch is not first.")
221 
222  @property
223  except_branches = property
224 
225  def except_branches(self):
226  return [branch for branch in self.bodybodybody if branch.type == BodyItem.EXCEPT]
227 
228  @property
229  else_branch = property
230 
231  def else_branch(self):
232  for branch in self.bodybodybody:
233  if branch.type == BodyItem.ELSE:
234  return branch
235  return None
236 
237  @property
238  finally_branch = property
239 
240  def finally_branch(self):
241  if self.bodybodybody and self.bodybodybody[-1].type == BodyItem.FINALLY:
242  return self.bodybodybody[-1]
243  return None
244 
245  @property
246 
247  id = property
248 
249  def id(self):
250  return None
251 
252  def visit(self, visitor):
253  visitor.visit_try(self)
254 
255 
256 @Body.register
258  type = BodyItem.RETURN
259  repr_args = ('values',)
260  __slots__ = ['values']
261 
262  def __init__(self, values=(), parent=None):
263  self.valuesvalues = values
264  self.parentparent = parent
265 
266  def visit(self, visitor):
267  visitor.visit_return(self)
268 
269 
270 @Body.register
272  type = BodyItem.CONTINUE
273  __slots__ = []
274 
275  def __init__(self, parent=None):
276  self.parentparent = parent
277 
278  def visit(self, visitor):
279  visitor.visit_continue(self)
280 
281 
282 @Body.register
284  type = BodyItem.BREAK
285  __slots__ = []
286 
287  def __init__(self, parent=None):
288  self.parentparent = parent
289 
290  def visit(self, visitor):
291  visitor.visit_break(self)
def _get_id(self, parent)
Definition: body.py:61
def visit(self, visitor)
Definition: control.py:290
def __init__(self, parent=None)
Definition: control.py:287
def __init__(self, parent=None)
Definition: control.py:275
def visit(self, visitor)
Definition: control.py:278
def __init__(self, variables=(), flavor='IN', values=(), parent=None)
Definition: control.py:29
def visit(self, visitor)
Definition: control.py:52
keywords
Deprecated since Robot Framework 4.0.
Definition: control.py:42
def body(self, body)
Definition: control.py:37
id
Branch id omits IF/ELSE root from the parent id part.
Definition: control.py:102
def body(self, body)
Definition: control.py:97
def __init__(self, type=BodyItem.IF, condition=None, parent=None)
Definition: control.py:90
def visit(self, visitor)
Definition: control.py:118
IF/ELSE structure root.
Definition: control.py:124
id
Root IF/ELSE id is always None.
Definition: control.py:140
def body(self, branches)
Definition: control.py:135
def visit(self, visitor)
Definition: control.py:145
def __init__(self, parent=None)
Definition: control.py:130
def visit(self, visitor)
Definition: control.py:266
def __init__(self, values=(), parent=None)
Definition: control.py:262
def visit(self, visitor)
Definition: control.py:194
id
Branch id omits TRY/EXCEPT root from the parent id part.
Definition: control.py:171
def body(self, body)
Definition: control.py:166
def __init__(self, type=BodyItem.TRY, patterns=(), pattern_type=None, variable=None, parent=None)
Definition: control.py:155
TRY/EXCEPT structure root.
Definition: control.py:200
def body(self, branches)
Definition: control.py:211
def __init__(self, parent=None)
Definition: control.py:206
def visit(self, visitor)
Definition: control.py:252
id
Root TRY/EXCEPT id is always None.
Definition: control.py:247
def __init__(self, condition=None, limit=None, parent=None)
Definition: control.py:68
def body(self, body)
Definition: control.py:75
def visit(self, visitor)
Definition: control.py:78
A list-like object representing keywords in a suite, a test or a keyword.
Definition: keyword.py:137