Robot Framework Integrated Development Environment (RIDE)
__init__.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 wx
18 
19 from .. import robotapi, utils
20 
21 
22 
25 class _AbstractValidator(wx.Validator):
26 
27  def Clone(self):
28  return self.__class__()
29 
30  def TransferFromWindow(self):
31  return True
32 
33  def TransferToWindow(self):
34  return True
35 
36  def Validate(self, win):
37  value = self.Window.Value
38  error = self._validate(value)
39  if error:
40  self._show_error_show_error(error)
41  return False
42  return True
43 
44  def _show_error(self, message, title="Validation Error"):
45  ret = wx.MessageBox(message, title, style=wx.ICON_ERROR)
46  self._set_focus_to_text_control_set_focus_to_text_control(self.Window)
47  return ret
48 
49  def _set_focus_to_text_control(self, ctrl):
50  ctrl.SetFocus()
51  ctrl.SelectAll()
52 
53 
55 
56  def _validate(self, value):
57  time_tokens = utils.split_value(value)
58  if not time_tokens:
59  return None
60  timestr = time_tokens[0]
61  try:
62  secs = utils.timestr_to_secs(timestr)
63  if secs <= 0:
64  raise ValueError("Timestring must be over zero")
65  time_tokens[0] = utils.secs_to_timestr(secs)
66  except ValueError as err:
67  if '${' not in timestr:
68  return str(err)
69  self._set_window_value_set_window_value(utils.join_value(time_tokens))
70  return None
71 
72  def _set_window_value(self, value):
73  self.Window.SetValue(value)
74 
75 
76 class ArgumentTypes():
77  SCALAR, DEFAULT, LIST, DICT = range(1, 5)
78 
79 
81 
82  def _validate(self, args_str):
83  try:
84  types = [self._get_type_get_type(arg)
85  for arg in utils.split_value(args_str)]
86  except ValueError as e:
87  return "Invalid argument syntax '%s'" % str(e) # DEBUG was arg
88  return self._validate_argument_order_validate_argument_order(types)
89 
90  def _get_type(self, arg):
91  if robotapi.is_scalar_var(arg):
92  return ArgumentTypes.SCALAR
93  elif robotapi.is_scalar_var(arg.split("=")[0]):
94  return ArgumentTypes.DEFAULT
95  elif robotapi.is_list_var(arg):
96  return ArgumentTypes.LIST
97  elif robotapi.is_dict_var(arg):
98  return ArgumentTypes.DICT
99  else:
100  raise ValueError(arg)
101 
102  def _validate_argument_order(self, types):
103  prev = ArgumentTypes.SCALAR
104  for t in types:
105  if t < prev:
106  return ("List and scalar arguments must be before named and "
107  "dictionary arguments")
108  prev = t
109  return None
110 
111 
113 
114  def __init__(self, field_name):
115  _AbstractValidator.__init__(self)
116  self._field_name_field_name = field_name
117 
118  def Clone(self):
119  return self.__class__(self._field_name_field_name)
120 
121  def _validate(self, value):
122  if not value:
123  return "%s cannot be empty" % self._field_name_field_name
124  return None
125 
126 
128 
129  def __init__(self, field_name, is_dir_type):
130  NonEmptyValidator.__init__(self, field_name)
131  self._is_dir_type_is_dir_type = is_dir_type
132 
133  def Clone(self):
134  return self.__class__(self._field_name_field_name, self._is_dir_type_is_dir_type)
135 
136  def _validate(self, value):
137  validity = NonEmptyValidator._validate(self, value)
138  if not self._is_dir_type_is_dir_type() and not validity:
139  if value.lower() == '__init__':
140  return "Invalid suite file name \"%s\"" % value
141  return validity
142 
143 
145 
146  def _validate(self, value):
147  if not os.path.isdir(value):
148  return "Chosen directory must exist"
149  return None
150 
151 
153 
154  def _validate(self, value):
155  path = os.path.normpath(value)
156  if os.path.exists(path):
157  return "Target file or directory must not exist"
158  parentdir, filename = os.path.split(path)
159  if "__init__" in filename:
160  parentdir = os.path.dirname(parentdir)
161  if not os.path.exists(parentdir):
162  return "Parent directory must exist"
163  return None
164 
165 
167 
168  def __init__(self, controller, orig_name=None):
169  _AbstractValidator.__init__(self)
170  self._controller_controller = controller
171  self._orig_name_orig_name = orig_name
172 
173  def Clone(self):
174  return self.__class__(self._controller_controller, self._orig_name_orig_name)
175 
176  def _validate(self, name):
177  if self._orig_name_orig_name is not None and utils.eq(
178  name, self._orig_name_orig_name, ignore=['_']):
179  return ''
180  return self._validation_method(name).error_message
181 
182 
184  @property
185  _validation_method = property
186 
188  return self._controller_controller.validate_test_name
189 
190 
192  @property
193  _validation_method = property
194 
196  return self._controller_controller.validate_keyword_name
197 
198 
200  @property
201  _validation_method = property
202 
204  return self._controller_controller.validate_scalar_variable_name
205 
206 
208  @property
209  _validation_method = property
210 
212  return self._controller_controller.validate_list_variable_name
213 
214 
216  @property
217  _validation_method = property
218 
220  return self._controller_controller.validate_dict_variable_name
def _validate_argument_order(self, types)
Definition: __init__.py:102
def __init__(self, field_name)
Definition: __init__.py:114
def __init__(self, field_name, is_dir_type)
Definition: __init__.py:129
def _set_window_value(self, value)
Definition: __init__.py:72
Implements methods to keep wxPython happy and some helper methods.
Definition: __init__.py:25
def _show_error(self, message, title="Validation Error")
Definition: __init__.py:44
def _set_focus_to_text_control(self, ctrl)
Definition: __init__.py:49
def __init__(self, controller, orig_name=None)
Definition: __init__.py:168