Skip to content

Commit 6a63260

Browse files
committed
Move the now-unused InventoryFileReader
1 parent b7d027b commit 6a63260

File tree

2 files changed

+84
-55
lines changed

2 files changed

+84
-55
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from __future__ import annotations
2+
3+
import zlib
4+
from typing import TYPE_CHECKING
5+
6+
from sphinx.util import logging
7+
8+
BUFSIZE = 16 * 1024
9+
logger = logging.getLogger(__name__)
10+
11+
if TYPE_CHECKING:
12+
from collections.abc import Iterator
13+
from typing import Protocol
14+
15+
# Readable file stream for inventory loading
16+
class _SupportsRead(Protocol):
17+
def read(self, size: int = ...) -> bytes: ...
18+
19+
20+
__all__ = ('InventoryFileReader',)
21+
22+
23+
class InventoryFileReader:
24+
"""A file reader for an inventory file.
25+
26+
This reader supports mixture of texts and compressed texts.
27+
"""
28+
29+
def __init__(self, stream: _SupportsRead) -> None:
30+
self.stream = stream
31+
self.buffer = b''
32+
self.eof = False
33+
34+
def read_buffer(self) -> None:
35+
chunk = self.stream.read(BUFSIZE)
36+
if chunk == b'':
37+
self.eof = True
38+
self.buffer += chunk
39+
40+
def readline(self) -> str:
41+
pos = self.buffer.find(b'\n')
42+
if pos != -1:
43+
line = self.buffer[:pos].decode()
44+
self.buffer = self.buffer[pos + 1 :]
45+
elif self.eof:
46+
line = self.buffer.decode()
47+
self.buffer = b''
48+
else:
49+
self.read_buffer()
50+
line = self.readline()
51+
52+
return line
53+
54+
def readlines(self) -> Iterator[str]:
55+
while not self.eof:
56+
line = self.readline()
57+
if line:
58+
yield line
59+
60+
def read_compressed_chunks(self) -> Iterator[bytes]:
61+
decompressor = zlib.decompressobj()
62+
while not self.eof:
63+
self.read_buffer()
64+
yield decompressor.decompress(self.buffer)
65+
self.buffer = b''
66+
yield decompressor.flush()
67+
68+
def read_compressed_lines(self) -> Iterator[str]:
69+
buf = b''
70+
for chunk in self.read_compressed_chunks():
71+
buf += chunk
72+
pos = buf.find(b'\n')
73+
while pos != -1:
74+
yield buf[:pos].decode()
75+
buf = buf[pos + 1 :]
76+
pos = buf.find(b'\n')

sphinx/util/inventory.py

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
if TYPE_CHECKING:
1717
import os
18-
from collections.abc import Callable, Iterator, Sequence
18+
from collections.abc import Callable, Sequence
1919
from typing import Protocol
2020

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

3131

32-
class InventoryFileReader:
33-
"""A file reader for an inventory file.
34-
35-
This reader supports mixture of texts and compressed texts.
36-
"""
37-
38-
def __init__(self, stream: _SupportsRead) -> None:
39-
self.stream = stream
40-
self.buffer = b''
41-
self.eof = False
42-
43-
def read_buffer(self) -> None:
44-
chunk = self.stream.read(BUFSIZE)
45-
if chunk == b'':
46-
self.eof = True
47-
self.buffer += chunk
48-
49-
def readline(self) -> str:
50-
pos = self.buffer.find(b'\n')
51-
if pos != -1:
52-
line = self.buffer[:pos].decode()
53-
self.buffer = self.buffer[pos + 1 :]
54-
elif self.eof:
55-
line = self.buffer.decode()
56-
self.buffer = b''
57-
else:
58-
self.read_buffer()
59-
line = self.readline()
60-
61-
return line
62-
63-
def readlines(self) -> Iterator[str]:
64-
while not self.eof:
65-
line = self.readline()
66-
if line:
67-
yield line
68-
69-
def read_compressed_chunks(self) -> Iterator[bytes]:
70-
decompressor = zlib.decompressobj()
71-
while not self.eof:
72-
self.read_buffer()
73-
yield decompressor.decompress(self.buffer)
74-
self.buffer = b''
75-
yield decompressor.flush()
76-
77-
def read_compressed_lines(self) -> Iterator[str]:
78-
buf = b''
79-
for chunk in self.read_compressed_chunks():
80-
buf += chunk
81-
pos = buf.find(b'\n')
82-
while pos != -1:
83-
yield buf[:pos].decode()
84-
buf = buf[pos + 1 :]
85-
pos = buf.find(b'\n')
32+
def __getattr__(name: str) -> object:
33+
if name == 'InventoryFileReader':
34+
from sphinx.util._inventory_file_reader import InventoryFileReader
35+
36+
return InventoryFileReader
37+
msg = f'module {__name__!r} has no attribute {name!r}'
38+
raise AttributeError(msg)
8639

8740

8841
class InventoryFile:

0 commit comments

Comments
 (0)