void-packages/srcpkgs/python-docutils/patches/0022-py3-Handle-urllib-urll...

203 lines
7.8 KiB
Diff

From 5ee24817411d9057c738dc1cd7cda8fc5fe03dd6 Mon Sep 17 00:00:00 2001
From: Stephen Finucane <stephen@that.guru>
Date: Tue, 27 Aug 2019 12:10:39 +0000
Subject: [PATCH 22/26] py3: Handle 'urllib', 'urllib2' to 'urlib.*' rename
Signed-off-by: Stephen Finucane <stephen@that.guru>
git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@8370 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Signed-off-by: Doan Tran Cong Danh <congdanhqx@gmail.com>
---
docutils/parsers/rst/directives/images.py | 11 +++++++++--
docutils/parsers/rst/directives/misc.py | 10 +++++++---
docutils/parsers/rst/directives/tables.py | 11 ++++++++---
docutils/utils/math/math2html.py | 8 ++++++--
docutils/writers/_html_base.py | 7 +++++--
docutils/writers/latex2e/__init__.py | 9 +++++++--
6 files changed, 42 insertions(+), 14 deletions(-)
diff --git a/docutils/parsers/rst/directives/images.py b/docutils/parsers/rst/directives/images.py
index c813fa3..383075b 100644
--- a/docutils/parsers/rst/directives/images.py
+++ b/docutils/parsers/rst/directives/images.py
@@ -10,12 +10,13 @@ __docformat__ = 'reStructuredText'
import sys
-import urllib
+
from docutils import nodes, utils
from docutils.parsers.rst import Directive
from docutils.parsers.rst import directives, states
from docutils.nodes import fully_normalize_name, whitespace_normalize_name
from docutils.parsers.rst.roles import set_classes
+
try: # check for the Python Imaging Library
import PIL.Image
except ImportError:
@@ -26,6 +27,12 @@ except ImportError:
except ImportError:
PIL = None
+if sys.version_info >= (3, 0):
+ from urllib.request import url2pathname
+else:
+ from urllib import url2pathname
+
+
class Image(Directive):
align_h_values = ('left', 'center', 'right')
@@ -125,7 +132,7 @@ class Figure(Image):
figure_node = nodes.figure('', image_node)
if figwidth == 'image':
if PIL and self.state.document.settings.file_insertion_enabled:
- imagepath = urllib.url2pathname(image_node['uri'])
+ imagepath = url2pathname(image_node['uri'])
try:
img = PIL.Image.open(
imagepath.encode(sys.getfilesystemencoding()))
diff --git a/docutils/parsers/rst/directives/misc.py b/docutils/parsers/rst/directives/misc.py
index 0fc3610..f22dae2 100644
--- a/docutils/parsers/rst/directives/misc.py
+++ b/docutils/parsers/rst/directives/misc.py
@@ -227,10 +227,14 @@ class Raw(Directive):
# Do not import urllib2 at the top of the module because
# it may fail due to broken SSL dependencies, and it takes
# about 0.15 seconds to load.
- import urllib2
+ if sys.version_info >= (3, 0):
+ from urllib.request import urlopen
+ from urllib.error import URLError
+ else:
+ from urllib2 import urlopen, URLError
try:
- raw_text = urllib2.urlopen(source).read()
- except (urllib2.URLError, IOError, OSError) as error:
+ raw_text = urlopen(source).read()
+ except (URLError, IOError, OSError) as error:
raise self.severe(u'Problems with "%s" directive URL "%s":\n%s.'
% (self.name, self.options['url'], ErrorString(error)))
raw_file = io.StringInput(source=raw_text, source_path=source,
diff --git a/docutils/parsers/rst/directives/tables.py b/docutils/parsers/rst/directives/tables.py
index b698e08..6284ab7 100644
--- a/docutils/parsers/rst/directives/tables.py
+++ b/docutils/parsers/rst/directives/tables.py
@@ -332,11 +332,16 @@ class CSVTable(Table):
# Do not import urllib2 at the top of the module because
# it may fail due to broken SSL dependencies, and it takes
# about 0.15 seconds to load.
- import urllib2
+ if sys.version_info >= (3, 0):
+ from urllib.request import urlopen
+ from urllib.error import URLError
+ else:
+ from urllib2 import urlopen, URLError
+
source = self.options['url']
try:
- csv_text = urllib2.urlopen(source).read()
- except (urllib2.URLError, IOError, OSError, ValueError) as error:
+ csv_text = urlopen(source).read()
+ except (URLError, IOError, OSError, ValueError) as error:
severe = self.state_machine.reporter.severe(
'Problems with "%s" directive URL "%s":\n%s.'
% (self.name, self.options['url'], SafeString(error)),
diff --git a/docutils/utils/math/math2html.py b/docutils/utils/math/math2html.py
index 475519f..757dec0 100644
--- a/docutils/utils/math/math2html.py
+++ b/docutils/utils/math/math2html.py
@@ -27,7 +27,11 @@ import io
import os.path
import sys
import unicodedata
-import urllib
+
+if sys.version_info >= (3, 0):
+ from urllib.parse import quote_plus
+else:
+ from urllib import quote_plus
if sys.version_info >= (3, 0):
@@ -2927,7 +2931,7 @@ class Formula(Container):
def googlecharts(self):
"Make the contents using Google Charts http://code.google.com/apis/chart/."
- url = FormulaConfig.urls['googlecharts'] + urllib.quote_plus(self.parsed)
+ url = FormulaConfig.urls['googlecharts'] + quote_plus(self.parsed)
img = '<img class="chart" src="' + url + '" alt="' + self.parsed + '"/>'
self.contents = [Constant(img)]
diff --git a/docutils/writers/_html_base.py b/docutils/writers/_html_base.py
index a957311..f91b06c 100644
--- a/docutils/writers/_html_base.py
+++ b/docutils/writers/_html_base.py
@@ -20,7 +20,6 @@
import sys
import os.path
import re
-import urllib
try: # check for the Python Imaging Library
import PIL.Image
@@ -39,6 +38,10 @@ from docutils.transforms import writer_aux
from docutils.utils.math import (unichar2tex, pick_math_environment,
math2html, latex2mathml, tex2mathml_extern)
+if sys.version_info >= (3, 0):
+ from urllib.request import url2pathname
+else:
+ from urllib import url2pathname
if sys.version_info >= (3, 0):
unicode = str # noqa
@@ -923,7 +926,7 @@ class HTMLTranslator(nodes.NodeVisitor):
if 'scale' in node:
if (PIL and not ('width' in node and 'height' in node)
and self.settings.file_insertion_enabled):
- imagepath = urllib.url2pathname(uri)
+ imagepath = url2pathname(uri)
try:
img = PIL.Image.open(
imagepath.encode(sys.getfilesystemencoding()))
diff --git a/docutils/writers/latex2e/__init__.py b/docutils/writers/latex2e/__init__.py
index 05b55eb..a60de48 100644
--- a/docutils/writers/latex2e/__init__.py
+++ b/docutils/writers/latex2e/__init__.py
@@ -17,16 +17,21 @@ import sys
import os
import re
import string
-import urllib
+
try:
import roman
except ImportError:
import docutils.utils.roman as roman
+
from docutils import frontend, nodes, languages, writers, utils, io
from docutils.utils.error_reporting import SafeString
from docutils.transforms import writer_aux
from docutils.utils.math import pick_math_environment, unichar2tex
+if sys.version_info >= (3, 0):
+ from urllib.request import url2pathname
+else:
+ from urllib import url2pathname
if sys.version_info >= (3, 0):
unicode = str # noqa
@@ -2369,7 +2374,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.requirements['graphicx'] = self.graphicx_package
attrs = node.attributes
# Convert image URI to a local file path
- imagepath = urllib.url2pathname(attrs['uri']).replace('\\', '/')
+ imagepath = url2pathname(attrs['uri']).replace('\\', '/')
# alignment defaults:
if not 'align' in attrs:
# Set default align of image in a figure to 'center'
--
2.24.0.375.geb5ae68d41