Coverage for src/robotide/utils/variablematcher.py: 91%

41 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 10:40 +0100

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 

16import re 1ab

17 

18from .. import utils 1ab

19 

20_VAR_BODY = r"([^\}]|\\\})*" 1ab

21_SCALAR_VARIABLE_MATCHER = re.compile(r"\$\{" + _VAR_BODY + "}") 1ab

22_SCALAR_VARIABLE_LINE_MATCHER = re.compile(r"^(\$\{" + _VAR_BODY + "}) *=?$") 1ab

23_LIST_VARIABLE_MATCHER = re.compile(r"^(@\{" + _VAR_BODY + r"})( ?=?|\[\d*])$") 1ab

24_DICT_VARIABLE_MATCHER = re.compile(r"^(&\{" + _VAR_BODY + r"})( ?=?|\[[a-zA-Z_]*])$") 1ab

25_LIST_VARIABLE_SUBITEM_END_MATCHER = re.compile(r"\[\d+]$") 1ab

26_DICT_VARIABLE_SUBITEM_END_MATCHER = re.compile(r"\[[a-zA-Z_]+]$") 1ab

27 

28 

29def is_variable(value): 1ab

30 return is_scalar_variable(value) or is_list_variable(value) or \ 1cIJKLMNOPxyQRST+UVWXYZzABCD0,1234567E*89!#$FG%'(gdefH)rk

31 is_dict_variable(value) 

32 

33 

34def is_scalar_variable(value): 1ab

35 return _SCALAR_VARIABLE_LINE_MATCHER.match(value.strip()) 1hcIJKLMNOPxyQRST+UVWXYZzABCD0,1234567E*89!#$FG%'(gdefH)rk-./

36 

37 

38def is_list_variable(value): 1ab

39 return _LIST_VARIABLE_MATCHER.match(value.strip()) 1hcIJKLMNOPxyQRSTUVWXYZzABCD01234567EFG%'(islgdejmfnopqHrtukvw:;-./

40 

41 

42def is_dict_variable(value): 1ab

43 return _DICT_VARIABLE_MATCHER.match(value.strip()) 1hcIJKLMNOPxyQRSTUVWXYZzABCD01234567EFG%'(isldejmfnopqrtuvw

44 

45 

46def is_list_variable_subitem(value): 1ab

47 return is_list_variable(value) and \ 1:

48 _LIST_VARIABLE_SUBITEM_END_MATCHER.search(value) 

49 

50 

51def is_dict_var_access(value): 1ab

52 return is_dict_variable(value) and \ 

53 _DICT_VARIABLE_SUBITEM_END_MATCHER.search(value) 

54 

55 

56def get_variable(value): 1ab

57 """Returns variables name without equal sign '=' and indexing '[2]' 

58 or None 

59 """ 

60 match = is_variable(value) 1cgdefH)rk

61 return match.groups()[0] if match else None 1cgdefH)rk

62 

63 

64def get_variable_basename(value): 1ab

65 """Return variable without extended variable syntax part""" 

66 if is_list_variable(value) or is_dict_variable(value): 1hcislgdejmfnopqtukvw

67 return get_variable(value) 1cgdefk

68 match = re.match(r"\${(.+?)[^\s\w-]+.*}?", value) 1hcisldejmfnopqtuvw

69 if not match: 1hcisldejmfnopqtuvw

70 return None 1isej

71 return '${%s}' % (match.groups()[0].strip()) 1hcildjmfnopqtuvw

72 

73 

74def find_variable_basenames(value): 1ab

75 return [get_variable_basename(var) 1hc=xyzABCDilgdejmfno?pq

76 for var in re.findall('[@$&]{[^}]*}', value)] 

77 

78 

79def contains_scalar_variable(value): 1ab

80 return bool(_SCALAR_VARIABLE_MATCHER.findall(value)) 2E @ [ ] ^ _ ` { | * 8 9 ! # $ } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbtbubvbwbxbybzbAbBbCbDbEbF G FbGbHb

81 

82 

83def value_contains_variable(value, varname): 1ab

84 return utils.Matcher("*%s*" % varname).match(value) 189!#$

85 

86 

87def find_unique(seq): 1ab

88 seen = set() 

89 seen_add = seen.add 

90 return [x for x in seq if not (x in seen or seen_add(x))]