Skip to content

Commit

Permalink
Move the now-unused InventoryFileReader
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jan 4, 2025
1 parent cbd2e38 commit b48e232
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 55 deletions.
76 changes: 76 additions & 0 deletions sphinx/util/_inventory_file_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from __future__ import annotations

import zlib
from typing import TYPE_CHECKING

from sphinx.util import logging

BUFSIZE = 16 * 1024
logger = logging.getLogger(__name__)

if TYPE_CHECKING:
from collections.abc import Iterator
from typing import Protocol

# Readable file stream for inventory loading
class _SupportsRead(Protocol):
def read(self, size: int = ...) -> bytes: ...


__all__ = ('InventoryFileReader',)


class InventoryFileReader:
"""A file reader for an inventory file.
This reader supports mixture of texts and compressed texts.
"""

def __init__(self, stream: _SupportsRead) -> None:
self.stream = stream
self.buffer = b''
self.eof = False

def read_buffer(self) -> None:
chunk = self.stream.read(BUFSIZE)
if chunk == b'':
self.eof = True
self.buffer += chunk

def readline(self) -> str:
pos = self.buffer.find(b'\n')
if pos != -1:
line = self.buffer[:pos].decode()
self.buffer = self.buffer[pos + 1 :]
elif self.eof:
line = self.buffer.decode()
self.buffer = b''
else:
self.read_buffer()
line = self.readline()

return line

def readlines(self) -> Iterator[str]:
while not self.eof:
line = self.readline()
if line:
yield line

def read_compressed_chunks(self) -> Iterator[bytes]:
decompressor = zlib.decompressobj()
while not self.eof:
self.read_buffer()
yield decompressor.decompress(self.buffer)
self.buffer = b''
yield decompressor.flush()

def read_compressed_lines(self) -> Iterator[str]:
buf = b''
for chunk in self.read_compressed_chunks():
buf += chunk
pos = buf.find(b'\n')
while pos != -1:
yield buf[:pos].decode()
buf = buf[pos + 1 :]
pos = buf.find(b'\n')
63 changes: 8 additions & 55 deletions sphinx/util/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

if TYPE_CHECKING:
import os
from collections.abc import Callable, Iterator, Sequence
from collections.abc import Callable, Sequence
from typing import Protocol

from sphinx.builders import Builder
Expand All @@ -29,60 +29,13 @@ def read(self, size: int = ...) -> bytes: ...
_JoinFunc = Callable[[str, str], str]


class InventoryFileReader:
"""A file reader for an inventory file.
This reader supports mixture of texts and compressed texts.
"""

def __init__(self, stream: _SupportsRead) -> None:
self.stream = stream
self.buffer = b''
self.eof = False

def read_buffer(self) -> None:
chunk = self.stream.read(BUFSIZE)
if chunk == b'':
self.eof = True
self.buffer += chunk

def readline(self) -> str:
pos = self.buffer.find(b'\n')
if pos != -1:
line = self.buffer[:pos].decode()
self.buffer = self.buffer[pos + 1 :]
elif self.eof:
line = self.buffer.decode()
self.buffer = b''
else:
self.read_buffer()
line = self.readline()

return line

def readlines(self) -> Iterator[str]:
while not self.eof:
line = self.readline()
if line:
yield line

def read_compressed_chunks(self) -> Iterator[bytes]:
decompressor = zlib.decompressobj()
while not self.eof:
self.read_buffer()
yield decompressor.decompress(self.buffer)
self.buffer = b''
yield decompressor.flush()

def read_compressed_lines(self) -> Iterator[str]:
buf = b''
for chunk in self.read_compressed_chunks():
buf += chunk
pos = buf.find(b'\n')
while pos != -1:
yield buf[:pos].decode()
buf = buf[pos + 1 :]
pos = buf.find(b'\n')
def __getattr__(name: str) -> object:
if name == 'InventoryFileReader':
from sphinx.util._inventory_file_reader import InventoryFileReader

return InventoryFileReader
msg = f'module {__name__!r} has no attribute {name!r}'
raise AttributeError(msg)


class InventoryFile:
Expand Down

0 comments on commit b48e232

Please sign in to comment.