|
16 | 16 | optional, |
17 | 17 | optional_str, |
18 | 18 | ) |
| 19 | +from ..reader import PushItemReader |
19 | 20 |
|
20 | 21 |
|
21 | 22 | LOG = logging.getLogger("pushsource") |
@@ -178,6 +179,14 @@ def _default_build_info(self): |
178 | 179 | doesn't enforce this. |
179 | 180 | """ |
180 | 181 |
|
| 182 | + opener = attr.ib(type=callable, default=None, repr=False) |
| 183 | + """The opener, when given a push item, should return a file-like object |
| 184 | + suitable for reading this item's bytes. The object can be retrieved from |
| 185 | + :meth:`~pushsource.PushItem.content()` method. |
| 186 | +
|
| 187 | + .. versionadded:: 2.51.0 |
| 188 | + """ |
| 189 | + |
181 | 190 | def with_checksums(self): |
182 | 191 | """Return a copy of this push item with checksums present. |
183 | 192 |
|
@@ -248,3 +257,36 @@ def with_checksums(self): |
248 | 257 | updated_sums[attribute] = hasher.hexdigest() |
249 | 258 |
|
250 | 259 | return attr.evolve(self, **updated_sums) |
| 260 | + |
| 261 | + def content(self): |
| 262 | + """Returns a read-only, non-seekable content of this push item. |
| 263 | +
|
| 264 | + For push items representing a single file, content will obtain a stream |
| 265 | + for reading that file's bytes. |
| 266 | + For example, ``RpmPushItem.content`` can be used to read the content of |
| 267 | + an RPM; ``VMIPushItem.content`` can be used to read the content of a VMI |
| 268 | + and so on. |
| 269 | +
|
| 270 | + Not every type of push item can be read in this way. |
| 271 | + For example, a single ``ContainerImagePushItem`` may represent any number |
| 272 | + of artifacts making up a container image, to be accessed from a container |
| 273 | + image registry in the usual way - not using this method. Other types of |
| 274 | + push items such as ``ErratumPushItem`` may be understood as metadata-only |
| 275 | + items and do not themselves have any content. For items such as these, |
| 276 | + this method will return None. |
| 277 | +
|
| 278 | +
|
| 279 | + Returns: |
| 280 | + :class:`~io.BufferedReader` |
| 281 | + A non-seekable object of the push item content |
| 282 | + ``None`` |
| 283 | + If the :attr:`~pushsource.PushItem.opener` in the pushitem is not defined to read |
| 284 | + the content. |
| 285 | +
|
| 286 | + .. versionadded:: 2.51.0 |
| 287 | + """ |
| 288 | + return ( |
| 289 | + PushItemReader(self.opener(self), self.src or self.name) |
| 290 | + if self.opener |
| 291 | + else None |
| 292 | + ) |
0 commit comments