|
56 | 56 | TransferProgress, |
57 | 57 | ) |
58 | 58 |
|
| 59 | +PatternType = type(re.compile("")) |
59 | 60 | JSONDecodeError = getattr(json, "JSONDecodeError", ValueError) |
60 | 61 | # This should be collected from server schema |
61 | 62 | PROJECT_NAME_ALLOWED_SYMBOLS = "a-zA-Z0-9_" |
@@ -1182,6 +1183,7 @@ def query_graphql(self, query, variables=None): |
1182 | 1183 | self._graphl_url, |
1183 | 1184 | json=data |
1184 | 1185 | ) |
| 1186 | + response.raise_for_status() |
1185 | 1187 | return GraphQlResponse(response) |
1186 | 1188 |
|
1187 | 1189 | def get_graphql_schema(self): |
@@ -3129,6 +3131,92 @@ def get_representations_parents(self, project_name, representation_ids): |
3129 | 3131 |
|
3130 | 3132 | return output |
3131 | 3133 |
|
| 3134 | + def get_repre_ids_by_context_filters( |
| 3135 | + self, |
| 3136 | + project_name, |
| 3137 | + context_filters, |
| 3138 | + representation_names=None, |
| 3139 | + version_ids=None |
| 3140 | + ): |
| 3141 | + """Find representation ids which match passed context filters. |
| 3142 | +
|
| 3143 | + Each representation has context integrated on representation entity in |
| 3144 | + database. The context may contain project, folder, task name or subset, |
| 3145 | + family and many more. This implementation gives option to quickly |
| 3146 | + filter representation based on representation data in database. |
| 3147 | +
|
| 3148 | + Context filters have defined structure. To define filter of nested |
| 3149 | + subfield use dot '.' as delimiter (For example 'task.name'). |
| 3150 | + Filter values can be regex filters. String or 're.Pattern' can be used. |
| 3151 | +
|
| 3152 | + Args: |
| 3153 | + project_name (str): Project where to look for representations. |
| 3154 | + context_filters (dict[str, list[str]]): Filters of context fields. |
| 3155 | + representation_names (Iterable[str]): Representation names, can be |
| 3156 | + used as additional filter for representations by their names. |
| 3157 | + version_ids (Iterable[str]): Version ids, can be used as additional |
| 3158 | + filter for representations by their parent ids. |
| 3159 | +
|
| 3160 | + Returns: |
| 3161 | + list[str]: Representation ids that match passed filters. |
| 3162 | +
|
| 3163 | + Example: |
| 3164 | + The function returns just representation ids so if entities are |
| 3165 | + required for funtionality they must be queried afterwards by |
| 3166 | + their ids. |
| 3167 | + >>> project_name = "testProject" |
| 3168 | + >>> filters = { |
| 3169 | + ... "task.name": ["[aA]nimation"], |
| 3170 | + ... "subset": [".*[Mm]ain"] |
| 3171 | + ... } |
| 3172 | + >>> repre_ids = get_repre_ids_by_context_filters( |
| 3173 | + ... project_name, filters) |
| 3174 | + >>> repres = get_representations(project_name, repre_ids) |
| 3175 | + """ |
| 3176 | + |
| 3177 | + if not isinstance(context_filters, dict): |
| 3178 | + raise TypeError( |
| 3179 | + "Expected 'dict' got {}".format(str(type(context_filters))) |
| 3180 | + ) |
| 3181 | + |
| 3182 | + filter_body = {} |
| 3183 | + if representation_names is not None: |
| 3184 | + if not representation_names: |
| 3185 | + return [] |
| 3186 | + filter_body["names"] = list(set(representation_names)) |
| 3187 | + |
| 3188 | + if version_ids is not None: |
| 3189 | + if not version_ids: |
| 3190 | + return [] |
| 3191 | + filter_body["versionIds"] = list(set(version_ids)) |
| 3192 | + |
| 3193 | + body_context_filters = [] |
| 3194 | + for key, filters in context_filters.items(): |
| 3195 | + if not isinstance(filters, (set, list, tuple)): |
| 3196 | + raise TypeError( |
| 3197 | + "Expected 'set', 'list', 'tuple' got {}".format( |
| 3198 | + str(type(filters)))) |
| 3199 | + |
| 3200 | + |
| 3201 | + new_filters = set() |
| 3202 | + for filter_value in filters: |
| 3203 | + if isinstance(filter_value, PatternType): |
| 3204 | + filter_value = filter_value.pattern |
| 3205 | + new_filters.add(filter_value) |
| 3206 | + |
| 3207 | + body_context_filters.append({ |
| 3208 | + "key": key, |
| 3209 | + "values": list(new_filters) |
| 3210 | + }) |
| 3211 | + |
| 3212 | + response = self.post( |
| 3213 | + "projects/{}/repreContextFilter".format(project_name), |
| 3214 | + context=body_context_filters, |
| 3215 | + **filter_body |
| 3216 | + ) |
| 3217 | + response.raise_for_status() |
| 3218 | + return response.data["ids"] |
| 3219 | + |
3132 | 3220 | def get_workfiles_info( |
3133 | 3221 | self, |
3134 | 3222 | project_name, |
|
0 commit comments