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.images
This commit is contained in:
parent
3be478f0b5
commit
316c61b172
@ -14,7 +14,7 @@ import warnings
|
|||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from os import path
|
from os import path
|
||||||
from typing import NamedTuple
|
from typing import IO, NamedTuple, Tuple
|
||||||
|
|
||||||
import imagesize
|
import imagesize
|
||||||
|
|
||||||
@ -25,10 +25,6 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
Image = None
|
Image = None
|
||||||
|
|
||||||
if False:
|
|
||||||
# For type annotation
|
|
||||||
from typing import IO, Tuple # NOQA
|
|
||||||
|
|
||||||
mime_suffixes = OrderedDict([
|
mime_suffixes = OrderedDict([
|
||||||
('.gif', 'image/gif'),
|
('.gif', 'image/gif'),
|
||||||
('.jpg', 'image/jpeg'),
|
('.jpg', 'image/jpeg'),
|
||||||
@ -43,8 +39,7 @@ DataURI = NamedTuple('DataURI', [('mimetype', str),
|
|||||||
('data', bytes)])
|
('data', bytes)])
|
||||||
|
|
||||||
|
|
||||||
def get_image_size(filename):
|
def get_image_size(filename: str) -> Tuple[int, int]:
|
||||||
# type: (str) -> Tuple[int, int]
|
|
||||||
try:
|
try:
|
||||||
size = imagesize.get(filename)
|
size = imagesize.get(filename)
|
||||||
if size[0] == -1:
|
if size[0] == -1:
|
||||||
@ -63,8 +58,7 @@ def get_image_size(filename):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def guess_mimetype_for_stream(stream, default=None):
|
def guess_mimetype_for_stream(stream: IO, default: str = None) -> str:
|
||||||
# type: (IO, str) -> str
|
|
||||||
imgtype = imghdr.what(stream) # type: ignore
|
imgtype = imghdr.what(stream) # type: ignore
|
||||||
if imgtype:
|
if imgtype:
|
||||||
return 'image/' + imgtype
|
return 'image/' + imgtype
|
||||||
@ -72,8 +66,7 @@ def guess_mimetype_for_stream(stream, default=None):
|
|||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
||||||
def guess_mimetype(filename='', content=None, default=None):
|
def guess_mimetype(filename: str = '', content: bytes = None, default: str = None) -> str:
|
||||||
# type: (str, bytes, str) -> str
|
|
||||||
_, ext = path.splitext(filename.lower())
|
_, ext = path.splitext(filename.lower())
|
||||||
if ext in mime_suffixes:
|
if ext in mime_suffixes:
|
||||||
return mime_suffixes[ext]
|
return mime_suffixes[ext]
|
||||||
@ -90,8 +83,7 @@ def guess_mimetype(filename='', content=None, default=None):
|
|||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
||||||
def get_image_extension(mimetype):
|
def get_image_extension(mimetype: str) -> str:
|
||||||
# type: (str) -> str
|
|
||||||
for ext, _mimetype in mime_suffixes.items():
|
for ext, _mimetype in mime_suffixes.items():
|
||||||
if mimetype == _mimetype:
|
if mimetype == _mimetype:
|
||||||
return ext
|
return ext
|
||||||
@ -99,8 +91,7 @@ def get_image_extension(mimetype):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def parse_data_uri(uri):
|
def parse_data_uri(uri: str) -> DataURI:
|
||||||
# type: (str) -> DataURI
|
|
||||||
if not uri.startswith('data:'):
|
if not uri.startswith('data:'):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -121,8 +112,7 @@ def parse_data_uri(uri):
|
|||||||
return DataURI(mimetype, charset, image_data)
|
return DataURI(mimetype, charset, image_data)
|
||||||
|
|
||||||
|
|
||||||
def test_svg(h, f):
|
def test_svg(h: bytes, f: IO) -> str:
|
||||||
# type: (bytes, IO) -> str
|
|
||||||
"""An additional imghdr library helper; test the header is SVG's or not."""
|
"""An additional imghdr library helper; test the header is SVG's or not."""
|
||||||
try:
|
try:
|
||||||
if '<svg' in h.decode().lower():
|
if '<svg' in h.decode().lower():
|
||||||
|
Loading…
Reference in New Issue
Block a user