|
From: <mi...@us...> - 2023-06-30 14:20:43
|
Revision: 9424
http://sourceforge.net/p/docutils/code/9424
Author: milde
Date: 2023-06-30 14:20:37 +0000 (Fri, 30 Jun 2023)
Log Message:
-----------
Revise image reading in ODT writer.
* Use `urllib.parse.urlparse()` to tell local image files from remote ones.
* Use context manager for file operations.
* `urllib.request.urlopen()` raises URLError, not HTTPError.
Modified Paths:
--------------
trunk/docutils/HISTORY.txt
trunk/docutils/docutils/writers/odf_odt/__init__.py
Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt 2023-06-30 13:59:40 UTC (rev 9423)
+++ trunk/docutils/HISTORY.txt 2023-06-30 14:20:37 UTC (rev 9424)
@@ -60,6 +60,11 @@
- Fix placement of hyperlink target (label) for tables (bug #440).
+* docutils/writers/odf_odt/__init__.py
+
+ - Use context manager for image reading operations.
+ Catch `URLError` when `urllib.request.urlopen()` fails.
+
* docutils/writers/s5_html/__init__.py
- Warn if the S5 writer cannot copy the theme files.
Modified: trunk/docutils/docutils/writers/odf_odt/__init__.py
===================================================================
--- trunk/docutils/docutils/writers/odf_odt/__init__.py 2023-06-30 13:59:40 UTC (rev 9423)
+++ trunk/docutils/docutils/writers/odf_odt/__init__.py 2023-06-30 14:20:37 UTC (rev 9424)
@@ -23,8 +23,9 @@
import subprocess
import tempfile
import time
+from urllib.error import URLError
+from urllib.parse import urlparse
from urllib.request import urlopen, url2pathname
-from urllib.error import HTTPError
import weakref
from xml.etree import ElementTree as etree
from xml.dom import minidom
@@ -2117,9 +2118,10 @@
def visit_image(self, node):
# Capture the image file.
- source = node.attributes['uri']
- if not (source.startswith('http:') or source.startswith('https:')):
- source = url2pathname(source)
+ source = node['uri']
+ uri_parts = urlparse(source)
+ if uri_parts.scheme in ('', 'file'):
+ source = url2pathname(uri_parts.path)
if not os.path.isabs(source):
# adapt relative paths
docsource, line = utils.get_source_line(node)
@@ -2129,7 +2131,7 @@
source = os.path.join(dirname, source)
if not self.check_file_exists(source):
self.document.reporter.warning(
- 'Cannot find image file %s.' % (source, ))
+ f'Cannot find image file "{source}".')
return
if source in self.image_dict:
filename, destination = self.image_dict[source]
@@ -2136,23 +2138,22 @@
else:
self.image_count += 1
filename = os.path.split(source)[1]
- destination = 'Pictures/1%08x%s' % (self.image_count, filename, )
- if source.startswith('http:') or source.startswith('https:'):
+ destination = 'Pictures/1%08x%s' % (self.image_count, filename)
+ if uri_parts.scheme in ('', 'file'):
+ spec = (os.path.abspath(source), destination,)
+ else:
try:
- imgfile = urlopen(source)
- content = imgfile.read()
- imgfile.close()
- imgfile2 = tempfile.NamedTemporaryFile('wb', delete=False)
+ with urlopen(source) as imgfile:
+ content = imgfile.read()
+ except URLError as err:
+ self.document.reporter.warning(
+ f'Cannot open image URL "{source}". {err}')
+ return
+ with tempfile.NamedTemporaryFile('wb',
+ delete=False) as imgfile2:
imgfile2.write(content)
- imgfile2.close()
- imgfilename = imgfile2.name
- source = imgfilename
- except HTTPError:
- self.document.reporter.warning(
- "Can't open image url %s." % (source, ))
+ source = imgfile2.name
spec = (source, destination,)
- else:
- spec = (os.path.abspath(source), destination,)
self.embedded_file_list.append(spec)
self.image_dict[source] = (source, destination,)
# Is this a figure (containing an image) or just a plain image?
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|