mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2026-10-12 16:32:10 -05:00
Emit warning when fetching remote images failed
This commit is contained in:
1 parent
a5d77a8f06
commit
25f4c004d8
3 files changed
+21
-6
No files matched your search
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from six import text_type
|
||||||
from docutils import nodes
|
from docutils import nodes
|
||||||
|
|
||||||
from sphinx.transforms import SphinxTransform
|
from sphinx.transforms import SphinxTransform
|
||||||
@@ -62,14 +63,22 @@ class ImageDownloader(BaseImageConverter):
|
|||||||
ord("&"): u"/"})
|
ord("&"): u"/"})
|
||||||
ensuredir(os.path.join(imgdir, dirname))
|
ensuredir(os.path.join(imgdir, dirname))
|
||||||
path = os.path.join(imgdir, dirname, basename)
|
path = os.path.join(imgdir, dirname, basename)
|
||||||
with open(path, 'wb') as f:
|
try:
|
||||||
r = requests.get(node['uri'])
|
r = requests.get(node['uri'])
|
||||||
f.write(r.content)
|
if r.status_code != 200:
|
||||||
|
logger.warning('Could not fetch remote image: %s [%d]' %
|
||||||
|
(node['uri'], r.status_code))
|
||||||
|
else:
|
||||||
|
with open(path, 'wb') as f:
|
||||||
|
f.write(r.content)
|
||||||
|
|
||||||
node['candidates'].pop('?')
|
node['candidates'].pop('?')
|
||||||
node['candidates']['*'] = path
|
node['candidates']['*'] = path
|
||||||
node['uri'] = path
|
node['uri'] = path
|
||||||
self.app.env.images.add_file(self.env.docname, path)
|
self.app.env.images.add_file(self.env.docname, path)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning('Could not fetch remote image: %s [%s]' %
|
||||||
|
(node['uri'], text_type(exc)))
|
||||||
|
|
||||||
|
|
||||||
def setup(app):
|
def setup(app):
|
||||||
|
|||||||
@@ -17,3 +17,6 @@ test-image
|
|||||||
|
|
||||||
.. a remote image
|
.. a remote image
|
||||||
.. image:: https://www.python.org/static/img/python-logo.png
|
.. image:: https://www.python.org/static/img/python-logo.png
|
||||||
|
|
||||||
|
.. non-exist remote image
|
||||||
|
.. image:: http://example.com/NOT_EXIST.PNG
|
||||||
@@ -1051,3 +1051,6 @@ def test_latex_remote_images(app, status, warning):
|
|||||||
result = (app.outdir / 'Python.tex').text(encoding='utf8')
|
result = (app.outdir / 'Python.tex').text(encoding='utf8')
|
||||||
assert '\\sphinxincludegraphics{{python-logo}.png}' in result
|
assert '\\sphinxincludegraphics{{python-logo}.png}' in result
|
||||||
assert (app.outdir / 'python-logo.png').exists()
|
assert (app.outdir / 'python-logo.png').exists()
|
||||||
|
assert '\\sphinxincludegraphics{{NOT_EXIST}.PNG}' not in result
|
||||||
|
assert ('WARNING: Could not fetch remote image: '
|
||||||
|
'http://example.com/NOT_EXIST.PNG [404]' in warning.getvalue())
|
||||||
Reference in new issue
Block a user