Recursively extract files from the given dictionary based on specified paths. A path may look like this ['foo', 'files', ' ', 'data']. ``array_format`` controls how `` `` segments contribute to the emitted field name. Supported values: ``"brackets"`` (``foo[]``), ``"repeat"
(
# TODO: this needs to take Dict but variance issues.....
# create protocol type ?
query: Mapping[str, object],
*,
paths: Sequence[Sequence[str]],
array_format: ArrayFormat = "brackets",
)
| 39 | |
| 40 | |
| 41 | def extract_files( |
| 42 | # TODO: this needs to take Dict but variance issues..... |
| 43 | # create protocol type ? |
| 44 | query: Mapping[str, object], |
| 45 | *, |
| 46 | paths: Sequence[Sequence[str]], |
| 47 | array_format: ArrayFormat = "brackets", |
| 48 | ) -> list[tuple[str, FileTypes]]: |
| 49 | """Recursively extract files from the given dictionary based on specified paths. |
| 50 | |
| 51 | A path may look like this ['foo', 'files', '<array>', 'data']. |
| 52 | |
| 53 | ``array_format`` controls how ``<array>`` segments contribute to the emitted |
| 54 | field name. Supported values: ``"brackets"`` (``foo[]``), ``"repeat"`` and |
| 55 | ``"comma"`` (``foo``), ``"indices"`` (``foo[0]``, ``foo[1]``). |
| 56 | |
| 57 | Note: this mutates the given dictionary. |
| 58 | """ |
| 59 | files: list[tuple[str, FileTypes]] = [] |
| 60 | for path in paths: |
| 61 | files.extend(_extract_items(query, path, index=0, flattened_key=None, array_format=array_format)) |
| 62 | return files |
| 63 | |
| 64 | |
| 65 | def _array_suffix(array_format: ArrayFormat, array_index: int) -> str: |