|
7 | 7 | # except according to those terms. |
8 | 8 |
|
9 | 9 | import fnmatch |
10 | | -from typing import Any, List |
| 10 | +from collections import OrderedDict |
| 11 | +from typing import Any, List, Union |
11 | 12 |
|
12 | 13 | from ramble.language.language_base import DirectiveError |
13 | 14 |
|
@@ -73,16 +74,17 @@ def merge_definitions( |
73 | 74 | single_type, multiple_type, single_arg_name, multiple_arg_name, directive_name |
74 | 75 | ) |
75 | 76 |
|
76 | | - all_types = [] |
| 77 | + merged_types = [] |
77 | 78 |
|
78 | 79 | if single_type: |
79 | | - all_types.append(single_type) |
| 80 | + merged_types.append(single_type) |
80 | 81 |
|
81 | 82 | if multiple_type: |
82 | | - expanded_multiple_type = expand_patterns(multiple_type, multiple_pattern_match) |
83 | | - all_types.extend(expanded_multiple_type) |
| 83 | + merged_types.extend(multiple_type) |
84 | 84 |
|
85 | | - return all_types |
| 85 | + merged_types_expanded = expand_patterns(merged_types, multiple_pattern_match) |
| 86 | + |
| 87 | + return merged_types_expanded |
86 | 88 |
|
87 | 89 |
|
88 | 90 | def require_definition( |
@@ -128,32 +130,52 @@ def require_definition( |
128 | 130 | ) |
129 | 131 |
|
130 | 132 |
|
131 | | -def expand_patterns(multiple_type: list, multiple_pattern_match: list): |
| 133 | +def expand_patterns(merged_types: list, multiple_pattern_match: Union[list, dict]): |
132 | 134 | """Expand wildcard patterns within a list of names |
133 | 135 |
|
134 | 136 | This method takes an input list containing wildcard patterns and expands the |
135 | 137 | wildcard with values matching a list of names. Returns a list containing |
136 | 138 | matching names and any inputs with zero matches. |
137 | 139 |
|
| 140 | + If multiple_pattern_match is a dict keyed on 'when', it checks the input |
| 141 | + against patterns in all 'when' conditions, without evaluating them, and |
| 142 | + returns a list containing names that match under any when condition, and |
| 143 | + any inputs with zero matches. |
| 144 | +
|
138 | 145 | Args: |
139 | | - multiple_types: List of strings for type names, may contain wildcards |
140 | | - multiple_pattern_match: List of strings to match against patterns in multiple_type |
| 146 | + merged_types: List of strings for type names, may contain wildcards |
| 147 | + multiple_pattern_match: List of strings (optional: nested in when_set |
| 148 | + dict) to match against patterns in merged_types |
141 | 149 |
|
142 | 150 | Returns: |
143 | 151 | List of expanded patterns matching the names list plus patterns |
144 | | - not found in the names list. |
| 152 | + not found in the names list. |
145 | 153 | """ |
146 | | - |
147 | | - expanded_patterns = [] |
148 | | - for input in multiple_type: |
149 | | - matched_inputs = fnmatch.filter(multiple_pattern_match, input) |
150 | | - if matched_inputs: |
151 | | - for matching_name in matched_inputs: |
152 | | - expanded_patterns.append(matching_name) |
| 154 | + expanded_patterns = OrderedDict() |
| 155 | + for input in merged_types: |
| 156 | + expanded = False |
| 157 | + if ( |
| 158 | + multiple_pattern_match |
| 159 | + and isinstance(multiple_pattern_match, dict) |
| 160 | + and isinstance(next(iter(multiple_pattern_match)), frozenset) |
| 161 | + ): |
| 162 | + for _, pattern_list in multiple_pattern_match.items(): |
| 163 | + matched_inputs = fnmatch.filter(pattern_list, input) |
| 164 | + if matched_inputs: |
| 165 | + expanded = True |
| 166 | + for match in matched_inputs: |
| 167 | + expanded_patterns[match] = "" |
153 | 168 | else: |
154 | | - expanded_patterns.append(input) |
| 169 | + matched_inputs = fnmatch.filter(multiple_pattern_match, input) |
| 170 | + if matched_inputs: |
| 171 | + expanded = True |
| 172 | + for match in matched_inputs: |
| 173 | + expanded_patterns[match] = "" |
| 174 | + |
| 175 | + if not expanded: |
| 176 | + expanded_patterns[input] = "" |
155 | 177 |
|
156 | | - return expanded_patterns |
| 178 | + return list(expanded_patterns.keys()) |
157 | 179 |
|
158 | 180 |
|
159 | 181 | def build_when_list( |
|
0 commit comments