Retrieve the content of the requested resource which is located at the given absolute path. This class method may be overridden by subclasses. Note that its signature is different from other overridable class methods (no ``settings`` argument); this is deliberate to
(
cls, abspath: str, start: Optional[int] = None, end: Optional[int] = None
)
| 3014 | |
| 3015 | @classmethod |
| 3016 | def get_content( |
| 3017 | cls, abspath: str, start: Optional[int] = None, end: Optional[int] = None |
| 3018 | ) -> Generator[bytes, None, None]: |
| 3019 | """Retrieve the content of the requested resource which is located |
| 3020 | at the given absolute path. |
| 3021 | |
| 3022 | This class method may be overridden by subclasses. Note that its |
| 3023 | signature is different from other overridable class methods |
| 3024 | (no ``settings`` argument); this is deliberate to ensure that |
| 3025 | ``abspath`` is able to stand on its own as a cache key. |
| 3026 | |
| 3027 | This method should either return a byte string or an iterator |
| 3028 | of byte strings. The latter is preferred for large files |
| 3029 | as it helps reduce memory fragmentation. |
| 3030 | |
| 3031 | .. versionadded:: 3.1 |
| 3032 | """ |
| 3033 | with open(abspath, "rb") as file: |
| 3034 | if start is not None: |
| 3035 | file.seek(start) |
| 3036 | if end is not None: |
| 3037 | remaining = end - (start or 0) # type: Optional[int] |
| 3038 | else: |
| 3039 | remaining = None |
| 3040 | while True: |
| 3041 | chunk_size = 64 * 1024 |
| 3042 | if remaining is not None and remaining < chunk_size: |
| 3043 | chunk_size = remaining |
| 3044 | chunk = file.read(chunk_size) |
| 3045 | if chunk: |
| 3046 | if remaining is not None: |
| 3047 | remaining -= len(chunk) |
| 3048 | yield chunk |
| 3049 | else: |
| 3050 | if remaining is not None: |
| 3051 | assert remaining == 0 |
| 3052 | return |
| 3053 | |
| 3054 | @classmethod |
| 3055 | def get_content_version(cls, abspath: str) -> str: |
no test coverage detected