mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Migrate to py3 style type annotation: sphinx.util.inventory
This commit is contained in:
parent
670c5311a1
commit
20f2845e21
@ -10,20 +10,20 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import zlib
|
import zlib
|
||||||
|
from typing import Callable, IO, Iterator
|
||||||
|
|
||||||
from sphinx.util import logging
|
from sphinx.util import logging
|
||||||
|
from sphinx.util.typing import Inventory
|
||||||
if False:
|
|
||||||
# For type annotation
|
|
||||||
from typing import Callable, IO, Iterator # NOQA
|
|
||||||
from sphinx.builders import Builder # NOQA
|
|
||||||
from sphinx.environment import BuildEnvironment # NOQA
|
|
||||||
from sphinx.util.typing import Inventory # NOQA
|
|
||||||
|
|
||||||
|
|
||||||
BUFSIZE = 16 * 1024
|
BUFSIZE = 16 * 1024
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
if False:
|
||||||
|
# For type annotation
|
||||||
|
from sphinx.builders import Builder
|
||||||
|
from sphinx.environment import BuildEnvironment
|
||||||
|
|
||||||
|
|
||||||
class InventoryFileReader:
|
class InventoryFileReader:
|
||||||
"""A file reader for inventory file.
|
"""A file reader for inventory file.
|
||||||
@ -31,21 +31,18 @@ class InventoryFileReader:
|
|||||||
This reader supports mixture of texts and compressed texts.
|
This reader supports mixture of texts and compressed texts.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, stream):
|
def __init__(self, stream: IO) -> None:
|
||||||
# type: (IO) -> None
|
|
||||||
self.stream = stream
|
self.stream = stream
|
||||||
self.buffer = b''
|
self.buffer = b''
|
||||||
self.eof = False
|
self.eof = False
|
||||||
|
|
||||||
def read_buffer(self):
|
def read_buffer(self) -> None:
|
||||||
# type: () -> None
|
|
||||||
chunk = self.stream.read(BUFSIZE)
|
chunk = self.stream.read(BUFSIZE)
|
||||||
if chunk == b'':
|
if chunk == b'':
|
||||||
self.eof = True
|
self.eof = True
|
||||||
self.buffer += chunk
|
self.buffer += chunk
|
||||||
|
|
||||||
def readline(self):
|
def readline(self) -> str:
|
||||||
# type: () -> str
|
|
||||||
pos = self.buffer.find(b'\n')
|
pos = self.buffer.find(b'\n')
|
||||||
if pos != -1:
|
if pos != -1:
|
||||||
line = self.buffer[:pos].decode()
|
line = self.buffer[:pos].decode()
|
||||||
@ -59,15 +56,13 @@ class InventoryFileReader:
|
|||||||
|
|
||||||
return line
|
return line
|
||||||
|
|
||||||
def readlines(self):
|
def readlines(self) -> Iterator[str]:
|
||||||
# type: () -> Iterator[str]
|
|
||||||
while not self.eof:
|
while not self.eof:
|
||||||
line = self.readline()
|
line = self.readline()
|
||||||
if line:
|
if line:
|
||||||
yield line
|
yield line
|
||||||
|
|
||||||
def read_compressed_chunks(self):
|
def read_compressed_chunks(self) -> Iterator[bytes]:
|
||||||
# type: () -> Iterator[bytes]
|
|
||||||
decompressor = zlib.decompressobj()
|
decompressor = zlib.decompressobj()
|
||||||
while not self.eof:
|
while not self.eof:
|
||||||
self.read_buffer()
|
self.read_buffer()
|
||||||
@ -75,8 +70,7 @@ class InventoryFileReader:
|
|||||||
self.buffer = b''
|
self.buffer = b''
|
||||||
yield decompressor.flush()
|
yield decompressor.flush()
|
||||||
|
|
||||||
def read_compressed_lines(self):
|
def read_compressed_lines(self) -> Iterator[str]:
|
||||||
# type: () -> Iterator[str]
|
|
||||||
buf = b''
|
buf = b''
|
||||||
for chunk in self.read_compressed_chunks():
|
for chunk in self.read_compressed_chunks():
|
||||||
buf += chunk
|
buf += chunk
|
||||||
@ -89,8 +83,7 @@ class InventoryFileReader:
|
|||||||
|
|
||||||
class InventoryFile:
|
class InventoryFile:
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls, stream, uri, joinfunc):
|
def load(cls, stream: IO, uri: str, joinfunc: Callable) -> Inventory:
|
||||||
# type: (IO, str, Callable) -> Inventory
|
|
||||||
reader = InventoryFileReader(stream)
|
reader = InventoryFileReader(stream)
|
||||||
line = reader.readline().rstrip()
|
line = reader.readline().rstrip()
|
||||||
if line == '# Sphinx inventory version 1':
|
if line == '# Sphinx inventory version 1':
|
||||||
@ -101,8 +94,7 @@ class InventoryFile:
|
|||||||
raise ValueError('invalid inventory header: %s' % line)
|
raise ValueError('invalid inventory header: %s' % line)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load_v1(cls, stream, uri, join):
|
def load_v1(cls, stream: InventoryFileReader, uri: str, join: Callable) -> Inventory:
|
||||||
# type: (InventoryFileReader, str, Callable) -> Inventory
|
|
||||||
invdata = {} # type: Inventory
|
invdata = {} # type: Inventory
|
||||||
projname = stream.readline().rstrip()[11:]
|
projname = stream.readline().rstrip()[11:]
|
||||||
version = stream.readline().rstrip()[11:]
|
version = stream.readline().rstrip()[11:]
|
||||||
@ -120,8 +112,7 @@ class InventoryFile:
|
|||||||
return invdata
|
return invdata
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load_v2(cls, stream, uri, join):
|
def load_v2(cls, stream: InventoryFileReader, uri: str, join: Callable) -> Inventory:
|
||||||
# type: (InventoryFileReader, str, Callable) -> Inventory
|
|
||||||
invdata = {} # type: Inventory
|
invdata = {} # type: Inventory
|
||||||
projname = stream.readline().rstrip()[11:]
|
projname = stream.readline().rstrip()[11:]
|
||||||
version = stream.readline().rstrip()[11:]
|
version = stream.readline().rstrip()[11:]
|
||||||
@ -150,10 +141,8 @@ class InventoryFile:
|
|||||||
return invdata
|
return invdata
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def dump(cls, filename, env, builder):
|
def dump(cls, filename: str, env: "BuildEnvironment", builder: "Builder") -> None:
|
||||||
# type: (str, BuildEnvironment, Builder) -> None
|
def escape(string: str) -> str:
|
||||||
def escape(string):
|
|
||||||
# type: (str) -> str
|
|
||||||
return re.sub("\\s+", " ", string)
|
return re.sub("\\s+", " ", string)
|
||||||
|
|
||||||
with open(os.path.join(filename), 'wb') as f:
|
with open(os.path.join(filename), 'wb') as f:
|
||||||
|
Loading…
Reference in New Issue
Block a user