|
| 1 | +from typing import Iterator |
| 2 | + |
1 | 3 | import cython |
2 | 4 | import cython.cimports.libav as lib |
3 | 5 | from cython.cimports.av.stream import Stream |
@@ -95,6 +97,27 @@ def data(self): |
95 | 97 | def attachments(self): |
96 | 98 | return tuple(s for s in self._streams if s.type == "attachment") |
97 | 99 |
|
| 100 | + def _get_streams(self, x) -> Iterator[Stream]: |
| 101 | + if x is None: |
| 102 | + pass |
| 103 | + elif isinstance(x, Stream): |
| 104 | + yield x |
| 105 | + elif isinstance(x, int): |
| 106 | + yield self._streams[x] |
| 107 | + elif isinstance(x, (tuple, list)): |
| 108 | + for item in x: |
| 109 | + yield from self._get_streams(item) |
| 110 | + elif isinstance(x, dict): |
| 111 | + for type_, indices in x.items(): |
| 112 | + # For compatibility with the pseudo signature |
| 113 | + streams = self._streams if type_ == "streams" else getattr(self, type_) |
| 114 | + if not isinstance(indices, (tuple, list)): |
| 115 | + indices = [indices] |
| 116 | + for i in indices: |
| 117 | + yield streams[i] |
| 118 | + else: |
| 119 | + raise TypeError("Argument must be Stream or int.", type(x)) |
| 120 | + |
98 | 121 | def get(self, *args, **kwargs): |
99 | 122 | """get(streams=None, video=None, audio=None, subtitles=None, data=None) |
100 | 123 |
|
@@ -122,35 +145,9 @@ def get(self, *args, **kwargs): |
122 | 145 | If nothing is selected, then all streams are returned. |
123 | 146 |
|
124 | 147 | """ |
125 | | - selection: list = [] |
126 | | - |
127 | | - def process(x): |
128 | | - if x is None: |
129 | | - pass |
130 | | - elif isinstance(x, Stream): |
131 | | - selection.append(x) |
132 | | - elif isinstance(x, int): |
133 | | - selection.append(self._streams[x]) |
134 | | - elif isinstance(x, (tuple, list)): |
135 | | - for item in x: |
136 | | - process(item) |
137 | | - elif isinstance(x, dict): |
138 | | - for type_, indices in x.items(): |
139 | | - # For compatibility with the pseudo signature |
140 | | - streams = ( |
141 | | - self._streams if type_ == "streams" else getattr(self, type_) |
142 | | - ) |
143 | | - if not isinstance(indices, (tuple, list)): |
144 | | - indices = [indices] |
145 | | - for i in indices: |
146 | | - selection.append(streams[i]) |
147 | | - else: |
148 | | - raise TypeError("Argument must be Stream or int.", type(x)) |
149 | | - |
150 | | - for arg in args: |
151 | | - process(arg) |
| 148 | + selection = list(self._get_streams(args)) |
152 | 149 | if kwargs: |
153 | | - process(kwargs) |
| 150 | + selection.extend(self._get_streams(kwargs)) |
154 | 151 |
|
155 | 152 | return selection or self._streams[:] |
156 | 153 |
|
|
0 commit comments