Robot Framework Integrated Development Environment (RIDE)
embedded.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 re
17 
18 from robotide.lib.robot.errors import DataError
19 from robotide.lib.robot.utils import get_error_message, py2to3
20 from robotide.lib.robot.variables import VariableIterator
21 
22 
23 @py2to3
25 
26  def __init__(self, name):
27  if '${' in name:
28  self.name, self.argsargs = EmbeddedArgumentParser().parse(name)
29  else:
30  self.name, self.argsargs = None, []
31 
32  def __nonzero__(self):
33  return self.name is not None
34 
35 
37 
40  _regexp_extension = re.compile(r'(?<!\\)\‍(\?.+\‍)')
41 
44  _regexp_group_start = re.compile(r'(?<!\\)\‍((.*?)\‍)')
45 
48  _regexp_group_escape = r'(?:\1)'
49 
52  _default_pattern = '.*?'
53 
56  _variable_pattern = r'\$\{[^\}]+\}'
57 
58  def parse(self, string):
59  args = []
60  name_regexp = ['^']
61  for before, variable, string in VariableIterator(string, identifiers='$'):
62  name, pattern = self._get_name_and_pattern_get_name_and_pattern(variable[2:-1])
63  args.append(name)
64  name_regexp.extend([re.escape(before), '(%s)' % pattern])
65  name_regexp.extend([re.escape(string), '$'])
66  name = self._compile_regexp_compile_regexp(name_regexp) if args else None
67  return name, args
68 
69  def _get_name_and_pattern(self, name):
70  if ':' not in name:
71  return name, self._default_pattern_default_pattern
72  name, pattern = name.split(':', 1)
73  return name, self._format_custom_regexp_format_custom_regexp(pattern)
74 
75  def _format_custom_regexp(self, pattern):
76  for formatter in (self._regexp_extensions_are_not_allowed_regexp_extensions_are_not_allowed,
77  self._make_groups_non_capturing_make_groups_non_capturing,
78  self._unescape_closing_curly_unescape_closing_curly,
79  self._add_automatic_variable_pattern_add_automatic_variable_pattern):
80  pattern = formatter(pattern)
81  return pattern
82 
84  if not self._regexp_extension_regexp_extension.search(pattern):
85  return pattern
86  raise DataError('Regexp extensions are not allowed in embedded '
87  'arguments.')
88 
89  def _make_groups_non_capturing(self, pattern):
90  return self._regexp_group_start_regexp_group_start.sub(self._regexp_group_escape_regexp_group_escape, pattern)
91 
92  def _unescape_closing_curly(self, pattern):
93  return pattern.replace('\\}', '}')
94 
95  def _add_automatic_variable_pattern(self, pattern):
96  return '%s|%s' % (pattern, self._variable_pattern_variable_pattern)
97 
98  def _compile_regexp(self, pattern):
99  try:
100  return re.compile(''.join(pattern), re.IGNORECASE)
101  except:
102  raise DataError("Compiling embedded arguments regexp failed: %s"
103  % get_error_message())
Used when variable does not exist.
Definition: errors.py:67
def get_error_message()
Returns error message of the last occurred exception.
Definition: error.py:41