|
8 | 8 | from robot.variables.search import search_variable as robot_search_variable
|
9 | 9 | from robotcode.robot.utils.match import normalize
|
10 | 10 |
|
| 11 | +from . import get_robot_version |
| 12 | + |
11 | 13 |
|
12 | 14 | class InvalidVariableError(Exception):
|
13 | 15 | pass
|
14 | 16 |
|
15 | 17 |
|
16 | 18 | class VariableMatcher:
|
17 |
| - def __init__(self, string: str, identifiers: str = "$@&%", ignore_errors: bool = True) -> None: |
18 |
| - self.string = string |
| 19 | + if get_robot_version() >= (7, 3): |
| 20 | + |
| 21 | + def __init__( |
| 22 | + self, string: str, identifiers: str = "$@&%", parse_type: bool = False, ignore_errors: bool = True |
| 23 | + ) -> None: |
| 24 | + self.string = string |
| 25 | + |
| 26 | + self.match = robot_search_variable( |
| 27 | + string, identifiers=identifiers, parse_type=parse_type, ignore_errors=ignore_errors |
| 28 | + ) |
| 29 | + |
| 30 | + if not ignore_errors and self.match.base is None: |
| 31 | + raise InvalidVariableError(f"Invalid variable '{string}'") |
| 32 | + |
| 33 | + self.base = self.match.base |
| 34 | + self.identifier = self.match.identifier |
| 35 | + self.name = "%s{%s}" % (self.identifier, self.base.strip()) if self.base else None |
| 36 | + self.type = self.match.type |
| 37 | + self.items = self.match.items |
| 38 | + self.start = self.match.start |
| 39 | + self.end = self.match.end |
| 40 | + self.after = self.match.after |
| 41 | + self.before = self.match.before |
| 42 | + |
| 43 | + self.normalized_name = normalize(self.base) if self.base else None |
| 44 | + |
| 45 | + else: |
| 46 | + |
| 47 | + def __init__( |
| 48 | + self, string: str, identifiers: str = "$@&%", parse_type: bool = False, ignore_errors: bool = True |
| 49 | + ) -> None: |
| 50 | + self.string = string |
19 | 51 |
|
20 |
| - self.match = robot_search_variable(string, identifiers=identifiers, ignore_errors=ignore_errors) |
| 52 | + self.match = robot_search_variable(string, identifiers=identifiers, ignore_errors=ignore_errors) |
21 | 53 |
|
22 |
| - if not ignore_errors and self.match.base is None: |
23 |
| - raise InvalidVariableError(f"Invalid variable '{string}'") |
| 54 | + if not ignore_errors and self.match.base is None: |
| 55 | + raise InvalidVariableError(f"Invalid variable '{string}'") |
24 | 56 |
|
25 |
| - self.base = self.match.base |
26 |
| - self.identifier = self.match.identifier |
27 |
| - self.name = "%s{%s}" % (self.identifier, self.base.strip()) if self.base else None |
28 |
| - self.items = self.match.items |
29 |
| - self.start = self.match.start |
30 |
| - self.end = self.match.end |
31 |
| - self.after = self.match.after |
32 |
| - self.before = self.match.before |
| 57 | + self.base = self.match.base |
| 58 | + self.identifier = self.match.identifier |
| 59 | + self.name = "%s{%s}" % (self.identifier, self.base.strip()) if self.base else None |
| 60 | + self.type = None |
| 61 | + self.items = self.match.items |
| 62 | + self.start = self.match.start |
| 63 | + self.end = self.match.end |
| 64 | + self.after = self.match.after |
| 65 | + self.before = self.match.before |
33 | 66 |
|
34 |
| - self.normalized_name = normalize(self.base) if self.base else None |
| 67 | + self.normalized_name = normalize(self.base) if self.base else None |
35 | 68 |
|
36 | 69 | def __eq__(self, o: object) -> bool:
|
37 | 70 | if self.normalized_name is None:
|
@@ -75,17 +108,57 @@ def is_list_variable(self) -> bool:
|
75 | 108 | def is_dict_variable(self) -> bool:
|
76 | 109 | return bool(self.match.is_dict_variable())
|
77 | 110 |
|
78 |
| - def is_assign(self, allow_assign_mark: bool = False) -> bool: |
79 |
| - return bool(self.match.is_assign(allow_assign_mark)) |
| 111 | + if get_robot_version() >= (6, 1): |
| 112 | + |
| 113 | + def is_assign( |
| 114 | + self, allow_assign_mark: bool = False, allow_nested: bool = False, allow_items: bool = False |
| 115 | + ) -> bool: |
| 116 | + return bool( |
| 117 | + self.match.is_assign( |
| 118 | + allow_assign_mark=allow_assign_mark, allow_nested=allow_nested, allow_items=allow_items |
| 119 | + ) |
| 120 | + ) |
| 121 | + else: |
| 122 | + |
| 123 | + def is_assign( |
| 124 | + self, allow_assign_mark: bool = False, allow_nested: bool = False, allow_items: bool = False |
| 125 | + ) -> bool: |
| 126 | + return bool(self.match.is_assign(allow_assign_mark=allow_assign_mark)) |
| 127 | + |
| 128 | + if get_robot_version() >= (6, 1): |
| 129 | + |
| 130 | + def is_scalar_assign(self, allow_assign_mark: bool = False, allow_nested: bool = False) -> bool: |
| 131 | + return bool(self.match.is_scalar_assign(allow_assign_mark=allow_assign_mark, allow_nested=allow_nested)) |
| 132 | + else: |
| 133 | + |
| 134 | + def is_scalar_assign(self, allow_assign_mark: bool = False, allow_nested: bool = False) -> bool: |
| 135 | + return bool(self.match.is_scalar_assign(allow_assign_mark=allow_assign_mark)) |
| 136 | + |
| 137 | + if get_robot_version() >= (6, 1): |
| 138 | + |
| 139 | + def is_list_assign( |
| 140 | + self, |
| 141 | + allow_assign_mark: bool = False, |
| 142 | + allow_nested: bool = False, |
| 143 | + ) -> bool: |
| 144 | + return bool(self.match.is_list_assign(allow_assign_mark=allow_assign_mark, allow_nested=allow_nested)) |
| 145 | + else: |
| 146 | + |
| 147 | + def is_list_assign( |
| 148 | + self, |
| 149 | + allow_assign_mark: bool = False, |
| 150 | + allow_nested: bool = False, |
| 151 | + ) -> bool: |
| 152 | + return bool(self.match.is_list_assign(allow_assign_mark=allow_assign_mark)) |
80 | 153 |
|
81 |
| - def is_scalar_assign(self, allow_assign_mark: bool = False) -> bool: |
82 |
| - return bool(self.match.is_scalar_assign(allow_assign_mark)) |
| 154 | + if get_robot_version() >= (6, 1): |
83 | 155 |
|
84 |
| - def is_list_assign(self, allow_assign_mark: bool = False) -> bool: |
85 |
| - return bool(self.match.is_list_assign(allow_assign_mark)) |
| 156 | + def is_dict_assign(self, allow_assign_mark: bool = False, allow_nested: bool = False) -> bool: |
| 157 | + return bool(self.match.is_dict_assign(allow_assign_mark=allow_assign_mark, allow_nested=allow_nested)) |
| 158 | + else: |
86 | 159 |
|
87 |
| - def is_dict_assign(self, allow_assign_mark: bool = False) -> bool: |
88 |
| - return bool(self.match.is_dict_assign(allow_assign_mark)) |
| 160 | + def is_dict_assign(self, allow_assign_mark: bool = False, allow_nested: bool = False) -> bool: |
| 161 | + return bool(self.match.is_dict_assign(allow_assign_mark=allow_assign_mark)) |
89 | 162 |
|
90 | 163 |
|
91 | 164 | BUILTIN_VARIABLES = [
|
@@ -143,8 +216,10 @@ def is_variable(string: str, identifiers: str = "$@&") -> bool:
|
143 | 216 |
|
144 | 217 |
|
145 | 218 | @functools.lru_cache(maxsize=8192)
|
146 |
| -def search_variable(string: str, identifiers: str = "$@&%*", ignore_errors: bool = False) -> VariableMatcher: |
147 |
| - return VariableMatcher(string, identifiers, ignore_errors) |
| 219 | +def search_variable( |
| 220 | + string: str, identifiers: str = "$@&%*", parse_type: bool = False, ignore_errors: bool = False |
| 221 | +) -> VariableMatcher: |
| 222 | + return VariableMatcher(string, identifiers, parse_type, ignore_errors) |
148 | 223 |
|
149 | 224 |
|
150 | 225 | @functools.lru_cache(maxsize=8192)
|
|
0 commit comments